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

library initialization

Conflicts:

	src/org/geometerplus/fbreader/book/BookCollection.java
This commit is contained in:
Nikolay Pultsin 2013-01-15 05:59:08 +04:00
parent 42a38f3ab9
commit e8b6c001ff
7 changed files with 101 additions and 65 deletions

View file

@ -100,12 +100,23 @@ public class BookCollectionShadow extends AbstractBookCollection implements Serv
} }
} }
public synchronized List<Book> books() {
if (myInterface == null) {
return Collections.emptyList();
}
try {
return SerializerUtil.deserializeBookList(myInterface.books());
} catch (RemoteException e) {
return Collections.emptyList();
}
}
public synchronized List<Book> books(String pattern) { public synchronized List<Book> books(String pattern) {
if (myInterface == null) { if (myInterface == null) {
return Collections.emptyList(); return Collections.emptyList();
} }
try { try {
return SerializerUtil.deserializeBookList(myInterface.books(pattern)); return SerializerUtil.deserializeBookList(myInterface.booksForPattern(pattern));
} catch (RemoteException e) { } catch (RemoteException e) {
return Collections.emptyList(); return Collections.emptyList();
} }

View file

@ -9,7 +9,8 @@ import org.geometerplus.android.fbreader.api.TextPosition;
interface LibraryInterface { interface LibraryInterface {
int size(); int size();
List<String> books(in String pattern); List<String> books();
List<String> booksForPattern(in String pattern);
List<String> recentBooks(); List<String> recentBooks();
List<String> favorites(); List<String> favorites();
String getBookByFile(in String file); String getBookByFile(in String file);

View file

@ -116,7 +116,11 @@ public class LibraryService extends Service {
return myCollection.size(); return myCollection.size();
} }
public List<String> books(String pattern) { public List<String> books() {
return SerializerUtil.serializeBookList(myCollection.books());
}
public List<String> booksForPattern(String pattern) {
return SerializerUtil.serializeBookList(myCollection.books(pattern)); return SerializerUtil.serializeBookList(myCollection.books(pattern));
} }

View file

@ -74,7 +74,7 @@ public class BookCollection extends AbstractBookCollection {
} }
if (book != null && fileInfos.check(physicalFile, physicalFile != bookFile)) { if (book != null && fileInfos.check(physicalFile, physicalFile != bookFile)) {
addBook(book); addBook(book, false);
return book; return book;
} }
fileInfos.save(); fileInfos.save();
@ -90,7 +90,7 @@ public class BookCollection extends AbstractBookCollection {
} }
saveBook(book, false); saveBook(book, false);
addBook(book); addBook(book, false);
return book; return book;
} }
@ -109,7 +109,7 @@ public class BookCollection extends AbstractBookCollection {
final ZLFile bookFile = book.File; final ZLFile bookFile = book.File;
final ZLPhysicalFile physicalFile = bookFile.getPhysicalFile(); final ZLPhysicalFile physicalFile = bookFile.getPhysicalFile();
if (physicalFile == null) { if (physicalFile == null) {
addBook(book); addBook(book, false);
return book; return book;
} }
if (!physicalFile.exists()) { if (!physicalFile.exists()) {
@ -118,35 +118,36 @@ public class BookCollection extends AbstractBookCollection {
FileInfoSet fileInfos = new FileInfoSet(myDatabase, physicalFile); FileInfoSet fileInfos = new FileInfoSet(myDatabase, physicalFile);
if (fileInfos.check(physicalFile, physicalFile != bookFile)) { if (fileInfos.check(physicalFile, physicalFile != bookFile)) {
addBook(book); addBook(book, false);
return book; return book;
} }
fileInfos.save(); fileInfos.save();
try { try {
book.readMetaInfo(); book.readMetaInfo();
addBook(book); addBook(book, false);
return book; return book;
} catch (BookReadingException e) { } catch (BookReadingException e) {
return null; return null;
} }
} }
private void addBook(Book book) { private void addBook(Book book, boolean force) {
if (book == null) {
return;
}
synchronized (myBooksByFile) { synchronized (myBooksByFile) {
if (book == null || myBooksByFile.containsKey(book.File)) { Listener.BookEvent event = Listener.BookEvent.Added;
return; if (myBooksByFile.containsKey(book.File)) {
if (!force) {
return;
}
event = Listener.BookEvent.Updated;
} }
myBooksByFile.put(book.File, book); myBooksByFile.put(book.File, book);
addBookById(book); myBooksById.put(book.getId(), book);
} fireBookEvent(event, book);
fireBookEvent(Listener.BookEvent.Added, book);
}
private void addBookById(Book book) {
final long id = book.getId();
if (id != -1) {
myBooksById.put(id, book);
} }
} }
@ -155,13 +156,7 @@ public class BookCollection extends AbstractBookCollection {
return false; return false;
} }
synchronized (myBooksByFile) { addBook(book, true);
final boolean replace = myBooksByFile.remove(book.File) != null;
myBooksById.remove(book.getId());
myBooksByFile.put(book.File, book);
addBookById(book);
fireBookEvent(replace ? Listener.BookEvent.Updated : Listener.BookEvent.Added, book);
}
return book.save(myDatabase, force); return book.save(myDatabase, force);
} }
@ -169,6 +164,7 @@ public class BookCollection extends AbstractBookCollection {
synchronized (myBooksByFile) { synchronized (myBooksByFile) {
myBooksByFile.remove(book.File); myBooksByFile.remove(book.File);
myBooksById.remove(book.getId()); myBooksById.remove(book.getId());
final List<Long> ids = myDatabase.loadRecentBookIds(); final List<Long> ids = myDatabase.loadRecentBookIds();
if (ids.remove(book.getId())) { if (ids.remove(book.getId())) {
myDatabase.saveRecentBookIds(ids); myDatabase.saveRecentBookIds(ids);
@ -321,7 +317,7 @@ public class BookCollection extends AbstractBookCollection {
file.setCached(false); file.setCached(false);
} }
if (doAdd) { if (doAdd) {
addBook(book); addBook(book, false);
} }
} else { } else {
orphanedBooks.add(book); orphanedBooks.add(book);
@ -356,7 +352,7 @@ public class BookCollection extends AbstractBookCollection {
if (helpBook == null) { if (helpBook == null) {
helpBook = new Book(helpFile); helpBook = new Book(helpFile);
} }
addBook(helpBook); addBook(helpBook, false);
} catch (BookReadingException e) { } catch (BookReadingException e) {
// that's impossible // that's impossible
e.printStackTrace(); e.printStackTrace();
@ -369,7 +365,7 @@ public class BookCollection extends AbstractBookCollection {
public void run() { public void run() {
for (Book book : newBooks) { for (Book book : newBooks) {
saveBook(book, false); saveBook(book, false);
addBookById(book); addBook(book, false);
} }
} }
}); });
@ -422,7 +418,6 @@ public class BookCollection extends AbstractBookCollection {
if (doReadMetaInfo) { if (doReadMetaInfo) {
book.readMetaInfo(); book.readMetaInfo();
} }
addBook(book);
newBooks.add(book); newBooks.add(book);
return; return;
} }
@ -432,7 +427,6 @@ public class BookCollection extends AbstractBookCollection {
try { try {
final Book book = new Book(file); final Book book = new Book(file);
addBook(book);
newBooks.add(book); newBooks.add(book);
return; return;
} catch (BookReadingException e) { } catch (BookReadingException e) {

View file

@ -49,6 +49,7 @@ public interface IBookCollection {
public void removeListener(Listener listener); public void removeListener(Listener listener);
int size(); int size();
List<Book> books();
List<Book> books(String pattern); List<Book> books(String pattern);
List<Book> recentBooks(); List<Book> recentBooks();
List<Book> favorites(); List<Book> favorites();

View file

@ -46,7 +46,7 @@ public abstract class SerializerUtil {
public static List<String> serializeBookList(List<Book> books) { public static List<String> serializeBookList(List<Book> books) {
final List<String> serialized = new ArrayList<String>(books.size()); final List<String> serialized = new ArrayList<String>(books.size());
for (Book b : books) { for (Book b : books) {
serialized.add(SerializerUtil.serialize(b)); serialized.add(defaultSerializer.serialize(b));
} }
return serialized; return serialized;
} }
@ -62,7 +62,7 @@ public abstract class SerializerUtil {
public static List<String> serializeBookmarkList(List<Bookmark> bookmarks) { 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(defaultSerializer.serialize(b));
} }
return serialized; return serialized;
} }

View file

@ -77,6 +77,7 @@ public final class Library {
} }
public final IBookCollection Collection; public final IBookCollection Collection;
private final HashMap<Long,Book> myBooks = new HashMap<Long,Book>();
private final RootTree myRootTree = new RootTree(); private final RootTree myRootTree = new RootTree();
private boolean myDoGroupTitlesByFirstLetter; private boolean myDoGroupTitlesByFirstLetter;
@ -97,31 +98,6 @@ public final class Library {
public Library(IBookCollection collection) { public Library(IBookCollection collection) {
Collection = collection; Collection = collection;
Collection.addListener(new BookCollection.Listener() {
public void onBookEvent(BookEvent event, Book book) {
switch (event) {
case Added:
addBookToLibrary(book);
if (Collection.size() % 16 == 0) {
Library.this.fireModelChangedEvent(ChangeListener.Code.BookAdded);
}
break;
}
}
public void onBuildEvent(BuildEvent event) {
switch (event) {
case Started:
Library.this.fireModelChangedEvent(ChangeListener.Code.StatusChanged);
setStatus(myStatusMask | STATUS_LOADING);
break;
case Completed:
Library.this.fireModelChangedEvent(ChangeListener.Code.BookAdded);
setStatus(myStatusMask & ~STATUS_LOADING);
break;
}
}
});
new FavoritesTree(myRootTree, ROOT_FAVORITES); new FavoritesTree(myRootTree, ROOT_FAVORITES);
new FirstLevelTree(myRootTree, ROOT_RECENT); new FirstLevelTree(myRootTree, ROOT_RECENT);
@ -132,12 +108,55 @@ public final class Library {
} }
public void init() { public void init() {
for (Book book : Collection.recentBooks()) { Collection.addListener(new BookCollection.Listener() {
new BookTree(getFirstLevelTree(ROOT_RECENT), book, true); public void onBookEvent(BookEvent event, Book book) {
} switch (event) {
for (Book book : Collection.favorites()) { case Added:
new BookTree(getFirstLevelTree(ROOT_FAVORITES), book, true); /*
} addBookToLibrary(book);
if (Collection.size() % 16 == 0) {
Library.this.fireModelChangedEvent(ChangeListener.Code.BookAdded);
}
*/
break;
}
}
public void onBuildEvent(BuildEvent event) {
switch (event) {
case Started:
//setStatus(myStatusMask | STATUS_LOADING);
break;
case Completed:
//setStatus(myStatusMask & ~STATUS_LOADING);
break;
}
}
});
final Thread initializer = new Thread() {
public void run() {
setStatus(myStatusMask | STATUS_LOADING);
getFirstLevelTree(ROOT_RECENT).clear();
for (Book book : Collection.recentBooks()) {
new BookTree(getFirstLevelTree(ROOT_RECENT), book, true);
}
getFirstLevelTree(ROOT_FAVORITES).clear();
for (Book book : Collection.favorites()) {
new BookTree(getFirstLevelTree(ROOT_FAVORITES), book, true);
}
for (Book book : Collection.books()) {
addBookToLibrary(book);
if (++count % 16 == 0) {
Library.this.fireModelChangedEvent(ChangeListener.Code.BookAdded);
}
}
Library.this.fireModelChangedEvent(ChangeListener.Code.BookAdded);
setStatus(myStatusMask & ~STATUS_LOADING);
}
};
initializer.setPriority((Thread.MIN_PRIORITY + Thread.NORM_PRIORITY) / 2);
initializer.start();
} }
public LibraryTree getRootTree() { public LibraryTree getRootTree() {
@ -170,6 +189,11 @@ public final class Library {
} }
private synchronized void addBookToLibrary(Book book) { private synchronized void addBookToLibrary(Book book) {
if (myBooks.containsKey(book.getId())) {
return;
}
myBooks.put(book.getId(), book);
List<Author> authors = book.authors(); List<Author> authors = book.authors();
if (authors.isEmpty()) { if (authors.isEmpty()) {
authors = (List<Author>)myNullList; authors = (List<Author>)myNullList;
@ -247,6 +271,7 @@ public final class Library {
} }
Collection.saveBook(book, true); Collection.saveBook(book, true);
myBooks.remove(book.getId());
refreshInTree(ROOT_FAVORITES, book); refreshInTree(ROOT_FAVORITES, book);
refreshInTree(ROOT_RECENT, book); refreshInTree(ROOT_RECENT, book);
removeFromTree(ROOT_FOUND, book); removeFromTree(ROOT_FOUND, book);