mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-20 14:49:53 +01:00
Renew the wake lock every 30min
Signed-off-by: goapunk <noobie@goapunks.net>
This commit is contained in:
@@ -36,6 +36,7 @@ import org.briarproject.bramble.api.settings.Settings;
|
|||||||
import org.briarproject.bramble.api.settings.event.SettingsUpdatedEvent;
|
import org.briarproject.bramble.api.settings.event.SettingsUpdatedEvent;
|
||||||
import org.briarproject.bramble.api.system.LocationUtils;
|
import org.briarproject.bramble.api.system.LocationUtils;
|
||||||
import org.briarproject.bramble.util.IoUtils;
|
import org.briarproject.bramble.util.IoUtils;
|
||||||
|
import org.briarproject.bramble.util.ScheduledExecutorServiceWakeLock;
|
||||||
import org.briarproject.bramble.util.StringUtils;
|
import org.briarproject.bramble.util.StringUtils;
|
||||||
|
|
||||||
import java.io.Closeable;
|
import java.io.Closeable;
|
||||||
@@ -119,10 +120,12 @@ class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
|
|||||||
private final ConnectionStatus connectionStatus;
|
private final ConnectionStatus connectionStatus;
|
||||||
private final File torDirectory, torFile, geoIpFile, configFile;
|
private final File torDirectory, torFile, geoIpFile, configFile;
|
||||||
private final File doneFile, cookieFile;
|
private final File doneFile, cookieFile;
|
||||||
private final PowerManager.WakeLock wakeLock;
|
|
||||||
private final AtomicReference<Future<?>> connectivityCheck =
|
private final AtomicReference<Future<?>> connectivityCheck =
|
||||||
new AtomicReference<>();
|
new AtomicReference<>();
|
||||||
private final AtomicBoolean used = new AtomicBoolean(false);
|
private final AtomicBoolean used = new AtomicBoolean(false);
|
||||||
|
private final ScheduledExecutorServiceWakeLock scheduledExecutorServiceWakeLock;
|
||||||
|
|
||||||
|
private PowerManager.WakeLock wakeLock;
|
||||||
|
|
||||||
private volatile boolean running = false;
|
private volatile boolean running = false;
|
||||||
private volatile ServerSocket socket = null;
|
private volatile ServerSocket socket = null;
|
||||||
@@ -155,14 +158,27 @@ class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
|
|||||||
configFile = new File(torDirectory, "torrc");
|
configFile = new File(torDirectory, "torrc");
|
||||||
doneFile = new File(torDirectory, "done");
|
doneFile = new File(torDirectory, "done");
|
||||||
cookieFile = new File(torDirectory, ".tor/control_auth_cookie");
|
cookieFile = new File(torDirectory, ".tor/control_auth_cookie");
|
||||||
Object o = appContext.getSystemService(POWER_SERVICE);
|
|
||||||
PowerManager pm = (PowerManager) o;
|
|
||||||
// This tag will prevent Huawei's powermanager from killing us.
|
|
||||||
wakeLock = pm.newWakeLock(PARTIAL_WAKE_LOCK, "LocationManagerService");
|
|
||||||
wakeLock.setReferenceCounted(false);
|
|
||||||
// Don't execute more than one connection status check at a time
|
// Don't execute more than one connection status check at a time
|
||||||
connectionStatusExecutor = new PoliteExecutor("TorPlugin",
|
connectionStatusExecutor = new PoliteExecutor("TorPlugin",
|
||||||
ioExecutor, 1);
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -514,6 +530,7 @@ class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
wakeLock.release();
|
wakeLock.release();
|
||||||
|
scheduledExecutorServiceWakeLock.cancelAlarm();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -0,0 +1,47 @@
|
|||||||
|
package org.briarproject.bramble.util;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
|
import java.util.concurrent.ThreadFactory;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
public class ScheduledExecutorServiceWakeLock {
|
||||||
|
|
||||||
|
final Context appContext;
|
||||||
|
|
||||||
|
private static final ThreadFactory THREAD_FACTORY = new ThreadFactory() {
|
||||||
|
@Override
|
||||||
|
public Thread newThread(Runnable r) {
|
||||||
|
Thread t = new Thread(r);
|
||||||
|
t.setDaemon(true);
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
private ScheduledExecutorService scheduledExecutorService = null; // Locking: this
|
||||||
|
private Runnable runnable;
|
||||||
|
|
||||||
|
public ScheduledExecutorServiceWakeLock(Context appContext) {
|
||||||
|
this.appContext = appContext;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRunnable(Runnable r){
|
||||||
|
runnable = r;
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized void setAlarm(long delay, TimeUnit unit) {
|
||||||
|
if(runnable == null)
|
||||||
|
return;
|
||||||
|
if (scheduledExecutorService == null)
|
||||||
|
scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(THREAD_FACTORY);
|
||||||
|
scheduledExecutorService.schedule(runnable, delay, unit);
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized void cancelAlarm() {
|
||||||
|
if (scheduledExecutorService == null) throw new IllegalStateException();
|
||||||
|
scheduledExecutorService.shutdownNow();
|
||||||
|
scheduledExecutorService = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user