/* * Copyright (C) 2009-2013 Geometer Plus * * 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 java.util.*; import android.app.Activity; import android.app.SearchManager; import android.content.*; import android.graphics.Color; import android.net.Uri; import android.os.Bundle; import android.os.PowerManager; import android.view.*; import android.widget.RelativeLayout; import org.geometerplus.zlibrary.core.filesystem.ZLFile; import org.geometerplus.zlibrary.core.library.ZLibrary; import org.geometerplus.zlibrary.text.view.ZLTextView; import org.geometerplus.zlibrary.ui.android.R; import org.geometerplus.zlibrary.ui.android.application.ZLAndroidApplicationWindow; import org.geometerplus.zlibrary.ui.android.library.*; import org.geometerplus.zlibrary.ui.android.view.AndroidFontUtil; import org.geometerplus.zlibrary.ui.android.view.ZLAndroidWidget; import org.geometerplus.fbreader.book.*; import org.geometerplus.fbreader.bookmodel.BookModel; import org.geometerplus.fbreader.fbreader.*; import org.geometerplus.fbreader.tips.TipsManager; import org.geometerplus.android.fbreader.api.*; import org.geometerplus.android.fbreader.library.BookInfoActivity; import org.geometerplus.android.fbreader.libraryService.BookCollectionShadow; import org.geometerplus.android.fbreader.tips.TipsActivity; import org.geometerplus.android.util.UIUtil; public final class FBReader extends Activity { public static final String ACTION_OPEN_BOOK = "android.fbreader.action.VIEW"; public static final String BOOK_KEY = "fbreader.book"; public static final String BOOKMARK_KEY = "fbreader.bookmark"; static final int ACTION_BAR_COLOR = Color.DKGRAY; public static final int REQUEST_PREFERENCES = 1; public static final int REQUEST_CANCEL_MENU = 2; public static final int RESULT_DO_NOTHING = RESULT_FIRST_USER; public static final int RESULT_REPAINT = RESULT_FIRST_USER + 1; public static void openBookActivity(Context context, Book book, Bookmark bookmark) { context.startActivity( new Intent(context, FBReader.class) .setAction(ACTION_OPEN_BOOK) .putExtra(BOOK_KEY, SerializerUtil.serialize(book)) .putExtra(BOOKMARK_KEY, SerializerUtil.serialize(bookmark)) .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) ); } private static ZLAndroidLibrary getZLibrary() { return (ZLAndroidLibrary)ZLAndroidLibrary.Instance(); } private FBReaderApp myFBReaderApp; private volatile Book myBook; private RelativeLayout myRootView; private ZLAndroidWidget myMainView; private int myFullScreenFlag; private String myMenuLanguage; private static final String PLUGIN_ACTION_PREFIX = "___"; private final List myPluginActions = new LinkedList(); private final BroadcastReceiver myPluginInfoReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { final ArrayList actions = getResultExtras(true).getParcelableArrayList(PluginApi.PluginInfo.KEY); if (actions != null) { synchronized (myPluginActions) { int index = 0; while (index < myPluginActions.size()) { myFBReaderApp.removeAction(PLUGIN_ACTION_PREFIX + index++); } myPluginActions.addAll(actions); index = 0; for (PluginApi.ActionInfo info : myPluginActions) { myFBReaderApp.addAction( PLUGIN_ACTION_PREFIX + index++, new RunPluginAction(FBReader.this, myFBReaderApp, info.getId()) ); } } } } }; private synchronized void openBook(Intent intent, Runnable action, boolean force) { if (!force && myBook != null) { return; } myBook = SerializerUtil.deserializeBook(intent.getStringExtra(BOOK_KEY)); final Bookmark bookmark = SerializerUtil.deserializeBookmark(intent.getStringExtra(BOOKMARK_KEY)); if (myBook == null) { final Uri data = intent.getData(); if (data != null) { myBook = createBookForFile(ZLFile.createFileByPath(data.getPath())); } } myFBReaderApp.openBook(myBook, bookmark, 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() { return new Runnable() { public void run() { runOnUiThread(new Runnable() { public void run() { new TipRunner().start(); DictionaryUtil.init(FBReader.this); } }); } }; } @Override protected void onCreate(Bundle icicle) { super.onCreate(icicle); Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler(this)); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.main); myRootView = (RelativeLayout)findViewById(R.id.root_view); myMainView = (ZLAndroidWidget)findViewById(R.id.main_view); setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL); getZLibrary().setActivity(this); myFBReaderApp = (FBReaderApp)FBReaderApp.Instance(); if (myFBReaderApp == null) { myFBReaderApp = new FBReaderApp(new BookCollectionShadow()); } getCollection().bindToService(this, null); myBook = null; final ZLAndroidApplication androidApplication = (ZLAndroidApplication)getApplication(); if (androidApplication.myMainWindow == null) { androidApplication.myMainWindow = new ZLAndroidApplicationWindow(myFBReaderApp); myFBReaderApp.initWindow(); } final ZLAndroidLibrary zlibrary = (ZLAndroidLibrary)ZLibrary.Instance(); myFullScreenFlag = zlibrary.ShowStatusBarOption.getValue() ? 0 : WindowManager.LayoutParams.FLAG_FULLSCREEN; getWindow().setFlags( WindowManager.LayoutParams.FLAG_FULLSCREEN, myFullScreenFlag ); if (myFBReaderApp.getPopupById(TextSearchPopup.ID) == null) { new TextSearchPopup(myFBReaderApp); } if (myFBReaderApp.getPopupById(NavigationPopup.ID) == null) { new NavigationPopup(myFBReaderApp); } if (myFBReaderApp.getPopupById(SelectionPopup.ID) == null) { new SelectionPopup(myFBReaderApp); } 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)); 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)); 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)); myFBReaderApp.addAction(ActionCode.PROCESS_HYPERLINK, new ProcessHyperlinkAction(this, myFBReaderApp)); myFBReaderApp.addAction(ActionCode.SHOW_CANCEL_MENU, new ShowCancelMenuAction(this, myFBReaderApp)); 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()) { 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)); } } @Override public boolean onPrepareOptionsMenu(Menu menu) { final ZLAndroidLibrary zlibrary = (ZLAndroidLibrary)ZLibrary.Instance(); if (!zlibrary.isKindleFire() && !zlibrary.ShowStatusBarOption.getValue()) { getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN); } setupMenu(menu); return super.onPrepareOptionsMenu(menu); } @Override public void onOptionsMenuClosed(Menu menu) { super.onOptionsMenuClosed(menu); final ZLAndroidLibrary zlibrary = (ZLAndroidLibrary)ZLibrary.Instance(); if (!zlibrary.isKindleFire() && !zlibrary.ShowStatusBarOption.getValue()) { getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN); } } @Override public boolean onOptionsItemSelected(MenuItem item) { final ZLAndroidLibrary zlibrary = (ZLAndroidLibrary)ZLibrary.Instance(); if (!zlibrary.isKindleFire() && !zlibrary.ShowStatusBarOption.getValue()) { getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN); } return super.onOptionsItemSelected(item); } public ZLAndroidWidget getMainView() { return myMainView; } @Override protected void onNewIntent(final Intent intent) { final String action = intent.getAction(); final Uri data = intent.getData(); if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) { super.onNewIntent(intent); } else if (Intent.ACTION_VIEW.equals(action) && data != null && "fbreader-action".equals(data.getScheme())) { myFBReaderApp.runAction(data.getEncodedSchemeSpecificPart(), data.getFragment()); } else if (Intent.ACTION_VIEW.equals(action) || ACTION_OPEN_BOOK.equals(action)) { getCollection().bindToService(this, new Runnable() { public void run() { openBook(intent, null, true); } }); } 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)myFBReaderApp.getPopupById(TextSearchPopup.ID); popup.initPosition(); myFBReaderApp.TextSearchPatternOption.setValue(pattern); if (myFBReaderApp.getTextView().search(pattern, true, false, false, false) != 0) { runOnUiThread(new Runnable() { public void run() { myFBReaderApp.showPopup(popup.getId()); } }); } else { runOnUiThread(new Runnable() { public void run() { UIUtil.showErrorMessage(FBReader.this, "textNotFound"); popup.StartPosition = null; } }); } } }; UIUtil.wait("search", runnable, this); } else { super.onNewIntent(intent); } } @Override protected void onStart() { super.onStart(); getCollection().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(); final int fullScreenFlag = zlibrary.ShowStatusBarOption.getValue() ? 0 : WindowManager.LayoutParams.FLAG_FULLSCREEN; if (fullScreenFlag != myFullScreenFlag) { finish(); startActivity(new Intent(this, getClass())); } SetScreenOrientationAction.setOrientation(this, zlibrary.getOrientationOption().getValue()); ((PopupPanel)myFBReaderApp.getPopupById(TextSearchPopup.ID)).setPanelInfo(this, myRootView); ((PopupPanel)myFBReaderApp.getPopupById(NavigationPopup.ID)).setPanelInfo(this, myRootView); ((PopupPanel)myFBReaderApp.getPopupById(SelectionPopup.ID)).setPanelInfo(this, myRootView); } @Override public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); switchWakeLock(hasFocus && getZLibrary().BatteryLevelToTurnScreenOffOption.getValue() < myFBReaderApp.getBatteryLevel() ); } private void initPluginActions() { synchronized (myPluginActions) { int index = 0; while (index < myPluginActions.size()) { myFBReaderApp.removeAction(PLUGIN_ACTION_PREFIX + index++); } myPluginActions.clear(); } sendOrderedBroadcast( new Intent(PluginApi.ACTION_REGISTER), null, myPluginInfoReceiver, null, RESULT_OK, null, null ); } private class TipRunner extends Thread { TipRunner() { setPriority(MIN_PRIORITY); } public void run() { final TipsManager manager = TipsManager.Instance(); switch (manager.requiredAction()) { case Initialize: startActivity(new Intent( TipsActivity.INITIALIZE_ACTION, null, FBReader.this, TipsActivity.class )); break; case Show: startActivity(new Intent( TipsActivity.SHOW_TIP_ACTION, null, FBReader.this, TipsActivity.class )); break; case Download: manager.startDownloading(); break; case None: break; } } } @Override protected void onResume() { super.onResume(); myStartTimer = true; final int brightnessLevel = getZLibrary().ScreenBrightnessLevelOption.getValue(); if (brightnessLevel != 0) { setScreenBrightness(brightnessLevel); } else { setScreenBrightnessAuto(); } if (getZLibrary().DisableButtonLightsOption.getValue()) { setButtonLight(false); } registerReceiver(myBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); PopupPanel.restoreVisibilities(myFBReaderApp); ApiServerImplementation.sendEvent(this, ApiListener.EVENT_READ_MODE_OPENED); getCollection().bindToService(this, new Runnable() { public void run() { final BookModel model = myFBReaderApp.Model; if (model == null || model.Book == null) { return; } onPreferencesUpdate(myFBReaderApp.Collection.getBookById(model.Book.getId())); } }); } @Override protected void onPause() { try { unregisterReceiver(myBatteryInfoReceiver); } catch (IllegalArgumentException e) { // do nothing, this exception means myBatteryInfoReceiver was not registered } myFBReaderApp.stopTimer(); if (getZLibrary().DisableButtonLightsOption.getValue()) { setButtonLight(true); } myFBReaderApp.onWindowClosing(); super.onPause(); } @Override protected void onStop() { ApiServerImplementation.sendEvent(this, ApiListener.EVENT_READ_MODE_CLOSED); PopupPanel.removeAllWindows(myFBReaderApp, this); super.onStop(); } @Override protected void onDestroy() { getCollection().unbind(); super.onDestroy(); } @Override public void onLowMemory() { myFBReaderApp.onWindowClosing(); super.onLowMemory(); } @Override public boolean onSearchRequested() { 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) { myFBReaderApp.showPopup(popup.getId()); } manager.setOnCancelListener(null); } }); startSearch(myFBReaderApp.TextSearchPatternOption.getValue(), true, null, false); return true; } public void showSelectionPanel() { final ZLTextView view = myFBReaderApp.getTextView(); ((SelectionPopup)myFBReaderApp.getPopupById(SelectionPopup.ID)) .move(view.getSelectionStartY(), view.getSelectionEndY()); myFBReaderApp.showPopup(SelectionPopup.ID); } public void hideSelectionPanel() { final FBReaderApp.PopupPanel popup = myFBReaderApp.getActivePopup(); if (popup != null && popup.getId() == SelectionPopup.ID) { myFBReaderApp.hideActivePopup(); } } private void onPreferencesUpdate(Book book) { AndroidFontUtil.clearFontCache(); myFBReaderApp.onBookUpdated(book); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { case REQUEST_PREFERENCES: if (resultCode != RESULT_DO_NOTHING) { final Book book = BookInfoActivity.bookByIntent(data); if (book != null) { getCollection().bindToService(this, new Runnable() { public void run() { onPreferencesUpdate(book); } }); } } break; case REQUEST_CANCEL_MENU: myFBReaderApp.runCancelAction(resultCode - 1); break; } } public void navigate() { ((NavigationPopup)myFBReaderApp.getPopupById(NavigationPopup.ID)).runNavigation(); } private Menu addSubMenu(Menu menu, String id) { final ZLAndroidApplication application = (ZLAndroidApplication)getApplication(); return application.myMainWindow.addSubMenu(menu, id); } private void addMenuItem(Menu menu, String actionId, String name) { final ZLAndroidApplication application = (ZLAndroidApplication)getApplication(); application.myMainWindow.addMenuItem(menu, actionId, null, name); } private void addMenuItem(Menu menu, String actionId, int iconId) { final ZLAndroidApplication application = (ZLAndroidApplication)getApplication(); application.myMainWindow.addMenuItem(menu, actionId, iconId, null); } private void addMenuItem(Menu menu, String actionId) { final ZLAndroidApplication application = (ZLAndroidApplication)getApplication(); application.myMainWindow.addMenuItem(menu, actionId, null, null); } private void setupMenu(Menu menu) { final String menuLanguage = ZLResource.getLanguageOption().getValue(); if (menuLanguage.equals(myMenuLanguage)) { return; } myMenuLanguage = menuLanguage; menu.clear(); 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.SHARE_BOOK, R.drawable.ic_menu_search); addMenuItem(menu, ActionCode.SHOW_PREFERENCES); addMenuItem(menu, ActionCode.SHOW_BOOK_INFO); final Menu subMenu = addSubMenu(menu, "screenOrientation"); addMenuItem(subMenu, ActionCode.SET_SCREEN_ORIENTATION_SYSTEM); addMenuItem(subMenu, ActionCode.SET_SCREEN_ORIENTATION_SENSOR); addMenuItem(subMenu, ActionCode.SET_SCREEN_ORIENTATION_PORTRAIT); addMenuItem(subMenu, ActionCode.SET_SCREEN_ORIENTATION_LANDSCAPE); if (ZLibrary.Instance().supportsAllOrientations()) { addMenuItem(subMenu, ActionCode.SET_SCREEN_ORIENTATION_REVERSE_PORTRAIT); addMenuItem(subMenu, ActionCode.SET_SCREEN_ORIENTATION_REVERSE_LANDSCAPE); } addMenuItem(menu, ActionCode.INCREASE_FONT); addMenuItem(menu, ActionCode.DECREASE_FONT); addMenuItem(menu, ActionCode.SHOW_NAVIGATION); synchronized (myPluginActions) { int index = 0; for (PluginApi.ActionInfo info : myPluginActions) { if (info instanceof PluginApi.MenuActionInfo) { addMenuItem( menu, PLUGIN_ACTION_PREFIX + index++, ((PluginApi.MenuActionInfo)info).MenuItemName ); } } } final ZLAndroidApplication application = (ZLAndroidApplication)getApplication(); application.myMainWindow.refresh(); } @Override public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); setupMenu(menu); return true; } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { return (myMainView != null && myMainView.onKeyDown(keyCode, event)) || super.onKeyDown(keyCode, event); } @Override public boolean onKeyUp(int keyCode, KeyEvent event) { return (myMainView != null && myMainView.onKeyUp(keyCode, event)) || super.onKeyUp(keyCode, event); } private void setButtonLight(boolean enabled) { try { final WindowManager.LayoutParams attrs = getWindow().getAttributes(); final Class cls = attrs.getClass(); final Field fld = cls.getField("buttonBrightness"); if (fld != null && "float".equals(fld.getType().toString())) { fld.setFloat(attrs, enabled ? -1.0f : 0.0f); getWindow().setAttributes(attrs); } } catch (NoSuchFieldException e) { } catch (IllegalAccessException e) { } } private PowerManager.WakeLock myWakeLock; private boolean myWakeLockToCreate; private boolean myStartTimer; public final void createWakeLock() { if (myWakeLockToCreate) { synchronized (this) { if (myWakeLockToCreate) { myWakeLockToCreate = false; myWakeLock = ((PowerManager)getSystemService(POWER_SERVICE)) .newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "FBReader"); myWakeLock.acquire(); } } } if (myStartTimer) { myFBReaderApp.startTimer(); myStartTimer = false; } } private final void switchWakeLock(boolean on) { if (on) { if (myWakeLock == null) { myWakeLockToCreate = true; } } else { if (myWakeLock != null) { synchronized (this) { if (myWakeLock != null) { myWakeLock.release(); myWakeLock = null; } } } } } private BroadcastReceiver myBatteryInfoReceiver = new BroadcastReceiver() { public void onReceive(Context context, Intent intent) { final int level = intent.getIntExtra("level", 100); final ZLAndroidApplication application = (ZLAndroidApplication)getApplication(); application.myMainWindow.setBatteryLevel(level); switchWakeLock( hasWindowFocus() && getZLibrary().BatteryLevelToTurnScreenOffOption.getValue() < level ); } }; private void setScreenBrightnessAuto() { final WindowManager.LayoutParams attrs = getWindow().getAttributes(); attrs.screenBrightness = -1.0f; getWindow().setAttributes(attrs); } public void setScreenBrightness(int percent) { if (percent < 1) { percent = 1; } else if (percent > 100) { percent = 100; } final WindowManager.LayoutParams attrs = getWindow().getAttributes(); attrs.screenBrightness = percent / 100.0f; getWindow().setAttributes(attrs); getZLibrary().ScreenBrightnessLevelOption.setValue(percent); } public int getScreenBrightness() { final int level = (int)(100 * getWindow().getAttributes().screenBrightness); return (level >= 0) ? level : 50; } private BookCollectionShadow getCollection() { return (BookCollectionShadow)myFBReaderApp.Collection; } }