mirror of
https://github.com/geometer/FBReaderJ.git
synced 2025-10-04 18:29:23 +02:00
more methods in IBookCollection
This commit is contained in:
parent
50e5a7e299
commit
522d32a585
8 changed files with 166 additions and 36 deletions
|
@ -60,9 +60,10 @@ public class LibraryActivity extends TreeActivity implements MenuItem.OnMenuItem
|
|||
myDatabase = new SQLiteBooksDatabase(this, "LIBRARY");
|
||||
}
|
||||
if (myLibrary == null) {
|
||||
myLibrary = Library.Instance();
|
||||
final BookCollection collection = new BookCollection(myDatabase);
|
||||
myLibrary = new Library(collection);
|
||||
myLibrary.addChangeListener(this);
|
||||
myLibrary.startBuild();
|
||||
collection.startBuild();
|
||||
}
|
||||
|
||||
final String selectedBookPath = getIntent().getStringExtra(SELECTED_BOOK_PATH_KEY);
|
||||
|
|
|
@ -61,12 +61,45 @@ public class BookCollectionShadow implements IBookCollection, ServiceConnection
|
|||
}
|
||||
}
|
||||
|
||||
public synchronized List<Book> books(String pattern) {
|
||||
if (myInterface == null) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
try {
|
||||
return SerializerUtil.deserializeBookList(myInterface.books(pattern));
|
||||
} catch (RemoteException e) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized List<Book> recentBooks() {
|
||||
if (myInterface == null) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
try {
|
||||
return SerializerUtil.deserializeBookList(myInterface.recentBooks());
|
||||
} catch (RemoteException e) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized List<Book> favorites() {
|
||||
if (myInterface == null) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
try {
|
||||
return SerializerUtil.deserializeBookList(myInterface.favorites());
|
||||
} catch (RemoteException e) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized Book getRecentBook(int index) {
|
||||
if (myInterface == null) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return SerializerUtil.deserializeBook(myInterface.recentBook(index));
|
||||
return SerializerUtil.deserializeBook(myInterface.getRecentBook(index));
|
||||
} catch (RemoteException e) {
|
||||
return null;
|
||||
}
|
||||
|
@ -77,12 +110,39 @@ public class BookCollectionShadow implements IBookCollection, ServiceConnection
|
|||
return null;
|
||||
}
|
||||
try {
|
||||
return SerializerUtil.deserializeBook(myInterface.bookById(id));
|
||||
return SerializerUtil.deserializeBook(myInterface.getBookById(id));
|
||||
} catch (RemoteException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized void removeBook(Book book, boolean deleteFromDisk) {
|
||||
if (myInterface != null) {
|
||||
try {
|
||||
myInterface.removeBook(SerializerUtil.serialize(book), deleteFromDisk);
|
||||
} catch (RemoteException e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized void addBookToRecentList(Book book) {
|
||||
if (myInterface != null) {
|
||||
try {
|
||||
myInterface.addBookToRecentList(SerializerUtil.serialize(book));
|
||||
} catch (RemoteException e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized void setBookFavorite(Book book, boolean favorite) {
|
||||
if (myInterface != null) {
|
||||
try {
|
||||
myInterface.setBookFavorite(SerializerUtil.serialize(book), favorite);
|
||||
} catch (RemoteException e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized List<Bookmark> allBookmarks() {
|
||||
if (myInterface == null) {
|
||||
return Collections.emptyList();
|
||||
|
|
|
@ -8,8 +8,15 @@ import java.util.List;
|
|||
|
||||
interface LibraryInterface {
|
||||
int size();
|
||||
String bookById(in long id);
|
||||
String recentBook(in int index);
|
||||
List<String> books(String pattern);
|
||||
List<String> recentBooks();
|
||||
List<String> favorites();
|
||||
String getBookById(in long id);
|
||||
String getRecentBook(in int index);
|
||||
|
||||
void removeBook(in String book, in boolean deleteFromDisk);
|
||||
void addBookToRecentList(in String book);
|
||||
void setBookFavorite(in String book, in boolean favorite);
|
||||
|
||||
List<String> allBookmarks();
|
||||
String saveBookmark(in String bookmark);
|
||||
|
|
|
@ -125,16 +125,40 @@ public class LibraryService extends Service {
|
|||
return myCollection.size();
|
||||
}
|
||||
|
||||
public String recentBook(int index) {
|
||||
public List<String> books(String pattern) {
|
||||
return SerializerUtil.serializeBookList(myCollection.books(pattern));
|
||||
}
|
||||
|
||||
public List<String> recentBooks() {
|
||||
return SerializerUtil.serializeBookList(myCollection.recentBooks());
|
||||
}
|
||||
|
||||
public List<String> favorites() {
|
||||
return SerializerUtil.serializeBookList(myCollection.favorites());
|
||||
}
|
||||
|
||||
public String getRecentBook(int index) {
|
||||
return SerializerUtil.serialize(myCollection.getRecentBook(index));
|
||||
}
|
||||
|
||||
public String bookById(long id) {
|
||||
public String getBookById(long id) {
|
||||
return SerializerUtil.serialize(myCollection.getBookById(id));
|
||||
}
|
||||
|
||||
public void removeBook(String book, boolean deleteFromDisk) {
|
||||
myCollection.removeBook(SerializerUtil.deserializeBook(book), deleteFromDisk);
|
||||
}
|
||||
|
||||
public void addBookToRecentList(String book) {
|
||||
myCollection.addBookToRecentList(SerializerUtil.deserializeBook(book));
|
||||
}
|
||||
|
||||
public void setBookFavorite(String book, boolean favorite) {
|
||||
myCollection.setBookFavorite(SerializerUtil.deserializeBook(book), favorite);
|
||||
}
|
||||
|
||||
public List<String> allBookmarks() {
|
||||
return SerializerUtil.serialize(myCollection.allBookmarks());
|
||||
return SerializerUtil.serializeBookmarkList(myCollection.allBookmarks());
|
||||
}
|
||||
|
||||
public String saveBookmark(String serialized) {
|
||||
|
|
|
@ -77,7 +77,12 @@ public class BookCollection implements IBookCollection {
|
|||
Collections.synchronizedMap(new LinkedHashMap<ZLFile,Book>());
|
||||
private final Map<Long,Book> myBooksById =
|
||||
Collections.synchronizedMap(new HashMap<Long,Book>());
|
||||
private volatile boolean myBuildStarted = false;
|
||||
private static enum BuildStatus {
|
||||
NotStarted,
|
||||
Started,
|
||||
Finished
|
||||
};
|
||||
private volatile BuildStatus myBuildStatus = BuildStatus.NotStarted;
|
||||
|
||||
public BookCollection(BooksDatabase db) {
|
||||
myDatabase = db;
|
||||
|
@ -165,6 +170,20 @@ public class BookCollection implements IBookCollection {
|
|||
}
|
||||
}
|
||||
|
||||
public List<Book> books(String pattern) {
|
||||
if (pattern == null || pattern.length() == 0) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
final LinkedList<Book> filtered = new LinkedList<Book>();
|
||||
for (Book b : books()) {
|
||||
if (b.matches(pattern)) {
|
||||
filtered.add(b);
|
||||
}
|
||||
}
|
||||
return filtered;
|
||||
}
|
||||
|
||||
public List<Book> recentBooks() {
|
||||
return books(myDatabase.loadRecentBookIds());
|
||||
}
|
||||
|
@ -210,11 +229,11 @@ public class BookCollection implements IBookCollection {
|
|||
}
|
||||
|
||||
public synchronized void startBuild() {
|
||||
if (myBuildStarted) {
|
||||
if (myBuildStatus != BuildStatus.NotStarted) {
|
||||
fireBuildEvent(Listener.BuildEvent.NotStarted);
|
||||
return;
|
||||
}
|
||||
myBuildStarted = true;
|
||||
myBuildStatus = BuildStatus.Started;
|
||||
|
||||
final Thread builder = new Thread("Library.build") {
|
||||
public void run() {
|
||||
|
@ -226,7 +245,7 @@ public class BookCollection implements IBookCollection {
|
|||
fireBuildEvent(Listener.BuildEvent.Failed);
|
||||
} finally {
|
||||
fireBuildEvent(Listener.BuildEvent.Completed);
|
||||
myBuildStarted = false;
|
||||
myBuildStatus = BuildStatus.Finished;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
|
@ -23,9 +23,16 @@ import java.util.List;
|
|||
|
||||
public interface IBookCollection {
|
||||
int size();
|
||||
List<Book> books(String pattern);
|
||||
List<Book> recentBooks();
|
||||
List<Book> favorites();
|
||||
Book getBookById(long id);
|
||||
Book getRecentBook(int index);
|
||||
|
||||
void removeBook(Book book, boolean deleteFromDisk);
|
||||
void addBookToRecentList(Book book);
|
||||
void setBookFavorite(Book book, boolean favorite);
|
||||
|
||||
List<Bookmark> allBookmarks();
|
||||
void saveBookmark(Bookmark bookmark);
|
||||
void deleteBookmark(Bookmark bookmark);
|
||||
|
|
|
@ -73,7 +73,8 @@ public final class Library {
|
|||
private static Library ourInstance;
|
||||
public static Library Instance() {
|
||||
if (ourInstance == null) {
|
||||
ourInstance = new Library(BooksDatabase.Instance());
|
||||
final BookCollection collection = new BookCollection(BooksDatabase.Instance());
|
||||
ourInstance = new Library(collection);
|
||||
}
|
||||
return ourInstance;
|
||||
}
|
||||
|
@ -97,8 +98,8 @@ public final class Library {
|
|||
}
|
||||
}
|
||||
|
||||
public Library(BooksDatabase db) {
|
||||
myCollection = new BookCollection(db);
|
||||
public Library(BookCollection collection) {
|
||||
myCollection = collection;
|
||||
myCollection.addListener(new BookCollection.Listener() {
|
||||
public void onBookEvent(BookEvent event, Book book) {
|
||||
switch (event) {
|
||||
|
@ -234,12 +235,13 @@ public final class Library {
|
|||
getTagTree(t).getBookSubTree(book, true);
|
||||
}
|
||||
|
||||
final SearchResultsTree found =
|
||||
(SearchResultsTree)getFirstLevelTree(ROOT_FOUND);
|
||||
synchronized (this) {
|
||||
final SearchResultsTree found = (SearchResultsTree)getFirstLevelTree(ROOT_FOUND);
|
||||
if (found != null && book.matches(found.getPattern())) {
|
||||
found.getBookSubTree(book, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void removeFromTree(String rootId, Book book) {
|
||||
final FirstLevelTree tree = getFirstLevelTree(rootId);
|
||||
|
@ -275,10 +277,6 @@ public final class Library {
|
|||
fireModelChangedEvent(ChangeListener.Code.BookAdded);
|
||||
}
|
||||
|
||||
public synchronized void startBuild() {
|
||||
myCollection.startBuild();
|
||||
}
|
||||
|
||||
public boolean isUpToDate() {
|
||||
return myStatusMask == 0;
|
||||
}
|
||||
|
@ -321,9 +319,8 @@ public final class Library {
|
|||
}
|
||||
|
||||
FirstLevelTree newSearchResults = null;
|
||||
for (Book book : myCollection.books()) {
|
||||
if (book.matches(pattern)) {
|
||||
synchronized (this) {
|
||||
for (Book book : myCollection.books(pattern)) {
|
||||
if (newSearchResults == null) {
|
||||
if (oldSearchResults != null) {
|
||||
oldSearchResults.removeSelf();
|
||||
|
@ -335,7 +332,6 @@ public final class Library {
|
|||
fireModelChangedEvent(ChangeListener.Code.BookAdded);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (newSearchResults == null) {
|
||||
fireModelChangedEvent(ChangeListener.Code.NotFound);
|
||||
}
|
||||
|
|
|
@ -43,7 +43,23 @@ public abstract class SerializerUtil {
|
|||
return xml != null ? defaultSerializer.deserializeBookmark(xml) : null;
|
||||
}
|
||||
|
||||
public static List<String> serialize(List<Bookmark> bookmarks) {
|
||||
public static List<String> serializeBookList(List<Book> books) {
|
||||
final List<String> serialized = new ArrayList<String>(books.size());
|
||||
for (Book b : books) {
|
||||
serialized.add(SerializerUtil.serialize(b));
|
||||
}
|
||||
return serialized;
|
||||
}
|
||||
|
||||
public static List<Book> deserializeBookList(List<String> xmlList) {
|
||||
final List<Book> books = new ArrayList<Book>(xmlList.size());
|
||||
for (String xml : xmlList) {
|
||||
books.add(defaultSerializer.deserializeBook(xml));
|
||||
}
|
||||
return books;
|
||||
}
|
||||
|
||||
public static List<String> serializeBookmarkList(List<Bookmark> bookmarks) {
|
||||
final List<String> serialized = new ArrayList<String>(bookmarks.size());
|
||||
for (Bookmark b : bookmarks) {
|
||||
serialized.add(SerializerUtil.serialize(b));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue