Added simple task wakelock

pull/190/head
M66B 4 years ago
parent 1e9c841e32
commit 06195f8325

@ -23,6 +23,7 @@ import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.os.Bundle; import android.os.Bundle;
import android.os.PowerManager;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
@ -56,6 +57,7 @@ public abstract class SimpleTask<T> implements LifecycleObserver {
private Future<?> future; private Future<?> future;
private ExecutorService localExecutor; private ExecutorService localExecutor;
private static PowerManager.WakeLock wl = null;
private static ExecutorService globalExecutor = null; private static ExecutorService globalExecutor = null;
private static final List<SimpleTask> tasks = new ArrayList<>(); private static final List<SimpleTask> tasks = new ArrayList<>();
@ -77,6 +79,11 @@ public abstract class SimpleTask<T> implements LifecycleObserver {
} }
private ExecutorService getExecutor(Context context) { private ExecutorService getExecutor(Context context) {
if (wl == null) {
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, BuildConfig.APPLICATION_ID + ":task");
}
if (localExecutor != null) if (localExecutor != null)
return localExecutor; return localExecutor;
@ -147,6 +154,8 @@ public abstract class SimpleTask<T> implements LifecycleObserver {
public void run() { public void run() {
// Run in background thread // Run in background thread
try { try {
wl.acquire();
if (log) if (log)
Log.i("Executing task=" + name); Log.i("Executing task=" + name);
long start = new Date().getTime(); long start = new Date().getTime();
@ -158,6 +167,8 @@ public abstract class SimpleTask<T> implements LifecycleObserver {
if (!(ex instanceof IllegalArgumentException)) if (!(ex instanceof IllegalArgumentException))
Log.e(ex); Log.e(ex);
this.ex = ex; this.ex = ex;
} finally {
wl.release();
} }
// Run on UI thread // Run on UI thread

Loading…
Cancel
Save