1
0
Fork 0
mirror of https://github.com/geometer/FBReaderJ.git synced 2025-10-05 10:49:24 +02:00

synchronization with library-service branch

This commit is contained in:
Nikolay Pultsin 2013-02-08 10:25:48 +00:00
parent e4912a2beb
commit d08131d68b
7 changed files with 97 additions and 31 deletions

View file

@ -49,7 +49,7 @@ public class BookCollectionShadow extends AbstractBookCollection implements Serv
final String type = intent.getStringExtra("type");
if (LibraryService.BOOK_EVENT_ACTION.equals(intent.getAction())) {
final Book book = SerializerUtil.deserializeBook(intent.getStringExtra("book"));
fireBookEvent(Listener.BookEvent.valueOf(type), book);
fireBookEvent(BookEvent.valueOf(type), book);
} else {
fireBuildEvent(Listener.BuildEvent.valueOf(type));
}
@ -121,6 +121,19 @@ public class BookCollectionShadow extends AbstractBookCollection implements Serv
}
}
public synchronized List<Book> books(Author author) {
if (myInterface == null) {
return Collections.emptyList();
}
try {
return SerializerUtil.deserializeBookList(
myInterface.booksForAuthor(Util.authorToString(author))
);
} catch (RemoteException e) {
return Collections.emptyList();
}
}
public synchronized List<Book> books(String pattern) {
if (myInterface == null) {
return Collections.emptyList();
@ -195,10 +208,7 @@ public class BookCollectionShadow extends AbstractBookCollection implements Serv
final List<String> strings = myInterface.authors();
final List<Author> authors = new ArrayList<Author>(strings.size());
for (String s : strings) {
final String[] splited = s.split("\000");
if (splited.length == 2) {
authors.add(new Author(splited[0], splited[1]));
}
authors.add(Util.stringToAuthor(s));
}
return authors;
} catch (RemoteException e) {

View file

@ -10,6 +10,7 @@ import org.geometerplus.android.fbreader.api.TextPosition;
interface LibraryInterface {
int size();
List<String> books();
List<String> booksForAuthor(in String author);
List<String> booksForPattern(in String pattern);
List<String> recentBooks();
List<String> favorites();

View file

@ -119,6 +119,10 @@ public class LibraryService extends Service {
return SerializerUtil.serializeBookList(myCollection.books());
}
public List<String> booksForAuthor(String author) {
return SerializerUtil.serializeBookList(myCollection.books(Util.stringToAuthor(author)));
}
public List<String> booksForPattern(String pattern) {
return SerializerUtil.serializeBookList(myCollection.books(pattern));
}
@ -147,12 +151,7 @@ public class LibraryService extends Service {
final List<Author> authors = myCollection.authors();
final List<String> strings = new ArrayList<String>(authors.size());
for (Author a : authors) {
strings.add(
new StringBuilder(a.DisplayName)
.append('\000')
.append(a.SortKey)
.toString()
);
strings.add(Util.authorToString(a));
}
return strings;
}

View file

@ -0,0 +1,37 @@
/*
* Copyright (C) 2007-2013 Geometer Plus <contact@geometerplus.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
package org.geometerplus.android.fbreader.libraryService;
import org.geometerplus.fbreader.book.Author;
abstract class Util {
static String authorToString(Author author) {
return new StringBuilder(author.DisplayName).append('\000').append(author.SortKey).toString();
}
static Author stringToAuthor(String string) {
final String[] splitted = string.split("\000");
if (splitted.length == 2) {
return new Author(splitted[0], splitted[1]);
} else {
return Author.NULL;
}
}
}

View file

@ -36,7 +36,7 @@ public abstract class AbstractBookCollection implements IBookCollection {
return !myListeners.isEmpty();
}
protected void fireBookEvent(Listener.BookEvent event, Book book) {
protected void fireBookEvent(BookEvent event, Book book) {
synchronized (myListeners) {
for (Listener l : myListeners) {
l.onBookEvent(event, book);

View file

@ -86,6 +86,8 @@ public class BookCollection extends AbstractBookCollection {
}
if (book != null && fileInfos.check(physicalFile, physicalFile != bookFile)) {
saveBook(book, false);
// saved
addBook(book, false);
return book;
}
@ -120,6 +122,7 @@ public class BookCollection extends AbstractBookCollection {
final ZLFile bookFile = book.File;
final ZLPhysicalFile physicalFile = bookFile.getPhysicalFile();
if (physicalFile == null) {
// loaded from db
addBook(book, false);
return book;
}
@ -129,6 +132,7 @@ public class BookCollection extends AbstractBookCollection {
FileInfoSet fileInfos = new FileInfoSet(myDatabase, physicalFile);
if (fileInfos.check(physicalFile, physicalFile != bookFile)) {
// loaded from db
addBook(book, false);
return book;
}
@ -136,6 +140,7 @@ public class BookCollection extends AbstractBookCollection {
try {
book.readMetaInfo();
// loaded from db
addBook(book, false);
return book;
} catch (BookReadingException e) {
@ -144,21 +149,20 @@ public class BookCollection extends AbstractBookCollection {
}
private void addBook(Book book, boolean force) {
if (book == null) {
if (book == null || book.getId() == -1) {
return;
}
synchronized (myBooksByFile) {
Listener.BookEvent event = Listener.BookEvent.Added;
if (myBooksByFile.containsKey(book.File)) {
if (!force) {
return;
}
event = Listener.BookEvent.Updated;
final Book existing = myBooksByFile.get(book.File);
if (existing == null) {
myBooksByFile.put(book.File, book);
myBooksById.put(book.getId(), book);
fireBookEvent(BookEvent.Added, book);
} else if (force) {
existing.updateFrom(book);
fireBookEvent(BookEvent.Updated, existing);
}
myBooksByFile.put(book.File, book);
myBooksById.put(book.getId(), book);
fireBookEvent(event, book);
}
}
@ -185,7 +189,7 @@ public class BookCollection extends AbstractBookCollection {
book.File.getPhysicalFile().delete();
}
}
fireBookEvent(Listener.BookEvent.Removed, book);
fireBookEvent(BookEvent.Removed, book);
}
public List<Book> books() {
@ -194,6 +198,18 @@ public class BookCollection extends AbstractBookCollection {
}
}
public List<Book> books(Author author) {
final boolean isNull = Author.NULL.equals(author);
final LinkedList<Book> filtered = new LinkedList<Book>();
for (Book b : books()) {
final List<Author> bookAuthors = b.authors();
if (isNull && bookAuthors.isEmpty() || bookAuthors.contains(author)) {
filtered.add(b);
}
}
return filtered;
}
public List<Book> books(String pattern) {
if (pattern == null || pattern.length() == 0) {
return Collections.emptyList();
@ -231,7 +247,12 @@ public class BookCollection extends AbstractBookCollection {
final Set<Author> authors = new TreeSet<Author>();
synchronized (myBooksByFile) {
for (Book book : myBooksByFile.values()) {
authors.addAll(book.authors());
final List<Author> bookAuthors = book.authors();
if (bookAuthors.isEmpty()) {
authors.add(Author.NULL);
} else {
authors.addAll(bookAuthors);
}
}
}
return new ArrayList<Author>(authors);
@ -293,7 +314,7 @@ public class BookCollection extends AbstractBookCollection {
} else {
myDatabase.removeFromFavorites(book.getId());
}
fireBookEvent(Listener.BookEvent.Updated, book);
fireBookEvent(BookEvent.Updated, book);
}
public synchronized void startBuild() {
@ -400,6 +421,7 @@ public class BookCollection extends AbstractBookCollection {
file.setCached(false);
}
if (doAdd) {
// loaded from db
addBook(book, false);
}
} else {
@ -434,6 +456,8 @@ public class BookCollection extends AbstractBookCollection {
if (helpBook == null) {
helpBook = new Book(helpFile);
}
saveBook(helpBook, false);
// saved
addBook(helpBook, false);
} catch (BookReadingException e) {
// that's impossible

View file

@ -27,12 +27,6 @@ import org.geometerplus.zlibrary.text.view.ZLTextPosition;
public interface IBookCollection {
public interface Listener {
public enum BookEvent {
Added,
Updated,
Removed
}
public enum BuildEvent {
Started,
NotStarted,
@ -50,6 +44,7 @@ public interface IBookCollection {
int size();
List<Book> books();
List<Book> books(Author author);
List<Book> books(String pattern);
List<Book> recentBooks();
List<Book> favorites();