1
0
Fork 0
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:
Nikolay Pultsin 2013-01-12 19:34:03 +04:00
parent 50e5a7e299
commit 522d32a585
8 changed files with 166 additions and 36 deletions

View file

@ -60,9 +60,10 @@ public class LibraryActivity extends TreeActivity implements MenuItem.OnMenuItem
myDatabase = new SQLiteBooksDatabase(this, "LIBRARY"); myDatabase = new SQLiteBooksDatabase(this, "LIBRARY");
} }
if (myLibrary == null) { if (myLibrary == null) {
myLibrary = Library.Instance(); final BookCollection collection = new BookCollection(myDatabase);
myLibrary = new Library(collection);
myLibrary.addChangeListener(this); myLibrary.addChangeListener(this);
myLibrary.startBuild(); collection.startBuild();
} }
final String selectedBookPath = getIntent().getStringExtra(SELECTED_BOOK_PATH_KEY); final String selectedBookPath = getIntent().getStringExtra(SELECTED_BOOK_PATH_KEY);

View file

@ -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) { public synchronized Book getRecentBook(int index) {
if (myInterface == null) { if (myInterface == null) {
return null; return null;
} }
try { try {
return SerializerUtil.deserializeBook(myInterface.recentBook(index)); return SerializerUtil.deserializeBook(myInterface.getRecentBook(index));
} catch (RemoteException e) { } catch (RemoteException e) {
return null; return null;
} }
@ -77,12 +110,39 @@ public class BookCollectionShadow implements IBookCollection, ServiceConnection
return null; return null;
} }
try { try {
return SerializerUtil.deserializeBook(myInterface.bookById(id)); return SerializerUtil.deserializeBook(myInterface.getBookById(id));
} catch (RemoteException e) { } catch (RemoteException e) {
return null; 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() { public synchronized List<Bookmark> allBookmarks() {
if (myInterface == null) { if (myInterface == null) {
return Collections.emptyList(); return Collections.emptyList();

View file

@ -8,8 +8,15 @@ import java.util.List;
interface LibraryInterface { interface LibraryInterface {
int size(); int size();
String bookById(in long id); List<String> books(String pattern);
String recentBook(in int index); 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(); List<String> allBookmarks();
String saveBookmark(in String bookmark); String saveBookmark(in String bookmark);

View file

@ -125,16 +125,40 @@ public class LibraryService extends Service {
return myCollection.size(); 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)); return SerializerUtil.serialize(myCollection.getRecentBook(index));
} }
public String bookById(long id) { public String getBookById(long id) {
return SerializerUtil.serialize(myCollection.getBookById(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() { public List<String> allBookmarks() {
return SerializerUtil.serialize(myCollection.allBookmarks()); return SerializerUtil.serializeBookmarkList(myCollection.allBookmarks());
} }
public String saveBookmark(String serialized) { public String saveBookmark(String serialized) {

View file

@ -77,7 +77,12 @@ public class BookCollection implements IBookCollection {
Collections.synchronizedMap(new LinkedHashMap<ZLFile,Book>()); Collections.synchronizedMap(new LinkedHashMap<ZLFile,Book>());
private final Map<Long,Book> myBooksById = private final Map<Long,Book> myBooksById =
Collections.synchronizedMap(new HashMap<Long,Book>()); 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) { public BookCollection(BooksDatabase db) {
myDatabase = 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() { public List<Book> recentBooks() {
return books(myDatabase.loadRecentBookIds()); return books(myDatabase.loadRecentBookIds());
} }
@ -210,11 +229,11 @@ public class BookCollection implements IBookCollection {
} }
public synchronized void startBuild() { public synchronized void startBuild() {
if (myBuildStarted) { if (myBuildStatus != BuildStatus.NotStarted) {
fireBuildEvent(Listener.BuildEvent.NotStarted); fireBuildEvent(Listener.BuildEvent.NotStarted);
return; return;
} }
myBuildStarted = true; myBuildStatus = BuildStatus.Started;
final Thread builder = new Thread("Library.build") { final Thread builder = new Thread("Library.build") {
public void run() { public void run() {
@ -226,7 +245,7 @@ public class BookCollection implements IBookCollection {
fireBuildEvent(Listener.BuildEvent.Failed); fireBuildEvent(Listener.BuildEvent.Failed);
} finally { } finally {
fireBuildEvent(Listener.BuildEvent.Completed); fireBuildEvent(Listener.BuildEvent.Completed);
myBuildStarted = false; myBuildStatus = BuildStatus.Finished;
} }
} }
}; };

View file

@ -23,9 +23,16 @@ import java.util.List;
public interface IBookCollection { public interface IBookCollection {
int size(); int size();
List<Book> books(String pattern);
List<Book> recentBooks();
List<Book> favorites();
Book getBookById(long id); Book getBookById(long id);
Book getRecentBook(int index); Book getRecentBook(int index);
void removeBook(Book book, boolean deleteFromDisk);
void addBookToRecentList(Book book);
void setBookFavorite(Book book, boolean favorite);
List<Bookmark> allBookmarks(); List<Bookmark> allBookmarks();
void saveBookmark(Bookmark bookmark); void saveBookmark(Bookmark bookmark);
void deleteBookmark(Bookmark bookmark); void deleteBookmark(Bookmark bookmark);

View file

@ -73,7 +73,8 @@ public final class Library {
private static Library ourInstance; private static Library ourInstance;
public static Library Instance() { public static Library Instance() {
if (ourInstance == null) { if (ourInstance == null) {
ourInstance = new Library(BooksDatabase.Instance()); final BookCollection collection = new BookCollection(BooksDatabase.Instance());
ourInstance = new Library(collection);
} }
return ourInstance; return ourInstance;
} }
@ -97,8 +98,8 @@ public final class Library {
} }
} }
public Library(BooksDatabase db) { public Library(BookCollection collection) {
myCollection = new BookCollection(db); myCollection = collection;
myCollection.addListener(new BookCollection.Listener() { myCollection.addListener(new BookCollection.Listener() {
public void onBookEvent(BookEvent event, Book book) { public void onBookEvent(BookEvent event, Book book) {
switch (event) { switch (event) {
@ -234,12 +235,13 @@ public final class Library {
getTagTree(t).getBookSubTree(book, true); getTagTree(t).getBookSubTree(book, true);
} }
final SearchResultsTree found = synchronized (this) {
(SearchResultsTree)getFirstLevelTree(ROOT_FOUND); final SearchResultsTree found = (SearchResultsTree)getFirstLevelTree(ROOT_FOUND);
if (found != null && book.matches(found.getPattern())) { if (found != null && book.matches(found.getPattern())) {
found.getBookSubTree(book, true); found.getBookSubTree(book, true);
} }
} }
}
private void removeFromTree(String rootId, Book book) { private void removeFromTree(String rootId, Book book) {
final FirstLevelTree tree = getFirstLevelTree(rootId); final FirstLevelTree tree = getFirstLevelTree(rootId);
@ -275,10 +277,6 @@ public final class Library {
fireModelChangedEvent(ChangeListener.Code.BookAdded); fireModelChangedEvent(ChangeListener.Code.BookAdded);
} }
public synchronized void startBuild() {
myCollection.startBuild();
}
public boolean isUpToDate() { public boolean isUpToDate() {
return myStatusMask == 0; return myStatusMask == 0;
} }
@ -321,9 +319,8 @@ public final class Library {
} }
FirstLevelTree newSearchResults = null; FirstLevelTree newSearchResults = null;
for (Book book : myCollection.books()) {
if (book.matches(pattern)) {
synchronized (this) { synchronized (this) {
for (Book book : myCollection.books(pattern)) {
if (newSearchResults == null) { if (newSearchResults == null) {
if (oldSearchResults != null) { if (oldSearchResults != null) {
oldSearchResults.removeSelf(); oldSearchResults.removeSelf();
@ -335,7 +332,6 @@ public final class Library {
fireModelChangedEvent(ChangeListener.Code.BookAdded); fireModelChangedEvent(ChangeListener.Code.BookAdded);
} }
} }
}
if (newSearchResults == null) { if (newSearchResults == null) {
fireModelChangedEvent(ChangeListener.Code.NotFound); fireModelChangedEvent(ChangeListener.Code.NotFound);
} }

View file

@ -43,7 +43,23 @@ public abstract class SerializerUtil {
return xml != null ? defaultSerializer.deserializeBookmark(xml) : null; 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()); final List<String> serialized = new ArrayList<String>(bookmarks.size());
for (Bookmark b : bookmarks) { for (Bookmark b : bookmarks) {
serialized.add(SerializerUtil.serialize(b)); serialized.add(SerializerUtil.serialize(b));