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) {
if (myInterface == null) {
return Collections.emptyList();
}
try {
return SerializerUtil.deserializeBookList(myInterface.books(pattern));
return SerializerUtil.deserializeBookList(myInterface.booksForPattern(pattern));
} catch (RemoteException e) {
return Collections.emptyList();
}

View file

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

View file

@ -116,7 +116,11 @@ public class LibraryService extends Service {
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));
}

View file

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

View file

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

View file

@ -46,7 +46,7 @@ public abstract class SerializerUtil {
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));
serialized.add(defaultSerializer.serialize(b));
}
return serialized;
}
@ -62,7 +62,7 @@ public abstract class SerializerUtil {
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));
serialized.add(defaultSerializer.serialize(b));
}
return serialized;
}

View file

@ -77,6 +77,7 @@ public final class Library {
}
public final IBookCollection Collection;
private final HashMap<Long,Book> myBooks = new HashMap<Long,Book>();
private final RootTree myRootTree = new RootTree();
private boolean myDoGroupTitlesByFirstLetter;
@ -97,31 +98,6 @@ public final class Library {
public Library(IBookCollection 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 FirstLevelTree(myRootTree, ROOT_RECENT);
@ -132,12 +108,55 @@ public final class Library {
}
public void init() {
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:
//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() {
@ -170,6 +189,11 @@ public final class Library {
}
private synchronized void addBookToLibrary(Book book) {
if (myBooks.containsKey(book.getId())) {
return;
}
myBooks.put(book.getId(), book);
List<Author> authors = book.authors();
if (authors.isEmpty()) {
authors = (List<Author>)myNullList;
@ -247,6 +271,7 @@ public final class Library {
}
Collection.saveBook(book, true);
myBooks.remove(book.getId());
refreshInTree(ROOT_FAVORITES, book);
refreshInTree(ROOT_RECENT, book);
removeFromTree(ROOT_FOUND, book);