From 53c146e532e3df82c0c8f6023cfc8a0425599bd0 Mon Sep 17 00:00:00 2001 From: Nikolay Pultsin Date: Sun, 20 Jan 2013 02:39:31 +0400 Subject: [PATCH] fixed book opening --- .../android/fbreader/BookmarksActivity.java | 4 +- .../android/fbreader/FBReader.java | 250 +++++++++--------- .../fbreader/library/BookInfoActivity.java | 4 +- .../fbreader/library/LibraryActivity.java | 8 +- .../libraryService/BookCollectionShadow.java | 26 +- .../fbreader/fbreader/FBReaderApp.java | 24 -- .../core/application/ZLApplication.java | 2 - 7 files changed, 144 insertions(+), 174 deletions(-) diff --git a/src/org/geometerplus/android/fbreader/BookmarksActivity.java b/src/org/geometerplus/android/fbreader/BookmarksActivity.java index 78468450c..9509e280d 100644 --- a/src/org/geometerplus/android/fbreader/BookmarksActivity.java +++ b/src/org/geometerplus/android/fbreader/BookmarksActivity.java @@ -46,7 +46,7 @@ public class BookmarksActivity extends TabActivity implements MenuItem.OnMenuIte private static final int EDIT_ITEM_ID = 1; private static final int DELETE_ITEM_ID = 2; - private final BookCollectionShadow myCollection = new BookCollectionShadow(this); + private final BookCollectionShadow myCollection = new BookCollectionShadow(); private List myAllBooksBookmarks; private final List myThisBookBookmarks = new LinkedList(); @@ -112,7 +112,7 @@ public class BookmarksActivity extends TabActivity implements MenuItem.OnMenuIte protected void onStart() { super.onStart(); - myCollection.bindToService(new Runnable() { + myCollection.bindToService(this, new Runnable() { public void run() { if (myAllBooksBookmarks == null) { init(); diff --git a/src/org/geometerplus/android/fbreader/FBReader.java b/src/org/geometerplus/android/fbreader/FBReader.java index c81e70e04..185659afb 100644 --- a/src/org/geometerplus/android/fbreader/FBReader.java +++ b/src/org/geometerplus/android/fbreader/FBReader.java @@ -56,7 +56,8 @@ import org.geometerplus.android.fbreader.tips.TipsActivity; import org.geometerplus.android.util.UIUtil; public final class FBReader extends Activity { - public static final String BOOK_PATH_KEY = "BookPath"; + public static final String ACTION_OPEN_BOOK = "android.fbreader.action.VIEW"; + public static final String BOOK_KEY = "fbreader.book"; public static final int REQUEST_PREFERENCES = 1; public static final int REQUEST_BOOK_INFO = 2; @@ -70,6 +71,9 @@ public final class FBReader extends Activity { return (ZLAndroidLibrary)ZLAndroidLibrary.Instance(); } + private FBReaderApp myFBReaderApp; + private Book myBook; + private int myFullScreenFlag; private static final String PLUGIN_ACTION_PREFIX = "___"; @@ -81,17 +85,16 @@ public final class FBReader extends Activity { final ArrayList actions = getResultExtras(true).getParcelableArrayList(PluginApi.PluginInfo.KEY); if (actions != null) { synchronized (myPluginActions) { - final FBReaderApp fbReader = (FBReaderApp)FBReaderApp.Instance(); int index = 0; while (index < myPluginActions.size()) { - fbReader.removeAction(PLUGIN_ACTION_PREFIX + index++); + myFBReaderApp.removeAction(PLUGIN_ACTION_PREFIX + index++); } myPluginActions.addAll(actions); index = 0; for (PluginApi.ActionInfo info : myPluginActions) { - fbReader.addAction( + myFBReaderApp.addAction( PLUGIN_ACTION_PREFIX + index++, - new RunPluginAction(FBReader.this, fbReader, info.getId()) + new RunPluginAction(FBReader.this, myFBReaderApp, info.getId()) ); } } @@ -99,15 +102,38 @@ public final class FBReader extends Activity { } }; - private ZLFile fileFromIntent(Intent intent) { - String filePath = intent.getStringExtra(BOOK_PATH_KEY); - if (filePath == null) { + private synchronized void openBook(Intent intent, Runnable action, boolean force) { + if (!force && myBook != null) { + return; + } + + myBook = SerializerUtil.deserializeBook(intent.getStringExtra(BOOK_KEY)); + if (myBook == null) { final Uri data = intent.getData(); if (data != null) { - filePath = data.getPath(); + myBook = createBookForFile(ZLFile.createFileByPath(data.getPath())); } } - return filePath != null ? ZLFile.createFileByPath(filePath) : null; + myFBReaderApp.openBook(myBook, null, action); + } + + private Book createBookForFile(ZLFile file) { + if (file == null) { + return null; + } + Book book = myFBReaderApp.Collection.getBookByFile(file); + if (book != null) { + return book; + } + if (file.isArchive()) { + for (ZLFile child : file.children()) { + book = myFBReaderApp.Collection.getBookByFile(child); + if (book != null) { + return book; + } + } + } + return null; } private Runnable getPostponedInitAction() { @@ -123,14 +149,6 @@ public final class FBReader extends Activity { }; } - private BookCollectionShadow getCollection() { - return (BookCollectionShadow)((FBReaderApp)FBReaderApp.Instance()).Collection; - } - - private void init(Runnable action) { - getCollection().bindToService(action); - } - @Override protected void onCreate(Bundle icicle) { super.onCreate(icicle); @@ -144,26 +162,15 @@ public final class FBReader extends Activity { getZLibrary().setActivity(this); final ZLAndroidApplication androidApplication = (ZLAndroidApplication)getApplication(); + myFBReaderApp = (FBReaderApp)FBReaderApp.Instance(); + if (myFBReaderApp == null) { + myFBReaderApp = new FBReaderApp(new BookCollectionShadow()); + } if (androidApplication.myMainWindow == null) { - final FBReaderApp fbreader = createApplication(); - androidApplication.myMainWindow = new ZLAndroidApplicationWindow(fbreader); - fbreader.initWindow(); + androidApplication.myMainWindow = new ZLAndroidApplicationWindow(myFBReaderApp); + myFBReaderApp.initWindow(); } - init(new Runnable() { - public void run() { - new Thread() { - public void run() { - FBReaderApp.Instance().openFile(fileFromIntent(getIntent()), getPostponedInitAction()); - FBReaderApp.Instance().getViewWidget().repaint(); - } - }.start(); - - FBReaderApp.Instance().getViewWidget().repaint(); - } - }); - - final FBReaderApp fbReader = (FBReaderApp)FBReaderApp.Instance(); final ZLAndroidLibrary zlibrary = (ZLAndroidLibrary)ZLibrary.Instance(); myFullScreenFlag = zlibrary.ShowStatusBarOption.getValue() ? 0 : WindowManager.LayoutParams.FLAG_FULLSCREEN; @@ -171,46 +178,46 @@ public final class FBReader extends Activity { WindowManager.LayoutParams.FLAG_FULLSCREEN, myFullScreenFlag ); - if (fbReader.getPopupById(TextSearchPopup.ID) == null) { - new TextSearchPopup(fbReader); + if (myFBReaderApp.getPopupById(TextSearchPopup.ID) == null) { + new TextSearchPopup(myFBReaderApp); } - if (fbReader.getPopupById(NavigationPopup.ID) == null) { - new NavigationPopup(fbReader); + if (myFBReaderApp.getPopupById(NavigationPopup.ID) == null) { + new NavigationPopup(myFBReaderApp); } - if (fbReader.getPopupById(SelectionPopup.ID) == null) { - new SelectionPopup(fbReader); + if (myFBReaderApp.getPopupById(SelectionPopup.ID) == null) { + new SelectionPopup(myFBReaderApp); } - fbReader.addAction(ActionCode.SHOW_LIBRARY, new ShowLibraryAction(this, fbReader)); - fbReader.addAction(ActionCode.SHOW_PREFERENCES, new ShowPreferencesAction(this, fbReader)); - fbReader.addAction(ActionCode.SHOW_BOOK_INFO, new ShowBookInfoAction(this, fbReader)); - fbReader.addAction(ActionCode.SHOW_TOC, new ShowTOCAction(this, fbReader)); - fbReader.addAction(ActionCode.SHOW_BOOKMARKS, new ShowBookmarksAction(this, fbReader)); - fbReader.addAction(ActionCode.SHOW_NETWORK_LIBRARY, new ShowNetworkLibraryAction(this, fbReader)); + myFBReaderApp.addAction(ActionCode.SHOW_LIBRARY, new ShowLibraryAction(this, myFBReaderApp)); + myFBReaderApp.addAction(ActionCode.SHOW_PREFERENCES, new ShowPreferencesAction(this, myFBReaderApp)); + myFBReaderApp.addAction(ActionCode.SHOW_BOOK_INFO, new ShowBookInfoAction(this, myFBReaderApp)); + myFBReaderApp.addAction(ActionCode.SHOW_TOC, new ShowTOCAction(this, myFBReaderApp)); + myFBReaderApp.addAction(ActionCode.SHOW_BOOKMARKS, new ShowBookmarksAction(this, myFBReaderApp)); + myFBReaderApp.addAction(ActionCode.SHOW_NETWORK_LIBRARY, new ShowNetworkLibraryAction(this, myFBReaderApp)); - fbReader.addAction(ActionCode.SHOW_MENU, new ShowMenuAction(this, fbReader)); - fbReader.addAction(ActionCode.SHOW_NAVIGATION, new ShowNavigationAction(this, fbReader)); - fbReader.addAction(ActionCode.SEARCH, new SearchAction(this, fbReader)); - fbReader.addAction(ActionCode.SHARE_BOOK, new ShareBookAction(this, fbReader)); + myFBReaderApp.addAction(ActionCode.SHOW_MENU, new ShowMenuAction(this, myFBReaderApp)); + myFBReaderApp.addAction(ActionCode.SHOW_NAVIGATION, new ShowNavigationAction(this, myFBReaderApp)); + myFBReaderApp.addAction(ActionCode.SEARCH, new SearchAction(this, myFBReaderApp)); + myFBReaderApp.addAction(ActionCode.SHARE_BOOK, new ShareBookAction(this, myFBReaderApp)); - fbReader.addAction(ActionCode.SELECTION_SHOW_PANEL, new SelectionShowPanelAction(this, fbReader)); - fbReader.addAction(ActionCode.SELECTION_HIDE_PANEL, new SelectionHidePanelAction(this, fbReader)); - fbReader.addAction(ActionCode.SELECTION_COPY_TO_CLIPBOARD, new SelectionCopyAction(this, fbReader)); - fbReader.addAction(ActionCode.SELECTION_SHARE, new SelectionShareAction(this, fbReader)); - fbReader.addAction(ActionCode.SELECTION_TRANSLATE, new SelectionTranslateAction(this, fbReader)); - fbReader.addAction(ActionCode.SELECTION_BOOKMARK, new SelectionBookmarkAction(this, fbReader)); + myFBReaderApp.addAction(ActionCode.SELECTION_SHOW_PANEL, new SelectionShowPanelAction(this, myFBReaderApp)); + myFBReaderApp.addAction(ActionCode.SELECTION_HIDE_PANEL, new SelectionHidePanelAction(this, myFBReaderApp)); + myFBReaderApp.addAction(ActionCode.SELECTION_COPY_TO_CLIPBOARD, new SelectionCopyAction(this, myFBReaderApp)); + myFBReaderApp.addAction(ActionCode.SELECTION_SHARE, new SelectionShareAction(this, myFBReaderApp)); + myFBReaderApp.addAction(ActionCode.SELECTION_TRANSLATE, new SelectionTranslateAction(this, myFBReaderApp)); + myFBReaderApp.addAction(ActionCode.SELECTION_BOOKMARK, new SelectionBookmarkAction(this, myFBReaderApp)); - fbReader.addAction(ActionCode.PROCESS_HYPERLINK, new ProcessHyperlinkAction(this, fbReader)); + myFBReaderApp.addAction(ActionCode.PROCESS_HYPERLINK, new ProcessHyperlinkAction(this, myFBReaderApp)); - fbReader.addAction(ActionCode.SHOW_CANCEL_MENU, new ShowCancelMenuAction(this, fbReader)); + myFBReaderApp.addAction(ActionCode.SHOW_CANCEL_MENU, new ShowCancelMenuAction(this, myFBReaderApp)); - fbReader.addAction(ActionCode.SET_SCREEN_ORIENTATION_SYSTEM, new SetScreenOrientationAction(this, fbReader, ZLibrary.SCREEN_ORIENTATION_SYSTEM)); - fbReader.addAction(ActionCode.SET_SCREEN_ORIENTATION_SENSOR, new SetScreenOrientationAction(this, fbReader, ZLibrary.SCREEN_ORIENTATION_SENSOR)); - fbReader.addAction(ActionCode.SET_SCREEN_ORIENTATION_PORTRAIT, new SetScreenOrientationAction(this, fbReader, ZLibrary.SCREEN_ORIENTATION_PORTRAIT)); - fbReader.addAction(ActionCode.SET_SCREEN_ORIENTATION_LANDSCAPE, new SetScreenOrientationAction(this, fbReader, ZLibrary.SCREEN_ORIENTATION_LANDSCAPE)); + myFBReaderApp.addAction(ActionCode.SET_SCREEN_ORIENTATION_SYSTEM, new SetScreenOrientationAction(this, myFBReaderApp, ZLibrary.SCREEN_ORIENTATION_SYSTEM)); + myFBReaderApp.addAction(ActionCode.SET_SCREEN_ORIENTATION_SENSOR, new SetScreenOrientationAction(this, myFBReaderApp, ZLibrary.SCREEN_ORIENTATION_SENSOR)); + myFBReaderApp.addAction(ActionCode.SET_SCREEN_ORIENTATION_PORTRAIT, new SetScreenOrientationAction(this, myFBReaderApp, ZLibrary.SCREEN_ORIENTATION_PORTRAIT)); + myFBReaderApp.addAction(ActionCode.SET_SCREEN_ORIENTATION_LANDSCAPE, new SetScreenOrientationAction(this, myFBReaderApp, ZLibrary.SCREEN_ORIENTATION_LANDSCAPE)); if (ZLibrary.Instance().supportsAllOrientations()) { - fbReader.addAction(ActionCode.SET_SCREEN_ORIENTATION_REVERSE_PORTRAIT, new SetScreenOrientationAction(this, fbReader, ZLibrary.SCREEN_ORIENTATION_REVERSE_PORTRAIT)); - fbReader.addAction(ActionCode.SET_SCREEN_ORIENTATION_REVERSE_LANDSCAPE, new SetScreenOrientationAction(this, fbReader, ZLibrary.SCREEN_ORIENTATION_REVERSE_LANDSCAPE)); + myFBReaderApp.addAction(ActionCode.SET_SCREEN_ORIENTATION_REVERSE_PORTRAIT, new SetScreenOrientationAction(this, myFBReaderApp, ZLibrary.SCREEN_ORIENTATION_REVERSE_PORTRAIT)); + myFBReaderApp.addAction(ActionCode.SET_SCREEN_ORIENTATION_REVERSE_LANDSCAPE, new SetScreenOrientationAction(this, myFBReaderApp, ZLibrary.SCREEN_ORIENTATION_REVERSE_LANDSCAPE)); } } @@ -245,31 +252,25 @@ public final class FBReader extends Activity { protected void onNewIntent(final Intent intent) { final String action = intent.getAction(); final Uri data = intent.getData(); - final FBReaderApp fbReader = (FBReaderApp)FBReaderApp.Instance(); if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) { super.onNewIntent(intent); - } else if (Intent.ACTION_VIEW.equals(action) || "android.fbreader.action.VIEW".equals(action)) { - FBReaderApp.Instance().openFile(fileFromIntent(intent), null); - init(new Runnable() { - public void run() { - FBReaderApp.Instance().openFile(fileFromIntent(intent), null); - } - }); + } else if (Intent.ACTION_VIEW.equals(action) || ACTION_OPEN_BOOK.equals(action)) { + openBook(intent, null, true); } else if (Intent.ACTION_VIEW.equals(action) && data != null && "fbreader-action".equals(data.getScheme())) { - fbReader.runAction(data.getEncodedSchemeSpecificPart(), data.getFragment()); + myFBReaderApp.runAction(data.getEncodedSchemeSpecificPart(), data.getFragment()); } else if (Intent.ACTION_SEARCH.equals(action)) { final String pattern = intent.getStringExtra(SearchManager.QUERY); final Runnable runnable = new Runnable() { public void run() { - final TextSearchPopup popup = (TextSearchPopup)fbReader.getPopupById(TextSearchPopup.ID); + final TextSearchPopup popup = (TextSearchPopup)myFBReaderApp.getPopupById(TextSearchPopup.ID); popup.initPosition(); - fbReader.TextSearchPatternOption.setValue(pattern); - if (fbReader.getTextView().search(pattern, true, false, false, false) != 0) { + myFBReaderApp.TextSearchPatternOption.setValue(pattern); + if (myFBReaderApp.getTextView().search(pattern, true, false, false, false) != 0) { runOnUiThread(new Runnable() { public void run() { - fbReader.showPopup(popup.getId()); + myFBReaderApp.showPopup(popup.getId()); } }); } else { @@ -288,16 +289,23 @@ public final class FBReader extends Activity { } } - @Override - protected void onRestart() { - super.onRestart(); - getCollection().bindToService(null); - } - @Override protected void onStart() { super.onStart(); + ((BookCollectionShadow)myFBReaderApp.Collection).bindToService(this, new Runnable() { + public void run() { + new Thread() { + public void run() { + openBook(getIntent(), getPostponedInitAction(), false); + myFBReaderApp.getViewWidget().repaint(); + } + }.start(); + + myFBReaderApp.getViewWidget().repaint(); + } + }); + initPluginActions(); final ZLAndroidLibrary zlibrary = (ZLAndroidLibrary)ZLibrary.Instance(); @@ -311,19 +319,17 @@ public final class FBReader extends Activity { SetScreenOrientationAction.setOrientation(this, zlibrary.OrientationOption.getValue()); - final FBReaderApp fbReader = (FBReaderApp)FBReaderApp.Instance(); final RelativeLayout root = (RelativeLayout)findViewById(R.id.root_view); - ((PopupPanel)fbReader.getPopupById(TextSearchPopup.ID)).setPanelInfo(this, root); - ((PopupPanel)fbReader.getPopupById(NavigationPopup.ID)).setPanelInfo(this, root); - ((PopupPanel)fbReader.getPopupById(SelectionPopup.ID)).setPanelInfo(this, root); + ((PopupPanel)myFBReaderApp.getPopupById(TextSearchPopup.ID)).setPanelInfo(this, root); + ((PopupPanel)myFBReaderApp.getPopupById(NavigationPopup.ID)).setPanelInfo(this, root); + ((PopupPanel)myFBReaderApp.getPopupById(SelectionPopup.ID)).setPanelInfo(this, root); } private void initPluginActions() { - final FBReaderApp fbReader = (FBReaderApp)FBReaderApp.Instance(); synchronized (myPluginActions) { int index = 0; while (index < myPluginActions.size()) { - fbReader.removeAction(PLUGIN_ACTION_PREFIX + index++); + myFBReaderApp.removeAction(PLUGIN_ACTION_PREFIX + index++); } myPluginActions.clear(); } @@ -371,8 +377,7 @@ public final class FBReader extends Activity { super.onResume(); switchWakeLock( - getZLibrary().BatteryLevelToTurnScreenOffOption.getValue() < - FBReaderApp.Instance().getBatteryLevel() + getZLibrary().BatteryLevelToTurnScreenOffOption.getValue() < myFBReaderApp.getBatteryLevel() ); myStartTimer = true; final int brightnessLevel = @@ -388,98 +393,90 @@ public final class FBReader extends Activity { registerReceiver(myBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); - PopupPanel.restoreVisibilities(FBReaderApp.Instance()); + PopupPanel.restoreVisibilities(myFBReaderApp); ApiServerImplementation.sendEvent(this, ApiListener.EVENT_READ_MODE_OPENED); } @Override protected void onPause() { unregisterReceiver(myBatteryInfoReceiver); - FBReaderApp.Instance().stopTimer(); + myFBReaderApp.stopTimer(); switchWakeLock(false); if (getZLibrary().DisableButtonLightsOption.getValue()) { setButtonLight(true); } - FBReaderApp.Instance().onWindowClosing(); + myFBReaderApp.onWindowClosing(); super.onPause(); } @Override protected void onStop() { ApiServerImplementation.sendEvent(this, ApiListener.EVENT_READ_MODE_CLOSED); - PopupPanel.removeAllWindows(FBReaderApp.Instance(), this); - getCollection().unbind(); + PopupPanel.removeAllWindows(myFBReaderApp, this); + ((BookCollectionShadow)myFBReaderApp.Collection).unbind(); super.onStop(); } @Override public void onLowMemory() { - FBReaderApp.Instance().onWindowClosing(); + myFBReaderApp.onWindowClosing(); super.onLowMemory(); } - private FBReaderApp createApplication() { - return new FBReaderApp(new BookCollectionShadow(this)); - } - @Override public boolean onSearchRequested() { - final FBReaderApp fbreader = (FBReaderApp)FBReaderApp.Instance(); - final FBReaderApp.PopupPanel popup = fbreader.getActivePopup(); - fbreader.hideActivePopup(); + final FBReaderApp.PopupPanel popup = myFBReaderApp.getActivePopup(); + myFBReaderApp.hideActivePopup(); final SearchManager manager = (SearchManager)getSystemService(SEARCH_SERVICE); manager.setOnCancelListener(new SearchManager.OnCancelListener() { public void onCancel() { if (popup != null) { - fbreader.showPopup(popup.getId()); + myFBReaderApp.showPopup(popup.getId()); } manager.setOnCancelListener(null); } }); - startSearch(fbreader.TextSearchPatternOption.getValue(), true, null, false); + startSearch(myFBReaderApp.TextSearchPatternOption.getValue(), true, null, false); return true; } public void showSelectionPanel() { - final FBReaderApp fbReader = (FBReaderApp)FBReaderApp.Instance(); - final ZLTextView view = fbReader.getTextView(); - ((SelectionPopup)fbReader.getPopupById(SelectionPopup.ID)) + final ZLTextView view = myFBReaderApp.getTextView(); + ((SelectionPopup)myFBReaderApp.getPopupById(SelectionPopup.ID)) .move(view.getSelectionStartY(), view.getSelectionEndY()); - fbReader.showPopup(SelectionPopup.ID); + myFBReaderApp.showPopup(SelectionPopup.ID); } public void hideSelectionPanel() { - final FBReaderApp fbReader = (FBReaderApp)FBReaderApp.Instance(); - final FBReaderApp.PopupPanel popup = fbReader.getActivePopup(); + final FBReaderApp.PopupPanel popup = myFBReaderApp.getActivePopup(); if (popup != null && popup.getId() == SelectionPopup.ID) { - fbReader.hideActivePopup(); + myFBReaderApp.hideActivePopup(); } } private void onPreferencesUpdate(int resultCode, Book book) { if (book != null) { - getCollection().saveBook(book, true); + myFBReaderApp.Collection.saveBook(book, true); } - final FBReaderApp fbReader = (FBReaderApp)FBReaderApp.Instance(); switch (resultCode) { case RESULT_DO_NOTHING: break; case RESULT_REPAINT: { AndroidFontUtil.clearFontCache(); - final BookModel model = fbReader.Model; + final BookModel model = myFBReaderApp.Model; if (model != null && book != null) { if (model.Book != null) { model.Book = book; ZLTextHyphenator.Instance().load(model.Book.getLanguage()); } } - fbReader.clearTextCaches(); - fbReader.getViewWidget().repaint(); + myFBReaderApp.clearTextCaches(); + myFBReaderApp.getViewWidget().repaint(); break; } case RESULT_RELOAD_BOOK: - fbReader.reloadBook(book); + myFBReaderApp.reloadBook(book); break; } } @@ -492,13 +489,13 @@ public final class FBReader extends Activity { onPreferencesUpdate(resultCode, BookInfoActivity.bookByIntent(data)); break; case REQUEST_CANCEL_MENU: - ((FBReaderApp)FBReaderApp.Instance()).runCancelAction(resultCode - 1); + myFBReaderApp.runCancelAction(resultCode - 1); break; } } public void navigate() { - ((NavigationPopup)FBReaderApp.Instance().getPopupById(NavigationPopup.ID)).runNavigation(); + ((NavigationPopup)myFBReaderApp.getPopupById(NavigationPopup.ID)).runNavigation(); } private Menu addSubMenu(Menu menu, String id) { @@ -566,18 +563,17 @@ public final class FBReader extends Activity { } void addSelectionBookmark() { - final FBReaderApp fbReader = (FBReaderApp)FBReaderApp.Instance(); - final FBView fbView = fbReader.getTextView(); + final FBView fbView = myFBReaderApp.getTextView(); final String text = fbView.getSelectedText(); final Bookmark bookmark = new Bookmark( - fbReader.Model.Book, + myFBReaderApp.Model.Book, fbView.getModel().getId(), fbView.getSelectionStartPosition(), text, true ); - fbReader.Collection.saveBookmark(bookmark); + myFBReaderApp.Collection.saveBookmark(bookmark); fbView.clearSelection(); UIUtil.showMessageText( @@ -629,7 +625,7 @@ public final class FBReader extends Activity { } } if (myStartTimer) { - FBReaderApp.Instance().startTimer(); + myFBReaderApp.startTimer(); myStartTimer = false; } } diff --git a/src/org/geometerplus/android/fbreader/library/BookInfoActivity.java b/src/org/geometerplus/android/fbreader/library/BookInfoActivity.java index 334268ad3..55cbb393f 100644 --- a/src/org/geometerplus/android/fbreader/library/BookInfoActivity.java +++ b/src/org/geometerplus/android/fbreader/library/BookInfoActivity.java @@ -102,8 +102,8 @@ public class BookInfoActivity extends Activity { } else { startActivity( new Intent(getApplicationContext(), FBReader.class) - .setAction(Intent.ACTION_VIEW) - .putExtra(FBReader.BOOK_PATH_KEY, myBook.File.getPath()) + .setAction(FBReader.ACTION_OPEN_BOOK) + .putExtra(FBReader.BOOK_KEY, SerializerUtil.serialize(myBook)) .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) ); } diff --git a/src/org/geometerplus/android/fbreader/library/LibraryActivity.java b/src/org/geometerplus/android/fbreader/library/LibraryActivity.java index 02e7236bf..edc8fc421 100644 --- a/src/org/geometerplus/android/fbreader/library/LibraryActivity.java +++ b/src/org/geometerplus/android/fbreader/library/LibraryActivity.java @@ -57,7 +57,7 @@ public class LibraryActivity extends TreeActivity implements MenuItem.OnMenuItem super.onCreate(icicle); if (myLibrary == null) { - myLibrary = new Library(new BookCollectionShadow(this)); + myLibrary = new Library(new BookCollectionShadow()); myLibrary.addChangeListener(this); } @@ -75,7 +75,7 @@ public class LibraryActivity extends TreeActivity implements MenuItem.OnMenuItem @Override protected void onStart() { super.onStart(); - ((BookCollectionShadow)myLibrary.Collection).bindToService(new Runnable() { + ((BookCollectionShadow)myLibrary.Collection).bindToService(this, new Runnable() { public void run() { myLibrary.init(); } @@ -255,8 +255,8 @@ public class LibraryActivity extends TreeActivity implements MenuItem.OnMenuItem private void openBook(Book book) { startActivity( new Intent(getApplicationContext(), FBReader.class) - .setAction(Intent.ACTION_VIEW) - .putExtra(FBReader.BOOK_PATH_KEY, book.File.getPath()) + .setAction(FBReader.ACTION_OPEN_BOOK) + .putExtra(FBReader.BOOK_KEY, SerializerUtil.serialize(book)) .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) ); } diff --git a/src/org/geometerplus/android/fbreader/libraryService/BookCollectionShadow.java b/src/org/geometerplus/android/fbreader/libraryService/BookCollectionShadow.java index 33a793abb..2d68dff1f 100644 --- a/src/org/geometerplus/android/fbreader/libraryService/BookCollectionShadow.java +++ b/src/org/geometerplus/android/fbreader/libraryService/BookCollectionShadow.java @@ -36,7 +36,7 @@ import org.geometerplus.fbreader.book.*; import org.geometerplus.android.fbreader.api.TextPosition; public class BookCollectionShadow extends AbstractBookCollection implements ServiceConnection { - private final Context myContext; + private Context myContext; private volatile LibraryInterface myInterface; private Runnable myOnBindAction; @@ -60,12 +60,8 @@ public class BookCollectionShadow extends AbstractBookCollection implements Serv } }; - public BookCollectionShadow(Context context) { - myContext = context; - } - - public synchronized void bindToService(Runnable onBindAction) { - if (myInterface != null) { + public synchronized void bindToService(Context context, Runnable onBindAction) { + if (myInterface != null && myContext == context) { if (onBindAction != null) { onBindAction.run(); } @@ -73,19 +69,21 @@ public class BookCollectionShadow extends AbstractBookCollection implements Serv if (onBindAction != null) { myOnBindAction = onBindAction; } - myContext.bindService( - new Intent(myContext, LibraryService.class), + context.bindService( + new Intent(context, LibraryService.class), this, LibraryService.BIND_AUTO_CREATE ); + myContext = context; } } public void unbind() { - if (myInterface != null) { + if (myContext != null && myInterface != null) { + myContext.unregisterReceiver(myReceiver); myContext.unbindService(this); myInterface = null; - myContext.unregisterReceiver(myReceiver); + myContext = null; } } @@ -318,8 +316,10 @@ public class BookCollectionShadow extends AbstractBookCollection implements Serv myOnBindAction.run(); myOnBindAction = null; } - myContext.registerReceiver(myReceiver, new IntentFilter(LibraryService.BOOK_EVENT_ACTION)); - myContext.registerReceiver(myReceiver, new IntentFilter(LibraryService.BUILD_EVENT_ACTION)); + if (myContext != null) { + myContext.registerReceiver(myReceiver, new IntentFilter(LibraryService.BOOK_EVENT_ACTION)); + myContext.registerReceiver(myReceiver, new IntentFilter(LibraryService.BUILD_EVENT_ACTION)); + } } // method from ServiceConnection interface diff --git a/src/org/geometerplus/fbreader/fbreader/FBReaderApp.java b/src/org/geometerplus/fbreader/fbreader/FBReaderApp.java index a89b34aa2..fa9e7096d 100644 --- a/src/org/geometerplus/fbreader/fbreader/FBReaderApp.java +++ b/src/org/geometerplus/fbreader/fbreader/FBReaderApp.java @@ -337,30 +337,6 @@ public final class FBReaderApp extends ZLApplication { setView(BookTextView); } - private Book createBookForFile(ZLFile file) { - if (file == null) { - return null; - } - Book book = Collection.getBookByFile(file); - if (book != null) { - return book; - } - if (file.isArchive()) { - for (ZLFile child : file.children()) { - book = Collection.getBookByFile(child); - if (book != null) { - return book; - } - } - } - return null; - } - - @Override - public void openFile(ZLFile file, Runnable postAction) { - openBook(createBookForFile(file), null, postAction); - } - public void onWindowClosing() { storePosition(); } diff --git a/src/org/geometerplus/zlibrary/core/application/ZLApplication.java b/src/org/geometerplus/zlibrary/core/application/ZLApplication.java index 5268fc7e6..0c98af370 100644 --- a/src/org/geometerplus/zlibrary/core/application/ZLApplication.java +++ b/src/org/geometerplus/zlibrary/core/application/ZLApplication.java @@ -176,8 +176,6 @@ public abstract class ZLApplication { public void onWindowClosing() { } - public abstract void openFile(ZLFile file, Runnable postAction); - //Action static abstract public class ZLAction { public boolean isVisible() {