Hold a wake lock while running due tasks.

This commit is contained in:
akwizgran
2020-08-06 15:09:01 +01:00
parent 94dd0a2661
commit 942bb28701
11 changed files with 68 additions and 52 deletions

View File

@@ -3,7 +3,9 @@ package org.briarproject.bramble.api.system;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
@NotNullByDefault
public interface AndroidWakeLockFactory {
public interface AndroidWakeLockManager {
AndroidWakeLock createWakeLock();
void runWakefully(Runnable r);
}

View File

@@ -6,7 +6,7 @@ import org.briarproject.bramble.api.io.TimeoutMonitor;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import org.briarproject.bramble.api.plugin.duplex.DuplexPlugin;
import org.briarproject.bramble.api.plugin.duplex.DuplexTransportConnection;
import org.briarproject.bramble.api.system.AndroidWakeLockFactory;
import org.briarproject.bramble.api.system.AndroidWakeLockManager;
import java.io.IOException;
@@ -15,15 +15,15 @@ class AndroidBluetoothConnectionFactory
implements BluetoothConnectionFactory<BluetoothSocket> {
private final BluetoothConnectionLimiter connectionLimiter;
private final AndroidWakeLockFactory wakeLockFactory;
private final AndroidWakeLockManager wakeLockManager;
private final TimeoutMonitor timeoutMonitor;
AndroidBluetoothConnectionFactory(
BluetoothConnectionLimiter connectionLimiter,
AndroidWakeLockFactory wakeLockFactory,
AndroidWakeLockManager wakeLockManager,
TimeoutMonitor timeoutMonitor) {
this.connectionLimiter = connectionLimiter;
this.wakeLockFactory = wakeLockFactory;
this.wakeLockManager = wakeLockManager;
this.timeoutMonitor = timeoutMonitor;
}
@@ -31,6 +31,6 @@ class AndroidBluetoothConnectionFactory
public DuplexTransportConnection wrapSocket(DuplexPlugin plugin,
BluetoothSocket s) throws IOException {
return new AndroidBluetoothTransportConnection(plugin,
connectionLimiter, wakeLockFactory, timeoutMonitor, s);
connectionLimiter, wakeLockManager, timeoutMonitor, s);
}
}

View File

@@ -13,7 +13,7 @@ import org.briarproject.bramble.api.plugin.TransportId;
import org.briarproject.bramble.api.plugin.duplex.DuplexPlugin;
import org.briarproject.bramble.api.plugin.duplex.DuplexPluginFactory;
import org.briarproject.bramble.api.system.AndroidExecutor;
import org.briarproject.bramble.api.system.AndroidWakeLockFactory;
import org.briarproject.bramble.api.system.AndroidWakeLockManager;
import org.briarproject.bramble.api.system.Clock;
import java.security.SecureRandom;
@@ -35,7 +35,7 @@ public class AndroidBluetoothPluginFactory implements DuplexPluginFactory {
private final Executor ioExecutor;
private final AndroidExecutor androidExecutor;
private final AndroidWakeLockFactory wakeLockFactory;
private final AndroidWakeLockManager wakeLockManager;
private final Context appContext;
private final SecureRandom secureRandom;
private final EventBus eventBus;
@@ -45,7 +45,7 @@ public class AndroidBluetoothPluginFactory implements DuplexPluginFactory {
public AndroidBluetoothPluginFactory(Executor ioExecutor,
AndroidExecutor androidExecutor,
AndroidWakeLockFactory wakeLockFactory,
AndroidWakeLockManager wakeLockManager,
Context appContext,
SecureRandom secureRandom,
EventBus eventBus,
@@ -54,7 +54,7 @@ public class AndroidBluetoothPluginFactory implements DuplexPluginFactory {
BackoffFactory backoffFactory) {
this.ioExecutor = ioExecutor;
this.androidExecutor = androidExecutor;
this.wakeLockFactory = wakeLockFactory;
this.wakeLockManager = wakeLockManager;
this.appContext = appContext;
this.secureRandom = secureRandom;
this.eventBus = eventBus;
@@ -79,7 +79,7 @@ public class AndroidBluetoothPluginFactory implements DuplexPluginFactory {
new BluetoothConnectionLimiterImpl(eventBus);
BluetoothConnectionFactory<BluetoothSocket> connectionFactory =
new AndroidBluetoothConnectionFactory(connectionLimiter,
wakeLockFactory, timeoutMonitor);
wakeLockManager, timeoutMonitor);
Backoff backoff = backoffFactory.createBackoff(MIN_POLLING_INTERVAL,
MAX_POLLING_INTERVAL, BACKOFF_BASE);
AndroidBluetoothPlugin plugin = new AndroidBluetoothPlugin(

View File

@@ -7,7 +7,7 @@ import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import org.briarproject.bramble.api.plugin.Plugin;
import org.briarproject.bramble.api.plugin.duplex.AbstractDuplexTransportConnection;
import org.briarproject.bramble.api.system.AndroidWakeLock;
import org.briarproject.bramble.api.system.AndroidWakeLockFactory;
import org.briarproject.bramble.api.system.AndroidWakeLockManager;
import java.io.IOException;
import java.io.InputStream;
@@ -27,7 +27,7 @@ class AndroidBluetoothTransportConnection
AndroidBluetoothTransportConnection(Plugin plugin,
BluetoothConnectionLimiter connectionLimiter,
AndroidWakeLockFactory wakeLockFactory,
AndroidWakeLockManager wakeLockManager,
TimeoutMonitor timeoutMonitor,
BluetoothSocket socket) throws IOException {
super(plugin);
@@ -35,7 +35,7 @@ class AndroidBluetoothTransportConnection
this.socket = socket;
in = timeoutMonitor.createTimeoutInputStream(
socket.getInputStream(), plugin.getMaxIdleTime() * 2);
wakeLock = wakeLockFactory.createWakeLock();
wakeLock = wakeLockManager.createWakeLock();
wakeLock.acquire();
String address = socket.getRemoteDevice().getAddress();
if (isValidBluetoothAddress(address)) remote.put(PROP_ADDRESS, address);

View File

@@ -12,7 +12,7 @@ import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
import org.briarproject.bramble.api.plugin.Backoff;
import org.briarproject.bramble.api.plugin.PluginCallback;
import org.briarproject.bramble.api.system.AndroidWakeLock;
import org.briarproject.bramble.api.system.AndroidWakeLockFactory;
import org.briarproject.bramble.api.system.AndroidWakeLockManager;
import org.briarproject.bramble.api.system.Clock;
import org.briarproject.bramble.api.system.LocationUtils;
import org.briarproject.bramble.api.system.ResourceProvider;
@@ -40,7 +40,7 @@ class AndroidTorPlugin extends TorPlugin {
ResourceProvider resourceProvider,
CircumventionProvider circumventionProvider,
BatteryManager batteryManager,
AndroidWakeLockFactory wakeLockFactory,
AndroidWakeLockManager wakeLockManager,
Backoff backoff,
TorRendezvousCrypto torRendezvousCrypto,
PluginCallback callback,
@@ -53,7 +53,7 @@ class AndroidTorPlugin extends TorPlugin {
maxLatency, maxIdleTime,
appContext.getDir("tor", MODE_PRIVATE));
this.appContext = appContext;
wakeLock = wakeLockFactory.createWakeLock();
wakeLock = wakeLockManager.createWakeLock();
}
@Override

View File

@@ -13,7 +13,7 @@ import org.briarproject.bramble.api.plugin.TorConstants;
import org.briarproject.bramble.api.plugin.TransportId;
import org.briarproject.bramble.api.plugin.duplex.DuplexPlugin;
import org.briarproject.bramble.api.plugin.duplex.DuplexPluginFactory;
import org.briarproject.bramble.api.system.AndroidWakeLockFactory;
import org.briarproject.bramble.api.system.AndroidWakeLockManager;
import org.briarproject.bramble.api.system.Clock;
import org.briarproject.bramble.api.system.LocationUtils;
import org.briarproject.bramble.api.system.ResourceProvider;
@@ -48,7 +48,7 @@ public class AndroidTorPluginFactory implements DuplexPluginFactory {
private final ResourceProvider resourceProvider;
private final CircumventionProvider circumventionProvider;
private final BatteryManager batteryManager;
private final AndroidWakeLockFactory wakeLockFactory;
private final AndroidWakeLockManager wakeLockManager;
private final Clock clock;
public AndroidTorPluginFactory(Executor ioExecutor,
@@ -61,7 +61,7 @@ public class AndroidTorPluginFactory implements DuplexPluginFactory {
ResourceProvider resourceProvider,
CircumventionProvider circumventionProvider,
BatteryManager batteryManager,
AndroidWakeLockFactory wakeLockFactory,
AndroidWakeLockManager wakeLockManager,
Clock clock) {
this.ioExecutor = ioExecutor;
this.appContext = appContext;
@@ -73,7 +73,7 @@ public class AndroidTorPluginFactory implements DuplexPluginFactory {
this.resourceProvider = resourceProvider;
this.circumventionProvider = circumventionProvider;
this.batteryManager = batteryManager;
this.wakeLockFactory = wakeLockFactory;
this.wakeLockManager = wakeLockManager;
this.clock = clock;
}
@@ -120,7 +120,7 @@ public class AndroidTorPluginFactory implements DuplexPluginFactory {
AndroidTorPlugin plugin = new AndroidTorPlugin(ioExecutor,
appContext, networkManager, locationUtils, torSocketFactory,
clock, resourceProvider, circumventionProvider, batteryManager,
wakeLockFactory, backoff, torRendezvousCrypto, callback,
wakeLockManager, backoff, torRendezvousCrypto, callback,
architecture, MAX_LATENCY, MAX_IDLE_TIME);
eventBus.addListener(plugin);
return plugin;

View File

@@ -2,7 +2,7 @@ package org.briarproject.bramble.system;
import org.briarproject.bramble.api.event.EventExecutor;
import org.briarproject.bramble.api.system.AndroidExecutor;
import org.briarproject.bramble.api.system.AndroidWakeLockFactory;
import org.briarproject.bramble.api.system.AndroidWakeLockManager;
import org.briarproject.bramble.api.system.LocationUtils;
import org.briarproject.bramble.api.system.ResourceProvider;
import org.briarproject.bramble.api.system.SecureRandomProvider;
@@ -51,8 +51,8 @@ public class AndroidSystemModule {
@Provides
@Singleton
AndroidWakeLockFactory provideWakeLockFactory(
AndroidWakeLockFactoryImpl wakeLockFactory) {
return wakeLockFactory;
AndroidWakeLockManager provideWakeLockManager(
AndroidWakeLockManagerImpl wakeLockManager) {
return wakeLockManager;
}
}

View File

@@ -12,7 +12,7 @@ import org.briarproject.bramble.api.lifecycle.Service;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
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.AndroidWakeLockManager;
import org.briarproject.bramble.api.system.TaskScheduler;
import java.util.ArrayList;
@@ -53,7 +53,7 @@ class AndroidTaskScheduler implements TaskScheduler, Service, AlarmListener {
private static final long ALARM_MS = INTERVAL_FIFTEEN_MINUTES;
private final Application app;
private final AndroidWakeLockFactory wakeLockFactory;
private final AndroidWakeLockManager wakeLockManager;
private final ScheduledExecutorService scheduledExecutorService;
private final AlarmManager alarmManager;
@@ -62,10 +62,10 @@ class AndroidTaskScheduler implements TaskScheduler, Service, AlarmListener {
private final Queue<ScheduledTask> tasks = new PriorityQueue<>();
AndroidTaskScheduler(Application app,
AndroidWakeLockFactory wakeLockFactory,
AndroidWakeLockManager wakeLockManager,
ScheduledExecutorService scheduledExecutorService) {
this.app = app;
this.wakeLockFactory = wakeLockFactory;
this.wakeLockManager = wakeLockManager;
this.scheduledExecutorService = scheduledExecutorService;
alarmManager = (AlarmManager)
requireNonNull(app.getSystemService(ALARM_SERVICE));
@@ -73,7 +73,8 @@ class AndroidTaskScheduler implements TaskScheduler, Service, AlarmListener {
@Override
public void startService() {
scheduledExecutorService.scheduleAtFixedRate(this::runDueTasks,
scheduledExecutorService.scheduleAtFixedRate(
() -> wakeLockManager.runWakefully(this::runDueTasks),
TICK_MS, TICK_MS, MILLISECONDS);
scheduleAlarm();
}
@@ -112,21 +113,23 @@ class AndroidTaskScheduler implements TaskScheduler, Service, AlarmListener {
@Override
public void onAlarm(Intent intent) {
int extraPid = intent.getIntExtra(EXTRA_PID, -1);
int currentPid = Process.myPid();
if (extraPid == currentPid) {
LOG.info("Alarm");
rescheduleAlarm();
runDueTasks();
} else {
LOG.info("Ignoring alarm with PID " + extraPid
+ ", current PID is " + currentPid);
}
wakeLockManager.runWakefully(() -> {
int extraPid = intent.getIntExtra(EXTRA_PID, -1);
int currentPid = Process.myPid();
if (extraPid == currentPid) {
LOG.info("Alarm");
rescheduleAlarm();
runDueTasks();
} else {
LOG.info("Ignoring alarm with PID " + extraPid
+ ", current PID is " + currentPid);
}
});
}
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();
AndroidWakeLock wakeLock = wakeLockManager.createWakeLock();
return () -> {
wakeLock.acquire();
executor.execute(() -> {

View File

@@ -4,7 +4,7 @@ import android.app.Application;
import org.briarproject.bramble.api.lifecycle.LifecycleManager;
import org.briarproject.bramble.api.system.AlarmListener;
import org.briarproject.bramble.api.system.AndroidWakeLockFactory;
import org.briarproject.bramble.api.system.AndroidWakeLockManager;
import org.briarproject.bramble.api.system.TaskScheduler;
import java.util.concurrent.RejectedExecutionHandler;
@@ -38,10 +38,10 @@ public class AndroidTaskSchedulerModule {
@Singleton
AndroidTaskScheduler provideAndroidTaskScheduler(
LifecycleManager lifecycleManager, Application app,
AndroidWakeLockFactory wakeLockFactory) {
AndroidWakeLockManager wakeLockManager) {
lifecycleManager.registerForShutdown(scheduledExecutorService);
AndroidTaskScheduler scheduler = new AndroidTaskScheduler(app,
wakeLockFactory, scheduledExecutorService);
wakeLockManager, scheduledExecutorService);
lifecycleManager.registerService(scheduler);
return scheduler;
}

View File

@@ -9,7 +9,7 @@ import android.os.PowerManager;
import org.briarproject.bramble.api.event.EventExecutor;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import org.briarproject.bramble.api.system.AndroidWakeLock;
import org.briarproject.bramble.api.system.AndroidWakeLockFactory;
import org.briarproject.bramble.api.system.AndroidWakeLockManager;
import org.briarproject.bramble.api.system.TaskScheduler;
import java.util.concurrent.Executor;
@@ -25,7 +25,7 @@ import static org.briarproject.bramble.api.nullsafety.NullSafety.requireNonNull;
@Immutable
@NotNullByDefault
class AndroidWakeLockFactoryImpl implements AndroidWakeLockFactory {
class AndroidWakeLockManagerImpl implements AndroidWakeLockManager {
/**
* How often to replace the wake lock.
@@ -41,7 +41,7 @@ class AndroidWakeLockFactoryImpl implements AndroidWakeLockFactory {
private final SharedWakeLock sharedWakeLock;
@Inject
AndroidWakeLockFactoryImpl(TaskScheduler scheduler,
AndroidWakeLockManagerImpl(TaskScheduler scheduler,
@EventExecutor Executor eventExecutor,
Application app) {
PowerManager powerManager = (PowerManager)
@@ -57,6 +57,17 @@ class AndroidWakeLockFactoryImpl implements AndroidWakeLockFactory {
return new AndroidWakeLockImpl(sharedWakeLock);
}
@Override
public void runWakefully(Runnable r) {
AndroidWakeLock wakeLock = createWakeLock();
wakeLock.acquire();
try {
r.run();
} finally {
wakeLock.release();
}
}
private String getWakeLockTag(Context ctx) {
PackageManager pm = ctx.getPackageManager();
for (PackageInfo info : pm.getInstalledPackages(0)) {