diff --git a/src/org/geometerplus/android/fbreader/libraryService/LibraryService.java b/src/org/geometerplus/android/fbreader/libraryService/LibraryService.java index 1bed4ede1..c79419fe6 100644 --- a/src/org/geometerplus/android/fbreader/libraryService/LibraryService.java +++ b/src/org/geometerplus/android/fbreader/libraryService/LibraryService.java @@ -29,14 +29,23 @@ import org.geometerplus.android.fbreader.library.SQLiteBooksDatabase; public class LibraryService extends Service implements Library.ChangeListener { public final class LibraryImplementation extends LibraryInterface.Stub { + private final AbstractLibrary myBaseLibrary; + + LibraryImplementation() { + BooksDatabase database = SQLiteBooksDatabase.Instance(); + if (database == null) { + database = new SQLiteBooksDatabase(LibraryService.this, "LIBRARY SERVICE"); + } + myBaseLibrary = new Library(database); + ((Library)myBaseLibrary).startBuild(); + } + public boolean isUpToDate() { - return myLibrary.isUpToDate(); + return myBaseLibrary.isUpToDate(); } } - private LibraryImplementation myImplementation; - private BooksDatabase myDatabase; - private AbstractLibrary myLibrary; + private LibraryImplementation myLibrary; @Override public void onStart(Intent intent, int startId) { @@ -53,29 +62,21 @@ public class LibraryService extends Service implements Library.ChangeListener { @Override public IBinder onBind(Intent intent) { System.err.println("LibraryService binded for intent " + intent); - return myImplementation; + return myLibrary; } @Override public void onCreate() { System.err.println("LibraryService.onCreate()"); super.onCreate(); - myDatabase = SQLiteBooksDatabase.Instance(); - if (myDatabase == null) { - myDatabase = new SQLiteBooksDatabase(this, "LIBRARY SERVICE"); - } - if (myLibrary == null) { - myLibrary = Library.Instance(); - myLibrary.addChangeListener(this); - ((Library)myLibrary).startBuild(); - } - myImplementation = new LibraryImplementation(); + myLibrary = new LibraryImplementation(); + myLibrary.myBaseLibrary.addChangeListener(this); } @Override public void onDestroy() { System.err.println("LibraryService.onDestroy()"); - myLibrary.removeChangeListener(this); + myLibrary.myBaseLibrary.removeChangeListener(this); myLibrary = null; super.onDestroy(); } diff --git a/src/org/geometerplus/fbreader/fbreader/FBReaderApp.java b/src/org/geometerplus/fbreader/fbreader/FBReaderApp.java index 338805ed0..487e0eeb5 100644 --- a/src/org/geometerplus/fbreader/fbreader/FBReaderApp.java +++ b/src/org/geometerplus/fbreader/fbreader/FBReaderApp.java @@ -145,7 +145,7 @@ public final class FBReaderApp extends ZLApplication { public void run() { Book book = createBookForFile(ZLFile.createFileByPath(myArg0)); if (book == null) { - book = Library.getRecentBook(); + book = Library.Instance().getRecentBook(); } if ((book == null) || !book.File.exists()) { book = Book.getByFile(Library.getHelpFile()); @@ -243,7 +243,7 @@ public final class FBReaderApp extends ZLApplication { } else { gotoBookmark(bookmark); } - Library.addBookToRecentList(book); + Library.Instance().addBookToRecentList(book); final StringBuilder title = new StringBuilder(book.getTitle()); if (!book.authors().isEmpty()) { boolean first = true; @@ -347,7 +347,7 @@ public final class FBReaderApp extends ZLApplication { public List getCancelActionsList() { myCancelActionsList.clear(); if (ShowPreviousBookInCancelMenuOption.getValue()) { - final Book previousBook = Library.getPreviousBook(); + final Book previousBook = Library.Instance().getPreviousBook(); if (previousBook != null) { myCancelActionsList.add(new CancelActionDescription( CancelActionType.previousBook, previousBook.getTitle() @@ -375,7 +375,7 @@ public final class FBReaderApp extends ZLApplication { final CancelActionDescription description = myCancelActionsList.get(index); switch (description.Type) { case previousBook: - openBook(Library.getPreviousBook(), null); + openBook(Library.Instance().getPreviousBook(), null); break; case returnTo: { diff --git a/src/org/geometerplus/fbreader/library/Library.java b/src/org/geometerplus/fbreader/library/Library.java index 8c624925c..01ba6c8e6 100644 --- a/src/org/geometerplus/fbreader/library/Library.java +++ b/src/org/geometerplus/fbreader/library/Library.java @@ -40,11 +40,13 @@ public final class Library extends AbstractLibrary { private static Library ourInstance; public static Library Instance() { if (ourInstance == null) { - ourInstance = new Library(); + ourInstance = new Library(BooksDatabase.Instance()); } return ourInstance; } + private final BooksDatabase myDatabase; + private final List myBooks = Collections.synchronizedList(new LinkedList()); private final RootTree myRootTree = new RootTree(); private boolean myDoGroupTitlesByFirstLetter; @@ -58,7 +60,9 @@ public final class Library extends AbstractLibrary { fireModelChangedEvent(ChangeListener.Code.StatusChanged); } - private Library() { + public Library(BooksDatabase db) { + myDatabase = db; + new FavoritesTree(myRootTree, ROOT_FAVORITES); new FirstLevelTree(myRootTree, ROOT_RECENT); new FirstLevelTree(myRootTree, ROOT_BY_AUTHOR); @@ -269,11 +273,9 @@ public final class Library extends AbstractLibrary { } private void build() { - final BooksDatabase db = BooksDatabase.Instance(); - // Step 0: get database books marked as "existing" final FileInfoSet fileInfos = new FileInfoSet(); - final Map savedBooksByFileId = db.loadBooks(fileInfos, true); + final Map savedBooksByFileId = myDatabase.loadBooks(fileInfos, true); final Map savedBooksByBookId = new HashMap(); for (Book b : savedBooksByFileId.values()) { savedBooksByBookId.put(b.getId(), b); @@ -292,7 +294,7 @@ public final class Library extends AbstractLibrary { myDoGroupTitlesByFirstLetter = savedBooksByFileId.values().size() > letterSet.size() * 5 / 4; } - for (long id : db.loadRecentBookIds()) { + for (long id : myDatabase.loadRecentBookIds()) { Book book = savedBooksByBookId.get(id); if (book == null) { book = Book.getById(id); @@ -305,7 +307,7 @@ public final class Library extends AbstractLibrary { } } - for (long id : db.loadFavoritesIds()) { + for (long id : myDatabase.loadFavoritesIds()) { Book book = savedBooksByBookId.get(id); if (book == null) { book = Book.getById(id); @@ -356,11 +358,11 @@ public final class Library extends AbstractLibrary { } } fireModelChangedEvent(ChangeListener.Code.BookAdded); - db.setExistingFlag(orphanedBooks, false); + myDatabase.setExistingFlag(orphanedBooks, false); // Step 3: collect books from physical files; add new, update already added, // unmark orphaned as existing again, collect newly added - final Map orphanedBooksByFileId = db.loadBooks(fileInfos, false); + final Map orphanedBooksByFileId = myDatabase.loadBooks(fileInfos, false); final Set newBooks = new HashSet(); final List physicalFilesList = collectPhysicalFiles(); @@ -387,14 +389,14 @@ public final class Library extends AbstractLibrary { // Step 5: save changes into database fileInfos.save(); - db.executeAsATransaction(new Runnable() { + myDatabase.executeAsATransaction(new Runnable() { public void run() { for (Book book : newBooks) { book.save(); } } }); - db.setExistingFlag(newBooks, true); + myDatabase.setExistingFlag(newBooks, true); } private volatile boolean myBuildStarted = false; @@ -425,13 +427,13 @@ public final class Library extends AbstractLibrary { return myStatusMask == 0; } - public static Book getRecentBook() { - List recentIds = BooksDatabase.Instance().loadRecentBookIds(); + public Book getRecentBook() { + List recentIds = myDatabase.loadRecentBookIds(); return recentIds.size() > 0 ? Book.getById(recentIds.get(0)) : null; } - public static Book getPreviousBook() { - List recentIds = BooksDatabase.Instance().loadRecentBookIds(); + public Book getPreviousBook() { + List recentIds = myDatabase.loadRecentBookIds(); return recentIds.size() > 1 ? Book.getById(recentIds.get(1)) : null; } @@ -490,16 +492,15 @@ public final class Library extends AbstractLibrary { } } - public static void addBookToRecentList(Book book) { - final BooksDatabase db = BooksDatabase.Instance(); - final List ids = db.loadRecentBookIds(); + public void addBookToRecentList(Book book) { + final List ids = myDatabase.loadRecentBookIds(); final Long bookId = book.getId(); ids.remove(bookId); ids.add(0, bookId); if (ids.size() > 12) { ids.remove(12); } - db.saveRecentBookIds(ids); + myDatabase.saveRecentBookIds(ids); } @Override @@ -523,13 +524,13 @@ public final class Library extends AbstractLibrary { } final LibraryTree rootFavorites = getFirstLevelTree(ROOT_FAVORITES); rootFavorites.getBookSubTree(book, true); - BooksDatabase.Instance().addToFavorites(book.getId()); + myDatabase.addToFavorites(book.getId()); } @Override public void removeBookFromFavorites(Book book) { if (getFirstLevelTree(ROOT_FAVORITES).removeBook(book, false)) { - BooksDatabase.Instance().removeFromFavorites(book.getId()); + myDatabase.removeFromFavorites(book.getId()); fireModelChangedEvent(ChangeListener.Code.BookRemoved); } } @@ -556,15 +557,14 @@ public final class Library extends AbstractLibrary { } myBooks.remove(book); if (getFirstLevelTree(ROOT_RECENT).removeBook(book, false)) { - final BooksDatabase db = BooksDatabase.Instance(); - final List ids = db.loadRecentBookIds(); + final List ids = myDatabase.loadRecentBookIds(); ids.remove(book.getId()); - db.saveRecentBookIds(ids); + myDatabase.saveRecentBookIds(ids); } getFirstLevelTree(ROOT_FAVORITES).removeBook(book, false); myRootTree.removeBook(book, true); - BooksDatabase.Instance().deleteFromBookList(book.getId()); + myDatabase.deleteFromBookList(book.getId()); if ((removeMode & REMOVE_FROM_DISK) != 0) { book.File.getPhysicalFile().delete(); }