mirror of
https://github.com/zxing/zxing.git
synced 2025-03-05 20:48:51 -08:00
Use AsyncTask for inactivity timer; fix case where device was unplugged while running
git-svn-id: https://zxing.googlecode.com/svn/trunk@2377 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
parent
e5be3c19da
commit
24f5b64a2f
|
@ -16,49 +16,38 @@
|
||||||
|
|
||||||
package com.google.zxing.client.android;
|
package com.google.zxing.client.android;
|
||||||
|
|
||||||
import java.util.concurrent.Executors;
|
|
||||||
import java.util.concurrent.RejectedExecutionException;
|
|
||||||
import java.util.concurrent.ScheduledExecutorService;
|
|
||||||
import java.util.concurrent.ScheduledFuture;
|
|
||||||
import java.util.concurrent.ThreadFactory;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
|
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.content.BroadcastReceiver;
|
import android.content.BroadcastReceiver;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.IntentFilter;
|
import android.content.IntentFilter;
|
||||||
|
import android.os.AsyncTask;
|
||||||
|
import android.os.BatteryManager;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Finishes an activity after a period of inactivity if the device is on battery power.
|
* Finishes an activity after a period of inactivity if the device is on battery power.
|
||||||
*/
|
*/
|
||||||
final class InactivityTimer {
|
final class InactivityTimer {
|
||||||
|
|
||||||
private static final int INACTIVITY_DELAY_SECONDS = 5 * 60;
|
private static final String TAG = InactivityTimer.class.getSimpleName();
|
||||||
|
|
||||||
|
private static final long INACTIVITY_DELAY_MS = 5 * 60 * 1000L;
|
||||||
|
|
||||||
private final ScheduledExecutorService inactivityTimer =
|
|
||||||
Executors.newSingleThreadScheduledExecutor(new DaemonThreadFactory());
|
|
||||||
private final Activity activity;
|
private final Activity activity;
|
||||||
private ScheduledFuture<?> inactivityFuture = null;
|
private final BroadcastReceiver powerStatusReceiver;
|
||||||
private final BroadcastReceiver powerStatusReceiver = new PowerStatusReceiver();
|
private InactivityAsyncTask inactivityTask;
|
||||||
|
|
||||||
InactivityTimer(Activity activity) {
|
InactivityTimer(Activity activity) {
|
||||||
this.activity = activity;
|
this.activity = activity;
|
||||||
|
powerStatusReceiver = new PowerStatusReceiver();
|
||||||
onActivity();
|
onActivity();
|
||||||
}
|
}
|
||||||
|
|
||||||
void onActivity() {
|
synchronized void onActivity() {
|
||||||
cancel();
|
cancel();
|
||||||
if (!inactivityTimer.isShutdown()) {
|
inactivityTask = new InactivityAsyncTask();
|
||||||
try {
|
inactivityTask.execute();
|
||||||
inactivityFuture = inactivityTimer.schedule(new FinishListener(activity),
|
|
||||||
INACTIVITY_DELAY_SECONDS,
|
|
||||||
TimeUnit.SECONDS);
|
|
||||||
} catch (RejectedExecutionException ree) {
|
|
||||||
// surprising, but could be normal if for some reason the implementation just doesn't
|
|
||||||
// think it can shcedule again. Since this time-out is non-essential, just forget it
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onPause() {
|
public void onPause() {
|
||||||
|
@ -71,26 +60,16 @@ final class InactivityTimer {
|
||||||
onActivity();
|
onActivity();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void cancel() {
|
private synchronized void cancel() {
|
||||||
ScheduledFuture<?> future = inactivityFuture;
|
AsyncTask<?,?,?> task = inactivityTask;
|
||||||
if (future != null) {
|
if (task != null) {
|
||||||
future.cancel(true);
|
task.cancel(true);
|
||||||
inactivityFuture = null;
|
inactivityTask = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void shutdown() {
|
void shutdown() {
|
||||||
cancel();
|
cancel();
|
||||||
inactivityTimer.shutdown();
|
|
||||||
}
|
|
||||||
|
|
||||||
private static final class DaemonThreadFactory implements ThreadFactory {
|
|
||||||
@Override
|
|
||||||
public Thread newThread(Runnable runnable) {
|
|
||||||
Thread thread = new Thread(runnable);
|
|
||||||
thread.setDaemon(true);
|
|
||||||
return thread;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private final class PowerStatusReceiver extends BroadcastReceiver {
|
private final class PowerStatusReceiver extends BroadcastReceiver {
|
||||||
|
@ -98,13 +77,28 @@ final class InactivityTimer {
|
||||||
public void onReceive(Context context, Intent intent){
|
public void onReceive(Context context, Intent intent){
|
||||||
if (Intent.ACTION_BATTERY_CHANGED.equals(intent.getAction())) {
|
if (Intent.ACTION_BATTERY_CHANGED.equals(intent.getAction())) {
|
||||||
// 0 indicates that we're on battery
|
// 0 indicates that we're on battery
|
||||||
// In Android 2.0+, use BatteryManager.EXTRA_PLUGGED
|
boolean onBatteryNow = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1) <= 0;
|
||||||
int batteryPlugged = intent.getIntExtra("plugged", -1);
|
if (onBatteryNow) {
|
||||||
if (batteryPlugged > 0) {
|
InactivityTimer.this.onActivity();
|
||||||
|
} else {
|
||||||
InactivityTimer.this.cancel();
|
InactivityTimer.this.cancel();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private final class InactivityAsyncTask extends AsyncTask<Void,Void,Void> {
|
||||||
|
@Override
|
||||||
|
protected Void doInBackground(Void... objects) {
|
||||||
|
try {
|
||||||
|
Thread.sleep(INACTIVITY_DELAY_MS);
|
||||||
|
Log.i(TAG, "Finishing activity due to inactivity");
|
||||||
|
activity.finish();
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
// continue without killing
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue