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

Book.getByFile() => IBookCollection.getBookByFile()

This commit is contained in:
Nikolay Pultsin 2013-01-13 05:55:34 +04:00
parent 2ec3c907af
commit bc3976f5f6
12 changed files with 138 additions and 92 deletions

View file

@ -65,7 +65,6 @@ public final class FBReader extends ZLAndroidActivity {
public static final int RESULT_RELOAD_BOOK = RESULT_FIRST_USER + 2; public static final int RESULT_RELOAD_BOOK = RESULT_FIRST_USER + 2;
private int myFullScreenFlag; private int myFullScreenFlag;
private final BookCollectionShadow myCollection = new BookCollectionShadow(this);
private static final String PLUGIN_ACTION_PREFIX = "___"; private static final String PLUGIN_ACTION_PREFIX = "___";
private final List<PluginApi.ActionInfo> myPluginActions = private final List<PluginApi.ActionInfo> myPluginActions =
@ -120,10 +119,17 @@ public final class FBReader extends ZLAndroidActivity {
}; };
} }
private BookCollectionShadow getCollection() {
return (BookCollectionShadow)((FBReaderApp)FBReaderApp.Instance()).Collection;
}
@Override
protected void init(Runnable action) {
getCollection().bindToService(action);
}
@Override @Override
public void onCreate(Bundle icicle) { public void onCreate(Bundle icicle) {
myCollection.bindToService(null);
super.onCreate(icicle); super.onCreate(icicle);
final FBReaderApp fbReader = (FBReaderApp)FBReaderApp.Instance(); final FBReaderApp fbReader = (FBReaderApp)FBReaderApp.Instance();
@ -245,7 +251,7 @@ public final class FBReader extends ZLAndroidActivity {
@Override @Override
protected void onRestart() { protected void onRestart() {
super.onRestart(); super.onRestart();
myCollection.bindToService(null); getCollection().bindToService(null);
} }
@Override @Override
@ -335,7 +341,7 @@ public final class FBReader extends ZLAndroidActivity {
public void onStop() { public void onStop() {
ApiServerImplementation.sendEvent(this, ApiListener.EVENT_READ_MODE_CLOSED); ApiServerImplementation.sendEvent(this, ApiListener.EVENT_READ_MODE_CLOSED);
PopupPanel.removeAllWindows(FBReaderApp.Instance(), this); PopupPanel.removeAllWindows(FBReaderApp.Instance(), this);
myCollection.unbind(); getCollection().unbind();
super.onStop(); super.onStop();
} }
@ -344,12 +350,7 @@ public final class FBReader extends ZLAndroidActivity {
if (SQLiteBooksDatabase.Instance() == null) { if (SQLiteBooksDatabase.Instance() == null) {
new SQLiteBooksDatabase(this, "READER"); new SQLiteBooksDatabase(this, "READER");
} }
return new FBReaderApp(myCollection); return new FBReaderApp(new BookCollectionShadow(this));
}
@Override
protected void updateApplication() {
((FBReaderApp)FBReaderApp.Instance()).setCollection(myCollection);
} }
@Override @Override
@ -507,7 +508,7 @@ public final class FBReader extends ZLAndroidActivity {
text, text,
true true
); );
myCollection.saveBookmark(bookmark); fbReader.Collection.saveBookmark(bookmark);
fbView.clearSelection(); fbView.clearSelection();
UIUtil.showMessageText( UIUtil.showMessageText(

View file

@ -26,6 +26,8 @@ import android.content.*;
import android.os.IBinder; import android.os.IBinder;
import android.os.RemoteException; import android.os.RemoteException;
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
import org.geometerplus.fbreader.library.*; import org.geometerplus.fbreader.library.*;
public class BookCollectionShadow implements IBookCollection, ServiceConnection { public class BookCollectionShadow implements IBookCollection, ServiceConnection {
@ -37,16 +39,21 @@ public class BookCollectionShadow implements IBookCollection, ServiceConnection
myContext = context; myContext = context;
} }
public void bindToService(Runnable onBindAction) { public synchronized void bindToService(Runnable onBindAction) {
if (myInterface != null) { if (myInterface != null) {
return; if (onBindAction != null) {
onBindAction.run();
}
} else {
if (onBindAction != null) {
myOnBindAction = onBindAction;
}
myContext.bindService(
new Intent(myContext, LibraryService.class),
this,
LibraryService.BIND_AUTO_CREATE
);
} }
myOnBindAction = onBindAction;
myContext.bindService(
new Intent(myContext, LibraryService.class),
this,
LibraryService.BIND_AUTO_CREATE
);
} }
public void unbind() { public void unbind() {
@ -120,6 +127,17 @@ public class BookCollectionShadow implements IBookCollection, ServiceConnection
} }
} }
public synchronized Book getBookByFile(ZLFile file) {
if (myInterface == null) {
return null;
}
try {
return SerializerUtil.deserializeBook(myInterface.getBookByFile(file.getPath()));
} catch (RemoteException e) {
return null;
}
}
public synchronized Book getBookById(long id) { public synchronized Book getBookById(long id) {
if (myInterface == null) { if (myInterface == null) {
return null; return null;
@ -207,6 +225,7 @@ public class BookCollectionShadow implements IBookCollection, ServiceConnection
myInterface = LibraryInterface.Stub.asInterface(service); myInterface = LibraryInterface.Stub.asInterface(service);
if (myOnBindAction != null) { if (myOnBindAction != null) {
myOnBindAction.run(); myOnBindAction.run();
myOnBindAction = null;
} }
} }

View file

@ -11,6 +11,7 @@ interface LibraryInterface {
List<String> books(String pattern); List<String> books(String pattern);
List<String> recentBooks(); List<String> recentBooks();
List<String> favorites(); List<String> favorites();
String getBookByFile(in String file);
String getBookById(in long id); String getBookById(in long id);
String getRecentBook(in int index); String getRecentBook(in int index);

View file

@ -27,6 +27,8 @@ import android.content.Intent;
import android.os.IBinder; import android.os.IBinder;
import android.os.FileObserver; import android.os.FileObserver;
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
import org.geometerplus.fbreader.library.*; import org.geometerplus.fbreader.library.*;
import org.geometerplus.android.fbreader.library.SQLiteBooksDatabase; import org.geometerplus.android.fbreader.library.SQLiteBooksDatabase;
@ -141,6 +143,10 @@ public class LibraryService extends Service {
return SerializerUtil.serialize(myCollection.getRecentBook(index)); return SerializerUtil.serialize(myCollection.getRecentBook(index));
} }
public String getBookByFile(String file) {
return SerializerUtil.serialize(myCollection.getBookByFile(ZLFile.createFileByPath(file)));
}
public String getBookById(long id) { public String getBookById(long id) {
return SerializerUtil.serialize(myCollection.getBookById(id)); return SerializerUtil.serialize(myCollection.getBookById(id));
} }

View file

@ -118,10 +118,10 @@ public final class FBReaderApp extends ZLApplication {
private ZLTextPosition myJumpEndPosition; private ZLTextPosition myJumpEndPosition;
private Date myJumpTimeStamp; private Date myJumpTimeStamp;
private IBookCollection myCollection; public final IBookCollection Collection;
public FBReaderApp(IBookCollection collection) { public FBReaderApp(IBookCollection collection) {
setCollection(collection); Collection = collection;
addAction(ActionCode.INCREASE_FONT, new ChangeFontSizeAction(this, +2)); addAction(ActionCode.INCREASE_FONT, new ChangeFontSizeAction(this, +2));
addAction(ActionCode.DECREASE_FONT, new ChangeFontSizeAction(this, -2)); addAction(ActionCode.DECREASE_FONT, new ChangeFontSizeAction(this, -2));
@ -154,10 +154,6 @@ public final class FBReaderApp extends ZLApplication {
setView(BookTextView); setView(BookTextView);
} }
public void setCollection(IBookCollection collection) {
myCollection = collection;
}
public void openBook(final Book book, final Bookmark bookmark, final Runnable postAction) { public void openBook(final Book book, final Bookmark bookmark, final Runnable postAction) {
if (book != null || Model == null) { if (book != null || Model == null) {
runWithMessage("loadingBook", new Runnable() { runWithMessage("loadingBook", new Runnable() {
@ -238,9 +234,9 @@ public final class FBReaderApp extends ZLApplication {
synchronized void openBookInternal(Book book, Bookmark bookmark, boolean force) { synchronized void openBookInternal(Book book, Bookmark bookmark, boolean force) {
if (book == null) { if (book == null) {
book = myCollection.getRecentBook(0); book = Collection.getRecentBook(0);
if (book == null || !book.File.exists()) { if (book == null || !book.File.exists()) {
book = Book.getByFile(Library.getHelpFile()); book = Collection.getBookByFile(Library.getHelpFile());
} }
if (book == null) { if (book == null) {
return; return;
@ -272,7 +268,7 @@ public final class FBReaderApp extends ZLApplication {
} else { } else {
gotoBookmark(bookmark); gotoBookmark(bookmark);
} }
myCollection.addBookToRecentList(book); Collection.addBookToRecentList(book);
final StringBuilder title = new StringBuilder(book.getTitle()); final StringBuilder title = new StringBuilder(book.getTitle());
if (!book.authors().isEmpty()) { if (!book.authors().isEmpty()) {
boolean first = true; boolean first = true;
@ -310,12 +306,12 @@ public final class FBReaderApp extends ZLApplication {
return false; return false;
} }
final List<Bookmark> bookmarks = myCollection.invisibleBookmarks(Model.Book); final List<Bookmark> bookmarks = Collection.invisibleBookmarks(Model.Book);
if (bookmarks.isEmpty()) { if (bookmarks.isEmpty()) {
return false; return false;
} }
final Bookmark b = bookmarks.get(0); final Bookmark b = bookmarks.get(0);
myCollection.deleteBookmark(b); Collection.deleteBookmark(b);
gotoBookmark(b); gotoBookmark(b);
return true; return true;
} finally { } finally {
@ -346,14 +342,14 @@ public final class FBReaderApp extends ZLApplication {
if (file == null) { if (file == null) {
return null; return null;
} }
Book book = Book.getByFile(file); Book book = Collection.getBookByFile(file);
if (book != null) { if (book != null) {
book.insertIntoBookList(); book.insertIntoBookList();
return book; return book;
} }
if (file.isArchive()) { if (file.isArchive()) {
for (ZLFile child : file.children()) { for (ZLFile child : file.children()) {
book = Book.getByFile(child); book = Collection.getBookByFile(child);
if (book != null) { if (book != null) {
book.insertIntoBookList(); book.insertIntoBookList();
return book; return book;
@ -424,7 +420,7 @@ public final class FBReaderApp extends ZLApplication {
)); ));
} }
if (ShowPreviousBookInCancelMenuOption.getValue()) { if (ShowPreviousBookInCancelMenuOption.getValue()) {
final Book previousBook = myCollection.getRecentBook(1); final Book previousBook = Collection.getRecentBook(1);
if (previousBook != null) { if (previousBook != null) {
myCancelActionsList.add(new CancelActionDescription( myCancelActionsList.add(new CancelActionDescription(
CancelActionType.previousBook, previousBook.getTitle() CancelActionType.previousBook, previousBook.getTitle()
@ -433,7 +429,7 @@ public final class FBReaderApp extends ZLApplication {
} }
if (ShowPositionsInCancelMenuOption.getValue()) { if (ShowPositionsInCancelMenuOption.getValue()) {
if (Model != null && Model.Book != null) { if (Model != null && Model.Book != null) {
for (Bookmark bookmark : myCollection.invisibleBookmarks(Model.Book)) { for (Bookmark bookmark : Collection.invisibleBookmarks(Model.Book)) {
myCancelActionsList.add(new BookmarkDescription(bookmark)); myCancelActionsList.add(new BookmarkDescription(bookmark));
} }
} }
@ -458,12 +454,12 @@ public final class FBReaderApp extends ZLApplication {
runAction(ActionCode.SHOW_NETWORK_LIBRARY); runAction(ActionCode.SHOW_NETWORK_LIBRARY);
break; break;
case previousBook: case previousBook:
openBook(myCollection.getRecentBook(1), null, null); openBook(Collection.getRecentBook(1), null, null);
break; break;
case returnTo: case returnTo:
{ {
final Bookmark b = ((BookmarkDescription)description).Bookmark; final Bookmark b = ((BookmarkDescription)description).Bookmark;
myCollection.deleteBookmark(b); Collection.deleteBookmark(b);
gotoBookmark(b); gotoBookmark(b);
break; break;
} }
@ -475,15 +471,15 @@ public final class FBReaderApp extends ZLApplication {
private synchronized void updateInvisibleBookmarksList(Bookmark b) { private synchronized void updateInvisibleBookmarksList(Bookmark b) {
if (Model != null && Model.Book != null && b != null) { if (Model != null && Model.Book != null && b != null) {
for (Bookmark bm : myCollection.invisibleBookmarks(Model.Book)) { for (Bookmark bm : Collection.invisibleBookmarks(Model.Book)) {
if (b.equals(bm)) { if (b.equals(bm)) {
myCollection.deleteBookmark(bm); Collection.deleteBookmark(bm);
} }
} }
myCollection.saveBookmark(b); Collection.saveBookmark(b);
final List<Bookmark> bookmarks = myCollection.invisibleBookmarks(Model.Book); final List<Bookmark> bookmarks = Collection.invisibleBookmarks(Model.Book);
for (int i = 3; i < bookmarks.size(); ++i) { for (int i = 3; i < bookmarks.size(); ++i) {
myCollection.deleteBookmark(bookmarks.get(i)); Collection.deleteBookmark(bookmarks.get(i));
} }
} }
} }

View file

@ -39,42 +39,6 @@ import org.geometerplus.fbreader.bookmodel.BookReadingException;
import org.geometerplus.fbreader.Paths; import org.geometerplus.fbreader.Paths;
public class Book { public class Book {
public static Book getByFile(ZLFile bookFile) {
if (bookFile == null) {
return null;
}
final ZLPhysicalFile physicalFile = bookFile.getPhysicalFile();
if (physicalFile != null && !physicalFile.exists()) {
return null;
}
final FileInfoSet fileInfos = new FileInfoSet(bookFile);
Book book = BooksDatabase.Instance().loadBookByFile(fileInfos.getId(bookFile), bookFile);
if (book != null) {
book.loadLists();
}
if (book != null && fileInfos.check(physicalFile, physicalFile != bookFile)) {
return book;
}
fileInfos.save();
try {
if (book == null) {
book = new Book(bookFile);
} else {
book.readMetaInfo();
}
} catch (BookReadingException e) {
return null;
}
book.save();
return book;
}
public final ZLFile File; public final ZLFile File;
private volatile long myId; private volatile long myId;

View file

@ -73,6 +73,49 @@ public class BookCollection implements IBookCollection {
return myBooksByFile.size(); return myBooksByFile.size();
} }
public Book getBookByFile(ZLFile bookFile) {
if (bookFile == null) {
return null;
}
Book book = myBooksByFile.get(bookFile);
if (book != null) {
return book;
}
final ZLPhysicalFile physicalFile = bookFile.getPhysicalFile();
if (physicalFile != null && !physicalFile.exists()) {
return null;
}
final FileInfoSet fileInfos = new FileInfoSet(bookFile);
book = BooksDatabase.Instance().loadBookByFile(fileInfos.getId(bookFile), bookFile);
if (book != null) {
book.loadLists();
}
if (book != null && fileInfos.check(physicalFile, physicalFile != bookFile)) {
addBook(book);
return book;
}
fileInfos.save();
try {
if (book == null) {
book = new Book(bookFile);
} else {
book.readMetaInfo();
}
} catch (BookReadingException e) {
return null;
}
book.save();
addBook(book);
return book;
}
public Book getBookById(long id) { public Book getBookById(long id) {
Book book = myBooksById.get(id); Book book = myBooksById.get(id);
if (book != null) { if (book != null) {

View file

@ -25,8 +25,11 @@ import org.geometerplus.zlibrary.core.filesystem.ZLFile;
import org.geometerplus.fbreader.Paths; import org.geometerplus.fbreader.Paths;
public class FileFirstLevelTree extends FirstLevelTree { public class FileFirstLevelTree extends FirstLevelTree {
FileFirstLevelTree(RootTree root, String id) { private final IBookCollection myCollection;
FileFirstLevelTree(IBookCollection collection, RootTree root, String id) {
super(root, id); super(root, id);
myCollection = collection;
addChild(Paths.BooksDirectoryOption().getValue(), "fileTreeLibrary"); addChild(Paths.BooksDirectoryOption().getValue(), "fileTreeLibrary");
addChild("/", "fileTreeRoot"); addChild("/", "fileTreeRoot");
addChild(Paths.cardDirectory(), "fileTreeCard"); addChild(Paths.cardDirectory(), "fileTreeCard");
@ -38,6 +41,7 @@ public class FileFirstLevelTree extends FirstLevelTree {
final ZLResource resource = LibraryUtil.resource().getResource(resourceKey); final ZLResource resource = LibraryUtil.resource().getResource(resourceKey);
new FileTree( new FileTree(
this, this,
myCollection,
file, file,
resource.getValue(), resource.getValue(),
resource.getResource("summary").getValue() resource.getResource("summary").getValue()

View file

@ -28,13 +28,15 @@ import org.geometerplus.fbreader.formats.PluginCollection;
import org.geometerplus.fbreader.tree.FBTree; import org.geometerplus.fbreader.tree.FBTree;
public class FileTree extends LibraryTree { public class FileTree extends LibraryTree {
private final IBookCollection myCollection;
private final ZLFile myFile; private final ZLFile myFile;
private final String myName; private final String myName;
private final String mySummary; private final String mySummary;
private final boolean myIsSelectable; private final boolean myIsSelectable;
FileTree(LibraryTree parent, ZLFile file, String name, String summary) { FileTree(LibraryTree parent, IBookCollection collection, ZLFile file, String name, String summary) {
super(parent); super(parent);
myCollection = collection;
myFile = file; myFile = file;
myName = name; myName = name;
mySummary = summary; mySummary = summary;
@ -43,6 +45,7 @@ public class FileTree extends LibraryTree {
public FileTree(FileTree parent, ZLFile file) { public FileTree(FileTree parent, ZLFile file) {
super(parent); super(parent);
myCollection = parent.myCollection;
myFile = file; myFile = file;
myName = null; myName = null;
mySummary = null; mySummary = null;
@ -98,7 +101,7 @@ public class FileTree extends LibraryTree {
@Override @Override
public Book getBook() { public Book getBook() {
if (myBook == null) { if (myBook == null) {
myBook = Book.getByFile(myFile); myBook = myCollection.getBookByFile(myFile);
if (myBook == null) { if (myBook == null) {
myBook = NULL_BOOK; myBook = NULL_BOOK;
} }

View file

@ -21,6 +21,8 @@ package org.geometerplus.fbreader.library;
import java.util.List; import java.util.List;
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
public interface IBookCollection { public interface IBookCollection {
public interface Listener { public interface Listener {
public enum BookEvent { public enum BookEvent {
@ -48,6 +50,7 @@ public interface IBookCollection {
List<Book> books(String pattern); List<Book> books(String pattern);
List<Book> recentBooks(); List<Book> recentBooks();
List<Book> favorites(); List<Book> favorites();
Book getBookByFile(ZLFile file);
Book getBookById(long id); Book getBookById(long id);
Book getRecentBook(int index); Book getRecentBook(int index);

View file

@ -128,7 +128,7 @@ public final class Library {
new FirstLevelTree(myRootTree, ROOT_BY_AUTHOR); new FirstLevelTree(myRootTree, ROOT_BY_AUTHOR);
new FirstLevelTree(myRootTree, ROOT_BY_TITLE); new FirstLevelTree(myRootTree, ROOT_BY_TITLE);
new FirstLevelTree(myRootTree, ROOT_BY_TAG); new FirstLevelTree(myRootTree, ROOT_BY_TAG);
new FileFirstLevelTree(myRootTree, ROOT_FILE_TREE); new FileFirstLevelTree(collection, myRootTree, ROOT_FILE_TREE);
} }
public LibraryTree getRootTree() { public LibraryTree getRootTree() {

View file

@ -37,7 +37,7 @@ import org.geometerplus.zlibrary.ui.android.application.ZLAndroidApplicationWind
public abstract class ZLAndroidActivity extends Activity { public abstract class ZLAndroidActivity extends Activity {
protected abstract ZLApplication createApplication(); protected abstract ZLApplication createApplication();
protected abstract void updateApplication(); protected abstract void init(Runnable action);
private static final String REQUESTED_ORIENTATION_KEY = "org.geometerplus.zlibrary.ui.android.library.androidActiviy.RequestedOrientation"; private static final String REQUESTED_ORIENTATION_KEY = "org.geometerplus.zlibrary.ui.android.library.androidActiviy.RequestedOrientation";
private static final String ORIENTATION_CHANGE_COUNTER_KEY = "org.geometerplus.zlibrary.ui.android.library.androidActiviy.ChangeCounter"; private static final String ORIENTATION_CHANGE_COUNTER_KEY = "org.geometerplus.zlibrary.ui.android.library.androidActiviy.ChangeCounter";
@ -98,18 +98,20 @@ public abstract class ZLAndroidActivity extends Activity {
final ZLApplication application = createApplication(); final ZLApplication application = createApplication();
androidApplication.myMainWindow = new ZLAndroidApplicationWindow(application); androidApplication.myMainWindow = new ZLAndroidApplicationWindow(application);
application.initWindow(); application.initWindow();
} else {
updateApplication();
} }
new Thread() { init(new Runnable() {
public void run() { public void run() {
ZLApplication.Instance().openFile(fileFromIntent(getIntent()), getPostponedInitAction()); new Thread() {
public void run() {
ZLApplication.Instance().openFile(fileFromIntent(getIntent()), getPostponedInitAction());
ZLApplication.Instance().getViewWidget().repaint();
}
}.start();
ZLApplication.Instance().getViewWidget().repaint(); ZLApplication.Instance().getViewWidget().repaint();
} }
}.start(); });
ZLApplication.Instance().getViewWidget().repaint();
} }
protected abstract Runnable getPostponedInitAction(); protected abstract Runnable getPostponedInitAction();
@ -194,11 +196,15 @@ public abstract class ZLAndroidActivity extends Activity {
} }
@Override @Override
protected void onNewIntent(Intent intent) { protected void onNewIntent(final Intent intent) {
super.onNewIntent(intent); super.onNewIntent(intent);
final String action = intent.getAction(); final String action = intent.getAction();
if (Intent.ACTION_VIEW.equals(action) || "android.fbreader.action.VIEW".equals(action)) { if (Intent.ACTION_VIEW.equals(action) || "android.fbreader.action.VIEW".equals(action)) {
ZLApplication.Instance().openFile(fileFromIntent(intent), null); init(new Runnable() {
public void run() {
ZLApplication.Instance().openFile(fileFromIntent(intent), null);
}
});
} }
} }