1
0
Fork 0
mirror of https://github.com/geometer/FBReaderJ.git synced 2025-10-06 03:50:19 +02:00
FBReaderJ/src/org/geometerplus/android/fbreader/FBReader.java
2011-06-09 00:03:32 +01:00

298 lines
10 KiB
Java

/*
* Copyright (C) 2009-2011 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;
import android.app.SearchManager;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
import org.geometerplus.zlibrary.text.hyphenation.ZLTextHyphenator;
import org.geometerplus.zlibrary.ui.android.R;
import org.geometerplus.zlibrary.ui.android.library.ZLAndroidActivity;
import org.geometerplus.zlibrary.ui.android.library.ZLAndroidApplication;
import org.geometerplus.fbreader.fbreader.ActionCode;
import org.geometerplus.fbreader.fbreader.FBReaderApp;
import org.geometerplus.fbreader.bookmodel.BookModel;
import org.geometerplus.fbreader.library.Book;
import org.geometerplus.android.fbreader.library.KillerCallback;
import org.geometerplus.android.util.UIUtil;
public final class FBReader extends ZLAndroidActivity {
public static final String BOOK_PATH_KEY = "BookPath";
final static int REPAINT_CODE = 1;
final static int CANCEL_CODE = 2;
private int myFullScreenFlag;
private static TextSearchButtonPanel ourTextSearchPanel;
private static NavigationButtonPanel ourNavigatePanel;
@Override
protected ZLFile fileFromIntent(Intent intent) {
String filePath = intent.getStringExtra(BOOK_PATH_KEY);
if (filePath == null) {
final Uri data = intent.getData();
if (data != null) {
filePath = data.getPath();
}
}
return filePath != null ? ZLFile.createFileByPath(filePath) : null;
}
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
final ZLAndroidApplication application = (ZLAndroidApplication)getApplication();
myFullScreenFlag =
application.ShowStatusBarOption.getValue() ? 0 : WindowManager.LayoutParams.FLAG_FULLSCREEN;
getWindow().setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN, myFullScreenFlag
);
final FBReaderApp fbReader = (FBReaderApp)FBReaderApp.Instance();
if (ourTextSearchPanel == null) {
ourTextSearchPanel = new TextSearchButtonPanel(fbReader);
}
if (ourNavigatePanel == null) {
ourNavigatePanel = new NavigationButtonPanel(fbReader);
}
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));
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.SELECTION_COPY_TO_CLIPBOARD, new SelectionCopyAction(this, fbReader));
fbReader.addAction(ActionCode.SELECTION_SHARE, new SelectionShareAction(this, fbReader));
fbReader.addAction(ActionCode.SELECTION_OPEN_IN_DICTIONARY, new SelectionDictionaryAction(this, fbReader));
fbReader.addAction(ActionCode.SELECTION_ADD_BOOKMARK, new SelectionBookmarkAction(this, fbReader));
fbReader.addAction(ActionCode.PROCESS_HYPERLINK, new ProcessHyperlinkAction(this, fbReader));
fbReader.addAction(ActionCode.SHOW_CANCEL_MENU, new ShowCancelMenuAction(this, fbReader));
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
final ZLAndroidApplication application = (ZLAndroidApplication)getApplication();
if (!application.ShowStatusBarOption.getValue() &&
application.ShowStatusBarWhenMenuIsActiveOption.getValue()) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
}
return super.onPrepareOptionsMenu(menu);
}
@Override
public void onOptionsMenuClosed(Menu menu) {
super.onOptionsMenuClosed(menu);
final ZLAndroidApplication application = (ZLAndroidApplication)getApplication();
if (!application.ShowStatusBarOption.getValue() &&
application.ShowStatusBarWhenMenuIsActiveOption.getValue()) {
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
}
}
@Override
protected void onNewIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
final String pattern = intent.getStringExtra(SearchManager.QUERY);
final Runnable runnable = new Runnable() {
public void run() {
ourTextSearchPanel.initPosition();
final FBReaderApp fbReader = (FBReaderApp)FBReaderApp.Instance();
fbReader.TextSearchPatternOption.setValue(pattern);
if (fbReader.getTextView().search(pattern, true, false, false, false) != 0) {
runOnUiThread(new Runnable() {
public void run() {
ourTextSearchPanel.show(true);
}
});
} else {
runOnUiThread(new Runnable() {
public void run() {
UIUtil.showErrorMessage(FBReader.this, "textNotFound");
ourTextSearchPanel.StartPosition = null;
}
});
}
}
};
UIUtil.wait("search", runnable, this);
startActivity(new Intent(this, getClass()));
} else {
super.onNewIntent(intent);
}
}
@Override
public void onStart() {
super.onStart();
final ZLAndroidApplication application = (ZLAndroidApplication)getApplication();
final int fullScreenFlag =
application.ShowStatusBarOption.getValue() ? 0 : WindowManager.LayoutParams.FLAG_FULLSCREEN;
if (fullScreenFlag != myFullScreenFlag) {
finish();
startActivity(new Intent(this, this.getClass()));
}
final RelativeLayout root = (RelativeLayout)findViewById(R.id.root_view);
if (!ourTextSearchPanel.hasControlPanel()) {
ourTextSearchPanel.createControlPanel(this, root, ControlPanel.Location.Bottom);
}
if (!ourNavigatePanel.hasControlPanel()) {
ourNavigatePanel.createControlPanel(this, root, ControlPanel.Location.Bottom);
}
}
@Override
public void onResume() {
super.onResume();
try {
sendBroadcast(new Intent(getApplicationContext(), KillerCallback.class));
} catch (Throwable t) {
}
ControlButtonPanel.restoreVisibilities(FBReaderApp.Instance());
}
@Override
public void onPause() {
ControlButtonPanel.saveVisibilities(FBReaderApp.Instance());
super.onPause();
}
@Override
public void onStop() {
ControlButtonPanel.removeControlPanels(FBReaderApp.Instance());
super.onStop();
}
@Override
protected FBReaderApp createApplication(ZLFile file) {
if (SQLiteBooksDatabase.Instance() == null) {
new SQLiteBooksDatabase(this, "READER");
}
return new FBReaderApp(file != null ? file.getPath() : null);
}
@Override
public boolean onSearchRequested() {
final FBReaderApp fbreader = (FBReaderApp)FBReaderApp.Instance();
ControlButtonPanel.saveVisibilities(fbreader);
ControlButtonPanel.hideAllPendingNotify(fbreader);
final SearchManager manager = (SearchManager)getSystemService(SEARCH_SERVICE);
manager.setOnCancelListener(new SearchManager.OnCancelListener() {
public void onCancel() {
ControlButtonPanel.restoreVisibilities(fbreader);
manager.setOnCancelListener(null);
}
});
startSearch(fbreader.TextSearchPatternOption.getValue(), true, null, false);
return true;
}
public void showSelectionPanel() {
final ZLTextView view = ((FBReaderApp)FBReaderApp.Instance()).getTextView();
//ourSelectionPanel.move(view.getSelectionStartY(), view.getSelectionEndY());
//ourSelectionPanel.show(true);
}
public void hideSelectionPanel() {
//ourSelectionPanel.hide();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
final FBReaderApp fbreader = (FBReaderApp)FBReaderApp.Instance();
switch (requestCode) {
case REPAINT_CODE:
{
final BookModel model = fbreader.Model;
if (model != null) {
final Book book = model.Book;
if (book != null) {
book.reloadInfoFromDatabase();
ZLTextHyphenator.Instance().load(book.getLanguage());
}
}
fbreader.clearTextCaches();
fbreader.getViewWidget().repaint();
break;
}
case CANCEL_CODE:
fbreader.runCancelAction(resultCode - 1);
break;
}
}
public void navigate() {
ourNavigatePanel.runNavigation();
}
private void addMenuItem(Menu menu, String actionId, int iconId) {
final ZLAndroidApplication application = (ZLAndroidApplication)getApplication();
application.myMainWindow.addMenuItem(menu, actionId, iconId);
}
private void addMenuItem(Menu menu, String actionId) {
final ZLAndroidApplication application = (ZLAndroidApplication)getApplication();
application.myMainWindow.addMenuItem(menu, actionId, null);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
addMenuItem(menu, ActionCode.SHOW_LIBRARY, R.drawable.ic_menu_library);
addMenuItem(menu, ActionCode.SHOW_NETWORK_LIBRARY, R.drawable.ic_menu_networklibrary);
addMenuItem(menu, ActionCode.SHOW_TOC, R.drawable.ic_menu_toc);
addMenuItem(menu, ActionCode.SHOW_BOOKMARKS, R.drawable.ic_menu_bookmarks);
addMenuItem(menu, ActionCode.SWITCH_TO_NIGHT_PROFILE, R.drawable.ic_menu_night);
addMenuItem(menu, ActionCode.SWITCH_TO_DAY_PROFILE, R.drawable.ic_menu_day);
addMenuItem(menu, ActionCode.SEARCH, R.drawable.ic_menu_search);
addMenuItem(menu, ActionCode.SHOW_PREFERENCES);
addMenuItem(menu, ActionCode.SHOW_BOOK_INFO);
addMenuItem(menu, ActionCode.ROTATE);
addMenuItem(menu, ActionCode.INCREASE_FONT);
addMenuItem(menu, ActionCode.DECREASE_FONT);
addMenuItem(menu, ActionCode.SHOW_NAVIGATION);
final ZLAndroidApplication application = (ZLAndroidApplication)getApplication();
application.myMainWindow.refreshMenu();
return true;
}
}