Attempt to acquire wakelock when alarm goes off (DOES NOT WORK)

This commit is contained in:
Shannon Stork
2018-07-25 19:20:54 -04:00
parent afa9b6193a
commit f77bde4edf
3 changed files with 73 additions and 6 deletions

View File

@@ -126,7 +126,7 @@ 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 RenewableWakeLock wakeLock;
private final AtomicReference<Future<?>> connectivityCheck =
new AtomicReference<>();
private final AtomicBoolean used = new AtomicBoolean(false);
@@ -169,8 +169,8 @@ class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
ioExecutor, 1);
PowerManager pm = (PowerManager)
appContext.getSystemService(POWER_SERVICE);
wakeLock = new RenewableWakeLock(pm, scheduler, PARTIAL_WAKE_LOCK,
WAKE_LOCK_TAG, 1, MINUTES);
//wakeLock = new RenewableWakeLock(pm, scheduler, PARTIAL_WAKE_LOCK,
// WAKE_LOCK_TAG, 1, MINUTES);
}
@Override
@@ -493,12 +493,12 @@ class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
private void enableNetwork(boolean enable) throws IOException {
if (!running) return;
if (enable) wakeLock.acquire();
//if (enable) wakeLock.acquire();
connectionStatus.enableNetwork(enable);
controlConnection.setConf("DisableNetwork", enable ? "0" : "1");
if (!enable) {
callback.transportDisabled();
wakeLock.release();
// wakeLock.release();
}
}
@@ -529,7 +529,7 @@ class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
logException(LOG, WARNING, e);
}
}
wakeLock.release();
//wakeLock.release();
}
@Override

View File

@@ -2,6 +2,7 @@ package org.briarproject.briar.android;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.app.AlarmManager;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
@@ -13,9 +14,13 @@ import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.Binder;
import android.os.Build;
import android.os.IBinder;
import android.os.PowerManager.WakeLock;
import android.os.PowerManager;
import android.support.v4.app.NotificationCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import org.briarproject.bramble.api.db.DatabaseConfig;
import org.briarproject.bramble.api.lifecycle.LifecycleManager;
@@ -25,8 +30,11 @@ import org.briarproject.briar.R;
import org.briarproject.briar.android.logout.HideUiActivity;
import org.briarproject.briar.android.navdrawer.NavDrawerActivity;
import java.util.Calendar;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
import javax.annotation.Nullable;
@@ -35,6 +43,7 @@ import javax.inject.Inject;
import static android.app.ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND;
import static android.app.NotificationManager.IMPORTANCE_DEFAULT;
import static android.app.NotificationManager.IMPORTANCE_NONE;
import static android.app.PendingIntent.FLAG_CANCEL_CURRENT;
import static android.app.PendingIntent.FLAG_UPDATE_CURRENT;
import static android.content.Intent.ACTION_SHUTDOWN;
import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TASK;
@@ -46,6 +55,7 @@ import static android.os.Build.VERSION.SDK_INT;
import static android.support.v4.app.NotificationCompat.CATEGORY_SERVICE;
import static android.support.v4.app.NotificationCompat.PRIORITY_MIN;
import static android.support.v4.app.NotificationCompat.VISIBILITY_SECRET;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.logging.Level.INFO;
import static java.util.logging.Level.WARNING;
import static org.briarproject.bramble.api.lifecycle.LifecycleManager.StartResult.ALREADY_RUNNING;
@@ -55,6 +65,7 @@ import static org.briarproject.briar.api.android.AndroidNotificationManager.FAIL
import static org.briarproject.briar.api.android.AndroidNotificationManager.ONGOING_CHANNEL_ID;
import static org.briarproject.briar.api.android.AndroidNotificationManager.ONGOING_NOTIFICATION_ID;
import static org.briarproject.briar.api.android.AndroidNotificationManager.REMINDER_NOTIFICATION_ID;
import static org.briarproject.briar.android.TestingConstants.ACTION_ALARM;
public class BriarService extends Service {
@@ -65,6 +76,10 @@ public class BriarService extends Service {
public static String EXTRA_STARTUP_FAILED =
"org.briarproject.briar.STARTUP_FAILED";
private WakeLock wakeLock;
private AlarmManager alarm;
private static final Logger LOG =
Logger.getLogger(BriarService.class.getName());
@@ -90,6 +105,10 @@ public class BriarService extends Service {
BriarApplication application = (BriarApplication) getApplication();
application.getApplicationComponent().inject(this);
alarm = (AlarmManager)getApplicationContext().getSystemService(Context.ALARM_SERVICE);
setAlarm();
LOG.info("Created");
if (created.getAndSet(true)) {
LOG.info("Already created");
@@ -165,11 +184,37 @@ public class BriarService extends Service {
};
IntentFilter filter = new IntentFilter();
filter.addAction(ACTION_SHUTDOWN);
filter.addAction(ACTION_ALARM);
filter.addAction("android.intent.action.QUICKBOOT_POWEROFF");
filter.addAction("com.htc.intent.action.QUICKBOOT_POWEROFF");
registerReceiver(receiver, filter);
}
private void setAlarm() {
PendingIntent pi = getPendingIntent();
long millis = getRealTimeMillis(5000, MILLISECONDS);
if (Build.VERSION.SDK_INT >= 23) alarm.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, millis, pi);
else if (Build.VERSION.SDK_INT >= 19) alarm.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, millis, pi);
else alarm.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, millis, pi);
Log.i("ALARM_TEST", "Alarm set");
}
long getRealTimeMillis(long delay, TimeUnit unit) {
return System.currentTimeMillis() + MILLISECONDS.convert(delay, unit);
}
PendingIntent getPendingIntent() {
return PendingIntent.getService(getApplicationContext(), 0, getAlarmIntent(), FLAG_CANCEL_CURRENT);
}
Intent getAlarmIntent() {
Intent i = new Intent(getApplicationContext(), BriarService.class);
i.setAction(ACTION_ALARM);
String action = i.getAction();
Log.i("ALARM_TEST", "Action set to "+action);
return i;
}
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(Localizer.getInstance().setLocale(base));
@@ -204,6 +249,25 @@ public class BriarService extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
String action = intent.getAction();
Log.i("ALARM_TEST", "Action = "+action);
if (ACTION_ALARM.equals(intent.getAction())) {
//acquire wakelock
PowerManager powerManager = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "briar:TestWakeLock");
wakeLock.acquire();
//do work
Log.i("ALARM_TEST", "WakeLock acquired");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
}
//release wakelock
wakeLock.release();
//repeat
setAlarm();
}
return START_NOT_STICKY; // Don't restart automatically if killed
}

View File

@@ -40,4 +40,7 @@ public interface TestingConstants {
* Feature flag for enabling the sign-in reminder in release builds.
*/
boolean FEATURE_FLAG_SIGN_IN_REMINDER = IS_DEBUG_BUILD;
String ACTION_ALARM = "org.briarproject.briar.android.ACTION_ALARM";
}