1
0
Fork 0
mirror of https://github.com/geometer/FBReaderJ.git synced 2025-10-04 18:29:23 +02:00

first service usage: open book from BookmarksActivity

This commit is contained in:
Nikolay Pultsin 2013-01-09 13:47:49 +01:00
parent 1844f38752
commit 7480dffedb
4 changed files with 33 additions and 10 deletions

View file

@ -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<Bookmark> AllBooksBookmarks;
private final List<Bookmark> myThisBookBookmarks = new LinkedList<Bookmark>();
private final List<Bookmark> mySearchResults = new LinkedList<Bookmark>();
@ -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 {

View file

@ -5,5 +5,5 @@
package org.geometerplus.android.fbreader.libraryService;
interface LibraryInterface {
boolean isUpToDate();
String bookById(in long id);
}

View file

@ -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));
}
}

View file

@ -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();