From 8925d6f5d70ca99788a76cad136d06ded15f48c2 Mon Sep 17 00:00:00 2001
From: Torsten Grote <t@grobox.de>
Date: Thu, 28 Jun 2018 16:34:55 -0300
Subject: [PATCH] Add an option to not show the sign-in reminder

This is done via another preference in the settings screen
and an action button attached to the notification itself
---
 .../briarproject/briar/android/AppModule.java |  1 +
 .../briar/android/BootReceiver.java           | 22 +++++++++++++----
 .../briar/android/BriarApplication.java       |  4 ++++
 .../briar/android/BriarApplicationImpl.java   | 10 ++++++--
 .../android/settings/SettingsActivity.java    | 24 +++++++++++++++++++
 .../android/settings/SettingsFragment.java    |  9 ++++++-
 briar-android/src/main/res/values/strings.xml |  3 +++
 briar-android/src/main/res/xml/settings.xml   |  6 +++++
 .../briar/android/TestBriarApplication.java   |  8 ++++++-
 9 files changed, 79 insertions(+), 8 deletions(-)

diff --git a/briar-android/src/main/java/org/briarproject/briar/android/AppModule.java b/briar-android/src/main/java/org/briarproject/briar/android/AppModule.java
index 5a2ad896c6..2296a8baff 100644
--- a/briar-android/src/main/java/org/briarproject/briar/android/AppModule.java
+++ b/briar-android/src/main/java/org/briarproject/briar/android/AppModule.java
@@ -162,6 +162,7 @@ public class AppModule {
 
 	@Provides
 	SharedPreferences provideSharedPreferences(Application app) {
+		// FIXME unify this with getDefaultSharedPreferences()
 		return app.getSharedPreferences("db", MODE_PRIVATE);
 	}
 
diff --git a/briar-android/src/main/java/org/briarproject/briar/android/BootReceiver.java b/briar-android/src/main/java/org/briarproject/briar/android/BootReceiver.java
index cf4e0ab13e..1d1fb1e2c5 100644
--- a/briar-android/src/main/java/org/briarproject/briar/android/BootReceiver.java
+++ b/briar-android/src/main/java/org/briarproject/briar/android/BootReceiver.java
@@ -6,12 +6,14 @@ import android.app.PendingIntent;
 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
+import android.content.SharedPreferences;
 import android.support.v4.app.NotificationCompat;
 import android.support.v4.content.ContextCompat;
 
 import org.briarproject.bramble.api.db.DatabaseConfig;
 import org.briarproject.briar.R;
 import org.briarproject.briar.android.navdrawer.NavDrawerActivity;
+import org.briarproject.briar.android.settings.SettingsActivity;
 
 import javax.inject.Inject;
 
@@ -24,6 +26,8 @@ import static android.os.Build.VERSION.SDK_INT;
 import static android.support.v4.app.NotificationCompat.PRIORITY_LOW;
 import static android.support.v4.app.NotificationCompat.VISIBILITY_SECRET;
 import static org.briarproject.briar.android.TestingConstants.FEATURE_FLAG_SIGN_IN_REMINDER;
+import static org.briarproject.briar.android.settings.SettingsActivity.NO_NOTIFY_SIGN_IN;
+import static org.briarproject.briar.android.settings.SettingsFragment.NOTIFY_SIGN_IN;
 import static org.briarproject.briar.api.android.AndroidNotificationManager.REMINDER_CHANNEL_ID;
 import static org.briarproject.briar.api.android.AndroidNotificationManager.REMINDER_NOTIFICATION_ID;
 
@@ -36,15 +40,17 @@ public class BootReceiver extends BroadcastReceiver {
 	public void onReceive(Context ctx, Intent intent) {
 		if (!FEATURE_FLAG_SIGN_IN_REMINDER) return;
 
-		AndroidComponent applicationComponent =
-				((BriarApplication) ctx.getApplicationContext())
-						.getApplicationComponent();
+		BriarApplication app = (BriarApplication) ctx.getApplicationContext();
+		AndroidComponent applicationComponent = app.getApplicationComponent();
 		applicationComponent.inject(this);
 
 		String action = intent.getAction();
 		if (action != null && action.equals(ACTION_BOOT_COMPLETED)) {
 			if (databaseConfig.databaseExists()) {
-				showSignInNotification(ctx);
+				SharedPreferences prefs = app.getDefaultSharedPreferences();
+				if (prefs.getBoolean(NOTIFY_SIGN_IN, true)) {
+					showSignInNotification(ctx);
+				}
 			}
 		}
 	}
@@ -73,6 +79,14 @@ public class BootReceiver extends BroadcastReceiver {
 		b.setWhen(0); // Don't show the time
 		b.setPriority(PRIORITY_LOW);
 
+		// Add a 'Do not show sign-in reminder' action
+		String actionTitle =
+				ctx.getString(R.string.reminder_notification_do_not_show_again);
+		Intent i1 = new Intent(ctx, SettingsActivity.class);
+		i1.setAction(NO_NOTIFY_SIGN_IN);
+		PendingIntent actionIntent = PendingIntent.getActivity(ctx, 0, i1, 0);
+		b.addAction(0, actionTitle, actionIntent);
+
 		Intent i = new Intent(ctx, NavDrawerActivity.class);
 		i.setFlags(FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TOP);
 		b.setContentIntent(PendingIntent.getActivity(ctx, 0, i, 0));
diff --git a/briar-android/src/main/java/org/briarproject/briar/android/BriarApplication.java b/briar-android/src/main/java/org/briarproject/briar/android/BriarApplication.java
index e537bd86d6..6ab1c9ee01 100644
--- a/briar-android/src/main/java/org/briarproject/briar/android/BriarApplication.java
+++ b/briar-android/src/main/java/org/briarproject/briar/android/BriarApplication.java
@@ -1,5 +1,7 @@
 package org.briarproject.briar.android;
 
+import android.content.SharedPreferences;
+
 import java.util.Collection;
 import java.util.logging.LogRecord;
 
@@ -12,4 +14,6 @@ public interface BriarApplication {
 	Collection<LogRecord> getRecentLogRecords();
 
 	AndroidComponent getApplicationComponent();
+
+	SharedPreferences getDefaultSharedPreferences();
 }
diff --git a/briar-android/src/main/java/org/briarproject/briar/android/BriarApplicationImpl.java b/briar-android/src/main/java/org/briarproject/briar/android/BriarApplicationImpl.java
index 0b2f034660..97891c389f 100644
--- a/briar-android/src/main/java/org/briarproject/briar/android/BriarApplicationImpl.java
+++ b/briar-android/src/main/java/org/briarproject/briar/android/BriarApplicationImpl.java
@@ -77,11 +77,12 @@ public class BriarApplicationImpl extends Application
 	private final CachingLogHandler logHandler = new CachingLogHandler();
 
 	private AndroidComponent applicationComponent;
+	private SharedPreferences prefs;
 
 	@Override
 	protected void attachBaseContext(Context base) {
-		SharedPreferences prefs =
-				PreferenceManager.getDefaultSharedPreferences(base);
+		if (prefs == null)
+			prefs = PreferenceManager.getDefaultSharedPreferences(base);
 		// Loading the language needs to be done here.
 		Localizer.initialize(prefs);
 		super.attachBaseContext(
@@ -156,4 +157,9 @@ public class BriarApplicationImpl extends Application
 	public AndroidComponent getApplicationComponent() {
 		return applicationComponent;
 	}
+
+	@Override
+	public SharedPreferences getDefaultSharedPreferences() {
+		return prefs;
+	}
 }
diff --git a/briar-android/src/main/java/org/briarproject/briar/android/settings/SettingsActivity.java b/briar-android/src/main/java/org/briarproject/briar/android/settings/SettingsActivity.java
index 48b4b7fa90..b2d5779ca1 100644
--- a/briar-android/src/main/java/org/briarproject/briar/android/settings/SettingsActivity.java
+++ b/briar-android/src/main/java/org/briarproject/briar/android/settings/SettingsActivity.java
@@ -1,19 +1,43 @@
 package org.briarproject.briar.android.settings;
 
+import android.app.NotificationManager;
+import android.content.Intent;
+import android.content.SharedPreferences;
 import android.os.Bundle;
 import android.support.v7.app.ActionBar;
 import android.view.MenuItem;
 
 import org.briarproject.briar.R;
+import org.briarproject.briar.android.BriarApplication;
 import org.briarproject.briar.android.activity.ActivityComponent;
 import org.briarproject.briar.android.activity.BriarActivity;
 
+import static org.briarproject.briar.android.settings.SettingsFragment.NOTIFY_SIGN_IN;
+import static org.briarproject.briar.api.android.AndroidNotificationManager.REMINDER_NOTIFICATION_ID;
+
 public class SettingsActivity extends BriarActivity {
 
+	public static final String NO_NOTIFY_SIGN_IN = "noNotifySignIn";
+
 	@Override
 	public void onCreate(Bundle bundle) {
 		super.onCreate(bundle);
 
+		// Maybe turn off sign-in reminder
+		Intent intent = getIntent();
+		if (intent != null && NO_NOTIFY_SIGN_IN.equals(intent.getAction())) {
+			// Turn it off
+			BriarApplication app = (BriarApplication) getApplication();
+			SharedPreferences prefs = app.getDefaultSharedPreferences();
+			prefs.edit().putBoolean(NOTIFY_SIGN_IN, false).apply();
+			// Remove sign-in reminder notification
+			NotificationManager nm = (NotificationManager)
+					getSystemService(NOTIFICATION_SERVICE);
+			if (nm != null) nm.cancel(REMINDER_NOTIFICATION_ID);
+			// Finish this activity again
+			finish();
+		}
+
 		ActionBar actionBar = getSupportActionBar();
 		if (actionBar != null) {
 			actionBar.setHomeButtonEnabled(true);
diff --git a/briar-android/src/main/java/org/briarproject/briar/android/settings/SettingsFragment.java b/briar-android/src/main/java/org/briarproject/briar/android/settings/SettingsFragment.java
index 17c50fca0f..955680ff52 100644
--- a/briar-android/src/main/java/org/briarproject/briar/android/settings/SettingsFragment.java
+++ b/briar-android/src/main/java/org/briarproject/briar/android/settings/SettingsFragment.java
@@ -71,6 +71,7 @@ import static org.briarproject.bramble.util.LogUtils.logDuration;
 import static org.briarproject.bramble.util.LogUtils.logException;
 import static org.briarproject.bramble.util.LogUtils.now;
 import static org.briarproject.briar.android.TestingConstants.FEATURE_FLAG_DARK_THEME;
+import static org.briarproject.briar.android.TestingConstants.FEATURE_FLAG_SIGN_IN_REMINDER;
 import static org.briarproject.briar.android.TestingConstants.IS_DEBUG_BUILD;
 import static org.briarproject.briar.android.activity.RequestCodes.REQUEST_RINGTONE;
 import static org.briarproject.briar.android.navdrawer.NavDrawerActivity.INTENT_SIGN_OUT;
@@ -97,6 +98,7 @@ public class SettingsFragment extends PreferenceFragmentCompat
 	public static final String BT_NAMESPACE = BluetoothConstants.ID.getString();
 	public static final String TOR_NAMESPACE = TorConstants.ID.getString();
 	public static final String LANGUAGE = "pref_key_language";
+	public static final String NOTIFY_SIGN_IN = "pref_key_notify_sign_in";
 
 	private static final Logger LOG =
 			Logger.getLogger(SettingsFragment.class.getName());
@@ -143,6 +145,8 @@ public class SettingsFragment extends PreferenceFragmentCompat
 				(ListPreference) findPreference("pref_key_theme");
 		enableBluetooth = (ListPreference) findPreference("pref_key_bluetooth");
 		torNetwork = (ListPreference) findPreference("pref_key_tor_network");
+		CheckBoxPreference notifySignIn =
+				(CheckBoxPreference) findPreference(NOTIFY_SIGN_IN);
 		notifyPrivateMessages = (CheckBoxPreference) findPreference(
 				"pref_key_notify_private_messages");
 		notifyGroupMessages = (CheckBoxPreference) findPreference(
@@ -199,6 +203,7 @@ public class SettingsFragment extends PreferenceFragmentCompat
 			);
 		} else {
 			theme.setVisible(FEATURE_FLAG_DARK_THEME);
+			notifySignIn.setVisible(FEATURE_FLAG_SIGN_IN_REMINDER);
 
 			findPreference("pref_key_explode").setVisible(false);
 			findPreference("pref_key_test_data").setVisible(false);
@@ -346,7 +351,9 @@ public class SettingsFragment extends PreferenceFragmentCompat
 	}
 
 	private void setSettingsEnabled(boolean enabled) {
-		// theme not needed here, because handled by SharedPreferences
+		// preferences not needed here, because handled by SharedPreferences:
+		// - pref_key_theme
+		// - pref_key_notify_sign_in
 		enableBluetooth.setEnabled(enabled);
 		torNetwork.setEnabled(enabled);
 		notifyPrivateMessages.setEnabled(enabled);
diff --git a/briar-android/src/main/res/values/strings.xml b/briar-android/src/main/res/values/strings.xml
index 194856e898..bf65830691 100644
--- a/briar-android/src/main/res/values/strings.xml
+++ b/briar-android/src/main/res/values/strings.xml
@@ -71,6 +71,7 @@
 	<string name="reminder_notification_title">Signed out of Briar</string>
 	<string name="reminder_notification_text">Tap to sign back in or swipe to dismiss.</string>
 	<string name="reminder_notification_channel_title">Briar Sign-in Reminder</string>
+	<string name="reminder_notification_do_not_show_again">Don\'t show again</string>
 	<string name="ongoing_notification_title">Signed into Briar</string>
 	<string name="ongoing_notification_text">Touch to open Briar.</string>
 	<plurals name="private_message_notification_text">
@@ -373,6 +374,8 @@
 
 	<!-- Settings Notifications -->
 	<string name="notification_settings_title">Notifications</string>
+	<string name="notify_sign_in_title">Remind me to sign in</string>
+	<string name="notify_sign_in_summary">Shows a reminder when the phone starts</string>
 	<string name="notify_private_messages_setting_title">Private messages</string>
 	<string name="notify_private_messages_setting_summary">Show alerts for private messages</string>
 	<string name="notify_private_messages_setting_summary_26">Configure alerts for private messages</string>
diff --git a/briar-android/src/main/res/xml/settings.xml b/briar-android/src/main/res/xml/settings.xml
index c74c7e3c3e..c81fbb2ad4 100644
--- a/briar-android/src/main/res/xml/settings.xml
+++ b/briar-android/src/main/res/xml/settings.xml
@@ -81,6 +81,12 @@
 		android:layout="@layout/preferences_category"
 		android:title="@string/notification_settings_title">
 
+		<CheckBoxPreference
+			android:defaultValue="true"
+			android:key="pref_key_notify_sign_in"
+			android:summary="@string/notify_sign_in_summary"
+			android:title="@string/notify_sign_in_title"/>
+
 		<CheckBoxPreference
 			android:defaultValue="true"
 			android:key="pref_key_notify_private_messages"
diff --git a/briar-android/src/test/java/org/briarproject/briar/android/TestBriarApplication.java b/briar-android/src/test/java/org/briarproject/briar/android/TestBriarApplication.java
index ae086abf33..bd50acd41f 100644
--- a/briar-android/src/test/java/org/briarproject/briar/android/TestBriarApplication.java
+++ b/briar-android/src/test/java/org/briarproject/briar/android/TestBriarApplication.java
@@ -23,13 +23,14 @@ public class TestBriarApplication extends Application
 			Logger.getLogger(TestBriarApplication.class.getName());
 
 	private AndroidComponent applicationComponent;
+	private SharedPreferences prefs;
 
 	@Override
 	public void onCreate() {
 		super.onCreate();
 		LOG.info("Created");
 
-		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
+		prefs = PreferenceManager.getDefaultSharedPreferences(this);
 		Localizer.initialize(prefs);
 		applicationComponent = DaggerAndroidComponent.builder()
 				.appModule(new AppModule(this))
@@ -51,4 +52,9 @@ public class TestBriarApplication extends Application
 	public AndroidComponent getApplicationComponent() {
 		return applicationComponent;
 	}
+
+	@Override
+	public SharedPreferences getDefaultSharedPreferences() {
+		return prefs;
+	}
 }
-- 
GitLab