From 698ca41720ea80b8b5904fd75193a806e0a68d74 Mon Sep 17 00:00:00 2001
From: Ernir Erlingsson <ernir@ymirmobile.com>
Date: Wed, 11 May 2016 23:50:11 +0200
Subject: [PATCH] Fixed the controllers and am now saving the current fragment

---
 .../briarproject/android/ActivityModule.java  |  9 ++++
 .../briarproject/android/BaseActivity.java    |  2 +
 .../briarproject/android/BriarActivity.java   |  6 ++-
 .../android/BriarApplicationImpl.java         |  3 +-
 .../android/NavDrawerActivity.java            | 29 ++++++++++---
 .../android/controller/BriarController.java   |  2 -
 .../controller/BriarControllerImpl.java       | 29 ++-----------
 .../android/controller/DBController.java      |  7 +++
 .../android/controller/DBControllerImpl.java  | 43 +++++++++++++++++++
 .../controller/NavDrawerController.java       |  2 +-
 .../controller/NavDrawerControllerImpl.java   | 12 ++++--
 11 files changed, 103 insertions(+), 41 deletions(-)
 create mode 100644 briar-android/src/org/briarproject/android/controller/DBController.java
 create mode 100644 briar-android/src/org/briarproject/android/controller/DBControllerImpl.java

diff --git a/briar-android/src/org/briarproject/android/ActivityModule.java b/briar-android/src/org/briarproject/android/ActivityModule.java
index b42007acb1..47d8d13418 100644
--- a/briar-android/src/org/briarproject/android/ActivityModule.java
+++ b/briar-android/src/org/briarproject/android/ActivityModule.java
@@ -10,6 +10,8 @@ import org.briarproject.android.controller.BriarController;
 import org.briarproject.android.controller.BriarControllerImpl;
 import org.briarproject.android.controller.ConfigController;
 import org.briarproject.android.controller.ConfigControllerImpl;
+import org.briarproject.android.controller.DBController;
+import org.briarproject.android.controller.DBControllerImpl;
 import org.briarproject.android.controller.NavDrawerController;
 import org.briarproject.android.controller.NavDrawerControllerImpl;
 import org.briarproject.android.controller.PasswordController;
@@ -89,6 +91,13 @@ public class ActivityModule {
 		return briarControllerImpl;
 	}
 
+	@ActivityScope
+	@Provides
+	protected DBController provideDBController(
+			DBControllerImpl dbController) {
+		return dbController;
+	}
+
 	@ActivityScope
 	@Provides
 	protected NavDrawerController provideNavDrawerController(
diff --git a/briar-android/src/org/briarproject/android/BaseActivity.java b/briar-android/src/org/briarproject/android/BaseActivity.java
index 38063e239e..792cca7a3f 100644
--- a/briar-android/src/org/briarproject/android/BaseActivity.java
+++ b/briar-android/src/org/briarproject/android/BaseActivity.java
@@ -3,6 +3,7 @@ package org.briarproject.android;
 import android.os.Bundle;
 import android.os.IBinder;
 import android.support.v7.app.AppCompatActivity;
+import android.util.Log;
 import android.view.View;
 import android.view.inputmethod.InputMethodManager;
 
@@ -25,6 +26,7 @@ public abstract class BaseActivity extends AppCompatActivity {
 	public abstract void injectActivity(ActivityComponent component);
 
 	public void addLifecycleController(ActivityLifecycleController alc) {
+		Log.d(getClass().getSimpleName(), "addLifecycleController: " + lifecycleControllers.size());
 		lifecycleControllers.add(alc);
 	}
 
diff --git a/briar-android/src/org/briarproject/android/BriarActivity.java b/briar-android/src/org/briarproject/android/BriarActivity.java
index ed9c8dd87e..b175224da8 100644
--- a/briar-android/src/org/briarproject/android/BriarActivity.java
+++ b/briar-android/src/org/briarproject/android/BriarActivity.java
@@ -5,6 +5,7 @@ import android.content.Intent;
 import android.os.Build;
 
 import org.briarproject.android.controller.BriarController;
+import org.briarproject.android.controller.DBController;
 import org.briarproject.android.controller.handler.UiResultHandler;
 import org.briarproject.android.panic.ExitActivity;
 
@@ -33,6 +34,9 @@ public abstract class BriarActivity extends BaseActivity {
 
 	@Inject
 	protected BriarController briarController;
+	// TODO remove this when the deprecated method runOnDbThread is removed
+	@Inject
+	protected DBController dbController;
 
 	@Override
 	protected void onActivityResult(int request, int result, Intent data) {
@@ -87,7 +91,7 @@ public abstract class BriarActivity extends BaseActivity {
 
 	@Deprecated
 	public void runOnDbThread(Runnable task) {
-		briarController.runOnDbThread(task);
+		dbController.runOnDbThread(task);
 	}
 
 	@Deprecated
diff --git a/briar-android/src/org/briarproject/android/BriarApplicationImpl.java b/briar-android/src/org/briarproject/android/BriarApplicationImpl.java
index 4d88c65367..709fac7995 100644
--- a/briar-android/src/org/briarproject/android/BriarApplicationImpl.java
+++ b/briar-android/src/org/briarproject/android/BriarApplicationImpl.java
@@ -3,7 +3,6 @@ package org.briarproject.android;
 import android.app.Application;
 import android.content.Context;
 
-import org.acra.ACRA;
 import org.acra.ReportingInteractionMode;
 import org.acra.annotation.ReportsCrashes;
 import org.briarproject.CoreModule;
@@ -34,7 +33,7 @@ public class BriarApplicationImpl extends Application
 	@Override
 	protected void attachBaseContext(Context base) {
 		super.attachBaseContext(base);
-		ACRA.init(this);
+//		ACRA.init(this);
 	}
 
 	@Override
diff --git a/briar-android/src/org/briarproject/android/NavDrawerActivity.java b/briar-android/src/org/briarproject/android/NavDrawerActivity.java
index 56caa5c8fb..3db285f5a3 100644
--- a/briar-android/src/org/briarproject/android/NavDrawerActivity.java
+++ b/briar-android/src/org/briarproject/android/NavDrawerActivity.java
@@ -45,6 +45,8 @@ public class NavDrawerActivity extends BriarFragmentActivity implements
 	public static final String INTENT_CONTACTS = "intent_contacts";
 	public static final String INTENT_FORUMS = "intent_forums";
 
+	private static final String KEY_CURRENT_FRAGMENT_ID = "key_current_id";
+
 	private static final Logger LOG =
 			Logger.getLogger(NavDrawerActivity.class.getName());
 
@@ -60,6 +62,7 @@ public class NavDrawerActivity extends BriarFragmentActivity implements
 
 	private List<Transport> transports;
 	private BaseAdapter transportsAdapter;
+	private int currentFragmentId = R.id.nav_btn_contacts;
 
 	@Override
 	protected void onNewIntent(Intent intent) {
@@ -103,8 +106,13 @@ public class NavDrawerActivity extends BriarFragmentActivity implements
 				R.string.nav_drawer_open_description,
 				R.string.nav_drawer_close_description);
 		drawerLayout.setDrawerListener(drawerToggle);
-		if (state == null)
+		LOG.info("NavDrawerActivity created: " + (state == null));
+		if (state == null) {
 			startFragment(activityComponent.newContactListFragment());
+		} else {
+			currentFragmentId = state.getInt(KEY_CURRENT_FRAGMENT_ID);
+			loadCurrentFragment();
+		}
 		checkAuthorHandle(getIntent());
 
 		initializeTransports(getLayoutInflater());
@@ -113,6 +121,12 @@ public class NavDrawerActivity extends BriarFragmentActivity implements
 		welcomeMessageCheck();
 	}
 
+	@Override
+	public void onSaveInstanceState(Bundle outState) {
+		super.onSaveInstanceState(outState);
+		outState.putInt(KEY_CURRENT_FRAGMENT_ID, currentFragmentId);
+	}
+
 	private void welcomeMessageCheck() {
 		SharedPreferences prefs = getPreferences(Context.MODE_PRIVATE);
 		if (!prefs.getBoolean(PREF_SEEN_WELCOME_MESSAGE, false)) {
@@ -159,10 +173,8 @@ public class NavDrawerActivity extends BriarFragmentActivity implements
 		});
 	}
 
-	public void onNavigationClick(View view) {
-		drawerLayout.closeDrawer(START);
-		clearBackStack();
-		switch (view.getId()) {
+	private void loadCurrentFragment() {
+		switch (currentFragmentId) {
 			case R.id.nav_btn_contacts:
 				startFragment(activityComponent.newContactListFragment());
 				break;
@@ -178,6 +190,13 @@ public class NavDrawerActivity extends BriarFragmentActivity implements
 		}
 	}
 
+	public void onNavigationClick(View view) {
+		drawerLayout.closeDrawer(START);
+		clearBackStack();
+		currentFragmentId = view.getId();
+		loadCurrentFragment();
+	}
+
 
 	@Override
 	public void onBackPressed() {
diff --git a/briar-android/src/org/briarproject/android/controller/BriarController.java b/briar-android/src/org/briarproject/android/controller/BriarController.java
index e2253c3dc7..cbb61cde1e 100644
--- a/briar-android/src/org/briarproject/android/controller/BriarController.java
+++ b/briar-android/src/org/briarproject/android/controller/BriarController.java
@@ -4,8 +4,6 @@ import org.briarproject.android.controller.handler.ResultHandler;
 
 public interface BriarController extends ActivityLifecycleController {
 
-	void runOnDbThread(final Runnable task);
-
 	void startAndBindService();
 
 	boolean hasEncryptionKey();
diff --git a/briar-android/src/org/briarproject/android/controller/BriarControllerImpl.java b/briar-android/src/org/briarproject/android/controller/BriarControllerImpl.java
index 337e0436e3..1d1fbf8606 100644
--- a/briar-android/src/org/briarproject/android/controller/BriarControllerImpl.java
+++ b/briar-android/src/org/briarproject/android/controller/BriarControllerImpl.java
@@ -9,10 +9,7 @@ import org.briarproject.android.BriarService;
 import org.briarproject.android.BriarService.BriarServiceConnection;
 import org.briarproject.android.controller.handler.ResultHandler;
 import org.briarproject.api.db.DatabaseConfig;
-import org.briarproject.api.db.DatabaseExecutor;
-import org.briarproject.api.lifecycle.LifecycleManager;
 
-import java.util.concurrent.Executor;
 import java.util.logging.Logger;
 
 import javax.inject.Inject;
@@ -29,13 +26,6 @@ public class BriarControllerImpl implements BriarController {
 	@Inject
 	protected Activity activity;
 
-	// Fields that are accessed from background threads must be volatile
-	@Inject
-	@DatabaseExecutor
-	protected volatile Executor dbExecutor;
-	@Inject
-	protected volatile LifecycleManager lifecycleManager;
-
 	private boolean bound = false;
 
 	@Inject
@@ -70,6 +60,7 @@ public class BriarControllerImpl implements BriarController {
 		activity.startService(new Intent(activity, BriarService.class));
 		bound = activity.bindService(new Intent(activity, BriarService.class),
 				serviceConnection, 0);
+		LOG.info("Briar service started " + bound);
 	}
 
 	@Override
@@ -100,23 +91,9 @@ public class BriarControllerImpl implements BriarController {
 		}.start();
 	}
 
-	private void unbindService() {
+	protected void unbindService() {
+		LOG.info("Briar service unbind " + bound);
 		if (bound) activity.unbindService(serviceConnection);
 	}
 
-	@Override
-	public void runOnDbThread(final Runnable task) {
-		dbExecutor.execute(new Runnable() {
-			@Override
-			public void run() {
-				try {
-					lifecycleManager.waitForDatabase();
-					task.run();
-				} catch (InterruptedException e) {
-					LOG.warning("Interrupted while waiting for database");
-					Thread.currentThread().interrupt();
-				}
-			}
-		});
-	}
 }
diff --git a/briar-android/src/org/briarproject/android/controller/DBController.java b/briar-android/src/org/briarproject/android/controller/DBController.java
new file mode 100644
index 0000000000..1117080399
--- /dev/null
+++ b/briar-android/src/org/briarproject/android/controller/DBController.java
@@ -0,0 +1,7 @@
+package org.briarproject.android.controller;
+
+public interface DBController {
+
+	void runOnDbThread(Runnable task);
+
+}
diff --git a/briar-android/src/org/briarproject/android/controller/DBControllerImpl.java b/briar-android/src/org/briarproject/android/controller/DBControllerImpl.java
new file mode 100644
index 0000000000..34d8b69e4a
--- /dev/null
+++ b/briar-android/src/org/briarproject/android/controller/DBControllerImpl.java
@@ -0,0 +1,43 @@
+package org.briarproject.android.controller;
+
+import org.briarproject.api.db.DatabaseExecutor;
+import org.briarproject.api.lifecycle.LifecycleManager;
+
+import java.util.concurrent.Executor;
+import java.util.logging.Logger;
+
+import javax.inject.Inject;
+
+public class DBControllerImpl implements DBController {
+
+	private static final Logger LOG =
+			Logger.getLogger(BriarControllerImpl.class.getName());
+
+	// Fields that are accessed from background threads must be volatile
+	@Inject
+	@DatabaseExecutor
+	protected volatile Executor dbExecutor;
+	@Inject
+	protected volatile LifecycleManager lifecycleManager;
+
+	@Inject
+	public DBControllerImpl() {
+
+	}
+
+	@Override
+	public void runOnDbThread(final Runnable task) {
+		dbExecutor.execute(new Runnable() {
+			@Override
+			public void run() {
+				try {
+					lifecycleManager.waitForDatabase();
+					task.run();
+				} catch (InterruptedException e) {
+					LOG.warning("Interrupted while waiting for database");
+					Thread.currentThread().interrupt();
+				}
+			}
+		});
+	}
+}
diff --git a/briar-android/src/org/briarproject/android/controller/NavDrawerController.java b/briar-android/src/org/briarproject/android/controller/NavDrawerController.java
index db389706b4..a2f339990f 100644
--- a/briar-android/src/org/briarproject/android/controller/NavDrawerController.java
+++ b/briar-android/src/org/briarproject/android/controller/NavDrawerController.java
@@ -4,7 +4,7 @@ import org.briarproject.android.controller.handler.UiResultHandler;
 import org.briarproject.api.TransportId;
 import org.briarproject.api.identity.LocalAuthor;
 
-public interface NavDrawerController extends BriarController {
+public interface NavDrawerController extends ActivityLifecycleController {
 
 	void setTransportListener(TransportStateListener transportListener);
 
diff --git a/briar-android/src/org/briarproject/android/controller/NavDrawerControllerImpl.java b/briar-android/src/org/briarproject/android/controller/NavDrawerControllerImpl.java
index 651f60bcbd..1e492b3dbc 100644
--- a/briar-android/src/org/briarproject/android/controller/NavDrawerControllerImpl.java
+++ b/briar-android/src/org/briarproject/android/controller/NavDrawerControllerImpl.java
@@ -23,7 +23,7 @@ import javax.inject.Inject;
 import static java.util.logging.Level.INFO;
 import static java.util.logging.Level.WARNING;
 
-public class NavDrawerControllerImpl extends BriarControllerImpl
+public class NavDrawerControllerImpl extends DBControllerImpl
 		implements NavDrawerController, EventListener {
 
 	private static final Logger LOG =
@@ -51,21 +51,24 @@ public class NavDrawerControllerImpl extends BriarControllerImpl
 
 	@Override
 	public void onActivityCreate() {
-		super.onActivityCreate();
+
 	}
 
 	@Override
 	public void onActivityResume() {
-		super.onActivityResume();
 		eventBus.addListener(this);
 	}
 
 	@Override
 	public void onActivityPause() {
-		super.onActivityPause();
 		eventBus.removeListener(this);
 	}
 
+	@Override
+	public void onActivityDestroy() {
+
+	}
+
 	@Override
 	public void eventOccurred(Event e) {
 		if (e instanceof TransportEnabledEvent) {
@@ -131,4 +134,5 @@ public class NavDrawerControllerImpl extends BriarControllerImpl
 	public LocalAuthor removeAuthorHandle(long handle) {
 		return referenceManager.removeReference(handle, LocalAuthor.class);
 	}
+
 }
-- 
GitLab