mirror of
https://github.com/geometer/FBReaderJ.git
synced 2025-10-05 19:42:17 +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
|
@ -66,16 +66,7 @@
|
||||||
<activity android:name="org.geometerplus.android.fbreader.BookInfoActivity" android:configChanges="orientation|keyboardHidden" android:process=":library"/>
|
<activity android:name="org.geometerplus.android.fbreader.BookInfoActivity" android:configChanges="orientation|keyboardHidden" android:process=":library"/>
|
||||||
<service android:name="org.geometerplus.android.fbreader.library.InitializationService" android:process=":library" />
|
<service android:name="org.geometerplus.android.fbreader.library.InitializationService" android:process=":library" />
|
||||||
<receiver android:name="org.geometerplus.android.fbreader.library.KillerCallback" android:process=":library" />
|
<receiver android:name="org.geometerplus.android.fbreader.library.KillerCallback" android:process=":library" />
|
||||||
<activity android:name="org.geometerplus.android.fbreader.library.LibraryTopLevelActivity" android:launchMode="singleTask" android:process=":library" android:configChanges="orientation|keyboardHidden">
|
<activity android:name="org.geometerplus.android.fbreader.library.LibraryActivity" android:launchMode="singleTask" android:process=":library" android:configChanges="orientation|keyboardHidden">
|
||||||
<intent-filter>
|
|
||||||
<action android:name="android.intent.action.SEARCH" />
|
|
||||||
</intent-filter>
|
|
||||||
<intent-filter>
|
|
||||||
<action android:name="fbreader.library.intent.FOUND" />
|
|
||||||
</intent-filter>
|
|
||||||
<meta-data android:name="android.app.searchable" android:resource="@xml/searchable" />
|
|
||||||
</activity>
|
|
||||||
<activity android:name="org.geometerplus.android.fbreader.library.LibraryTreeActivity" android:process=":library" android:configChanges="orientation|keyboardHidden">
|
|
||||||
<intent-filter>
|
<intent-filter>
|
||||||
<action android:name="android.intent.action.SEARCH" />
|
<action android:name="android.intent.action.SEARCH" />
|
||||||
</intent-filter>
|
</intent-filter>
|
||||||
|
|
|
@ -66,16 +66,7 @@
|
||||||
<activity android:name="org.geometerplus.android.fbreader.BookInfoActivity" android:configChanges="orientation|keyboardHidden" android:process=":library"/>
|
<activity android:name="org.geometerplus.android.fbreader.BookInfoActivity" android:configChanges="orientation|keyboardHidden" android:process=":library"/>
|
||||||
<service android:name="org.geometerplus.android.fbreader.library.InitializationService" android:process=":library" />
|
<service android:name="org.geometerplus.android.fbreader.library.InitializationService" android:process=":library" />
|
||||||
<receiver android:name="org.geometerplus.android.fbreader.library.KillerCallback" android:process=":library" />
|
<receiver android:name="org.geometerplus.android.fbreader.library.KillerCallback" android:process=":library" />
|
||||||
<activity android:name="org.geometerplus.android.fbreader.library.LibraryTopLevelActivity" android:launchMode="singleTask" android:process=":library" android:configChanges="orientation|keyboardHidden">
|
<activity android:name="org.geometerplus.android.fbreader.library.LibraryActivity" android:launchMode="singleTask" android:process=":library" android:configChanges="orientation|keyboardHidden">
|
||||||
<intent-filter>
|
|
||||||
<action android:name="android.intent.action.SEARCH" />
|
|
||||||
</intent-filter>
|
|
||||||
<intent-filter>
|
|
||||||
<action android:name="fbreader.library.intent.FOUND" />
|
|
||||||
</intent-filter>
|
|
||||||
<meta-data android:name="android.app.searchable" android:resource="@xml/searchable" />
|
|
||||||
</activity>
|
|
||||||
<activity android:name="org.geometerplus.android.fbreader.library.LibraryTreeActivity" android:process=":library" android:configChanges="orientation|keyboardHidden">
|
|
||||||
<intent-filter>
|
<intent-filter>
|
||||||
<action android:name="android.intent.action.SEARCH" />
|
<action android:name="android.intent.action.SEARCH" />
|
||||||
</intent-filter>
|
</intent-filter>
|
||||||
|
|
|
@ -24,7 +24,7 @@ import android.content.Intent;
|
||||||
import org.geometerplus.fbreader.fbreader.FBReaderApp;
|
import org.geometerplus.fbreader.fbreader.FBReaderApp;
|
||||||
import org.geometerplus.fbreader.bookmodel.BookModel;
|
import org.geometerplus.fbreader.bookmodel.BookModel;
|
||||||
|
|
||||||
import org.geometerplus.android.fbreader.library.LibraryTopLevelActivity;
|
import org.geometerplus.android.fbreader.library.LibraryActivity;
|
||||||
|
|
||||||
class ShowLibraryAction extends FBAndroidAction {
|
class ShowLibraryAction extends FBAndroidAction {
|
||||||
ShowLibraryAction(FBReader baseActivity, FBReaderApp fbreader) {
|
ShowLibraryAction(FBReader baseActivity, FBReaderApp fbreader) {
|
||||||
|
@ -33,9 +33,9 @@ class ShowLibraryAction extends FBAndroidAction {
|
||||||
|
|
||||||
public void run() {
|
public void run() {
|
||||||
final BookModel model = Reader.Model;
|
final BookModel model = Reader.Model;
|
||||||
Intent intent = new Intent(BaseActivity.getApplicationContext(), LibraryTopLevelActivity.class);
|
Intent intent = new Intent(BaseActivity.getApplicationContext(), LibraryActivity.class);
|
||||||
if (model != null && model.Book != null) {
|
if (model != null && model.Book != null) {
|
||||||
intent.putExtra(LibraryTopLevelActivity.SELECTED_BOOK_PATH_KEY, model.Book.File.getPath());
|
intent.putExtra(LibraryActivity.SELECTED_BOOK_PATH_KEY, model.Book.File.getPath());
|
||||||
}
|
}
|
||||||
BaseActivity.startActivity(intent);
|
BaseActivity.startActivity(intent);
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,83 +19,34 @@
|
||||||
|
|
||||||
package org.geometerplus.android.fbreader.library;
|
package org.geometerplus.android.fbreader.library;
|
||||||
|
|
||||||
import java.util.Map;
|
import android.app.ListActivity;
|
||||||
import java.util.HashMap;
|
|
||||||
|
|
||||||
import android.app.*;
|
|
||||||
import android.content.DialogInterface;
|
import android.content.DialogInterface;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.view.*;
|
import android.view.KeyEvent;
|
||||||
import android.widget.*;
|
import android.view.View;
|
||||||
|
|
||||||
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
|
|
||||||
import org.geometerplus.zlibrary.core.resources.ZLResource;
|
|
||||||
|
|
||||||
import org.geometerplus.zlibrary.ui.android.R;
|
|
||||||
|
|
||||||
import org.geometerplus.android.util.UIUtil;
|
import org.geometerplus.android.util.UIUtil;
|
||||||
|
|
||||||
import org.geometerplus.fbreader.library.*;
|
|
||||||
import org.geometerplus.fbreader.tree.FBTree;
|
import org.geometerplus.fbreader.tree.FBTree;
|
||||||
|
|
||||||
import org.geometerplus.android.fbreader.FBReader;
|
|
||||||
import org.geometerplus.android.fbreader.BookInfoActivity;
|
|
||||||
import org.geometerplus.android.fbreader.SQLiteBooksDatabase;
|
|
||||||
|
|
||||||
abstract class BaseActivity extends ListActivity implements View.OnCreateContextMenuListener {
|
abstract class BaseActivity extends ListActivity implements View.OnCreateContextMenuListener {
|
||||||
static final String TREE_KEY_KEY = "TreeKey";
|
static final String TREE_KEY_KEY = "TreeKey";
|
||||||
public static final String SELECTED_BOOK_PATH_KEY = "SelectedBookPath";
|
public static final String SELECTED_BOOK_PATH_KEY = "SelectedBookPath";
|
||||||
|
|
||||||
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;
|
|
||||||
|
|
||||||
protected static final int CHILD_LIST_REQUEST = 0;
|
protected static final int CHILD_LIST_REQUEST = 0;
|
||||||
protected static final int BOOK_INFO_REQUEST = 1;
|
protected static final int BOOK_INFO_REQUEST = 1;
|
||||||
|
|
||||||
protected static final int RESULT_DONT_INVALIDATE_VIEWS = 0;
|
protected static final int RESULT_DONT_INVALIDATE_VIEWS = 0;
|
||||||
protected static final int RESULT_DO_INVALIDATE_VIEWS = 1;
|
protected static final int RESULT_DO_INVALIDATE_VIEWS = 1;
|
||||||
|
|
||||||
static BooksDatabase DatabaseInstance;
|
protected FBTree myCurrentTree;
|
||||||
static Library LibraryInstance;
|
|
||||||
|
|
||||||
protected String mySelectedBookPath;
|
|
||||||
private Book mySelectedBook;
|
|
||||||
protected LibraryTree myCurrentTree;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(Bundle icicle) {
|
public void onCreate(Bundle icicle) {
|
||||||
super.onCreate(icicle);
|
super.onCreate(icicle);
|
||||||
Thread.setDefaultUncaughtExceptionHandler(new org.geometerplus.zlibrary.ui.android.library.UncaughtExceptionHandler(this));
|
Thread.setDefaultUncaughtExceptionHandler(new org.geometerplus.zlibrary.ui.android.library.UncaughtExceptionHandler(this));
|
||||||
|
|
||||||
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);
|
|
||||||
getListView().setOnCreateContextMenuListener(this);
|
getListView().setOnCreateContextMenuListener(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -105,127 +56,20 @@ abstract class BaseActivity extends ListActivity implements View.OnCreateContext
|
||||||
}
|
}
|
||||||
|
|
||||||
protected abstract int getCoverResourceId(FBTree tree);
|
protected abstract int getCoverResourceId(FBTree tree);
|
||||||
|
abstract boolean isTreeSelected(FBTree tree);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onListItemClick(ListView listView, View view, int position, long rowId) {
|
public boolean onKeyDown(int keyCode, KeyEvent event) {
|
||||||
final LibraryTree tree = (LibraryTree)getListAdapter().getItem(position);
|
if (keyCode == KeyEvent.KEYCODE_BACK && myCurrentTree.Parent != null) {
|
||||||
final Book book = tree.getBook();
|
final FBTree oldTree = myCurrentTree;
|
||||||
if (book != null) {
|
new OpenTreeRunnable(myCurrentTree.Parent).run();
|
||||||
showBookInfo(book);
|
setSelection(getListAdapter().getIndex(oldTree));
|
||||||
|
return true;
|
||||||
} else {
|
} else {
|
||||||
new OpenTreeRunnable(tree).run();
|
return super.onKeyDown(keyCode, event);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
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());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void deleteBook(Book book, int mode) {
|
|
||||||
LibraryInstance.removeBook(book, mode);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void showBookInfo(Book book) {
|
|
||||||
startActivityForResult(
|
|
||||||
new Intent(getApplicationContext(), BookInfoActivity.class)
|
|
||||||
.putExtra(BookInfoActivity.CURRENT_BOOK_PATH_KEY, book.File.getPath()),
|
|
||||||
BOOK_INFO_REQUEST
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
@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;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected class OpenTreeRunnable implements Runnable {
|
protected class OpenTreeRunnable implements Runnable {
|
||||||
private final FBTree myTree;
|
private final FBTree myTree;
|
||||||
|
|
||||||
|
@ -267,12 +111,10 @@ abstract class BaseActivity extends ListActivity implements View.OnCreateContext
|
||||||
switch (myTree.getOpeningStatus()) {
|
switch (myTree.getOpeningStatus()) {
|
||||||
case READY_TO_OPEN:
|
case READY_TO_OPEN:
|
||||||
case ALWAYS_RELOAD_BEFORE_OPENING:
|
case ALWAYS_RELOAD_BEFORE_OPENING:
|
||||||
startActivityForResult(
|
myCurrentTree = myTree;
|
||||||
new Intent(BaseActivity.this, LibraryTreeActivity.class)
|
getListAdapter().replaceAll(myCurrentTree.subTrees());
|
||||||
.putExtra(SELECTED_BOOK_PATH_KEY, mySelectedBookPath)
|
setTitle(myCurrentTree.getTreeTitle());
|
||||||
.putExtra(TREE_KEY_KEY, myTree.getUniqueKey()),
|
setSelection(getListAdapter().getFirstSelectedItemIndex());
|
||||||
CHILD_LIST_REQUEST
|
|
||||||
);
|
|
||||||
break;
|
break;
|
||||||
case CANNOT_OPEN:
|
case CANNOT_OPEN:
|
||||||
UIUtil.showErrorMessage(BaseActivity.this, myTree.getOpeningStatusMessage());
|
UIUtil.showErrorMessage(BaseActivity.this, myTree.getOpeningStatusMessage());
|
||||||
|
|
|
@ -20,12 +20,17 @@
|
||||||
package org.geometerplus.android.fbreader.library;
|
package org.geometerplus.android.fbreader.library;
|
||||||
|
|
||||||
import android.app.SearchManager;
|
import android.app.SearchManager;
|
||||||
|
import android.app.AlertDialog;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.view.Menu;
|
import android.content.DialogInterface;
|
||||||
import android.view.MenuItem;
|
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.options.ZLStringOption;
|
||||||
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
|
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
|
||||||
|
import org.geometerplus.zlibrary.core.resources.ZLResource;
|
||||||
|
|
||||||
import org.geometerplus.zlibrary.ui.android.R;
|
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.library.*;
|
||||||
import org.geometerplus.fbreader.tree.FBTree;
|
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 =
|
static final ZLStringOption BookSearchPatternOption =
|
||||||
new ZLStringOption("BookSearch", "Pattern", "");
|
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
|
@Override
|
||||||
protected void onActivityResult(int requestCode, int returnCode, Intent intent) {
|
protected void onActivityResult(int requestCode, int returnCode, Intent intent) {
|
||||||
if (requestCode == CHILD_LIST_REQUEST && returnCode == RESULT_DO_INVALIDATE_VIEWS) {
|
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");
|
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
|
@Override
|
||||||
public boolean onCreateOptionsMenu(Menu menu) {
|
public boolean onCreateOptionsMenu(Menu menu) {
|
||||||
super.onCreateOptionsMenu(menu);
|
super.onCreateOptionsMenu(menu);
|
||||||
|
@ -105,6 +273,9 @@ abstract class LibraryActivity extends BaseActivity implements MenuItem.OnMenuIt
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Item icons
|
||||||
|
//
|
||||||
@Override
|
@Override
|
||||||
protected int getCoverResourceId(FBTree tree) {
|
protected int getCoverResourceId(FBTree tree) {
|
||||||
if (((LibraryTree)tree).getBook() != null) {
|
if (((LibraryTree)tree).getBook() != null) {
|
||||||
|
@ -142,9 +313,40 @@ abstract class LibraryActivity extends BaseActivity implements MenuItem.OnMenuIt
|
||||||
return R.drawable.ic_list_library_books;
|
return R.drawable.ic_list_library_books;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
//
|
||||||
protected void deleteBook(Book book, int mode) {
|
// Book deletion
|
||||||
super.deleteBook(book, mode);
|
//
|
||||||
|
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) {
|
if (myCurrentTree instanceof FileTree) {
|
||||||
getListAdapter().remove(new FileTree((FileTree)myCurrentTree, book.File));
|
getListAdapter().remove(new FileTree((FileTree)myCurrentTree, book.File));
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -46,7 +46,7 @@ public class LibraryTopLevelActivity extends LibraryActivity {
|
||||||
final List<FBTree> trees = myCurrentTree.subTrees();
|
final List<FBTree> trees = myCurrentTree.subTrees();
|
||||||
getListAdapter().replaceAll(trees);
|
getListAdapter().replaceAll(trees);
|
||||||
getListView().invalidateViews();
|
getListView().invalidateViews();
|
||||||
new OpenTreeRunnable(trees.get(0)).run();
|
//new OpenTreeRunnable(trees.get(0)).run();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onNewIntent(Intent intent) {
|
public void onNewIntent(Intent intent) {
|
||||||
|
|
|
@ -44,7 +44,6 @@ public class LibraryTreeActivity extends LibraryActivity {
|
||||||
|
|
||||||
final ListAdapter adapter = new ListAdapter(this, myCurrentTree.subTrees());
|
final ListAdapter adapter = new ListAdapter(this, myCurrentTree.subTrees());
|
||||||
setSelection(adapter.getFirstSelectedItemIndex());
|
setSelection(adapter.getFirstSelectedItemIndex());
|
||||||
|
|
||||||
getListView().setTextFilterEnabled(true);
|
getListView().setTextFilterEnabled(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -96,6 +96,10 @@ public class ListAdapter extends BaseAdapter {
|
||||||
return position;
|
return position;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int getIndex(FBTree item) {
|
||||||
|
return myItems.indexOf(item);
|
||||||
|
}
|
||||||
|
|
||||||
public int getFirstSelectedItemIndex() {
|
public int getFirstSelectedItemIndex() {
|
||||||
int index = 0;
|
int index = 0;
|
||||||
synchronized (myItems) {
|
synchronized (myItems) {
|
||||||
|
|
|
@ -36,10 +36,6 @@ public abstract class LibraryTree extends FBTree {
|
||||||
super(parent, position);
|
super(parent, position);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getTreeTitle() {
|
|
||||||
return getName();
|
|
||||||
}
|
|
||||||
|
|
||||||
public Book getBook() {
|
public Book getBook() {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
|
@ -115,6 +115,10 @@ public abstract class FBTree extends ZLTree<FBTree> implements Comparable<FBTree
|
||||||
|
|
||||||
public abstract String getName();
|
public abstract String getName();
|
||||||
|
|
||||||
|
public String getTreeTitle() {
|
||||||
|
return getName();
|
||||||
|
}
|
||||||
|
|
||||||
protected String getSortKey() {
|
protected String getSortKey() {
|
||||||
return getName();
|
return getName();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue