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 5a2ad896c626dbf0e26d282e9d730f117115090a..2296a8bafffb48d0e8c315e3460846b9b3d3bef8 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 cf4e0ab13eb058412c7730e997982de41c6c327d..1d1fb1e2c561f5b438fa4146292bf10e4c7d1f9f 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 e537bd86d6fe804e9aa55a2811a9c8eb5522fa2f..6ab1c9ee01e318068441254f829702a060221c08 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 0b2f034660a83ef46fb3d7b0922da1acd45d2eda..97891c389f5b13c2c70620c4ec6ecd41a5d2f84e 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 48b4b7fa9023082623a678fbcbb7ba81c60a6ea5..b2d5779ca16efdf69360abedea6542fe047ed312 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 17c50fca0f464946818c0c86aec39c0b394748b7..955680ff521ed9c2fea89b5712993444f5beedb6 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 194856e898b19f95da1dc1402b499f1623f28fd9..bf65830691391c366d174a8f5985019739801f87 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 c74c7e3c3ec69ab980320ec5ce5a0ceacbb8ac1e..c81fbb2ad497c3493f7e3766678f3b4aeef7dfdd 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 ae086abf3385fde2ede3dd979a0e73cf79525edc..bd50acd41fac254fb9c631264fca479a0fe0f449 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; + } }