From 1f9def84183b506da085206761032907b28161fa Mon Sep 17 00:00:00 2001 From: Torsten Grote Date: Tue, 19 Jun 2018 17:26:19 -0300 Subject: [PATCH] Minimal Sign-in reminder --- .../artwork/notification_ongoing.svg | 58 ++++++++++++++++ .../artwork/notification_reminder.svg | 57 ++++++++++++++++ briar-android/src/main/AndroidManifest.xml | 9 +++ .../AndroidNotificationManagerImpl.java | 7 -- .../briar/android/BootReceiver.java | 67 +++++++++++++++++++ .../briar/android/BriarService.java | 19 +++--- .../android/AndroidNotificationManager.java | 15 +++++ .../res/drawable/notification_reminder.xml | 10 +++ briar-android/src/main/res/values/strings.xml | 3 + 9 files changed, 228 insertions(+), 17 deletions(-) create mode 100644 briar-android/artwork/notification_ongoing.svg create mode 100644 briar-android/artwork/notification_reminder.svg create mode 100644 briar-android/src/main/java/org/briarproject/briar/android/BootReceiver.java create mode 100644 briar-android/src/main/res/drawable/notification_reminder.xml diff --git a/briar-android/artwork/notification_ongoing.svg b/briar-android/artwork/notification_ongoing.svg new file mode 100644 index 000000000..b85939f22 --- /dev/null +++ b/briar-android/artwork/notification_ongoing.svg @@ -0,0 +1,58 @@ + + + +image/svg+xml diff --git a/briar-android/artwork/notification_reminder.svg b/briar-android/artwork/notification_reminder.svg new file mode 100644 index 000000000..7d30fd683 --- /dev/null +++ b/briar-android/artwork/notification_reminder.svg @@ -0,0 +1,57 @@ + + + +image/svg+xml \ No newline at end of file diff --git a/briar-android/src/main/AndroidManifest.xml b/briar-android/src/main/AndroidManifest.xml index 3c3ac063e..f105d57e9 100644 --- a/briar-android/src/main/AndroidManifest.xml +++ b/briar-android/src/main/AndroidManifest.xml @@ -14,6 +14,7 @@ + + + + + + + diff --git a/briar-android/src/main/java/org/briarproject/briar/android/AndroidNotificationManagerImpl.java b/briar-android/src/main/java/org/briarproject/briar/android/AndroidNotificationManagerImpl.java index c18d2a7c6..91743a06c 100644 --- a/briar-android/src/main/java/org/briarproject/briar/android/AndroidNotificationManagerImpl.java +++ b/briar-android/src/main/java/org/briarproject/briar/android/AndroidNotificationManagerImpl.java @@ -85,13 +85,6 @@ import static org.briarproject.briar.android.settings.SettingsFragment.SETTINGS_ class AndroidNotificationManagerImpl implements AndroidNotificationManager, Service, EventListener { - // Notification IDs - private static final int PRIVATE_MESSAGE_NOTIFICATION_ID = 3; - private static final int GROUP_MESSAGE_NOTIFICATION_ID = 4; - private static final int FORUM_POST_NOTIFICATION_ID = 5; - private static final int BLOG_POST_NOTIFICATION_ID = 6; - private static final int INTRODUCTION_SUCCESS_NOTIFICATION_ID = 7; - private static final long SOUND_DELAY = TimeUnit.SECONDS.toMillis(2); private static final Logger LOG = 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 new file mode 100644 index 000000000..20fed7c13 --- /dev/null +++ b/briar-android/src/main/java/org/briarproject/briar/android/BootReceiver.java @@ -0,0 +1,67 @@ +package org.briarproject.briar.android; + +import android.app.NotificationChannel; +import android.app.NotificationManager; +import android.app.PendingIntent; +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.support.v4.app.NotificationCompat; +import android.support.v4.content.ContextCompat; + +import org.briarproject.briar.R; +import org.briarproject.briar.android.navdrawer.NavDrawerActivity; + +import static android.app.NotificationManager.IMPORTANCE_LOW; +import static android.content.Context.NOTIFICATION_SERVICE; +import static android.content.Intent.ACTION_BOOT_COMPLETED; +import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP; +import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK; +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.api.android.AndroidNotificationManager.REMINDER_CHANNEL_ID; +import static org.briarproject.briar.api.android.AndroidNotificationManager.REMINDER_NOTIFICATION_ID; + +public class BootReceiver extends BroadcastReceiver { + + @Override + public void onReceive(Context ctx, Intent intent) { + String action = intent.getAction(); + if (action != null && action.equals(ACTION_BOOT_COMPLETED)) { + showSignInNotification(ctx); + } + } + + private void showSignInNotification(Context ctx) { + NotificationManager nm = (NotificationManager) + ctx.getSystemService(NOTIFICATION_SERVICE); + if (nm == null) return; + + if (SDK_INT >= 26) { + NotificationChannel channel = + new NotificationChannel(REMINDER_CHANNEL_ID, ctx.getString( + R.string.reminder_notification_channel_title), + IMPORTANCE_LOW); + channel.setLockscreenVisibility(VISIBILITY_SECRET); + nm.createNotificationChannel(channel); + } + + NotificationCompat.Builder b = + new NotificationCompat.Builder(ctx, REMINDER_CHANNEL_ID); + b.setSmallIcon(R.drawable.notification_reminder); + b.setColor(ContextCompat.getColor(ctx, R.color.briar_primary)); + b.setContentTitle(ctx.getText(R.string.reminder_notification_title)); + b.setContentText(ctx.getText(R.string.reminder_notification_text)); + b.setAutoCancel(true); + b.setWhen(0); // Don't show the time + b.setPriority(PRIORITY_LOW); + + 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)); + + nm.notify(REMINDER_NOTIFICATION_ID, b.build()); + } + +} diff --git a/briar-android/src/main/java/org/briarproject/briar/android/BriarService.java b/briar-android/src/main/java/org/briarproject/briar/android/BriarService.java index bfd2ed156..523935278 100644 --- a/briar-android/src/main/java/org/briarproject/briar/android/BriarService.java +++ b/briar-android/src/main/java/org/briarproject/briar/android/BriarService.java @@ -50,6 +50,11 @@ import static java.util.logging.Level.INFO; 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.SUCCESS; +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.ONGOING_CHANNEL_ID; +import static org.briarproject.briar.api.android.AndroidNotificationManager.ONGOING_NOTIFICATION_ID; +import static org.briarproject.briar.api.android.AndroidNotificationManager.REMINDER_NOTIFICATION_ID; public class BriarService extends Service { @@ -60,14 +65,6 @@ public class BriarService extends Service { public static String EXTRA_STARTUP_FAILED = "org.briarproject.briar.STARTUP_FAILED"; - private static final int ONGOING_NOTIFICATION_ID = 1; - private static final int FAILURE_NOTIFICATION_ID = 2; - - // Channels are sorted by channel ID in the Settings app, so use IDs - // that will sort below the main channels such as contacts - private static final String ONGOING_CHANNEL_ID = "zForegroundService"; - private static final String FAILURE_CHANNEL_ID = "zStartupFailure"; - private static final Logger LOG = Logger.getLogger(BriarService.class.getName()); @@ -106,9 +103,9 @@ public class BriarService extends Service { } // Create notification channels + NotificationManager nm = (NotificationManager) + getSystemService(NOTIFICATION_SERVICE); if (SDK_INT >= 26) { - NotificationManager nm = (NotificationManager) - getSystemService(NOTIFICATION_SERVICE); NotificationChannel ongoingChannel = new NotificationChannel( ONGOING_CHANNEL_ID, getString(R.string.ongoing_notification_title), @@ -140,6 +137,8 @@ public class BriarService extends Service { } b.setPriority(PRIORITY_MIN); startForeground(ONGOING_NOTIFICATION_ID, b.build()); + // Remove sign-in reminder notification + nm.cancel(REMINDER_NOTIFICATION_ID); // Start the services in a background thread new Thread(() -> { String nickname = databaseConfig.getLocalAuthorName(); diff --git a/briar-android/src/main/java/org/briarproject/briar/api/android/AndroidNotificationManager.java b/briar-android/src/main/java/org/briarproject/briar/api/android/AndroidNotificationManager.java index 0579dc873..c9415cbd5 100644 --- a/briar-android/src/main/java/org/briarproject/briar/api/android/AndroidNotificationManager.java +++ b/briar-android/src/main/java/org/briarproject/briar/api/android/AndroidNotificationManager.java @@ -21,11 +21,26 @@ public interface AndroidNotificationManager { String PREF_NOTIFY_VIBRATION = "notifyVibration"; String PREF_NOTIFY_LOCK_SCREEN = "notifyLockScreen"; + // Notification IDs + int ONGOING_NOTIFICATION_ID = 1; + int FAILURE_NOTIFICATION_ID = 2; + int REMINDER_NOTIFICATION_ID = 3; + int PRIVATE_MESSAGE_NOTIFICATION_ID = 4; + int GROUP_MESSAGE_NOTIFICATION_ID = 5; + int FORUM_POST_NOTIFICATION_ID = 6; + int BLOG_POST_NOTIFICATION_ID = 7; + int INTRODUCTION_SUCCESS_NOTIFICATION_ID = 8; + // Channel IDs String CONTACT_CHANNEL_ID = "contacts"; String GROUP_CHANNEL_ID = "groups"; String FORUM_CHANNEL_ID = "forums"; String BLOG_CHANNEL_ID = "blogs"; + // Channels are sorted by channel ID in the Settings app, so use IDs + // that will sort below the main channels such as contacts + String ONGOING_CHANNEL_ID = "zForegroundService"; + String FAILURE_CHANNEL_ID = "zStartupFailure"; + String REMINDER_CHANNEL_ID = "zSignInReminder"; // Content URIs for pending intents String CONTACT_URI = "content://org.briarproject.briar/contact"; diff --git a/briar-android/src/main/res/drawable/notification_reminder.xml b/briar-android/src/main/res/drawable/notification_reminder.xml new file mode 100644 index 000000000..1defd7d3d --- /dev/null +++ b/briar-android/src/main/res/drawable/notification_reminder.xml @@ -0,0 +1,10 @@ + + + + \ No newline at end of file diff --git a/briar-android/src/main/res/values/strings.xml b/briar-android/src/main/res/values/strings.xml index 1c0fb4d57..93fb23f4d 100644 --- a/briar-android/src/main/res/values/strings.xml +++ b/briar-android/src/main/res/values/strings.xml @@ -68,6 +68,9 @@ Wi-Fi + Signed out of Briar + Tap to sign back in or swipe to dismiss. + Briar Sign-in Reminder Signed into Briar Touch to open Briar.