Refactor wake lock to use existing ScheduledExecutorService.

This commit is contained in:
akwizgran
2017-10-11 15:39:22 +01:00
parent 98a0d09899
commit c089a099f0
3 changed files with 95 additions and 68 deletions

View File

@@ -36,7 +36,7 @@ import org.briarproject.bramble.api.settings.Settings;
import org.briarproject.bramble.api.settings.event.SettingsUpdatedEvent;
import org.briarproject.bramble.api.system.LocationUtils;
import org.briarproject.bramble.util.IoUtils;
import org.briarproject.bramble.util.ScheduledExecutorServiceWakeLock;
import org.briarproject.bramble.util.RenewableWakeLock;
import org.briarproject.bramble.util.StringUtils;
import java.io.Closeable;
@@ -120,12 +120,10 @@ class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
private final ConnectionStatus connectionStatus;
private final File torDirectory, torFile, geoIpFile, configFile;
private final File doneFile, cookieFile;
private final RenewableWakeLock wakeLock;
private final AtomicReference<Future<?>> connectivityCheck =
new AtomicReference<>();
private final AtomicBoolean used = new AtomicBoolean(false);
private final ScheduledExecutorServiceWakeLock scheduledExecutorServiceWakeLock;
private PowerManager.WakeLock wakeLock;
private volatile boolean running = false;
private volatile ServerSocket socket = null;
@@ -161,24 +159,11 @@ class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
// Don't execute more than one connection status check at a time
connectionStatusExecutor = new PoliteExecutor("TorPlugin",
ioExecutor, 1);
scheduledExecutorServiceWakeLock =
new ScheduledExecutorServiceWakeLock(appContext);
scheduledExecutorServiceWakeLock.setRunnable((Runnable) () -> {
LOG.info("Renewing wake lock");
wakeLock.release();
aquireWakeLock();
});
aquireWakeLock();
}
private void aquireWakeLock(){
LOG.info("Aquiring wake lock");
PowerManager pm = (PowerManager)
appContext.getSystemService(POWER_SERVICE);
// This tag will prevent Huawei's powermanager from killing us.
wakeLock = pm.newWakeLock(PARTIAL_WAKE_LOCK, "LocationManagerService");
wakeLock.setReferenceCounted(false);
scheduledExecutorServiceWakeLock.setAlarm(1800000, MILLISECONDS);
// This tag will prevent Huawei's power manager from killing us
wakeLock = new RenewableWakeLock(pm, scheduler, PARTIAL_WAKE_LOCK,
"LocationManagerService", 30, MINUTES);
}
@Override
@@ -530,7 +515,6 @@ class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
}
}
wakeLock.release();
scheduledExecutorServiceWakeLock.cancelAlarm();
}
@Override