mirror of
https://github.com/geometer/FBReaderJ.git
synced 2025-10-04 10:19:33 +02:00
synchronization with library-service branch
This commit is contained in:
parent
fd338adbd1
commit
d01cd4841d
8 changed files with 89 additions and 12 deletions
|
@ -60,15 +60,28 @@ public class BookCollectionShadow extends AbstractBookCollection implements Serv
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
private static Runnable combined(final Runnable action0, final Runnable action1) {
|
||||||
|
if (action0 == null) {
|
||||||
|
return action1;
|
||||||
|
}
|
||||||
|
if (action1 == null) {
|
||||||
|
return action0;
|
||||||
|
}
|
||||||
|
return new Runnable() {
|
||||||
|
public void run() {
|
||||||
|
action0.run();
|
||||||
|
action1.run();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
public synchronized void bindToService(Context context, Runnable onBindAction) {
|
public synchronized void bindToService(Context context, Runnable onBindAction) {
|
||||||
if (myInterface != null && myContext == context) {
|
if (myInterface != null && myContext == context) {
|
||||||
if (onBindAction != null) {
|
if (onBindAction != null) {
|
||||||
onBindAction.run();
|
onBindAction.run();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (onBindAction != null) {
|
myOnBindAction = combined(myOnBindAction, onBindAction);
|
||||||
myOnBindAction = onBindAction;
|
|
||||||
}
|
|
||||||
context.bindService(
|
context.bindService(
|
||||||
new Intent(context, LibraryService.class),
|
new Intent(context, LibraryService.class),
|
||||||
this,
|
this,
|
||||||
|
@ -149,7 +162,6 @@ public class BookCollectionShadow extends AbstractBookCollection implements Serv
|
||||||
try {
|
try {
|
||||||
return SerializerUtil.deserializeBook(myInterface.getRecentBook(index));
|
return SerializerUtil.deserializeBook(myInterface.getRecentBook(index));
|
||||||
} catch (RemoteException e) {
|
} catch (RemoteException e) {
|
||||||
e.printStackTrace();
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -205,6 +217,26 @@ public class BookCollectionShadow extends AbstractBookCollection implements Serv
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public synchronized boolean hasFavorites() {
|
||||||
|
if (myInterface != null) {
|
||||||
|
try {
|
||||||
|
return myInterface.hasFavorites();
|
||||||
|
} catch (RemoteException e) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized boolean isFavorite(Book book) {
|
||||||
|
if (myInterface != null) {
|
||||||
|
try {
|
||||||
|
return myInterface.isFavorite(SerializerUtil.serialize(book));
|
||||||
|
} catch (RemoteException e) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
public synchronized void setBookFavorite(Book book, boolean favorite) {
|
public synchronized void setBookFavorite(Book book, boolean favorite) {
|
||||||
if (myInterface != null) {
|
if (myInterface != null) {
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -20,6 +20,9 @@ interface LibraryInterface {
|
||||||
boolean saveBook(in String book, in boolean force);
|
boolean saveBook(in String book, in boolean force);
|
||||||
void removeBook(in String book, in boolean deleteFromDisk);
|
void removeBook(in String book, in boolean deleteFromDisk);
|
||||||
void addBookToRecentList(in String book);
|
void addBookToRecentList(in String book);
|
||||||
|
|
||||||
|
boolean hasFavorites();
|
||||||
|
boolean isFavorite(in String book);
|
||||||
void setBookFavorite(in String book, in boolean favorite);
|
void setBookFavorite(in String book, in boolean favorite);
|
||||||
|
|
||||||
TextPosition getStoredPosition(in long bookId);
|
TextPosition getStoredPosition(in long bookId);
|
||||||
|
|
|
@ -156,6 +156,14 @@ public class LibraryService extends Service {
|
||||||
myCollection.addBookToRecentList(SerializerUtil.deserializeBook(book));
|
myCollection.addBookToRecentList(SerializerUtil.deserializeBook(book));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean hasFavorites() {
|
||||||
|
return myCollection.hasFavorites();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isFavorite(String book) {
|
||||||
|
return myCollection.isFavorite(SerializerUtil.deserializeBook(book));
|
||||||
|
}
|
||||||
|
|
||||||
public void setBookFavorite(String book, boolean favorite) {
|
public void setBookFavorite(String book, boolean favorite) {
|
||||||
myCollection.setBookFavorite(SerializerUtil.deserializeBook(book), favorite);
|
myCollection.setBookFavorite(SerializerUtil.deserializeBook(book), favorite);
|
||||||
}
|
}
|
||||||
|
|
|
@ -690,8 +690,27 @@ public final class SQLiteBooksDatabase extends BooksDatabase {
|
||||||
return ids;
|
return ids;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected boolean hasFavorites() {
|
||||||
|
final Cursor cursor = myDatabase.rawQuery(
|
||||||
|
"SELECT book_id FROM Favorites LIMIT 1", null
|
||||||
|
);
|
||||||
|
boolean result = cursor.moveToNext();
|
||||||
|
cursor.close();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected boolean isFavorite(long bookId) {
|
||||||
|
final Cursor cursor = myDatabase.rawQuery(
|
||||||
|
"SELECT book_id FROM Favorites WHERE book_id = ? LIMIT 1",
|
||||||
|
new String[] { String.valueOf(bookId) }
|
||||||
|
);
|
||||||
|
boolean result = cursor.moveToNext();
|
||||||
|
cursor.close();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
private SQLiteStatement myAddToFavoritesStatement;
|
private SQLiteStatement myAddToFavoritesStatement;
|
||||||
public /*protected*/ void addToFavorites(long bookId) {
|
protected void addToFavorites(long bookId) {
|
||||||
if (myAddToFavoritesStatement == null) {
|
if (myAddToFavoritesStatement == null) {
|
||||||
myAddToFavoritesStatement = myDatabase.compileStatement(
|
myAddToFavoritesStatement = myDatabase.compileStatement(
|
||||||
"INSERT OR IGNORE INTO Favorites(book_id) VALUES (?)"
|
"INSERT OR IGNORE INTO Favorites(book_id) VALUES (?)"
|
||||||
|
@ -702,7 +721,7 @@ public final class SQLiteBooksDatabase extends BooksDatabase {
|
||||||
}
|
}
|
||||||
|
|
||||||
private SQLiteStatement myRemoveFromFavoritesStatement;
|
private SQLiteStatement myRemoveFromFavoritesStatement;
|
||||||
public /*protected*/ void removeFromFavorites(long bookId) {
|
protected void removeFromFavorites(long bookId) {
|
||||||
if (myRemoveFromFavoritesStatement == null) {
|
if (myRemoveFromFavoritesStatement == null) {
|
||||||
myRemoveFromFavoritesStatement = myDatabase.compileStatement(
|
myRemoveFromFavoritesStatement = myDatabase.compileStatement(
|
||||||
"DELETE FROM Favorites WHERE book_id = ?"
|
"DELETE FROM Favorites WHERE book_id = ?"
|
||||||
|
|
|
@ -243,6 +243,17 @@ public class BookCollection extends AbstractBookCollection {
|
||||||
myDatabase.saveRecentBookIds(ids);
|
myDatabase.saveRecentBookIds(ids);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean hasFavorites() {
|
||||||
|
return myDatabase.hasFavorites();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isFavorite(Book book) {
|
||||||
|
if (book == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return myDatabase.isFavorite(book.getId());
|
||||||
|
}
|
||||||
|
|
||||||
public void setBookFavorite(Book book, boolean favorite) {
|
public void setBookFavorite(Book book, boolean favorite) {
|
||||||
if (favorite) {
|
if (favorite) {
|
||||||
myDatabase.addToFavorites(book.getId());
|
myDatabase.addToFavorites(book.getId());
|
||||||
|
|
|
@ -25,8 +25,6 @@ import org.geometerplus.zlibrary.core.filesystem.ZLFile;
|
||||||
|
|
||||||
import org.geometerplus.zlibrary.text.view.ZLTextPosition;
|
import org.geometerplus.zlibrary.text.view.ZLTextPosition;
|
||||||
|
|
||||||
import org.geometerplus.fbreader.book.*;
|
|
||||||
|
|
||||||
public abstract class BooksDatabase {
|
public abstract class BooksDatabase {
|
||||||
private static BooksDatabase ourInstance;
|
private static BooksDatabase ourInstance;
|
||||||
|
|
||||||
|
@ -89,8 +87,10 @@ public abstract class BooksDatabase {
|
||||||
public /*protected*/ abstract void saveRecentBookIds(final List<Long> ids);
|
public /*protected*/ abstract void saveRecentBookIds(final List<Long> ids);
|
||||||
|
|
||||||
protected abstract List<Long> loadFavoriteIds();
|
protected abstract List<Long> loadFavoriteIds();
|
||||||
public /*protected*/ abstract void addToFavorites(long bookId);
|
protected abstract boolean hasFavorites();
|
||||||
public /*protected*/ abstract void removeFromFavorites(long bookId);
|
protected abstract boolean isFavorite(long bookId);
|
||||||
|
protected abstract void addToFavorites(long bookId);
|
||||||
|
protected abstract void removeFromFavorites(long bookId);
|
||||||
|
|
||||||
protected Bookmark createBookmark(long id, long bookId, String bookTitle, String text, Date creationDate, Date modificationDate, Date accessDate, int accessCounter, String modelId, int paragraphIndex, int wordIndex, int charIndex, boolean isVisible) {
|
protected Bookmark createBookmark(long id, long bookId, String bookTitle, String text, Date creationDate, Date modificationDate, Date accessDate, int accessCounter, String modelId, int paragraphIndex, int wordIndex, int charIndex, boolean isVisible) {
|
||||||
return new Bookmark(id, bookId, bookTitle, text, creationDate, modificationDate, accessDate, accessCounter, modelId, paragraphIndex, wordIndex, charIndex, isVisible);
|
return new Bookmark(id, bookId, bookTitle, text, creationDate, modificationDate, accessDate, accessCounter, modelId, paragraphIndex, wordIndex, charIndex, isVisible);
|
||||||
|
|
|
@ -59,7 +59,11 @@ public interface IBookCollection {
|
||||||
|
|
||||||
boolean saveBook(Book book, boolean force);
|
boolean saveBook(Book book, boolean force);
|
||||||
void removeBook(Book book, boolean deleteFromDisk);
|
void removeBook(Book book, boolean deleteFromDisk);
|
||||||
|
|
||||||
void addBookToRecentList(Book book);
|
void addBookToRecentList(Book book);
|
||||||
|
|
||||||
|
boolean hasFavorites();
|
||||||
|
boolean isFavorite(Book book);
|
||||||
void setBookFavorite(Book book, boolean favorite);
|
void setBookFavorite(Book book, boolean favorite);
|
||||||
|
|
||||||
ZLTextPosition getStoredPosition(long bookId);
|
ZLTextPosition getStoredPosition(long bookId);
|
||||||
|
|
|
@ -570,12 +570,12 @@ public final class Library {
|
||||||
}
|
}
|
||||||
final LibraryTree rootFavorites = getFirstLevelTree(ROOT_FAVORITES);
|
final LibraryTree rootFavorites = getFirstLevelTree(ROOT_FAVORITES);
|
||||||
rootFavorites.getBookSubTree(book, true);
|
rootFavorites.getBookSubTree(book, true);
|
||||||
myDatabase.addToFavorites(book.getId());
|
Collection.setBookFavorite(book, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void removeBookFromFavorites(Book book) {
|
public void removeBookFromFavorites(Book book) {
|
||||||
if (getFirstLevelTree(ROOT_FAVORITES).removeBook(book, false)) {
|
if (getFirstLevelTree(ROOT_FAVORITES).removeBook(book, false)) {
|
||||||
myDatabase.removeFromFavorites(book.getId());
|
Collection.setBookFavorite(book, false);
|
||||||
fireModelChangedEvent(ChangeListener.Code.BookRemoved);
|
fireModelChangedEvent(ChangeListener.Code.BookRemoved);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue