mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-21 15:19:53 +01:00
Add method for executing a task on an executor wakefully.
This commit is contained in:
@@ -2,10 +2,14 @@ package org.briarproject.bramble.api.system;
|
|||||||
|
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
|
|
||||||
|
import java.util.concurrent.Executor;
|
||||||
|
|
||||||
@NotNullByDefault
|
@NotNullByDefault
|
||||||
public interface AndroidWakeLockManager {
|
public interface AndroidWakeLockManager {
|
||||||
|
|
||||||
AndroidWakeLock createWakeLock();
|
AndroidWakeLock createWakeLock();
|
||||||
|
|
||||||
void runWakefully(Runnable r);
|
void runWakefully(Runnable r);
|
||||||
|
|
||||||
|
void executeWakefully(Runnable r, Executor executor);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,7 +11,6 @@ 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.AndroidWakeLockManager;
|
import org.briarproject.bramble.api.system.AndroidWakeLockManager;
|
||||||
import org.briarproject.bramble.api.system.TaskScheduler;
|
import org.briarproject.bramble.api.system.TaskScheduler;
|
||||||
|
|
||||||
@@ -89,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);
|
||||||
Runnable wakeful = createWakefulTask(task, executor);
|
Runnable wakeful = () ->
|
||||||
|
wakeLockManager.executeWakefully(task, executor);
|
||||||
ScheduledTask s = new ScheduledTask(wakeful, dueMillis);
|
ScheduledTask s = new ScheduledTask(wakeful, dueMillis);
|
||||||
if (dueMillis <= now) {
|
if (dueMillis <= now) {
|
||||||
scheduledExecutorService.execute(s);
|
scheduledExecutorService.execute(s);
|
||||||
@@ -127,21 +127,6 @@ 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 = wakeLockManager.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<>();
|
||||||
|
|||||||
@@ -68,6 +68,24 @@ class AndroidWakeLockManagerImpl implements AndroidWakeLockManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void executeWakefully(Runnable r, Executor executor) {
|
||||||
|
AndroidWakeLock wakeLock = createWakeLock();
|
||||||
|
wakeLock.acquire();
|
||||||
|
try {
|
||||||
|
executor.execute(() -> {
|
||||||
|
try {
|
||||||
|
r.run();
|
||||||
|
} finally {
|
||||||
|
wakeLock.release();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} catch (Exception e) {
|
||||||
|
wakeLock.release();
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private String getWakeLockTag(Context ctx) {
|
private String getWakeLockTag(Context ctx) {
|
||||||
PackageManager pm = ctx.getPackageManager();
|
PackageManager pm = ctx.getPackageManager();
|
||||||
for (PackageInfo info : pm.getInstalledPackages(0)) {
|
for (PackageInfo info : pm.getInstalledPackages(0)) {
|
||||||
|
|||||||
Reference in New Issue
Block a user