mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-21 15:19:53 +01:00
Hold a wake lock while scheduled tasks are running.
This commit is contained in:
@@ -11,6 +11,8 @@ import android.os.SystemClock;
|
|||||||
import org.briarproject.bramble.api.lifecycle.Service;
|
import org.briarproject.bramble.api.lifecycle.Service;
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
import org.briarproject.bramble.api.system.AlarmListener;
|
import org.briarproject.bramble.api.system.AlarmListener;
|
||||||
|
import org.briarproject.bramble.api.system.AndroidWakeLock;
|
||||||
|
import org.briarproject.bramble.api.system.AndroidWakeLockFactory;
|
||||||
import org.briarproject.bramble.api.system.TaskScheduler;
|
import org.briarproject.bramble.api.system.TaskScheduler;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@@ -51,6 +53,7 @@ class AndroidTaskScheduler implements TaskScheduler, Service, AlarmListener {
|
|||||||
private static final long ALARM_MS = INTERVAL_FIFTEEN_MINUTES;
|
private static final long ALARM_MS = INTERVAL_FIFTEEN_MINUTES;
|
||||||
|
|
||||||
private final Application app;
|
private final Application app;
|
||||||
|
private final AndroidWakeLockFactory wakeLockFactory;
|
||||||
private final ScheduledExecutorService scheduledExecutorService;
|
private final ScheduledExecutorService scheduledExecutorService;
|
||||||
private final AlarmManager alarmManager;
|
private final AlarmManager alarmManager;
|
||||||
|
|
||||||
@@ -59,8 +62,10 @@ class AndroidTaskScheduler implements TaskScheduler, Service, AlarmListener {
|
|||||||
private final Queue<ScheduledTask> tasks = new PriorityQueue<>();
|
private final Queue<ScheduledTask> tasks = new PriorityQueue<>();
|
||||||
|
|
||||||
AndroidTaskScheduler(Application app,
|
AndroidTaskScheduler(Application app,
|
||||||
|
AndroidWakeLockFactory wakeLockFactory,
|
||||||
ScheduledExecutorService scheduledExecutorService) {
|
ScheduledExecutorService scheduledExecutorService) {
|
||||||
this.app = app;
|
this.app = app;
|
||||||
|
this.wakeLockFactory = wakeLockFactory;
|
||||||
this.scheduledExecutorService = scheduledExecutorService;
|
this.scheduledExecutorService = scheduledExecutorService;
|
||||||
alarmManager = (AlarmManager)
|
alarmManager = (AlarmManager)
|
||||||
requireNonNull(app.getSystemService(ALARM_SERVICE));
|
requireNonNull(app.getSystemService(ALARM_SERVICE));
|
||||||
@@ -83,7 +88,8 @@ class AndroidTaskScheduler implements TaskScheduler, Service, AlarmListener {
|
|||||||
TimeUnit unit) {
|
TimeUnit unit) {
|
||||||
long now = SystemClock.elapsedRealtime();
|
long now = SystemClock.elapsedRealtime();
|
||||||
long dueMillis = now + MILLISECONDS.convert(delay, unit);
|
long dueMillis = now + MILLISECONDS.convert(delay, unit);
|
||||||
ScheduledTask s = new ScheduledTask(task, executor, dueMillis);
|
Runnable wakeful = createWakefulTask(task, executor);
|
||||||
|
ScheduledTask s = new ScheduledTask(wakeful, dueMillis);
|
||||||
if (dueMillis <= now) {
|
if (dueMillis <= now) {
|
||||||
scheduledExecutorService.execute(s);
|
scheduledExecutorService.execute(s);
|
||||||
} else {
|
} else {
|
||||||
@@ -118,6 +124,21 @@ class AndroidTaskScheduler implements TaskScheduler, Service, AlarmListener {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Runnable createWakefulTask(Runnable task, Executor executor) {
|
||||||
|
// Hold a wake lock from before we submit the task until after it runs
|
||||||
|
AndroidWakeLock wakeLock = wakeLockFactory.createWakeLock();
|
||||||
|
return () -> {
|
||||||
|
wakeLock.acquire();
|
||||||
|
executor.execute(() -> {
|
||||||
|
try {
|
||||||
|
task.run();
|
||||||
|
} finally {
|
||||||
|
wakeLock.release();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
private void runDueTasks() {
|
private void runDueTasks() {
|
||||||
long now = SystemClock.elapsedRealtime();
|
long now = SystemClock.elapsedRealtime();
|
||||||
List<ScheduledTask> due = new ArrayList<>();
|
List<ScheduledTask> due = new ArrayList<>();
|
||||||
@@ -177,9 +198,8 @@ class AndroidTaskScheduler implements TaskScheduler, Service, AlarmListener {
|
|||||||
|
|
||||||
private final long dueMillis;
|
private final long dueMillis;
|
||||||
|
|
||||||
public ScheduledTask(Runnable runnable, Executor executor,
|
public ScheduledTask(Runnable runnable, long dueMillis) {
|
||||||
long dueMillis) {
|
super(runnable, null);
|
||||||
super(() -> executor.execute(runnable), null);
|
|
||||||
this.dueMillis = dueMillis;
|
this.dueMillis = dueMillis;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import android.app.Application;
|
|||||||
|
|
||||||
import org.briarproject.bramble.api.lifecycle.LifecycleManager;
|
import org.briarproject.bramble.api.lifecycle.LifecycleManager;
|
||||||
import org.briarproject.bramble.api.system.AlarmListener;
|
import org.briarproject.bramble.api.system.AlarmListener;
|
||||||
|
import org.briarproject.bramble.api.system.AndroidWakeLockFactory;
|
||||||
import org.briarproject.bramble.api.system.TaskScheduler;
|
import org.briarproject.bramble.api.system.TaskScheduler;
|
||||||
|
|
||||||
import java.util.concurrent.RejectedExecutionHandler;
|
import java.util.concurrent.RejectedExecutionHandler;
|
||||||
@@ -36,10 +37,11 @@ public class AndroidTaskSchedulerModule {
|
|||||||
@Provides
|
@Provides
|
||||||
@Singleton
|
@Singleton
|
||||||
AndroidTaskScheduler provideAndroidTaskScheduler(
|
AndroidTaskScheduler provideAndroidTaskScheduler(
|
||||||
LifecycleManager lifecycleManager, Application app) {
|
LifecycleManager lifecycleManager, Application app,
|
||||||
|
AndroidWakeLockFactory wakeLockFactory) {
|
||||||
lifecycleManager.registerForShutdown(scheduledExecutorService);
|
lifecycleManager.registerForShutdown(scheduledExecutorService);
|
||||||
AndroidTaskScheduler scheduler =
|
AndroidTaskScheduler scheduler = new AndroidTaskScheduler(app,
|
||||||
new AndroidTaskScheduler(app, scheduledExecutorService);
|
wakeLockFactory, scheduledExecutorService);
|
||||||
lifecycleManager.registerService(scheduler);
|
lifecycleManager.registerService(scheduler);
|
||||||
return scheduler;
|
return scheduler;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user