Issue 787 inactivity timer battery check

git-svn-id: https://zxing.googlecode.com/svn/trunk@1734 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
srowen 2011-04-03 18:46:38 +00:00
parent 169a94c45b
commit 50277f6486
2 changed files with 32 additions and 1 deletions

View file

@ -223,6 +223,8 @@ public final class CaptureActivity extends Activity implements SurfaceHolder.Cal
copyToClipboard = prefs.getBoolean(PreferencesActivity.KEY_COPY_TO_CLIPBOARD, true); copyToClipboard = prefs.getBoolean(PreferencesActivity.KEY_COPY_TO_CLIPBOARD, true);
beepManager.updatePrefs(); beepManager.updatePrefs();
inactivityTimer.onResume();
} }
@Override @Override
@ -232,6 +234,7 @@ public final class CaptureActivity extends Activity implements SurfaceHolder.Cal
handler.quitSynchronously(); handler.quitSynchronously();
handler = null; handler = null;
} }
inactivityTimer.onPause();
CameraManager.get().closeDriver(); CameraManager.get().closeDriver();
} }

View file

@ -23,9 +23,13 @@ import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import android.app.Activity; import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
/** /**
* Finishes an activity after a period of inactivity. * Finishes an activity after a period of inactivity if the device is on battery power.
*/ */
final class InactivityTimer { final class InactivityTimer {
@ -35,6 +39,7 @@ final class InactivityTimer {
Executors.newSingleThreadScheduledExecutor(new DaemonThreadFactory()); Executors.newSingleThreadScheduledExecutor(new DaemonThreadFactory());
private final Activity activity; private final Activity activity;
private ScheduledFuture<?> inactivityFuture = null; private ScheduledFuture<?> inactivityFuture = null;
private final PowerStatusReceiver powerStatusReceiver = new PowerStatusReceiver();
InactivityTimer(Activity activity) { InactivityTimer(Activity activity) {
this.activity = activity; this.activity = activity;
@ -48,6 +53,14 @@ final class InactivityTimer {
TimeUnit.SECONDS); TimeUnit.SECONDS);
} }
public void onPause(){
activity.unregisterReceiver(powerStatusReceiver);
}
public void onResume(){
activity.registerReceiver(powerStatusReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
}
private void cancel() { private void cancel() {
if (inactivityFuture != null) { if (inactivityFuture != null) {
inactivityFuture.cancel(true); inactivityFuture.cancel(true);
@ -68,4 +81,19 @@ final class InactivityTimer {
} }
} }
private final class PowerStatusReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent){
if (Intent.ACTION_BATTERY_CHANGED.equals(intent.getAction())) {
// 0 indicates that we're on battery
// In Android 2.0+, use BatteryManager.EXTRA_PLUGGED
if (intent.getIntExtra("plugged", -1) == 0) {
InactivityTimer.this.onActivity();
} else {
InactivityTimer.this.cancel();
}
}
}
}
} }