From 413b04682f21b1efb60b21cdf6d2664e554c924c Mon Sep 17 00:00:00 2001
From: akwizgran <akwizgran@users.sourceforge.net>
Date: Mon, 6 Jan 2014 10:17:06 +0000
Subject: [PATCH] Don't allow BriarService to restart automatically without a
 DB key.

Fixes bug #7. The service is restarted after a crash even if
onStartCommand() returns START_NOT_STICKY, but after restarting the DB
key is not restored and so the app can't usefully proceed.
---
 .../src/net/sf/briar/android/BriarService.java       | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/briar-android/src/net/sf/briar/android/BriarService.java b/briar-android/src/net/sf/briar/android/BriarService.java
index 65e29f24b5..13d54fbc2e 100644
--- a/briar-android/src/net/sf/briar/android/BriarService.java
+++ b/briar-android/src/net/sf/briar/android/BriarService.java
@@ -11,6 +11,7 @@ import javax.inject.Inject;
 
 import net.sf.briar.R;
 import net.sf.briar.api.android.AndroidExecutor;
+import net.sf.briar.api.db.DatabaseConfig;
 import net.sf.briar.api.lifecycle.LifecycleManager;
 import roboguice.service.RoboService;
 import android.app.PendingIntent;
@@ -28,6 +29,9 @@ public class BriarService extends RoboService {
 
 	private final Binder binder = new BriarBinder();
 
+	@Inject private DatabaseConfig databaseConfig;
+	private boolean started = false;
+
 	// Fields that are accessed from background threads must be volatile
 	@Inject private volatile LifecycleManager lifecycleManager;
 	@Inject private volatile AndroidExecutor androidExecutor;
@@ -36,6 +40,11 @@ public class BriarService extends RoboService {
 	public void onCreate() {
 		super.onCreate();
 		if(LOG.isLoggable(INFO)) LOG.info("Created");
+		if(databaseConfig.getEncryptionKey() == null) {
+			if(LOG.isLoggable(INFO)) LOG.info("No database key");
+			stopSelf();
+			return;
+		}
 		// Show an ongoing notification that the service is running
 		NotificationCompat.Builder b = new NotificationCompat.Builder(this);
 		b.setSmallIcon(R.drawable.notification_icon);
@@ -56,6 +65,7 @@ public class BriarService extends RoboService {
 				lifecycleManager.startServices();
 			}
 		}.start();
+		started = true;
 	}
 
 	@Override
@@ -74,7 +84,7 @@ public class BriarService extends RoboService {
 		super.onDestroy();
 		if(LOG.isLoggable(INFO)) LOG.info("Destroyed");
 		// Stop the services in a background thread
-		new Thread() {
+		if(started) new Thread() {
 			@Override
 			public void run() {
 				androidExecutor.shutdown();
-- 
GitLab