mirror of
https://github.com/zxing/zxing.git
synced 2025-01-12 11:47:26 -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;
|
||||
|
||||
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.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
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.
|
||||
*/
|
||||
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 ScheduledFuture<?> inactivityFuture = null;
|
||||
private final BroadcastReceiver powerStatusReceiver = new PowerStatusReceiver();
|
||||
private final BroadcastReceiver powerStatusReceiver;
|
||||
private InactivityAsyncTask inactivityTask;
|
||||
|
||||
InactivityTimer(Activity activity) {
|
||||
this.activity = activity;
|
||||
powerStatusReceiver = new PowerStatusReceiver();
|
||||
onActivity();
|
||||
}
|
||||
|
||||
void onActivity() {
|
||||
synchronized void onActivity() {
|
||||
cancel();
|
||||
if (!inactivityTimer.isShutdown()) {
|
||||
try {
|
||||
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
|
||||
}
|
||||
}
|
||||
inactivityTask = new InactivityAsyncTask();
|
||||
inactivityTask.execute();
|
||||
}
|
||||
|
||||
public void onPause() {
|
||||
|
@ -71,26 +60,16 @@ final class InactivityTimer {
|
|||
onActivity();
|
||||
}
|
||||
|
||||
private void cancel() {
|
||||
ScheduledFuture<?> future = inactivityFuture;
|
||||
if (future != null) {
|
||||
future.cancel(true);
|
||||
inactivityFuture = null;
|
||||
private synchronized void cancel() {
|
||||
AsyncTask<?,?,?> task = inactivityTask;
|
||||
if (task != null) {
|
||||
task.cancel(true);
|
||||
inactivityTask = null;
|
||||
}
|
||||
}
|
||||
|
||||
void shutdown() {
|
||||
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 {
|
||||
|
@ -98,13 +77,28 @@ final class InactivityTimer {
|
|||
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
|
||||
int batteryPlugged = intent.getIntExtra("plugged", -1);
|
||||
if (batteryPlugged > 0) {
|
||||
boolean onBatteryNow = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1) <= 0;
|
||||
if (onBatteryNow) {
|
||||
InactivityTimer.this.onActivity();
|
||||
} else {
|
||||
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