diff --git a/src/org/geometerplus/android/fbreader/libraryService/BookCollectionShadow.java b/src/org/geometerplus/android/fbreader/libraryService/BookCollectionShadow.java index 00143b4bf..82cab9689 100644 --- a/src/org/geometerplus/android/fbreader/libraryService/BookCollectionShadow.java +++ b/src/org/geometerplus/android/fbreader/libraryService/BookCollectionShadow.java @@ -135,15 +135,14 @@ public class BookCollectionShadow extends AbstractBookCollection implements Serv } } - public synchronized List 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 books(final BookQuery query) { + return listCall(new ListCallable() { + public List 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 recentlyAddedBooks(int count) { - if (myInterface == null) { - return Collections.emptyList(); - } - try { - return SerializerUtil.deserializeBookList(myInterface.recentlyAddedBooks(count)); - } catch (RemoteException e) { - return Collections.emptyList(); - } + public List recentlyAddedBooks(final int count) { + return listCall(new ListCallable() { + public List call() throws RemoteException { + return SerializerUtil.deserializeBookList(myInterface.recentlyAddedBooks(count)); + } + }); } - public synchronized List recentlyOpenedBooks(int count) { - if (myInterface == null) { - return Collections.emptyList(); - } - try { - return SerializerUtil.deserializeBookList(myInterface.recentlyOpenedBooks(count)); - } catch (RemoteException e) { - return Collections.emptyList(); - } + public List recentlyOpenedBooks(final int count) { + return listCall(new ListCallable() { + public List 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 authors() { - if (myInterface == null) { - return Collections.emptyList(); - } - try { - final List strings = myInterface.authors(); - final List authors = new ArrayList(strings.size()); - for (String s : strings) { - authors.add(Util.stringToAuthor(s)); + public List authors() { + return listCall(new ListCallable() { + public List call() throws RemoteException { + final List strings = myInterface.authors(); + final List authors = new ArrayList(strings.size()); + for (String s : strings) { + authors.add(Util.stringToAuthor(s)); + } + return authors; } - return authors; - } catch (RemoteException e) { - return Collections.emptyList(); - } + }); } - public synchronized List tags() { - if (myInterface == null) { - return Collections.emptyList(); - } - try { - final List strings = myInterface.tags(); - final List tags = new ArrayList(strings.size()); - for (String s : strings) { - tags.add(Util.stringToTag(s)); + public List tags() { + return listCall(new ListCallable() { + public List call() throws RemoteException { + final List strings = myInterface.tags(); + final List tags = new ArrayList(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 series() { - if (myInterface == null) { - return Collections.emptyList(); - } - try { - return myInterface.series(); - } catch (RemoteException e) { - return Collections.emptyList(); - } + public List series() { + return listCall(new ListCallable() { + public List call() throws RemoteException { + return myInterface.series(); + } + }); } - public synchronized List titles(BookQuery query) { - if (myInterface == null) { - return Collections.emptyList(); - } - try { - return myInterface.titles(SerializerUtil.serialize(query)); - } catch (RemoteException e) { - return Collections.emptyList(); - } + public List titles(final BookQuery query) { + return listCall(new ListCallable() { + public List call() throws RemoteException { + return myInterface.titles(SerializerUtil.serialize(query)); + } + }); } - public synchronized List firstTitleLetters() { - if (myInterface == null) { - return Collections.emptyList(); - } - try { - return myInterface.firstTitleLetters(); - } catch (RemoteException e) { - return Collections.emptyList(); - } + public List firstTitleLetters() { + return listCall(new ListCallable() { + public List 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 labels() { - if (myInterface != null) { - try { + public List labels() { + return listCall(new ListCallable() { + public List 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 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 bookmarks(final BookmarkQuery query) { + return listCall(new ListCallable() { + public List 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 highlightingStyles() { - if (myInterface == null) { - return Collections.emptyList(); - } - try { - return SerializerUtil.deserializeStyleList(myInterface.highlightingStyles()); - } catch (RemoteException e) { - return Collections.emptyList(); - } + public List highlightingStyles() { + return listCall(new ListCallable() { + public List 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 formats() { - if (myInterface == null) { - return Collections.emptyList(); - } - try { - return myInterface.formats(); - } catch (RemoteException e) { - return Collections.emptyList(); - } - } - - public List activeFormats() { - if (myInterface == null) { - return Collections.emptyList(); - } - try { - return myInterface.formats(); - } catch (RemoteException e) { - return Collections.emptyList(); - } + public List formats() { + return listCall(new ListCallable() { + public List call() throws RemoteException { + final List serialized = myInterface.formats(); + final List formats = + new ArrayList(serialized.size()); + for (String s : serialized) { + formats.add(Util.stringToFormatDescriptor(s)); + } + return formats; + } + }); } public void setActiveFormats(List formats) { @@ -583,6 +545,24 @@ public class BookCollectionShadow extends AbstractBookCollection implements Serv } } + private interface ListCallable { + List call() throws RemoteException; + } + + private synchronized List listCall(ListCallable 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); diff --git a/src/org/geometerplus/android/fbreader/libraryService/LibraryInterface.aidl b/src/org/geometerplus/android/fbreader/libraryService/LibraryInterface.aidl index 8daf09d1e..c1408c721 100644 --- a/src/org/geometerplus/android/fbreader/libraryService/LibraryInterface.aidl +++ b/src/org/geometerplus/android/fbreader/libraryService/LibraryInterface.aidl @@ -75,6 +75,5 @@ interface LibraryInterface { boolean canRemoveBook(in String book, in boolean deleteFromDisk); List formats(); - List activeFormats(); void setActiveFormats(in List formats); } diff --git a/src/org/geometerplus/android/fbreader/libraryService/LibraryService.java b/src/org/geometerplus/android/fbreader/libraryService/LibraryService.java index c8f946e18..67cb2dbb6 100644 --- a/src/org/geometerplus/android/fbreader/libraryService/LibraryService.java +++ b/src/org/geometerplus/android/fbreader/libraryService/LibraryService.java @@ -427,15 +427,16 @@ public class LibraryService extends Service { } public List formats() { - return myCollection.formats(); + final List descriptors = myCollection.formats(); + final List serialized = new ArrayList(descriptors.size()); + for (IBookCollection.FormatDescriptor d : descriptors) { + serialized.add(Util.formatDescriptorToString(d)); + } + return serialized; } - public List activeFormats() { - return myCollection.activeFormats(); - } - - public void setActiveFormats(List formats) { - myCollection.setActiveFormats(formats); + public void setActiveFormats(List formatIds) { + myCollection.setActiveFormats(formatIds); } } diff --git a/src/org/geometerplus/android/fbreader/libraryService/Util.java b/src/org/geometerplus/android/fbreader/libraryService/Util.java index ac53ff1e0..44d84cd46 100644 --- a/src/org/geometerplus/android/fbreader/libraryService/Util.java +++ b/src/org/geometerplus/android/fbreader/libraryService/Util.java @@ -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; } } diff --git a/src/org/geometerplus/fbreader/book/BookCollection.java b/src/org/geometerplus/fbreader/book/BookCollection.java index e94f8ca5a..bbf52d60d 100644 --- a/src/org/geometerplus/fbreader/book/BookCollection.java +++ b/src/org/geometerplus/fbreader/book/BookCollection.java @@ -801,21 +801,20 @@ public class BookCollection extends AbstractBookCollection { } } - public List formats() { + public List formats() { final List plugins = PluginCollection.Instance().plugins(); - final List ids = new ArrayList(plugins.size()); + final List descriptors = new ArrayList(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 activeFormats() { - // TODO: implement - return Collections.emptyList(); - } - - public void setActiveFormats(List formats) { + public void setActiveFormats(List formatIds) { // TODO: implement } } diff --git a/src/org/geometerplus/fbreader/book/IBookCollection.java b/src/org/geometerplus/fbreader/book/IBookCollection.java index bb712ad2d..f6a6b4233 100644 --- a/src/org/geometerplus/fbreader/book/IBookCollection.java +++ b/src/org/geometerplus/fbreader/book/IBookCollection.java @@ -100,9 +100,14 @@ public interface IBookCollection { List highlightingStyles(); void saveHighlightingStyle(HighlightingStyle style); - List formats(); - List activeFormats(); - void setActiveFormats(List formats); + public static class FormatDescriptor { + public String Id; + public String Name; + public boolean IsActive; + } + + List formats(); + void setActiveFormats(List formatIds); void rescan(String path); } diff --git a/src/org/geometerplus/fbreader/formats/FormatPlugin.java b/src/org/geometerplus/fbreader/formats/FormatPlugin.java index a0495d307..e4d360081 100644 --- a/src/org/geometerplus/fbreader/formats/FormatPlugin.java +++ b/src/org/geometerplus/fbreader/formats/FormatPlugin.java @@ -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; }