diff --git a/src/org/geometerplus/android/fbreader/BookmarksActivity.java b/src/org/geometerplus/android/fbreader/BookmarksActivity.java index c18ba3aa7..dec99889f 100644 --- a/src/org/geometerplus/android/fbreader/BookmarksActivity.java +++ b/src/org/geometerplus/android/fbreader/BookmarksActivity.java @@ -36,6 +36,7 @@ import org.geometerplus.zlibrary.ui.android.R; import org.geometerplus.fbreader.fbreader.FBReaderApp; import org.geometerplus.fbreader.library.*; +import org.geometerplus.android.fbreader.libraryService.LibraryInterface; import org.geometerplus.android.fbreader.libraryService.LibraryService; import org.geometerplus.android.util.UIUtil; @@ -44,6 +45,8 @@ public class BookmarksActivity extends TabActivity implements MenuItem.OnMenuIte private static final int EDIT_ITEM_ID = 1; private static final int DELETE_ITEM_ID = 2; + LibraryInterface myLibraryInterface; + List AllBooksBookmarks; private final List myThisBookBookmarks = new LinkedList(); private final List mySearchResults = new LinkedList(); @@ -233,7 +236,13 @@ public class BookmarksActivity extends TabActivity implements MenuItem.OnMenuIte final FBReaderApp fbreader = (FBReaderApp)FBReaderApp.Instance(); final long bookId = bookmark.getBookId(); if (fbreader.Model == null || fbreader.Model.Book.getId() != bookId) { - final Book book = new BookCollection(BooksDatabase.Instance()).getBookById(bookId); + Book book = null; + if (myLibraryInterface != null) { + try { + book = BookSerializerUtil.deserialize(myLibraryInterface.bookById(bookId)); + } catch (RemoteException e) { + } + } if (book != null) { finish(); fbreader.openBook(book, bookmark, null); @@ -246,12 +255,16 @@ public class BookmarksActivity extends TabActivity implements MenuItem.OnMenuIte } } + // method from ServiceConnection interface public void onServiceConnected(ComponentName name, IBinder service) { - // TODO: implement + myLibraryInterface = LibraryInterface.Stub.asInterface(service); + System.err.println("onServiceConnected " + name + " : " + myLibraryInterface); } + // method from ServiceConnection interface public void onServiceDisconnected(ComponentName name) { - // TODO: implement + myLibraryInterface = null; + System.err.println("onServiceDisconnected " + name); } private final class BookmarksAdapter extends BaseAdapter implements AdapterView.OnItemClickListener, View.OnCreateContextMenuListener { diff --git a/src/org/geometerplus/android/fbreader/libraryService/LibraryInterface.aidl b/src/org/geometerplus/android/fbreader/libraryService/LibraryInterface.aidl index 1e8947286..8527bd20d 100644 --- a/src/org/geometerplus/android/fbreader/libraryService/LibraryInterface.aidl +++ b/src/org/geometerplus/android/fbreader/libraryService/LibraryInterface.aidl @@ -5,5 +5,5 @@ package org.geometerplus.android.fbreader.libraryService; interface LibraryInterface { - boolean isUpToDate(); + String bookById(in long id); } diff --git a/src/org/geometerplus/android/fbreader/libraryService/LibraryService.java b/src/org/geometerplus/android/fbreader/libraryService/LibraryService.java index b9fc8320f..440013b04 100644 --- a/src/org/geometerplus/android/fbreader/libraryService/LibraryService.java +++ b/src/org/geometerplus/android/fbreader/libraryService/LibraryService.java @@ -29,14 +29,16 @@ import org.geometerplus.android.fbreader.library.SQLiteBooksDatabase; public class LibraryService extends Service { public final class LibraryImplementation extends LibraryInterface.Stub { + private final BookCollection myCollection; + LibraryImplementation() { BooksDatabase database = SQLiteBooksDatabase.Instance(); if (database == null) { database = new SQLiteBooksDatabase(LibraryService.this, "LIBRARY SERVICE"); } - final BookCollection collection = new BookCollection(database); + myCollection = new BookCollection(database); final long start = System.currentTimeMillis(); - collection.addListener(new BookCollection.Listener() { + myCollection.addListener(new BookCollection.Listener() { public void onBookEvent(BookEvent event, Book book) { switch (event) { case Added: @@ -57,16 +59,16 @@ public class LibraryService extends Service { System.err.println("Build failed"); break; case Completed: - System.err.println("Build completed with " + collection.size() + " books in " + (System.currentTimeMillis() - start) + " milliseconds"); + System.err.println("Build completed with " + myCollection.size() + " books in " + (System.currentTimeMillis() - start) + " milliseconds"); break; } } }); - collection.startBuild(); + myCollection.startBuild(); } - public boolean isUpToDate() { - return true; + public String bookById(long id) { + return BookSerializerUtil.serialize(myCollection.getBookById(id)); } } diff --git a/src/org/geometerplus/fbreader/library/BookSerializerUtil.java b/src/org/geometerplus/fbreader/library/BookSerializerUtil.java index 6b430e20a..c1c5089a1 100644 --- a/src/org/geometerplus/fbreader/library/BookSerializerUtil.java +++ b/src/org/geometerplus/fbreader/library/BookSerializerUtil.java @@ -31,6 +31,10 @@ public abstract class BookSerializerUtil { } public static String serialize(Book book) { + if (book == null) { + return null; + } + final StringBuilder buffer = new StringBuilder(); appendTagWithAttributes( buffer, "entry", false, @@ -81,6 +85,10 @@ public abstract class BookSerializerUtil { } public static Book deserialize(String xml) { + if (xml == null) { + return null; + } + final Deserializer deserializer = new Deserializer(); deserializer.readQuietly(xml); return deserializer.getBook();