mirror of
https://github.com/geometer/FBReaderJ.git
synced 2025-10-05 19:42:17 +02:00
a service for background library initialization has been added
This commit is contained in:
parent
9103c30cb5
commit
703ab47731
9 changed files with 96 additions and 413 deletions
|
@ -60,6 +60,7 @@
|
|||
</intent-filter>
|
||||
<meta-data android:name="android.app.searchable" android:resource="@xml/searchable" />
|
||||
</activity>
|
||||
<!--
|
||||
<activity android:name="org.geometerplus.android.fbreader.BookSearchActivity" android:process=":library">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.SEARCH" />
|
||||
|
@ -67,6 +68,7 @@
|
|||
</intent-filter>
|
||||
<meta-data android:name="android.app.searchable" android:resource="@xml/searchable" />
|
||||
</activity>
|
||||
-->
|
||||
<activity android:name="org.geometerplus.android.fbreader.BookmarkSearchActivity">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.SEARCH" />
|
||||
|
@ -74,9 +76,12 @@
|
|||
</intent-filter>
|
||||
<meta-data android:name="android.app.searchable" android:resource="@xml/searchable" />
|
||||
</activity>
|
||||
<!--
|
||||
<activity android:name="org.geometerplus.android.fbreader.LibraryTabActivity" android:process=":library" android:configChanges="orientation|keyboardHidden">
|
||||
<meta-data android:name="android.app.default_searchable" android:value="org.geometerplus.android.fbreader.BookSearchActivity" />
|
||||
</activity>
|
||||
-->
|
||||
<service android:name="org.geometerplus.android.fbreader.library.InitializationService" android:process=":library" />
|
||||
<activity android:name="org.geometerplus.android.fbreader.library.LibraryTopLevelActivity" android:process=":library" android:configChanges="orientation|keyboardHidden">
|
||||
<meta-data android:name="android.app.default_searchable" android:value="org.geometerplus.android.fbreader.BookSearchActivity" />
|
||||
</activity>
|
||||
|
|
|
@ -60,6 +60,7 @@
|
|||
</intent-filter>
|
||||
<meta-data android:name="android.app.searchable" android:resource="@xml/searchable" />
|
||||
</activity>
|
||||
<!--
|
||||
<activity android:name="org.geometerplus.android.fbreader.BookSearchActivity" android:process=":library">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.SEARCH" />
|
||||
|
@ -67,6 +68,7 @@
|
|||
</intent-filter>
|
||||
<meta-data android:name="android.app.searchable" android:resource="@xml/searchable" />
|
||||
</activity>
|
||||
-->
|
||||
<activity android:name="org.geometerplus.android.fbreader.BookmarkSearchActivity">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.SEARCH" />
|
||||
|
@ -74,9 +76,12 @@
|
|||
</intent-filter>
|
||||
<meta-data android:name="android.app.searchable" android:resource="@xml/searchable" />
|
||||
</activity>
|
||||
<!--
|
||||
<activity android:name="org.geometerplus.android.fbreader.LibraryTabActivity" android:process=":library" android:configChanges="orientation|keyboardHidden">
|
||||
<meta-data android:name="android.app.default_searchable" android:value="org.geometerplus.android.fbreader.BookSearchActivity" />
|
||||
</activity>
|
||||
-->
|
||||
<service android:name="org.geometerplus.android.fbreader.library.InitializationService" android:process=":library" />
|
||||
<activity android:name="org.geometerplus.android.fbreader.library.LibraryTopLevelActivity" android:process=":library" android:configChanges="orientation|keyboardHidden">
|
||||
<meta-data android:name="android.app.default_searchable" android:value="org.geometerplus.android.fbreader.BookSearchActivity" />
|
||||
</activity>
|
||||
|
|
|
@ -2,8 +2,10 @@
|
|||
* Database updating in backgorund
|
||||
* Watch filesystem after loading
|
||||
* Covers loading in background
|
||||
* Wait messages (?)
|
||||
DONE Wait messages
|
||||
* Favorites
|
||||
* Search
|
||||
* File view
|
||||
* Activity caption
|
||||
* Book deleting
|
||||
* Show book info activity instead of immediate opening/menu
|
||||
|
|
|
@ -1,60 +0,0 @@
|
|||
/*
|
||||
* Copyright (C) 2009-2010 Geometer Plus <contact@geometerplus.com>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
||||
* 02110-1301, USA.
|
||||
*/
|
||||
|
||||
package org.geometerplus.android.fbreader;
|
||||
|
||||
import android.app.Activity;
|
||||
|
||||
import org.geometerplus.fbreader.library.*;
|
||||
|
||||
public class BookSearchActivity extends SearchActivity {
|
||||
private LibraryTree myTree;
|
||||
|
||||
@Override
|
||||
void onSuccess() {
|
||||
LibraryTabActivity.Instance.showSearchResultsTab(myTree);
|
||||
}
|
||||
|
||||
/*@Override
|
||||
void onFailure() {
|
||||
}*/
|
||||
|
||||
@Override
|
||||
String getFailureMessageResourceKey() {
|
||||
return "bookNotFound";
|
||||
}
|
||||
|
||||
@Override
|
||||
String getWaitMessageResourceKey() {
|
||||
return "search";
|
||||
}
|
||||
|
||||
@Override
|
||||
boolean runSearch(final String pattern) {
|
||||
final LibraryTabActivity parentActivity = LibraryTabActivity.Instance;
|
||||
parentActivity.BookSearchPatternOption.setValue(pattern);
|
||||
myTree = parentActivity.library().searchBooks(pattern);
|
||||
return myTree.hasChildren();
|
||||
}
|
||||
|
||||
@Override
|
||||
Activity getParentActivity() {
|
||||
return LibraryTabActivity.Instance;
|
||||
}
|
||||
}
|
|
@ -1,325 +0,0 @@
|
|||
/*
|
||||
* Copyright (C) 2009-2010 Geometer Plus <contact@geometerplus.com>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
||||
* 02110-1301, USA.
|
||||
*/
|
||||
|
||||
package org.geometerplus.android.fbreader;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import android.app.*;
|
||||
import android.os.Bundle;
|
||||
import android.view.*;
|
||||
import android.widget.*;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
|
||||
import org.geometerplus.zlibrary.ui.android.R;
|
||||
|
||||
import org.geometerplus.zlibrary.core.tree.ZLTree;
|
||||
import org.geometerplus.zlibrary.core.options.ZLStringOption;
|
||||
import org.geometerplus.zlibrary.core.resources.ZLResource;
|
||||
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
|
||||
|
||||
import org.geometerplus.fbreader.library.*;
|
||||
import org.geometerplus.fbreader.tree.FBTree;
|
||||
|
||||
public class LibraryTabActivity extends TabActivity implements MenuItem.OnMenuItemClickListener {
|
||||
public static final String CURRENT_BOOK_PATH_KEY = "CurrentBookPath";
|
||||
|
||||
static LibraryTabActivity Instance;
|
||||
|
||||
final ZLStringOption BookSearchPatternOption = new ZLStringOption("BookSearch", "Pattern", "");
|
||||
final ZLStringOption mySelectedTabOption = new ZLStringOption("TabActivity", "SelectedTab", "");
|
||||
|
||||
private final ZLResource myResource = ZLResource.resource("libraryView");
|
||||
private String myCurrentBookPath;
|
||||
|
||||
private Library myLibrary;
|
||||
|
||||
Library library() {
|
||||
return myLibrary;
|
||||
}
|
||||
|
||||
private ListView createTab(String tag, int viewId, int iconId) {
|
||||
final TabHost host = getTabHost();
|
||||
final String label = myResource.getResource(tag).getValue();
|
||||
host.addTab(host.newTabSpec(tag).setIndicator(label, getResources().getDrawable(iconId)).setContent(viewId));
|
||||
return (ListView)findViewById(viewId);
|
||||
}
|
||||
|
||||
private void createDefaultTabs() {
|
||||
new LibraryAdapter(createTab("byAuthor", R.id.by_author, R.drawable.ic_tab_library_author), myLibrary.byAuthor(), Type.TREE);
|
||||
new LibraryAdapter(createTab("byTag", R.id.by_tag, R.drawable.ic_tab_library_tag), myLibrary.byTag(), Type.TREE);
|
||||
new LibraryAdapter(createTab("recent", R.id.recent, R.drawable.ic_tab_library_recent), myLibrary.recentBooks(), Type.FLAT);
|
||||
findViewById(R.id.search_results).setVisibility(View.GONE);
|
||||
}
|
||||
|
||||
private boolean isSelectedItem(FBTree tree) {
|
||||
return
|
||||
(tree instanceof BookTree) &&
|
||||
((BookTree)tree).Book.File.getPath().equals(myCurrentBookPath);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle icicle) {
|
||||
super.onCreate(icicle);
|
||||
|
||||
Thread.setDefaultUncaughtExceptionHandler(new org.geometerplus.zlibrary.ui.android.library.UncaughtExceptionHandler(this));
|
||||
|
||||
if (SQLiteBooksDatabase.Instance() == null) {
|
||||
new SQLiteBooksDatabase(this, "LIBRARY");
|
||||
}
|
||||
|
||||
if (myLibrary == null) {
|
||||
myLibrary = new Library();
|
||||
}
|
||||
final Runnable action = new Runnable() {
|
||||
public void run() {
|
||||
myLibrary.clear();
|
||||
myLibrary.synchronize();
|
||||
}
|
||||
};
|
||||
action.run();
|
||||
//UIUtil.wait("loadingBookList", action, this);
|
||||
|
||||
final Intent intent = getIntent();
|
||||
myCurrentBookPath = intent.getStringExtra(CURRENT_BOOK_PATH_KEY);
|
||||
|
||||
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
||||
setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
|
||||
|
||||
final TabHost host = getTabHost();
|
||||
LayoutInflater.from(this).inflate(R.layout.library, host.getTabContentView(), true);
|
||||
|
||||
createDefaultTabs();
|
||||
|
||||
host.setCurrentTabByTag(mySelectedTabOption.getValue());
|
||||
}
|
||||
|
||||
private LibraryAdapter mySearchResultsAdapter;
|
||||
void showSearchResultsTab(LibraryTree tree) {
|
||||
if (mySearchResultsAdapter == null) {
|
||||
mySearchResultsAdapter =
|
||||
new LibraryAdapter(createTab("searchResults", R.id.search_results, R.drawable.ic_tab_library_results), tree, Type.FLAT);
|
||||
} else {
|
||||
mySearchResultsAdapter.resetTree(tree);
|
||||
}
|
||||
getTabHost().setCurrentTabByTag("searchResults");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
Instance = this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPause() {
|
||||
super.onPause();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStop() {
|
||||
mySelectedTabOption.setValue(getTabHost().getCurrentTabTag());
|
||||
Instance = null;
|
||||
super.onStop();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCreateOptionsMenu(Menu menu) {
|
||||
super.onCreateOptionsMenu(menu);
|
||||
addMenuItem(menu, 1, "localSearch", R.drawable.ic_menu_search);
|
||||
return true;
|
||||
}
|
||||
|
||||
private MenuItem addMenuItem(Menu menu, int index, String resourceKey, int iconId) {
|
||||
final String label = myResource.getResource("menu").getResource(resourceKey).getValue();
|
||||
final MenuItem item = menu.add(0, index, Menu.NONE, label);
|
||||
item.setOnMenuItemClickListener(this);
|
||||
item.setIcon(iconId);
|
||||
return item;
|
||||
}
|
||||
|
||||
public boolean onMenuItemClick(MenuItem item) {
|
||||
switch (item.getItemId()) {
|
||||
case 1:
|
||||
return onSearchRequested();
|
||||
default:
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onSearchRequested() {
|
||||
startSearch(BookSearchPatternOption.getValue(), true, null, false);
|
||||
return true;
|
||||
}
|
||||
|
||||
interface Type {
|
||||
int TREE = 0;
|
||||
int FLAT = 1;
|
||||
}
|
||||
|
||||
private final class LibraryAdapter extends ZLTreeAdapter {
|
||||
private final LibraryTree myLibraryTree;
|
||||
|
||||
private final int myType;
|
||||
|
||||
LibraryAdapter(ListView view, LibraryTree tree, int type) {
|
||||
super(view, tree);
|
||||
myLibraryTree = tree;
|
||||
myType = type;
|
||||
selectItem(findFirstSelectedItem());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo) {
|
||||
final int position = ((AdapterView.AdapterContextMenuInfo)menuInfo).position;
|
||||
final LibraryTree tree = (LibraryTree)getItem(position);
|
||||
if (tree instanceof BookTree) {
|
||||
menu.setHeaderTitle(tree.getName());
|
||||
final ZLResource resource = ZLResource.resource("libraryView");
|
||||
menu.add(0, OPEN_BOOK_ITEM_ID, 0, resource.getResource("openBook").getValue());
|
||||
if ((myLibrary.getRemoveBookMode(((BookTree)tree).Book)
|
||||
& Library.REMOVE_FROM_DISK) != 0) {
|
||||
menu.add(0, DELETE_BOOK_ITEM_ID, 0, resource.getResource("deleteBook").getValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private ZLTree<?> findFirstSelectedItem() {
|
||||
if (myCurrentBookPath == null) {
|
||||
return null;
|
||||
}
|
||||
for (FBTree tree : myLibraryTree) {
|
||||
if (isSelectedItem(tree)) {
|
||||
return tree;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public View getView(int position, View convertView, ViewGroup parent) {
|
||||
final View view = (convertView != null) ? convertView :
|
||||
LayoutInflater.from(parent.getContext()).inflate(R.layout.library_tree_item, parent, false);
|
||||
final LibraryTree tree = (LibraryTree)getItem(position);
|
||||
if (isSelectedItem(tree)) {
|
||||
view.setBackgroundColor(0xff808080);
|
||||
} else {
|
||||
view.setBackgroundColor(0);
|
||||
}
|
||||
final ImageView iconView = (ImageView)view.findViewById(R.id.library_tree_item_icon);
|
||||
switch (myType) {
|
||||
case Type.FLAT:
|
||||
iconView.setVisibility(View.GONE);
|
||||
break;
|
||||
case Type.TREE:
|
||||
setIcon(iconView, tree);
|
||||
break;
|
||||
}
|
||||
((TextView)view.findViewById(R.id.library_tree_item_name)).setText(tree.getName());
|
||||
((TextView)view.findViewById(R.id.library_tree_item_childrenlist)).setText(tree.getSecondString());
|
||||
return view;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean runTreeItem(ZLTree<?> tree) {
|
||||
if (super.runTreeItem(tree)) {
|
||||
return true;
|
||||
}
|
||||
finish();
|
||||
final Book book = ((BookTree)tree).Book;
|
||||
if (!book.File.getPath().equals(myCurrentBookPath)) {
|
||||
ZLFile physicalFile = book.File.getPhysicalFile();
|
||||
startActivity(getFBReaderIntent(physicalFile != null ? new File(physicalFile.getPath()) : null));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
private Intent getFBReaderIntent(final File file) {
|
||||
final Intent intent = new Intent(getApplicationContext(), FBReader.class);
|
||||
intent.setAction(Intent.ACTION_VIEW);
|
||||
if (file != null) {
|
||||
intent.setData(Uri.fromFile(file));
|
||||
} else {
|
||||
intent.setData(Uri.parse("file:///"));
|
||||
}
|
||||
return intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
}
|
||||
|
||||
private static final int OPEN_BOOK_ITEM_ID = 0;
|
||||
private static final int DELETE_BOOK_ITEM_ID = 1;
|
||||
|
||||
@Override
|
||||
public boolean onContextItemSelected(MenuItem item) {
|
||||
final LibraryAdapter adapter =
|
||||
(LibraryAdapter)((ListView)getTabHost().getCurrentView()).getAdapter();
|
||||
final int position = ((AdapterView.AdapterContextMenuInfo)item.getMenuInfo()).position;
|
||||
final BookTree tree = (BookTree)adapter.getItem(position);
|
||||
switch (item.getItemId()) {
|
||||
case OPEN_BOOK_ITEM_ID:
|
||||
adapter.runTreeItem(tree);
|
||||
return true;
|
||||
case DELETE_BOOK_ITEM_ID:
|
||||
tryToDeleteBook(tree.Book);
|
||||
return true;
|
||||
}
|
||||
return super.onContextItemSelected(item);
|
||||
}
|
||||
|
||||
private class BookDeleter implements DialogInterface.OnClickListener {
|
||||
private final Book myBook;
|
||||
private final int myMode;
|
||||
|
||||
BookDeleter(Book book, int removeMode) {
|
||||
myBook = book;
|
||||
myMode = removeMode;
|
||||
}
|
||||
|
||||
private void invalidateView(View v) {
|
||||
ZLTreeAdapter adapter = (ZLTreeAdapter)((ListView)v).getAdapter();
|
||||
if (adapter != null) {
|
||||
adapter.resetTree();
|
||||
}
|
||||
}
|
||||
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
myLibrary.removeBook(myBook, myMode);
|
||||
|
||||
invalidateView(findViewById(R.id.by_author));
|
||||
invalidateView(findViewById(R.id.by_tag));
|
||||
invalidateView(findViewById(R.id.recent));
|
||||
invalidateView(findViewById(R.id.search_results));
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
/*
|
||||
* Copyright (C) 2010 Geometer Plus <contact@geometerplus.com>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
||||
* 02110-1301, USA.
|
||||
*/
|
||||
|
||||
package org.geometerplus.android.fbreader.library;
|
||||
|
||||
import android.app.Service;
|
||||
import android.content.Intent;
|
||||
import android.os.IBinder;
|
||||
|
||||
public class InitializationService extends Service {
|
||||
@Override
|
||||
public IBinder onBind(Intent intent) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStart(Intent intent, int startId) {
|
||||
LibraryTopLevelActivity.Library.synchronize();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||
onStart(intent, startId);
|
||||
return 0;
|
||||
}
|
||||
}
|
|
@ -39,6 +39,7 @@ import org.geometerplus.fbreader.library.*;
|
|||
|
||||
import org.geometerplus.zlibrary.ui.android.R;
|
||||
|
||||
import org.geometerplus.android.util.UIUtil;
|
||||
import org.geometerplus.android.fbreader.tree.ZLAndroidTree;
|
||||
|
||||
abstract class LibraryBaseActivity extends ListActivity {
|
||||
|
@ -141,6 +142,13 @@ abstract class LibraryBaseActivity extends ListActivity {
|
|||
}
|
||||
|
||||
public void run() {
|
||||
if (!LibraryTopLevelActivity.Library.hasState(Library.STATE_FULLY_INITIALIZED)) {
|
||||
UIUtil.wait("loadingBookList", new Runnable() {
|
||||
public void run() {
|
||||
LibraryTopLevelActivity.Library.waitForState(Library.STATE_FULLY_INITIALIZED);
|
||||
}
|
||||
}, LibraryBaseActivity.this);
|
||||
}
|
||||
startActivity(
|
||||
new Intent(LibraryBaseActivity.this, LibraryTreeActivity.class)
|
||||
.putExtra(SELECTED_BOOK_PATH_KEY, mySelectedBookPath)
|
||||
|
|
|
@ -50,6 +50,7 @@ public class LibraryTopLevelActivity extends LibraryBaseActivity {
|
|||
}
|
||||
if (Library == null) {
|
||||
Library = new Library();
|
||||
startService(new Intent(getApplicationContext(), InitializationService.class));
|
||||
}
|
||||
|
||||
final ArrayList<FBTree> items = new ArrayList<FBTree>();
|
||||
|
|
|
@ -28,6 +28,9 @@ import org.geometerplus.zlibrary.core.util.ZLMiscUtil;
|
|||
import org.geometerplus.fbreader.Paths;
|
||||
|
||||
public final class Library {
|
||||
public static final int STATE_NOT_INITIALIZED = 0;
|
||||
public static final int STATE_FULLY_INITIALIZED = 1;
|
||||
|
||||
private final LinkedList<Book> myBooks = new LinkedList<Book>();
|
||||
private final HashSet<Book> myExternalBooks = new HashSet<Book>();
|
||||
private final LibraryTree myLibraryByAuthor = new RootTree();
|
||||
|
@ -35,20 +38,28 @@ public final class Library {
|
|||
private final LibraryTree myRecentBooks = new RootTree();
|
||||
private final LibraryTree mySearchResult = new RootTree();
|
||||
|
||||
private boolean myDoRebuild = true;
|
||||
private volatile int myState = STATE_NOT_INITIALIZED;
|
||||
|
||||
public Library() {
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
myDoRebuild = true;
|
||||
public boolean hasState(int state) {
|
||||
return myState >= state;
|
||||
}
|
||||
|
||||
myBooks.clear();
|
||||
myExternalBooks.clear();
|
||||
myLibraryByAuthor.clear();
|
||||
myLibraryByTag.clear();
|
||||
myRecentBooks.clear();
|
||||
mySearchResult.clear();
|
||||
public void waitForState(int state) {
|
||||
while (myState < state) {
|
||||
synchronized(this) {
|
||||
if (myState < state) {
|
||||
System.err.println("waiting for: " + state + " (" + myState + ")");
|
||||
try {
|
||||
wait();
|
||||
} catch (InterruptedException e) {
|
||||
}
|
||||
System.err.println("found: " + state + " (" + myState + ")");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static ZLResourceFile getHelpFile() {
|
||||
|
@ -252,7 +263,6 @@ public final class Library {
|
|||
}
|
||||
|
||||
final BooksDatabase db = BooksDatabase.Instance();
|
||||
myRecentBooks.clear();
|
||||
for (long id : db.loadRecentBookIds()) {
|
||||
Book book = bookById.get(id);
|
||||
if (book != null) {
|
||||
|
@ -269,37 +279,32 @@ public final class Library {
|
|||
});
|
||||
}
|
||||
|
||||
public void synchronize() {
|
||||
if (myDoRebuild) {
|
||||
public synchronized void synchronize() {
|
||||
if (myState == STATE_NOT_INITIALIZED) {
|
||||
System.err.println("building: start");
|
||||
build();
|
||||
|
||||
myLibraryByAuthor.sortAllChildren();
|
||||
myLibraryByTag.sortAllChildren();
|
||||
|
||||
myDoRebuild = false;
|
||||
myState = STATE_FULLY_INITIALIZED;
|
||||
notifyAll();
|
||||
System.err.println("building: done");
|
||||
}
|
||||
}
|
||||
|
||||
public LibraryTree byAuthor() {
|
||||
synchronize();
|
||||
waitForState(STATE_FULLY_INITIALIZED);
|
||||
return myLibraryByAuthor;
|
||||
}
|
||||
|
||||
public LibraryTree byTag() {
|
||||
synchronize();
|
||||
waitForState(STATE_FULLY_INITIALIZED);
|
||||
return myLibraryByTag;
|
||||
}
|
||||
|
||||
public LibraryTree recentBooks() {
|
||||
if (!myRecentBooks.hasChildren()) {
|
||||
final BooksDatabase db = BooksDatabase.Instance();
|
||||
for (long id : db.loadRecentBookIds()) {
|
||||
Book book = Book.getById(id);
|
||||
if (book != null) {
|
||||
myRecentBooks.createBookSubTree(book, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
waitForState(STATE_FULLY_INITIALIZED);
|
||||
return myRecentBooks;
|
||||
}
|
||||
|
||||
|
@ -309,7 +314,7 @@ public final class Library {
|
|||
}
|
||||
|
||||
public LibraryTree searchBooks(String pattern) {
|
||||
synchronize();
|
||||
waitForState(STATE_FULLY_INITIALIZED);
|
||||
mySearchResult.clear();
|
||||
if (pattern != null) {
|
||||
pattern = pattern.toLowerCase();
|
||||
|
@ -341,7 +346,7 @@ public final class Library {
|
|||
public static final int REMOVE_FROM_LIBRARY_AND_DISK = REMOVE_FROM_LIBRARY | REMOVE_FROM_DISK;
|
||||
|
||||
public int getRemoveBookMode(Book book) {
|
||||
synchronize();
|
||||
waitForState(STATE_FULLY_INITIALIZED);
|
||||
return (myExternalBooks.contains(book) ? REMOVE_FROM_LIBRARY : REMOVE_DONT_REMOVE)
|
||||
| (canDeleteBookFile(book) ? REMOVE_FROM_DISK : REMOVE_DONT_REMOVE);
|
||||
}
|
||||
|
@ -364,7 +369,7 @@ public final class Library {
|
|||
if (removeMode == REMOVE_DONT_REMOVE) {
|
||||
return;
|
||||
}
|
||||
synchronize();
|
||||
waitForState(STATE_FULLY_INITIALIZED);
|
||||
myBooks.remove(book);
|
||||
myLibraryByAuthor.removeBook(book);
|
||||
myLibraryByTag.removeBook(book);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue