Hold a wake lock during app startup and shutdown.

This commit is contained in:
akwizgran
2020-08-10 16:50:40 +01:00
parent 452c3afbb3
commit 1e2dc862ef

View File

@@ -19,12 +19,15 @@ import org.briarproject.bramble.api.crypto.SecretKey;
import org.briarproject.bramble.api.lifecycle.LifecycleManager; import org.briarproject.bramble.api.lifecycle.LifecycleManager;
import org.briarproject.bramble.api.lifecycle.LifecycleManager.StartResult; import org.briarproject.bramble.api.lifecycle.LifecycleManager.StartResult;
import org.briarproject.bramble.api.system.AndroidExecutor; import org.briarproject.bramble.api.system.AndroidExecutor;
import org.briarproject.bramble.api.system.AndroidWakeLockManager;
import org.briarproject.bramble.api.system.WakefulIoExecutor;
import org.briarproject.briar.R; import org.briarproject.briar.R;
import org.briarproject.briar.android.logout.HideUiActivity; import org.briarproject.briar.android.logout.HideUiActivity;
import org.briarproject.briar.api.android.AndroidNotificationManager; import org.briarproject.briar.api.android.AndroidNotificationManager;
import org.briarproject.briar.api.android.LockManager; import org.briarproject.briar.api.android.LockManager;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Logger; import java.util.logging.Logger;
@@ -48,6 +51,7 @@ import static java.util.logging.Level.INFO;
import static java.util.logging.Level.WARNING; import static java.util.logging.Level.WARNING;
import static org.briarproject.bramble.api.lifecycle.LifecycleManager.StartResult.ALREADY_RUNNING; import static org.briarproject.bramble.api.lifecycle.LifecycleManager.StartResult.ALREADY_RUNNING;
import static org.briarproject.bramble.api.lifecycle.LifecycleManager.StartResult.SUCCESS; import static org.briarproject.bramble.api.lifecycle.LifecycleManager.StartResult.SUCCESS;
import static org.briarproject.bramble.api.nullsafety.NullSafety.requireNonNull;
import static org.briarproject.briar.android.BriarApplication.ENTRY_ACTIVITY; import static org.briarproject.briar.android.BriarApplication.ENTRY_ACTIVITY;
import static org.briarproject.briar.api.android.AndroidNotificationManager.FAILURE_CHANNEL_ID; import static org.briarproject.briar.api.android.AndroidNotificationManager.FAILURE_CHANNEL_ID;
import static org.briarproject.briar.api.android.AndroidNotificationManager.FAILURE_NOTIFICATION_ID; import static org.briarproject.briar.api.android.AndroidNotificationManager.FAILURE_NOTIFICATION_ID;
@@ -80,6 +84,11 @@ public class BriarService extends Service {
AccountManager accountManager; AccountManager accountManager;
@Inject @Inject
LockManager lockManager; LockManager lockManager;
@Inject
AndroidWakeLockManager wakeLockManager;
@Inject
@WakefulIoExecutor
Executor wakefulIoExecutor;
// Fields that are accessed from background threads must be volatile // Fields that are accessed from background threads must be volatile
@Inject @Inject
@@ -108,10 +117,12 @@ public class BriarService extends Service {
return; return;
} }
// Hold a wake lock during startup
wakeLockManager.runWakefully(() -> {
// Create notification channels // Create notification channels
if (SDK_INT >= 26) { if (SDK_INT >= 26) {
NotificationManager nm = (NotificationManager) NotificationManager nm = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE); requireNonNull(getSystemService(NOTIFICATION_SERVICE));
NotificationChannel ongoingChannel = new NotificationChannel( NotificationChannel ongoingChannel = new NotificationChannel(
ONGOING_CHANNEL_ID, ONGOING_CHANNEL_ID,
getString(R.string.ongoing_notification_title), getString(R.string.ongoing_notification_title),
@@ -129,7 +140,7 @@ public class BriarService extends Service {
notificationManager.getForegroundNotification(); notificationManager.getForegroundNotification();
startForeground(ONGOING_NOTIFICATION_ID, foregroundNotification); startForeground(ONGOING_NOTIFICATION_ID, foregroundNotification);
// Start the services in a background thread // Start the services in a background thread
new Thread(() -> { wakefulIoExecutor.execute(() -> {
StartResult result = lifecycleManager.startServices(dbKey); StartResult result = lifecycleManager.startServices(dbKey);
if (result == SUCCESS) { if (result == SUCCESS) {
started = true; started = true;
@@ -142,7 +153,7 @@ public class BriarService extends Service {
showStartupFailureNotification(result); showStartupFailureNotification(result);
stopSelf(); stopSelf();
} }
}).start(); });
// Register for device shutdown broadcasts // Register for device shutdown broadcasts
receiver = new BroadcastReceiver() { receiver = new BroadcastReceiver() {
@Override @Override
@@ -156,6 +167,7 @@ public class BriarService extends Service {
filter.addAction("android.intent.action.QUICKBOOT_POWEROFF"); filter.addAction("android.intent.action.QUICKBOOT_POWEROFF");
filter.addAction("com.htc.intent.action.QUICKBOOT_POWEROFF"); filter.addAction("com.htc.intent.action.QUICKBOOT_POWEROFF");
registerReceiver(receiver, filter); registerReceiver(receiver, filter);
}, "LifecycleStartup");
} }
@Override @Override
@@ -179,8 +191,8 @@ public class BriarService extends Service {
i.putExtra(EXTRA_NOTIFICATION_ID, FAILURE_NOTIFICATION_ID); i.putExtra(EXTRA_NOTIFICATION_ID, FAILURE_NOTIFICATION_ID);
b.setContentIntent(PendingIntent.getActivity(BriarService.this, b.setContentIntent(PendingIntent.getActivity(BriarService.this,
0, i, FLAG_UPDATE_CURRENT)); 0, i, FLAG_UPDATE_CURRENT));
Object o = getSystemService(NOTIFICATION_SERVICE); NotificationManager nm = (NotificationManager)
NotificationManager nm = (NotificationManager) o; requireNonNull(getSystemService(NOTIFICATION_SERVICE));
nm.notify(FAILURE_NOTIFICATION_ID, b.build()); nm.notify(FAILURE_NOTIFICATION_ID, b.build());
// Bring the dashboard to the front to clear the back stack // Bring the dashboard to the front to clear the back stack
i = new Intent(BriarService.this, ENTRY_ACTIVITY); i = new Intent(BriarService.this, ENTRY_ACTIVITY);
@@ -205,14 +217,17 @@ public class BriarService extends Service {
@Override @Override
public void onDestroy() { public void onDestroy() {
// Hold a wake lock during shutdown
wakeLockManager.runWakefully(() -> {
super.onDestroy(); super.onDestroy();
LOG.info("Destroyed"); LOG.info("Destroyed");
stopForeground(true); stopForeground(true);
if (receiver != null) unregisterReceiver(receiver); if (receiver != null) unregisterReceiver(receiver);
// Stop the services in a background thread // Stop the services in a background thread
new Thread(() -> { wakefulIoExecutor.execute(() -> {
if (started) lifecycleManager.stopServices(); if (started) lifecycleManager.stopServices();
}).start(); });
}, "LifecycleShutdown");
} }
@Override @Override
@@ -257,12 +272,14 @@ public class BriarService extends Service {
} }
private void shutdownFromBackground() { private void shutdownFromBackground() {
// Hold a wake lock during shutdown
wakeLockManager.runWakefully(() -> {
// Stop the service // Stop the service
stopSelf(); stopSelf();
// Hide the UI // Hide the UI
hideUi(); hideUi();
// Wait for shutdown to complete, then exit // Wait for shutdown to complete, then exit
new Thread(() -> { wakefulIoExecutor.execute(() -> {
try { try {
if (started) lifecycleManager.waitForShutdown(); if (started) lifecycleManager.waitForShutdown();
} catch (InterruptedException e) { } catch (InterruptedException e) {
@@ -270,7 +287,8 @@ public class BriarService extends Service {
} }
LOG.info("Exiting"); LOG.info("Exiting");
System.exit(0); System.exit(0);
}).start(); });
}, "BackgroundShutdown");
} }
/** /**