diff --git a/src/org/geometerplus/android/fbreader/library/LibraryActivity.java b/src/org/geometerplus/android/fbreader/library/LibraryActivity.java index 19f0f1480..02e7236bf 100644 --- a/src/org/geometerplus/android/fbreader/library/LibraryActivity.java +++ b/src/org/geometerplus/android/fbreader/library/LibraryActivity.java @@ -41,7 +41,7 @@ import org.geometerplus.fbreader.tree.FBTree; import org.geometerplus.android.util.UIUtil; import org.geometerplus.android.fbreader.*; -import org.geometerplus.android.fbreader.libraryService.SQLiteBooksDatabase; +import org.geometerplus.android.fbreader.libraryService.BookCollectionShadow; import org.geometerplus.android.fbreader.tree.TreeActivity; public class LibraryActivity extends TreeActivity implements MenuItem.OnMenuItemClickListener, View.OnCreateContextMenuListener, Library.ChangeListener { @@ -57,14 +57,8 @@ public class LibraryActivity extends TreeActivity implements MenuItem.OnMenuItem super.onCreate(icicle); if (myLibrary == null) { - BooksDatabase db = SQLiteBooksDatabase.Instance(); - if (db == null) { - db = new SQLiteBooksDatabase(this, "LIBRARY"); - } - final BookCollection collection = new BookCollection(db); - myLibrary = new Library(collection); + myLibrary = new Library(new BookCollectionShadow(this)); myLibrary.addChangeListener(this); - collection.startBuild(); } mySelectedBook = @@ -78,6 +72,16 @@ public class LibraryActivity extends TreeActivity implements MenuItem.OnMenuItem getListView().setOnCreateContextMenuListener(this); } + @Override + protected void onStart() { + super.onStart(); + ((BookCollectionShadow)myLibrary.Collection).bindToService(new Runnable() { + public void run() { + myLibrary.init(); + } + }); + } + @Override protected void onNewIntent(Intent intent) { if (START_SEARCH_ACTION.equals(intent.getAction())) { @@ -102,6 +106,12 @@ public class LibraryActivity extends TreeActivity implements MenuItem.OnMenuItem return key != null ? myLibrary.getLibraryTree(key) : myLibrary.getRootTree(); } + @Override + protected void onStop() { + ((BookCollectionShadow)myLibrary.Collection).unbind(); + super.onStop(); + } + @Override protected void onDestroy() { myLibrary.removeChangeListener(this); diff --git a/src/org/geometerplus/android/fbreader/libraryService/LibraryService.java b/src/org/geometerplus/android/fbreader/libraryService/LibraryService.java index 82fd64c8b..f1ec90a80 100644 --- a/src/org/geometerplus/android/fbreader/libraryService/LibraryService.java +++ b/src/org/geometerplus/android/fbreader/libraryService/LibraryService.java @@ -82,11 +82,7 @@ public class LibraryService extends Service { private final List myFileObservers = new LinkedList(); LibraryImplementation() { - BooksDatabase database = SQLiteBooksDatabase.Instance(); - if (database == null) { - database = new SQLiteBooksDatabase(LibraryService.this, "LIBRARY SERVICE"); - } - myCollection = new BookCollection(database); + myCollection = new BookCollection(SQLiteBooksDatabase.Instance(LibraryService.this)); for (String path : myCollection.bookDirectories()) { final Observer observer = new Observer(path); observer.startWatching(); diff --git a/src/org/geometerplus/android/fbreader/libraryService/SQLiteBooksDatabase.java b/src/org/geometerplus/android/fbreader/libraryService/SQLiteBooksDatabase.java index 2b99e0f18..f63b3ca32 100644 --- a/src/org/geometerplus/android/fbreader/libraryService/SQLiteBooksDatabase.java +++ b/src/org/geometerplus/android/fbreader/libraryService/SQLiteBooksDatabase.java @@ -37,17 +37,23 @@ import org.geometerplus.zlibrary.text.view.ZLTextFixedPosition; import org.geometerplus.fbreader.book.*; -import org.geometerplus.android.util.UIUtil; import org.geometerplus.android.util.SQLiteUtil; -public final class SQLiteBooksDatabase extends BooksDatabase { - private final String myInstanceId; +final class SQLiteBooksDatabase extends BooksDatabase { + private static BooksDatabase ourInstance; + + static BooksDatabase Instance(Context context) { + if (ourInstance == null) { + ourInstance = new SQLiteBooksDatabase(context); + } + return ourInstance; + } + private final SQLiteDatabase myDatabase; - public SQLiteBooksDatabase(Context context, String instanceId) { - myInstanceId = instanceId; + private SQLiteBooksDatabase(Context context) { myDatabase = context.openOrCreateDatabase("books.db", Context.MODE_PRIVATE, null); - migrate(context); + migrate(); } protected void executeAsATransaction(Runnable actions) { @@ -69,65 +75,61 @@ public final class SQLiteBooksDatabase extends BooksDatabase { } } - private void migrate(Context context) { + private void migrate() { final int version = myDatabase.getVersion(); final int currentVersion = 20; if (version >= currentVersion) { return; } - UIUtil.wait(version == 0 ? "creatingBooksDatabase" : "updatingBooksDatabase", new Runnable() { - public void run() { - myDatabase.beginTransaction(); + myDatabase.beginTransaction(); - switch (myDatabase.getVersion()) { - case 0: - createTables(); - case 1: - updateTables1(); - case 2: - updateTables2(); - case 3: - updateTables3(); - case 4: - updateTables4(); - case 5: - updateTables5(); - case 6: - updateTables6(); - case 7: - updateTables7(); - case 8: - updateTables8(); - case 9: - updateTables9(); - case 10: - updateTables10(); - case 11: - updateTables11(); - case 12: - updateTables12(); - case 13: - updateTables13(); - case 14: - updateTables14(); - case 15: - updateTables15(); - case 16: - updateTables16(); - case 17: - updateTables17(); - case 18: - updateTables18(); - case 19: - updateTables19(); - } - myDatabase.setTransactionSuccessful(); - myDatabase.setVersion(currentVersion); - myDatabase.endTransaction(); + switch (myDatabase.getVersion()) { + case 0: + createTables(); + case 1: + updateTables1(); + case 2: + updateTables2(); + case 3: + updateTables3(); + case 4: + updateTables4(); + case 5: + updateTables5(); + case 6: + updateTables6(); + case 7: + updateTables7(); + case 8: + updateTables8(); + case 9: + updateTables9(); + case 10: + updateTables10(); + case 11: + updateTables11(); + case 12: + updateTables12(); + case 13: + updateTables13(); + case 14: + updateTables14(); + case 15: + updateTables15(); + case 16: + updateTables16(); + case 17: + updateTables17(); + case 18: + updateTables18(); + case 19: + updateTables19(); + } + myDatabase.setTransactionSuccessful(); + myDatabase.setVersion(currentVersion); + myDatabase.endTransaction(); - myDatabase.execSQL("VACUUM"); - } - }, context); + myDatabase.execSQL("VACUUM"); } @Override diff --git a/src/org/geometerplus/fbreader/book/BookCollection.java b/src/org/geometerplus/fbreader/book/BookCollection.java index b1be1b7bb..fb768485d 100644 --- a/src/org/geometerplus/fbreader/book/BookCollection.java +++ b/src/org/geometerplus/fbreader/book/BookCollection.java @@ -35,6 +35,7 @@ public class BookCollection extends AbstractBookCollection { Collections.synchronizedMap(new LinkedHashMap()); private final Map myBooksById = Collections.synchronizedMap(new HashMap()); + private static enum BuildStatus { NotStarted, Started, @@ -264,7 +265,7 @@ public class BookCollection extends AbstractBookCollection { } } }; - builder.setPriority((Thread.MIN_PRIORITY + Thread.NORM_PRIORITY) / 2); + builder.setPriority(Thread.MIN_PRIORITY); builder.start(); } diff --git a/src/org/geometerplus/fbreader/book/BooksDatabase.java b/src/org/geometerplus/fbreader/book/BooksDatabase.java index 4a5eadfb5..ebc6f401b 100644 --- a/src/org/geometerplus/fbreader/book/BooksDatabase.java +++ b/src/org/geometerplus/fbreader/book/BooksDatabase.java @@ -26,16 +26,6 @@ import org.geometerplus.zlibrary.core.filesystem.ZLFile; import org.geometerplus.zlibrary.text.view.ZLTextPosition; public abstract class BooksDatabase { - private static BooksDatabase ourInstance; - - public static BooksDatabase Instance() { - return ourInstance; - } - - protected BooksDatabase() { - ourInstance = this; - } - protected Book createBook(long id, long fileId, String title, String encoding, String language) { final FileInfoSet infos = new FileInfoSet(this, fileId); return createBook(id, infos.getFile(fileId), title, encoding, language); diff --git a/src/org/geometerplus/fbreader/library/Library.java b/src/org/geometerplus/fbreader/library/Library.java index e5472b3c4..d91f758ce 100644 --- a/src/org/geometerplus/fbreader/library/Library.java +++ b/src/org/geometerplus/fbreader/library/Library.java @@ -113,12 +113,6 @@ public final class Library { switch (event) { case Started: Library.this.fireModelChangedEvent(ChangeListener.Code.StatusChanged); - for (Book book : Collection.recentBooks()) { - new BookTree(getFirstLevelTree(ROOT_RECENT), book, true); - } - for (Book book : Collection.favorites()) { - new BookTree(getFirstLevelTree(ROOT_FAVORITES), book, true); - } setStatus(myStatusMask | STATUS_LOADING); break; case Completed: @@ -137,6 +131,15 @@ public final class Library { new FileFirstLevelTree(collection, myRootTree, ROOT_FILE_TREE); } + public void init() { + for (Book book : Collection.recentBooks()) { + new BookTree(getFirstLevelTree(ROOT_RECENT), book, true); + } + for (Book book : Collection.favorites()) { + new BookTree(getFirstLevelTree(ROOT_FAVORITES), book, true); + } + } + public LibraryTree getRootTree() { return myRootTree; }