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

new style format info + functional interface for all list calls in BookCollectionShadow

This commit is contained in:
Nikolay Pultsin 2015-02-13 07:40:46 +00:00
parent a725008af9
commit 949eda31ba
7 changed files with 174 additions and 158 deletions

View file

@ -135,15 +135,14 @@ public class BookCollectionShadow extends AbstractBookCollection implements Serv
}
}
public synchronized List<Book> books(BookQuery query) {
if (myInterface == null) {
return Collections.emptyList();
}
try {
return SerializerUtil.deserializeBookList(myInterface.books(SerializerUtil.serialize(query)));
} catch (RemoteException e) {
return Collections.emptyList();
}
public List<Book> books(final BookQuery query) {
return listCall(new ListCallable<Book>() {
public List<Book> call() throws RemoteException {
return SerializerUtil.deserializeBookList(
myInterface.books(SerializerUtil.serialize(query))
);
}
});
}
public synchronized boolean hasBooks(Filter filter) {
@ -157,26 +156,20 @@ public class BookCollectionShadow extends AbstractBookCollection implements Serv
}
}
public synchronized List<Book> recentlyAddedBooks(int count) {
if (myInterface == null) {
return Collections.emptyList();
}
try {
return SerializerUtil.deserializeBookList(myInterface.recentlyAddedBooks(count));
} catch (RemoteException e) {
return Collections.emptyList();
}
public List<Book> recentlyAddedBooks(final int count) {
return listCall(new ListCallable<Book>() {
public List<Book> call() throws RemoteException {
return SerializerUtil.deserializeBookList(myInterface.recentlyAddedBooks(count));
}
});
}
public synchronized List<Book> recentlyOpenedBooks(int count) {
if (myInterface == null) {
return Collections.emptyList();
}
try {
return SerializerUtil.deserializeBookList(myInterface.recentlyOpenedBooks(count));
} catch (RemoteException e) {
return Collections.emptyList();
}
public List<Book> recentlyOpenedBooks(final int count) {
return listCall(new ListCallable<Book>() {
public List<Book> call() throws RemoteException {
return SerializerUtil.deserializeBookList(myInterface.recentlyOpenedBooks(count));
}
});
}
public synchronized Book getRecentBook(int index) {
@ -235,36 +228,30 @@ public class BookCollectionShadow extends AbstractBookCollection implements Serv
}
}
public synchronized List<Author> authors() {
if (myInterface == null) {
return Collections.emptyList();
}
try {
final List<String> strings = myInterface.authors();
final List<Author> authors = new ArrayList<Author>(strings.size());
for (String s : strings) {
authors.add(Util.stringToAuthor(s));
public List<Author> authors() {
return listCall(new ListCallable<Author>() {
public List<Author> call() throws RemoteException {
final List<String> strings = myInterface.authors();
final List<Author> authors = new ArrayList<Author>(strings.size());
for (String s : strings) {
authors.add(Util.stringToAuthor(s));
}
return authors;
}
return authors;
} catch (RemoteException e) {
return Collections.emptyList();
}
});
}
public synchronized List<Tag> tags() {
if (myInterface == null) {
return Collections.emptyList();
}
try {
final List<String> strings = myInterface.tags();
final List<Tag> tags = new ArrayList<Tag>(strings.size());
for (String s : strings) {
tags.add(Util.stringToTag(s));
public List<Tag> tags() {
return listCall(new ListCallable<Tag>() {
public List<Tag> call() throws RemoteException {
final List<String> strings = myInterface.tags();
final List<Tag> tags = new ArrayList<Tag>(strings.size());
for (String s : strings) {
tags.add(Util.stringToTag(s));
}
return tags;
}
return tags;
} catch (RemoteException e) {
return Collections.emptyList();
}
});
}
public synchronized boolean hasSeries() {
@ -277,37 +264,28 @@ public class BookCollectionShadow extends AbstractBookCollection implements Serv
return false;
}
public synchronized List<String> series() {
if (myInterface == null) {
return Collections.emptyList();
}
try {
return myInterface.series();
} catch (RemoteException e) {
return Collections.emptyList();
}
public List<String> series() {
return listCall(new ListCallable<String>() {
public List<String> call() throws RemoteException {
return myInterface.series();
}
});
}
public synchronized List<String> titles(BookQuery query) {
if (myInterface == null) {
return Collections.emptyList();
}
try {
return myInterface.titles(SerializerUtil.serialize(query));
} catch (RemoteException e) {
return Collections.emptyList();
}
public List<String> titles(final BookQuery query) {
return listCall(new ListCallable<String>() {
public List<String> call() throws RemoteException {
return myInterface.titles(SerializerUtil.serialize(query));
}
});
}
public synchronized List<String> firstTitleLetters() {
if (myInterface == null) {
return Collections.emptyList();
}
try {
return myInterface.firstTitleLetters();
} catch (RemoteException e) {
return Collections.emptyList();
}
public List<String> firstTitleLetters() {
return listCall(new ListCallable<String>() {
public List<String> call() throws RemoteException {
return myInterface.firstTitleLetters();
}
});
}
public synchronized boolean saveBook(Book book) {
@ -359,14 +337,12 @@ public class BookCollectionShadow extends AbstractBookCollection implements Serv
}
}
public synchronized List<String> labels() {
if (myInterface != null) {
try {
public List<String> labels() {
return listCall(new ListCallable<String>() {
public List<String> call() throws RemoteException {
return myInterface.labels();
} catch (RemoteException e) {
}
}
return Collections.emptyList();
});
}
public String getHash(Book book, boolean force) {
@ -477,17 +453,14 @@ public class BookCollectionShadow extends AbstractBookCollection implements Serv
}
@Override
public synchronized List<Bookmark> bookmarks(BookmarkQuery query) {
if (myInterface == null) {
return Collections.emptyList();
}
try {
return SerializerUtil.deserializeBookmarkList(
myInterface.bookmarks(SerializerUtil.serialize(query))
);
} catch (RemoteException e) {
return Collections.emptyList();
}
public List<Bookmark> bookmarks(final BookmarkQuery query) {
return listCall(new ListCallable<Bookmark>() {
public List<Bookmark> call() throws RemoteException {
return SerializerUtil.deserializeBookmarkList(
myInterface.bookmarks(SerializerUtil.serialize(query))
);
}
});
}
public synchronized void saveBookmark(Bookmark bookmark) {
@ -521,15 +494,12 @@ public class BookCollectionShadow extends AbstractBookCollection implements Serv
}
}
public synchronized List<HighlightingStyle> highlightingStyles() {
if (myInterface == null) {
return Collections.emptyList();
}
try {
return SerializerUtil.deserializeStyleList(myInterface.highlightingStyles());
} catch (RemoteException e) {
return Collections.emptyList();
}
public List<HighlightingStyle> highlightingStyles() {
return listCall(new ListCallable<HighlightingStyle>() {
public List<HighlightingStyle> call() throws RemoteException {
return SerializerUtil.deserializeStyleList(myInterface.highlightingStyles());
}
});
}
public synchronized void saveHighlightingStyle(HighlightingStyle style) {
@ -552,26 +522,18 @@ public class BookCollectionShadow extends AbstractBookCollection implements Serv
}
}
public List<String> formats() {
if (myInterface == null) {
return Collections.emptyList();
}
try {
return myInterface.formats();
} catch (RemoteException e) {
return Collections.emptyList();
}
}
public List<String> activeFormats() {
if (myInterface == null) {
return Collections.emptyList();
}
try {
return myInterface.formats();
} catch (RemoteException e) {
return Collections.emptyList();
}
public List<FormatDescriptor> formats() {
return listCall(new ListCallable<FormatDescriptor>() {
public List<FormatDescriptor> call() throws RemoteException {
final List<String> serialized = myInterface.formats();
final List<FormatDescriptor> formats =
new ArrayList<FormatDescriptor>(serialized.size());
for (String s : serialized) {
formats.add(Util.stringToFormatDescriptor(s));
}
return formats;
}
});
}
public void setActiveFormats(List<String> formats) {
@ -583,6 +545,24 @@ public class BookCollectionShadow extends AbstractBookCollection implements Serv
}
}
private interface ListCallable<T> {
List<T> call() throws RemoteException;
}
private synchronized <T> List<T> listCall(ListCallable<T> callable) {
if (myInterface == null) {
return Collections.emptyList();
}
try {
return callable.call();
} catch (Exception e) {
return Collections.emptyList();
} catch (Throwable e) {
// TODO: report problem
return Collections.emptyList();
}
}
// method from ServiceConnection interface
public synchronized void onServiceConnected(ComponentName name, IBinder service) {
myInterface = LibraryInterface.Stub.asInterface(service);

View file

@ -75,6 +75,5 @@ interface LibraryInterface {
boolean canRemoveBook(in String book, in boolean deleteFromDisk);
List<String> formats();
List<String> activeFormats();
void setActiveFormats(in List<String> formats);
}

View file

@ -427,15 +427,16 @@ public class LibraryService extends Service {
}
public List<String> formats() {
return myCollection.formats();
final List<IBookCollection.FormatDescriptor> descriptors = myCollection.formats();
final List<String> serialized = new ArrayList<String>(descriptors.size());
for (IBookCollection.FormatDescriptor d : descriptors) {
serialized.add(Util.formatDescriptorToString(d));
}
return serialized;
}
public List<String> activeFormats() {
return myCollection.activeFormats();
}
public void setActiveFormats(List<String> formats) {
myCollection.setActiveFormats(formats);
public void setActiveFormats(List<String> formatIds) {
myCollection.setActiveFormats(formatIds);
}
}

View file

@ -19,21 +19,24 @@
package org.geometerplus.android.fbreader.libraryService;
import org.geometerplus.fbreader.book.Author;
import org.geometerplus.fbreader.book.Tag;
import org.geometerplus.fbreader.book.*;
abstract class Util {
static String authorToString(Author author) {
return new StringBuilder(author.DisplayName).append('\000').append(author.SortKey).toString();
return author.DisplayName + "\000" + author.SortKey;
}
static Author stringToAuthor(String string) {
final String[] split = string.split("\000");
if (split.length == 2) {
return new Author(split[0], split[1]);
} else {
if (string == null) {
return Author.NULL;
}
final String[] split = string.split("\000");
if (split.length != 2) {
return Author.NULL;
}
return new Author(split[0], split[1]);
}
static String tagToString(Tag tag) {
@ -41,11 +44,36 @@ abstract class Util {
}
static Tag stringToTag(String string) {
final String[] split = string.split("\000");
if (split.length > 0) {
return Tag.getTag(split);
} else {
if (string == null) {
return Tag.NULL;
}
final String[] split = string.split("\000");
if (split.length == 0) {
return Tag.NULL;
}
return Tag.getTag(split);
}
static String formatDescriptorToString(IBookCollection.FormatDescriptor descriptor) {
return descriptor.Id + "\000" + descriptor.Name + "\000" + (descriptor.IsActive ? 1 : 0);
}
static IBookCollection.FormatDescriptor stringToFormatDescriptor(String string) {
if (string == null) {
throw new IllegalArgumentException();
}
final String[] split = string.split("\000");
if (split.length != 3) {
throw new IllegalArgumentException();
}
final IBookCollection.FormatDescriptor descriptor = new IBookCollection.FormatDescriptor();
descriptor.Id = split[0];
descriptor.Name = split[1];
descriptor.IsActive = "1".equals(split[2]);
return descriptor;
}
}

View file

@ -801,21 +801,20 @@ public class BookCollection extends AbstractBookCollection {
}
}
public List<String> formats() {
public List<FormatDescriptor> formats() {
final List<FormatPlugin> plugins = PluginCollection.Instance().plugins();
final List<String> ids = new ArrayList<String>(plugins.size());
final List<FormatDescriptor> descriptors = new ArrayList<FormatDescriptor>(plugins.size());
for (FormatPlugin p : plugins) {
ids.add(p.supportedFileType());
final FormatDescriptor d = new FormatDescriptor();
d.Id = p.supportedFileType();
d.Name = p.name();
d.IsActive = true;
descriptors.add(d);
}
return ids;
return descriptors;
}
public List<String> activeFormats() {
// TODO: implement
return Collections.emptyList();
}
public void setActiveFormats(List<String> formats) {
public void setActiveFormats(List<String> formatIds) {
// TODO: implement
}
}

View file

@ -100,9 +100,14 @@ public interface IBookCollection {
List<HighlightingStyle> highlightingStyles();
void saveHighlightingStyle(HighlightingStyle style);
List<String> formats();
List<String> activeFormats();
void setActiveFormats(List<String> formats);
public static class FormatDescriptor {
public String Id;
public String Name;
public boolean IsActive;
}
List<FormatDescriptor> formats();
void setActiveFormats(List<String> formatIds);
void rescan(String path);
}

View file

@ -41,6 +41,10 @@ public abstract class FormatPlugin {
return myFileType;
}
public final String name() {
return myFileType + "!!!";
}
public ZLFile realBookFile(ZLFile file) throws BookReadingException {
return file;
}