mirror of
https://github.com/geometer/FBReaderJ.git
synced 2025-10-05 10:49:24 +02:00
library-in-single-activity feature, BaseActivity is free of library references
This commit is contained in:
parent
317bc9d55d
commit
62f4ca8d75
10 changed files with 238 additions and 209 deletions
|
@ -20,12 +20,17 @@
|
|||
package org.geometerplus.android.fbreader.library;
|
||||
|
||||
import android.app.SearchManager;
|
||||
import android.app.AlertDialog;
|
||||
import android.content.Intent;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuItem;
|
||||
import android.content.DialogInterface;
|
||||
import android.view.*;
|
||||
import android.os.Bundle;
|
||||
import android.widget.AdapterView;
|
||||
import android.widget.ListView;
|
||||
|
||||
import org.geometerplus.zlibrary.core.options.ZLStringOption;
|
||||
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
|
||||
import org.geometerplus.zlibrary.core.resources.ZLResource;
|
||||
|
||||
import org.geometerplus.zlibrary.ui.android.R;
|
||||
|
||||
|
@ -34,10 +39,104 @@ import org.geometerplus.android.util.UIUtil;
|
|||
import org.geometerplus.fbreader.library.*;
|
||||
import org.geometerplus.fbreader.tree.FBTree;
|
||||
|
||||
abstract class LibraryActivity extends BaseActivity implements MenuItem.OnMenuItemClickListener {
|
||||
import org.geometerplus.android.fbreader.SQLiteBooksDatabase;
|
||||
import org.geometerplus.android.fbreader.FBReader;
|
||||
import org.geometerplus.android.fbreader.BookInfoActivity;
|
||||
|
||||
public class LibraryActivity extends BaseActivity implements MenuItem.OnMenuItemClickListener {
|
||||
static BooksDatabase DatabaseInstance;
|
||||
static Library LibraryInstance;
|
||||
|
||||
static final ZLStringOption BookSearchPatternOption =
|
||||
new ZLStringOption("BookSearch", "Pattern", "");
|
||||
|
||||
protected String mySelectedBookPath;
|
||||
private Book mySelectedBook;
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle icicle) {
|
||||
super.onCreate(icicle);
|
||||
|
||||
DatabaseInstance = SQLiteBooksDatabase.Instance();
|
||||
if (DatabaseInstance == null) {
|
||||
DatabaseInstance = new SQLiteBooksDatabase(this, "LIBRARY");
|
||||
}
|
||||
if (LibraryInstance == null) {
|
||||
LibraryInstance = new Library();
|
||||
startService(new Intent(getApplicationContext(), InitializationService.class));
|
||||
}
|
||||
|
||||
final FBTree.Key key = (FBTree.Key)getIntent().getSerializableExtra(TREE_KEY_KEY);
|
||||
myCurrentTree = key != null
|
||||
? LibraryInstance.getLibraryTree(key)
|
||||
: LibraryInstance.getRootTree();
|
||||
setTitle(myCurrentTree.getTreeTitle());
|
||||
|
||||
mySelectedBookPath = getIntent().getStringExtra(SELECTED_BOOK_PATH_KEY);
|
||||
mySelectedBook = null;
|
||||
if (mySelectedBookPath != null) {
|
||||
final ZLFile file = ZLFile.createFileByPath(mySelectedBookPath);
|
||||
if (file != null) {
|
||||
mySelectedBook = Book.getByFile(file);
|
||||
}
|
||||
}
|
||||
|
||||
setResult(RESULT_DONT_INVALIDATE_VIEWS);
|
||||
|
||||
final Intent intent = getIntent();
|
||||
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
|
||||
if (runSearch(intent)) {
|
||||
startActivity(intent
|
||||
.setAction(ACTION_FOUND)
|
||||
.setClass(getApplicationContext(), LibraryTopLevelActivity.class)
|
||||
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK)
|
||||
);
|
||||
} else {
|
||||
showNotFoundToast();
|
||||
finish();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
final ListAdapter adapter = new ListAdapter(this, myCurrentTree.subTrees());
|
||||
setSelection(adapter.getFirstSelectedItemIndex());
|
||||
getListView().setTextFilterEnabled(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
boolean isTreeSelected(FBTree tree) {
|
||||
final LibraryTree lTree = (LibraryTree)tree;
|
||||
return lTree.isSelectable() && lTree.containsBook(mySelectedBook);
|
||||
}
|
||||
|
||||
protected void openBook(Book book) {
|
||||
startActivity(
|
||||
new Intent(getApplicationContext(), FBReader.class)
|
||||
.setAction(Intent.ACTION_VIEW)
|
||||
.putExtra(FBReader.BOOK_PATH_KEY, book.File.getPath())
|
||||
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onListItemClick(ListView listView, View view, int position, long rowId) {
|
||||
final LibraryTree tree = (LibraryTree)getListAdapter().getItem(position);
|
||||
final Book book = tree.getBook();
|
||||
if (book != null) {
|
||||
showBookInfo(book);
|
||||
} else {
|
||||
new OpenTreeRunnable(tree).run();
|
||||
}
|
||||
}
|
||||
|
||||
protected void showBookInfo(Book book) {
|
||||
startActivityForResult(
|
||||
new Intent(getApplicationContext(), BookInfoActivity.class)
|
||||
.putExtra(BookInfoActivity.CURRENT_BOOK_PATH_KEY, book.File.getPath()),
|
||||
BOOK_INFO_REQUEST
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onActivityResult(int requestCode, int returnCode, Intent intent) {
|
||||
if (requestCode == CHILD_LIST_REQUEST && returnCode == RESULT_DO_INVALIDATE_VIEWS) {
|
||||
|
@ -81,6 +180,75 @@ abstract class LibraryActivity extends BaseActivity implements MenuItem.OnMenuIt
|
|||
UIUtil.showErrorMessage(this, "bookNotFound");
|
||||
}
|
||||
|
||||
//
|
||||
// Context menu
|
||||
//
|
||||
private static final int OPEN_BOOK_ITEM_ID = 0;
|
||||
private static final int SHOW_BOOK_INFO_ITEM_ID = 1;
|
||||
private static final int ADD_TO_FAVORITES_ITEM_ID = 2;
|
||||
private static final int REMOVE_FROM_FAVORITES_ITEM_ID = 3;
|
||||
private static final int DELETE_BOOK_ITEM_ID = 4;
|
||||
|
||||
@Override
|
||||
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo) {
|
||||
final int position = ((AdapterView.AdapterContextMenuInfo)menuInfo).position;
|
||||
final Book book = ((LibraryTree)getListAdapter().getItem(position)).getBook();
|
||||
if (book != null) {
|
||||
createBookContextMenu(menu, book);
|
||||
}
|
||||
}
|
||||
|
||||
private void createBookContextMenu(ContextMenu menu, Book book) {
|
||||
final ZLResource resource = Library.resource();
|
||||
menu.setHeaderTitle(book.getTitle());
|
||||
menu.add(0, OPEN_BOOK_ITEM_ID, 0, resource.getResource("openBook").getValue());
|
||||
menu.add(0, SHOW_BOOK_INFO_ITEM_ID, 0, resource.getResource("showBookInfo").getValue());
|
||||
if (LibraryInstance.isBookInFavorites(book)) {
|
||||
menu.add(0, REMOVE_FROM_FAVORITES_ITEM_ID, 0, resource.getResource("removeFromFavorites").getValue());
|
||||
} else {
|
||||
menu.add(0, ADD_TO_FAVORITES_ITEM_ID, 0, resource.getResource("addToFavorites").getValue());
|
||||
}
|
||||
if ((LibraryInstance.getRemoveBookMode(book) & Library.REMOVE_FROM_DISK) != 0) {
|
||||
menu.add(0, DELETE_BOOK_ITEM_ID, 0, resource.getResource("deleteBook").getValue());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onContextItemSelected(MenuItem item) {
|
||||
final int position = ((AdapterView.AdapterContextMenuInfo)item.getMenuInfo()).position;
|
||||
final Book book = ((LibraryTree)getListAdapter().getItem(position)).getBook();
|
||||
if (book != null) {
|
||||
return onContextItemSelected(item.getItemId(), book);
|
||||
}
|
||||
return super.onContextItemSelected(item);
|
||||
}
|
||||
|
||||
private boolean onContextItemSelected(int itemId, Book book) {
|
||||
switch (itemId) {
|
||||
case OPEN_BOOK_ITEM_ID:
|
||||
openBook(book);
|
||||
return true;
|
||||
case SHOW_BOOK_INFO_ITEM_ID:
|
||||
showBookInfo(book);
|
||||
return true;
|
||||
case ADD_TO_FAVORITES_ITEM_ID:
|
||||
LibraryInstance.addBookToFavorites(book);
|
||||
return true;
|
||||
case REMOVE_FROM_FAVORITES_ITEM_ID:
|
||||
LibraryInstance.removeBookFromFavorites(book);
|
||||
getListView().invalidateViews();
|
||||
return true;
|
||||
case DELETE_BOOK_ITEM_ID:
|
||||
tryToDeleteBook(book);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
//
|
||||
// Options menu
|
||||
//
|
||||
|
||||
@Override
|
||||
public boolean onCreateOptionsMenu(Menu menu) {
|
||||
super.onCreateOptionsMenu(menu);
|
||||
|
@ -105,6 +273,9 @@ abstract class LibraryActivity extends BaseActivity implements MenuItem.OnMenuIt
|
|||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Item icons
|
||||
//
|
||||
@Override
|
||||
protected int getCoverResourceId(FBTree tree) {
|
||||
if (((LibraryTree)tree).getBook() != null) {
|
||||
|
@ -142,9 +313,40 @@ abstract class LibraryActivity extends BaseActivity implements MenuItem.OnMenuIt
|
|||
return R.drawable.ic_list_library_books;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void deleteBook(Book book, int mode) {
|
||||
super.deleteBook(book, mode);
|
||||
//
|
||||
// Book deletion
|
||||
//
|
||||
private class BookDeleter implements DialogInterface.OnClickListener {
|
||||
private final Book myBook;
|
||||
private final int myMode;
|
||||
|
||||
BookDeleter(Book book, int removeMode) {
|
||||
myBook = book;
|
||||
myMode = removeMode;
|
||||
}
|
||||
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
deleteBook(myBook, myMode);
|
||||
setResult(RESULT_DO_INVALIDATE_VIEWS);
|
||||
}
|
||||
}
|
||||
|
||||
private void tryToDeleteBook(Book book) {
|
||||
final ZLResource dialogResource = ZLResource.resource("dialog");
|
||||
final ZLResource buttonResource = dialogResource.getResource("button");
|
||||
final ZLResource boxResource = dialogResource.getResource("deleteBookBox");
|
||||
new AlertDialog.Builder(this)
|
||||
.setTitle(book.getTitle())
|
||||
.setMessage(boxResource.getResource("message").getValue())
|
||||
.setIcon(0)
|
||||
.setPositiveButton(buttonResource.getResource("yes").getValue(), new BookDeleter(book, Library.REMOVE_FROM_DISK))
|
||||
.setNegativeButton(buttonResource.getResource("no").getValue(), null)
|
||||
.create().show();
|
||||
}
|
||||
|
||||
private void deleteBook(Book book, int mode) {
|
||||
LibraryInstance.removeBook(book, mode);
|
||||
|
||||
if (myCurrentTree instanceof FileTree) {
|
||||
getListAdapter().remove(new FileTree((FileTree)myCurrentTree, book.File));
|
||||
} else {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue