mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-20 22:59:54 +01:00
Hold a wake lock while running due tasks.
This commit is contained in:
@@ -3,7 +3,9 @@ package org.briarproject.bramble.api.system;
|
|||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
|
|
||||||
@NotNullByDefault
|
@NotNullByDefault
|
||||||
public interface AndroidWakeLockFactory {
|
public interface AndroidWakeLockManager {
|
||||||
|
|
||||||
AndroidWakeLock createWakeLock();
|
AndroidWakeLock createWakeLock();
|
||||||
|
|
||||||
|
void runWakefully(Runnable r);
|
||||||
}
|
}
|
||||||
@@ -6,7 +6,7 @@ import org.briarproject.bramble.api.io.TimeoutMonitor;
|
|||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
import org.briarproject.bramble.api.plugin.duplex.DuplexPlugin;
|
import org.briarproject.bramble.api.plugin.duplex.DuplexPlugin;
|
||||||
import org.briarproject.bramble.api.plugin.duplex.DuplexTransportConnection;
|
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;
|
import java.io.IOException;
|
||||||
|
|
||||||
@@ -15,15 +15,15 @@ class AndroidBluetoothConnectionFactory
|
|||||||
implements BluetoothConnectionFactory<BluetoothSocket> {
|
implements BluetoothConnectionFactory<BluetoothSocket> {
|
||||||
|
|
||||||
private final BluetoothConnectionLimiter connectionLimiter;
|
private final BluetoothConnectionLimiter connectionLimiter;
|
||||||
private final AndroidWakeLockFactory wakeLockFactory;
|
private final AndroidWakeLockManager wakeLockManager;
|
||||||
private final TimeoutMonitor timeoutMonitor;
|
private final TimeoutMonitor timeoutMonitor;
|
||||||
|
|
||||||
AndroidBluetoothConnectionFactory(
|
AndroidBluetoothConnectionFactory(
|
||||||
BluetoothConnectionLimiter connectionLimiter,
|
BluetoothConnectionLimiter connectionLimiter,
|
||||||
AndroidWakeLockFactory wakeLockFactory,
|
AndroidWakeLockManager wakeLockManager,
|
||||||
TimeoutMonitor timeoutMonitor) {
|
TimeoutMonitor timeoutMonitor) {
|
||||||
this.connectionLimiter = connectionLimiter;
|
this.connectionLimiter = connectionLimiter;
|
||||||
this.wakeLockFactory = wakeLockFactory;
|
this.wakeLockManager = wakeLockManager;
|
||||||
this.timeoutMonitor = timeoutMonitor;
|
this.timeoutMonitor = timeoutMonitor;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -31,6 +31,6 @@ class AndroidBluetoothConnectionFactory
|
|||||||
public DuplexTransportConnection wrapSocket(DuplexPlugin plugin,
|
public DuplexTransportConnection wrapSocket(DuplexPlugin plugin,
|
||||||
BluetoothSocket s) throws IOException {
|
BluetoothSocket s) throws IOException {
|
||||||
return new AndroidBluetoothTransportConnection(plugin,
|
return new AndroidBluetoothTransportConnection(plugin,
|
||||||
connectionLimiter, wakeLockFactory, timeoutMonitor, s);
|
connectionLimiter, wakeLockManager, timeoutMonitor, s);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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.DuplexPlugin;
|
||||||
import org.briarproject.bramble.api.plugin.duplex.DuplexPluginFactory;
|
import org.briarproject.bramble.api.plugin.duplex.DuplexPluginFactory;
|
||||||
import org.briarproject.bramble.api.system.AndroidExecutor;
|
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 org.briarproject.bramble.api.system.Clock;
|
||||||
|
|
||||||
import java.security.SecureRandom;
|
import java.security.SecureRandom;
|
||||||
@@ -35,7 +35,7 @@ public class AndroidBluetoothPluginFactory implements DuplexPluginFactory {
|
|||||||
|
|
||||||
private final Executor ioExecutor;
|
private final Executor ioExecutor;
|
||||||
private final AndroidExecutor androidExecutor;
|
private final AndroidExecutor androidExecutor;
|
||||||
private final AndroidWakeLockFactory wakeLockFactory;
|
private final AndroidWakeLockManager wakeLockManager;
|
||||||
private final Context appContext;
|
private final Context appContext;
|
||||||
private final SecureRandom secureRandom;
|
private final SecureRandom secureRandom;
|
||||||
private final EventBus eventBus;
|
private final EventBus eventBus;
|
||||||
@@ -45,7 +45,7 @@ public class AndroidBluetoothPluginFactory implements DuplexPluginFactory {
|
|||||||
|
|
||||||
public AndroidBluetoothPluginFactory(Executor ioExecutor,
|
public AndroidBluetoothPluginFactory(Executor ioExecutor,
|
||||||
AndroidExecutor androidExecutor,
|
AndroidExecutor androidExecutor,
|
||||||
AndroidWakeLockFactory wakeLockFactory,
|
AndroidWakeLockManager wakeLockManager,
|
||||||
Context appContext,
|
Context appContext,
|
||||||
SecureRandom secureRandom,
|
SecureRandom secureRandom,
|
||||||
EventBus eventBus,
|
EventBus eventBus,
|
||||||
@@ -54,7 +54,7 @@ public class AndroidBluetoothPluginFactory implements DuplexPluginFactory {
|
|||||||
BackoffFactory backoffFactory) {
|
BackoffFactory backoffFactory) {
|
||||||
this.ioExecutor = ioExecutor;
|
this.ioExecutor = ioExecutor;
|
||||||
this.androidExecutor = androidExecutor;
|
this.androidExecutor = androidExecutor;
|
||||||
this.wakeLockFactory = wakeLockFactory;
|
this.wakeLockManager = wakeLockManager;
|
||||||
this.appContext = appContext;
|
this.appContext = appContext;
|
||||||
this.secureRandom = secureRandom;
|
this.secureRandom = secureRandom;
|
||||||
this.eventBus = eventBus;
|
this.eventBus = eventBus;
|
||||||
@@ -79,7 +79,7 @@ public class AndroidBluetoothPluginFactory implements DuplexPluginFactory {
|
|||||||
new BluetoothConnectionLimiterImpl(eventBus);
|
new BluetoothConnectionLimiterImpl(eventBus);
|
||||||
BluetoothConnectionFactory<BluetoothSocket> connectionFactory =
|
BluetoothConnectionFactory<BluetoothSocket> connectionFactory =
|
||||||
new AndroidBluetoothConnectionFactory(connectionLimiter,
|
new AndroidBluetoothConnectionFactory(connectionLimiter,
|
||||||
wakeLockFactory, timeoutMonitor);
|
wakeLockManager, timeoutMonitor);
|
||||||
Backoff backoff = backoffFactory.createBackoff(MIN_POLLING_INTERVAL,
|
Backoff backoff = backoffFactory.createBackoff(MIN_POLLING_INTERVAL,
|
||||||
MAX_POLLING_INTERVAL, BACKOFF_BASE);
|
MAX_POLLING_INTERVAL, BACKOFF_BASE);
|
||||||
AndroidBluetoothPlugin plugin = new AndroidBluetoothPlugin(
|
AndroidBluetoothPlugin plugin = new AndroidBluetoothPlugin(
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
|||||||
import org.briarproject.bramble.api.plugin.Plugin;
|
import org.briarproject.bramble.api.plugin.Plugin;
|
||||||
import org.briarproject.bramble.api.plugin.duplex.AbstractDuplexTransportConnection;
|
import org.briarproject.bramble.api.plugin.duplex.AbstractDuplexTransportConnection;
|
||||||
import org.briarproject.bramble.api.system.AndroidWakeLock;
|
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.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
@@ -27,7 +27,7 @@ class AndroidBluetoothTransportConnection
|
|||||||
|
|
||||||
AndroidBluetoothTransportConnection(Plugin plugin,
|
AndroidBluetoothTransportConnection(Plugin plugin,
|
||||||
BluetoothConnectionLimiter connectionLimiter,
|
BluetoothConnectionLimiter connectionLimiter,
|
||||||
AndroidWakeLockFactory wakeLockFactory,
|
AndroidWakeLockManager wakeLockManager,
|
||||||
TimeoutMonitor timeoutMonitor,
|
TimeoutMonitor timeoutMonitor,
|
||||||
BluetoothSocket socket) throws IOException {
|
BluetoothSocket socket) throws IOException {
|
||||||
super(plugin);
|
super(plugin);
|
||||||
@@ -35,7 +35,7 @@ class AndroidBluetoothTransportConnection
|
|||||||
this.socket = socket;
|
this.socket = socket;
|
||||||
in = timeoutMonitor.createTimeoutInputStream(
|
in = timeoutMonitor.createTimeoutInputStream(
|
||||||
socket.getInputStream(), plugin.getMaxIdleTime() * 2);
|
socket.getInputStream(), plugin.getMaxIdleTime() * 2);
|
||||||
wakeLock = wakeLockFactory.createWakeLock();
|
wakeLock = wakeLockManager.createWakeLock();
|
||||||
wakeLock.acquire();
|
wakeLock.acquire();
|
||||||
String address = socket.getRemoteDevice().getAddress();
|
String address = socket.getRemoteDevice().getAddress();
|
||||||
if (isValidBluetoothAddress(address)) remote.put(PROP_ADDRESS, address);
|
if (isValidBluetoothAddress(address)) remote.put(PROP_ADDRESS, address);
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
|||||||
import org.briarproject.bramble.api.plugin.Backoff;
|
import org.briarproject.bramble.api.plugin.Backoff;
|
||||||
import org.briarproject.bramble.api.plugin.PluginCallback;
|
import org.briarproject.bramble.api.plugin.PluginCallback;
|
||||||
import org.briarproject.bramble.api.system.AndroidWakeLock;
|
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.Clock;
|
||||||
import org.briarproject.bramble.api.system.LocationUtils;
|
import org.briarproject.bramble.api.system.LocationUtils;
|
||||||
import org.briarproject.bramble.api.system.ResourceProvider;
|
import org.briarproject.bramble.api.system.ResourceProvider;
|
||||||
@@ -40,7 +40,7 @@ class AndroidTorPlugin extends TorPlugin {
|
|||||||
ResourceProvider resourceProvider,
|
ResourceProvider resourceProvider,
|
||||||
CircumventionProvider circumventionProvider,
|
CircumventionProvider circumventionProvider,
|
||||||
BatteryManager batteryManager,
|
BatteryManager batteryManager,
|
||||||
AndroidWakeLockFactory wakeLockFactory,
|
AndroidWakeLockManager wakeLockManager,
|
||||||
Backoff backoff,
|
Backoff backoff,
|
||||||
TorRendezvousCrypto torRendezvousCrypto,
|
TorRendezvousCrypto torRendezvousCrypto,
|
||||||
PluginCallback callback,
|
PluginCallback callback,
|
||||||
@@ -53,7 +53,7 @@ class AndroidTorPlugin extends TorPlugin {
|
|||||||
maxLatency, maxIdleTime,
|
maxLatency, maxIdleTime,
|
||||||
appContext.getDir("tor", MODE_PRIVATE));
|
appContext.getDir("tor", MODE_PRIVATE));
|
||||||
this.appContext = appContext;
|
this.appContext = appContext;
|
||||||
wakeLock = wakeLockFactory.createWakeLock();
|
wakeLock = wakeLockManager.createWakeLock();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ import org.briarproject.bramble.api.plugin.TorConstants;
|
|||||||
import org.briarproject.bramble.api.plugin.TransportId;
|
import org.briarproject.bramble.api.plugin.TransportId;
|
||||||
import org.briarproject.bramble.api.plugin.duplex.DuplexPlugin;
|
import org.briarproject.bramble.api.plugin.duplex.DuplexPlugin;
|
||||||
import org.briarproject.bramble.api.plugin.duplex.DuplexPluginFactory;
|
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.Clock;
|
||||||
import org.briarproject.bramble.api.system.LocationUtils;
|
import org.briarproject.bramble.api.system.LocationUtils;
|
||||||
import org.briarproject.bramble.api.system.ResourceProvider;
|
import org.briarproject.bramble.api.system.ResourceProvider;
|
||||||
@@ -48,7 +48,7 @@ public class AndroidTorPluginFactory implements DuplexPluginFactory {
|
|||||||
private final ResourceProvider resourceProvider;
|
private final ResourceProvider resourceProvider;
|
||||||
private final CircumventionProvider circumventionProvider;
|
private final CircumventionProvider circumventionProvider;
|
||||||
private final BatteryManager batteryManager;
|
private final BatteryManager batteryManager;
|
||||||
private final AndroidWakeLockFactory wakeLockFactory;
|
private final AndroidWakeLockManager wakeLockManager;
|
||||||
private final Clock clock;
|
private final Clock clock;
|
||||||
|
|
||||||
public AndroidTorPluginFactory(Executor ioExecutor,
|
public AndroidTorPluginFactory(Executor ioExecutor,
|
||||||
@@ -61,7 +61,7 @@ public class AndroidTorPluginFactory implements DuplexPluginFactory {
|
|||||||
ResourceProvider resourceProvider,
|
ResourceProvider resourceProvider,
|
||||||
CircumventionProvider circumventionProvider,
|
CircumventionProvider circumventionProvider,
|
||||||
BatteryManager batteryManager,
|
BatteryManager batteryManager,
|
||||||
AndroidWakeLockFactory wakeLockFactory,
|
AndroidWakeLockManager wakeLockManager,
|
||||||
Clock clock) {
|
Clock clock) {
|
||||||
this.ioExecutor = ioExecutor;
|
this.ioExecutor = ioExecutor;
|
||||||
this.appContext = appContext;
|
this.appContext = appContext;
|
||||||
@@ -73,7 +73,7 @@ public class AndroidTorPluginFactory implements DuplexPluginFactory {
|
|||||||
this.resourceProvider = resourceProvider;
|
this.resourceProvider = resourceProvider;
|
||||||
this.circumventionProvider = circumventionProvider;
|
this.circumventionProvider = circumventionProvider;
|
||||||
this.batteryManager = batteryManager;
|
this.batteryManager = batteryManager;
|
||||||
this.wakeLockFactory = wakeLockFactory;
|
this.wakeLockManager = wakeLockManager;
|
||||||
this.clock = clock;
|
this.clock = clock;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -120,7 +120,7 @@ public class AndroidTorPluginFactory implements DuplexPluginFactory {
|
|||||||
AndroidTorPlugin plugin = new AndroidTorPlugin(ioExecutor,
|
AndroidTorPlugin plugin = new AndroidTorPlugin(ioExecutor,
|
||||||
appContext, networkManager, locationUtils, torSocketFactory,
|
appContext, networkManager, locationUtils, torSocketFactory,
|
||||||
clock, resourceProvider, circumventionProvider, batteryManager,
|
clock, resourceProvider, circumventionProvider, batteryManager,
|
||||||
wakeLockFactory, backoff, torRendezvousCrypto, callback,
|
wakeLockManager, backoff, torRendezvousCrypto, callback,
|
||||||
architecture, MAX_LATENCY, MAX_IDLE_TIME);
|
architecture, MAX_LATENCY, MAX_IDLE_TIME);
|
||||||
eventBus.addListener(plugin);
|
eventBus.addListener(plugin);
|
||||||
return plugin;
|
return plugin;
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ package org.briarproject.bramble.system;
|
|||||||
|
|
||||||
import org.briarproject.bramble.api.event.EventExecutor;
|
import org.briarproject.bramble.api.event.EventExecutor;
|
||||||
import org.briarproject.bramble.api.system.AndroidExecutor;
|
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.LocationUtils;
|
||||||
import org.briarproject.bramble.api.system.ResourceProvider;
|
import org.briarproject.bramble.api.system.ResourceProvider;
|
||||||
import org.briarproject.bramble.api.system.SecureRandomProvider;
|
import org.briarproject.bramble.api.system.SecureRandomProvider;
|
||||||
@@ -51,8 +51,8 @@ public class AndroidSystemModule {
|
|||||||
|
|
||||||
@Provides
|
@Provides
|
||||||
@Singleton
|
@Singleton
|
||||||
AndroidWakeLockFactory provideWakeLockFactory(
|
AndroidWakeLockManager provideWakeLockManager(
|
||||||
AndroidWakeLockFactoryImpl wakeLockFactory) {
|
AndroidWakeLockManagerImpl wakeLockManager) {
|
||||||
return wakeLockFactory;
|
return wakeLockManager;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ 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.AndroidWakeLock;
|
||||||
import org.briarproject.bramble.api.system.AndroidWakeLockFactory;
|
import org.briarproject.bramble.api.system.AndroidWakeLockManager;
|
||||||
import org.briarproject.bramble.api.system.TaskScheduler;
|
import org.briarproject.bramble.api.system.TaskScheduler;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@@ -53,7 +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 AndroidWakeLockManager wakeLockManager;
|
||||||
private final ScheduledExecutorService scheduledExecutorService;
|
private final ScheduledExecutorService scheduledExecutorService;
|
||||||
private final AlarmManager alarmManager;
|
private final AlarmManager alarmManager;
|
||||||
|
|
||||||
@@ -62,10 +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,
|
AndroidWakeLockManager wakeLockManager,
|
||||||
ScheduledExecutorService scheduledExecutorService) {
|
ScheduledExecutorService scheduledExecutorService) {
|
||||||
this.app = app;
|
this.app = app;
|
||||||
this.wakeLockFactory = wakeLockFactory;
|
this.wakeLockManager = wakeLockManager;
|
||||||
this.scheduledExecutorService = scheduledExecutorService;
|
this.scheduledExecutorService = scheduledExecutorService;
|
||||||
alarmManager = (AlarmManager)
|
alarmManager = (AlarmManager)
|
||||||
requireNonNull(app.getSystemService(ALARM_SERVICE));
|
requireNonNull(app.getSystemService(ALARM_SERVICE));
|
||||||
@@ -73,7 +73,8 @@ class AndroidTaskScheduler implements TaskScheduler, Service, AlarmListener {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void startService() {
|
public void startService() {
|
||||||
scheduledExecutorService.scheduleAtFixedRate(this::runDueTasks,
|
scheduledExecutorService.scheduleAtFixedRate(
|
||||||
|
() -> wakeLockManager.runWakefully(this::runDueTasks),
|
||||||
TICK_MS, TICK_MS, MILLISECONDS);
|
TICK_MS, TICK_MS, MILLISECONDS);
|
||||||
scheduleAlarm();
|
scheduleAlarm();
|
||||||
}
|
}
|
||||||
@@ -112,21 +113,23 @@ class AndroidTaskScheduler implements TaskScheduler, Service, AlarmListener {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onAlarm(Intent intent) {
|
public void onAlarm(Intent intent) {
|
||||||
int extraPid = intent.getIntExtra(EXTRA_PID, -1);
|
wakeLockManager.runWakefully(() -> {
|
||||||
int currentPid = Process.myPid();
|
int extraPid = intent.getIntExtra(EXTRA_PID, -1);
|
||||||
if (extraPid == currentPid) {
|
int currentPid = Process.myPid();
|
||||||
LOG.info("Alarm");
|
if (extraPid == currentPid) {
|
||||||
rescheduleAlarm();
|
LOG.info("Alarm");
|
||||||
runDueTasks();
|
rescheduleAlarm();
|
||||||
} else {
|
runDueTasks();
|
||||||
LOG.info("Ignoring alarm with PID " + extraPid
|
} else {
|
||||||
+ ", current PID is " + currentPid);
|
LOG.info("Ignoring alarm with PID " + extraPid
|
||||||
}
|
+ ", current PID is " + currentPid);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private Runnable createWakefulTask(Runnable task, Executor executor) {
|
private Runnable createWakefulTask(Runnable task, Executor executor) {
|
||||||
// Hold a wake lock from before we submit the task until after it runs
|
// Hold a wake lock from before we submit the task until after it runs
|
||||||
AndroidWakeLock wakeLock = wakeLockFactory.createWakeLock();
|
AndroidWakeLock wakeLock = wakeLockManager.createWakeLock();
|
||||||
return () -> {
|
return () -> {
|
||||||
wakeLock.acquire();
|
wakeLock.acquire();
|
||||||
executor.execute(() -> {
|
executor.execute(() -> {
|
||||||
|
|||||||
@@ -4,7 +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.AndroidWakeLockManager;
|
||||||
import org.briarproject.bramble.api.system.TaskScheduler;
|
import org.briarproject.bramble.api.system.TaskScheduler;
|
||||||
|
|
||||||
import java.util.concurrent.RejectedExecutionHandler;
|
import java.util.concurrent.RejectedExecutionHandler;
|
||||||
@@ -38,10 +38,10 @@ public class AndroidTaskSchedulerModule {
|
|||||||
@Singleton
|
@Singleton
|
||||||
AndroidTaskScheduler provideAndroidTaskScheduler(
|
AndroidTaskScheduler provideAndroidTaskScheduler(
|
||||||
LifecycleManager lifecycleManager, Application app,
|
LifecycleManager lifecycleManager, Application app,
|
||||||
AndroidWakeLockFactory wakeLockFactory) {
|
AndroidWakeLockManager wakeLockManager) {
|
||||||
lifecycleManager.registerForShutdown(scheduledExecutorService);
|
lifecycleManager.registerForShutdown(scheduledExecutorService);
|
||||||
AndroidTaskScheduler scheduler = new AndroidTaskScheduler(app,
|
AndroidTaskScheduler scheduler = new AndroidTaskScheduler(app,
|
||||||
wakeLockFactory, scheduledExecutorService);
|
wakeLockManager, scheduledExecutorService);
|
||||||
lifecycleManager.registerService(scheduler);
|
lifecycleManager.registerService(scheduler);
|
||||||
return scheduler;
|
return scheduler;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import android.os.PowerManager;
|
|||||||
import org.briarproject.bramble.api.event.EventExecutor;
|
import org.briarproject.bramble.api.event.EventExecutor;
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
import org.briarproject.bramble.api.system.AndroidWakeLock;
|
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 org.briarproject.bramble.api.system.TaskScheduler;
|
||||||
|
|
||||||
import java.util.concurrent.Executor;
|
import java.util.concurrent.Executor;
|
||||||
@@ -25,7 +25,7 @@ import static org.briarproject.bramble.api.nullsafety.NullSafety.requireNonNull;
|
|||||||
|
|
||||||
@Immutable
|
@Immutable
|
||||||
@NotNullByDefault
|
@NotNullByDefault
|
||||||
class AndroidWakeLockFactoryImpl implements AndroidWakeLockFactory {
|
class AndroidWakeLockManagerImpl implements AndroidWakeLockManager {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* How often to replace the wake lock.
|
* How often to replace the wake lock.
|
||||||
@@ -41,7 +41,7 @@ class AndroidWakeLockFactoryImpl implements AndroidWakeLockFactory {
|
|||||||
private final SharedWakeLock sharedWakeLock;
|
private final SharedWakeLock sharedWakeLock;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
AndroidWakeLockFactoryImpl(TaskScheduler scheduler,
|
AndroidWakeLockManagerImpl(TaskScheduler scheduler,
|
||||||
@EventExecutor Executor eventExecutor,
|
@EventExecutor Executor eventExecutor,
|
||||||
Application app) {
|
Application app) {
|
||||||
PowerManager powerManager = (PowerManager)
|
PowerManager powerManager = (PowerManager)
|
||||||
@@ -57,6 +57,17 @@ class AndroidWakeLockFactoryImpl implements AndroidWakeLockFactory {
|
|||||||
return new AndroidWakeLockImpl(sharedWakeLock);
|
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) {
|
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)) {
|
||||||
@@ -28,7 +28,7 @@ import org.briarproject.bramble.api.plugin.duplex.DuplexPluginFactory;
|
|||||||
import org.briarproject.bramble.api.plugin.simplex.SimplexPluginFactory;
|
import org.briarproject.bramble.api.plugin.simplex.SimplexPluginFactory;
|
||||||
import org.briarproject.bramble.api.reporting.DevConfig;
|
import org.briarproject.bramble.api.reporting.DevConfig;
|
||||||
import org.briarproject.bramble.api.system.AndroidExecutor;
|
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 org.briarproject.bramble.api.system.Clock;
|
||||||
import org.briarproject.bramble.api.system.LocationUtils;
|
import org.briarproject.bramble.api.system.LocationUtils;
|
||||||
import org.briarproject.bramble.api.system.ResourceProvider;
|
import org.briarproject.bramble.api.system.ResourceProvider;
|
||||||
@@ -135,17 +135,17 @@ public class AppModule {
|
|||||||
ResourceProvider resourceProvider,
|
ResourceProvider resourceProvider,
|
||||||
CircumventionProvider circumventionProvider,
|
CircumventionProvider circumventionProvider,
|
||||||
BatteryManager batteryManager,
|
BatteryManager batteryManager,
|
||||||
AndroidWakeLockFactory wakeLockFactory,
|
AndroidWakeLockManager wakeLockManager,
|
||||||
Clock clock,
|
Clock clock,
|
||||||
TimeoutMonitor timeoutMonitor) {
|
TimeoutMonitor timeoutMonitor) {
|
||||||
Context appContext = app.getApplicationContext();
|
Context appContext = app.getApplicationContext();
|
||||||
DuplexPluginFactory bluetooth = new AndroidBluetoothPluginFactory(
|
DuplexPluginFactory bluetooth = new AndroidBluetoothPluginFactory(
|
||||||
ioExecutor, androidExecutor, wakeLockFactory, appContext,
|
ioExecutor, androidExecutor, wakeLockManager, appContext,
|
||||||
random, eventBus, clock, timeoutMonitor, backoffFactory);
|
random, eventBus, clock, timeoutMonitor, backoffFactory);
|
||||||
DuplexPluginFactory tor = new AndroidTorPluginFactory(ioExecutor,
|
DuplexPluginFactory tor = new AndroidTorPluginFactory(ioExecutor,
|
||||||
appContext, networkManager, locationUtils, eventBus,
|
appContext, networkManager, locationUtils, eventBus,
|
||||||
torSocketFactory, backoffFactory, resourceProvider,
|
torSocketFactory, backoffFactory, resourceProvider,
|
||||||
circumventionProvider, batteryManager, wakeLockFactory, clock);
|
circumventionProvider, batteryManager, wakeLockManager, clock);
|
||||||
DuplexPluginFactory lan = new AndroidLanTcpPluginFactory(ioExecutor,
|
DuplexPluginFactory lan = new AndroidLanTcpPluginFactory(ioExecutor,
|
||||||
eventBus, backoffFactory, appContext);
|
eventBus, backoffFactory, appContext);
|
||||||
Collection<DuplexPluginFactory> duplex = asList(bluetooth, tor, lan);
|
Collection<DuplexPluginFactory> duplex = asList(bluetooth, tor, lan);
|
||||||
|
|||||||
Reference in New Issue
Block a user