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

more methods moved from ZLAndroidActivity to FBReader

This commit is contained in:
Nikolay Pultsin 2013-01-19 22:59:46 +04:00
parent 8d227b4400
commit 5f428e149a
4 changed files with 140 additions and 148 deletions

View file

@ -19,12 +19,14 @@
package org.geometerplus.android.fbreader;
import java.lang.reflect.*;
import java.util.*;
import android.app.SearchManager;
import android.content.*;
import android.net.Uri;
import android.os.Bundle;
import android.os.PowerManager;
import android.view.*;
import android.widget.RelativeLayout;
@ -226,14 +228,18 @@ public final class FBReader extends ZLAndroidActivity {
@Override
protected void onNewIntent(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(intent.getAction())
} else if (Intent.ACTION_VIEW.equals(action) || "android.fbreader.action.VIEW".equals(action)) {
FBReaderApp.Instance().openFile(fileFromIntent(intent), null);
} else if (Intent.ACTION_VIEW.equals(action)
&& data != null && "fbreader-action".equals(data.getScheme())) {
fbReader.runAction(data.getEncodedSchemeSpecificPart(), data.getFragment());
} else if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
} else if (Intent.ACTION_SEARCH.equals(action)) {
final String pattern = intent.getStringExtra(SearchManager.QUERY);
final Runnable runnable = new Runnable() {
public void run() {
@ -337,6 +343,24 @@ public final class FBReader extends ZLAndroidActivity {
@Override
public void onResume() {
super.onResume();
switchWakeLock(
getZLibrary().BatteryLevelToTurnScreenOffOption.getValue() <
FBReaderApp.Instance().getBatteryLevel()
);
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));
try {
sendBroadcast(new Intent(getApplicationContext(), KillerCallback.class));
} catch (Throwable t) {
@ -345,6 +369,18 @@ public final class FBReader extends ZLAndroidActivity {
ApiServerImplementation.sendEvent(this, ApiListener.EVENT_READ_MODE_OPENED);
}
@Override
public void onPause() {
unregisterReceiver(myBatteryInfoReceiver);
FBReaderApp.Instance().stopTimer();
switchWakeLock(false);
if (getZLibrary().DisableButtonLightsOption.getValue()) {
setButtonLight(true);
}
FBReaderApp.Instance().onWindowClosing();
super.onPause();
}
@Override
public void onStop() {
ApiServerImplementation.sendEvent(this, ApiListener.EVENT_READ_MODE_CLOSED);
@ -352,6 +388,12 @@ public final class FBReader extends ZLAndroidActivity {
super.onStop();
}
@Override
public void onLowMemory() {
FBReaderApp.Instance().onWindowClosing();
super.onLowMemory();
}
private FBReaderApp createApplication() {
if (SQLiteBooksDatabase.Instance() == null) {
new SQLiteBooksDatabase(this, "READER");
@ -511,4 +553,91 @@ public final class FBReader extends ZLAndroidActivity {
final View view = findViewById(R.id.main_view);
return (view != null && view.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) {
FBReaderApp.Instance().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(
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;
}
}

View file

@ -19,15 +19,12 @@
package org.geometerplus.zlibrary.ui.android.library;
import java.lang.reflect.*;
import android.app.Activity;
import android.os.Bundle;
import android.content.*;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.view.*;
import android.os.PowerManager;
import org.geometerplus.zlibrary.core.application.ZLApplication;
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
@ -36,147 +33,11 @@ import org.geometerplus.zlibrary.ui.android.R;
import org.geometerplus.zlibrary.ui.android.application.ZLAndroidApplicationWindow;
public abstract class ZLAndroidActivity extends Activity {
private void setScreenBrightnessAuto() {
final WindowManager.LayoutParams attrs = getWindow().getAttributes();
attrs.screenBrightness = -1.0f;
getWindow().setAttributes(attrs);
}
final 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);
}
final int getScreenBrightness() {
final int level = (int)(100 * getWindow().getAttributes().screenBrightness);
return (level >= 0) ? level : 50;
}
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) {
}
}
protected abstract ZLFile fileFromIntent(Intent intent);
protected abstract Runnable getPostponedInitAction();
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) {
ZLApplication.Instance().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;
}
}
}
}
}
@Override
public void onResume() {
super.onResume();
switchWakeLock(
getZLibrary().BatteryLevelToTurnScreenOffOption.getValue() <
ZLApplication.Instance().getBatteryLevel()
);
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));
}
@Override
public void onPause() {
unregisterReceiver(myBatteryInfoReceiver);
ZLApplication.Instance().stopTimer();
switchWakeLock(false);
if (getZLibrary().DisableButtonLightsOption.getValue()) {
setButtonLight(true);
}
ZLApplication.Instance().onWindowClosing();
super.onPause();
}
@Override
public void onLowMemory() {
ZLApplication.Instance().onWindowClosing();
super.onLowMemory();
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
final String action = intent.getAction();
if (Intent.ACTION_VIEW.equals(action) || "android.fbreader.action.VIEW".equals(action)) {
ZLApplication.Instance().openFile(fileFromIntent(intent), null);
}
}
protected static ZLAndroidLibrary getZLibrary() {
return (ZLAndroidLibrary)ZLAndroidLibrary.Instance();
}
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(
getZLibrary().BatteryLevelToTurnScreenOffOption.getValue() < level
);
}
};
}

View file

@ -43,6 +43,8 @@ import org.geometerplus.zlibrary.core.options.ZLIntegerRangeOption;
import org.geometerplus.zlibrary.ui.android.R;
import org.geometerplus.zlibrary.ui.android.view.ZLAndroidWidget;
import org.geometerplus.android.fbreader.FBReader;
public final class ZLAndroidLibrary extends ZLibrary {
public final ZLBooleanOption ShowStatusBarOption = new ZLBooleanOption("LookNFeel", "ShowStatusBar", hasNoHardwareMenuButton());
public final ZLIntegerRangeOption BatteryLevelToTurnScreenOffOption = new ZLIntegerRangeOption("LookNFeel", "BatteryLevelToTurnScreenOff", 0, 100, 50);
@ -73,14 +75,14 @@ public final class ZLAndroidLibrary extends ZLibrary {
return "GT-S5830".equals(Build.MODEL);
}
private ZLAndroidActivity myActivity;
private FBReader myActivity;
private final Application myApplication;
ZLAndroidLibrary(Application application) {
myApplication = application;
}
public void setActivity(ZLAndroidActivity activity) {
public void setActivity(FBReader activity) {
myActivity = activity;
}
@ -90,7 +92,7 @@ public final class ZLAndroidLibrary extends ZLibrary {
}
}
public ZLAndroidActivity getActivity() {
public FBReader getActivity() {
return myActivity;
}

View file

@ -28,7 +28,7 @@ import org.geometerplus.zlibrary.core.view.ZLView;
import org.geometerplus.zlibrary.core.view.ZLViewWidget;
import org.geometerplus.zlibrary.core.application.ZLApplication;
import org.geometerplus.zlibrary.ui.android.library.ZLAndroidActivity;
import org.geometerplus.android.fbreader.FBReader;
public class ZLAndroidWidget extends View implements ZLViewWidget, View.OnLongClickListener {
private final Paint myPaint = new Paint();
@ -72,10 +72,10 @@ public class ZLAndroidWidget extends View implements ZLViewWidget, View.OnLongCl
@Override
protected void onDraw(final Canvas canvas) {
final Context context = getContext();
if (context instanceof ZLAndroidActivity) {
((ZLAndroidActivity)context).createWakeLock();
if (context instanceof FBReader) {
((FBReader)context).createWakeLock();
} else {
System.err.println("A surprise: view's context is not a ZLAndroidActivity");
System.err.println("A surprise: view's context is not an FBReader");
}
super.onDraw(canvas);