Move SignIn reminder code into AndroidNotificationManager and don't show reminder once PasswordActivity was opened

This commit is contained in:
Torsten Grote
2018-08-03 15:07:03 -03:00
parent 7e009ceaf2
commit 0b2594a693
4 changed files with 81 additions and 73 deletions

View File

@@ -10,6 +10,7 @@ import android.content.Intent;
import android.net.Uri; import android.net.Uri;
import android.support.annotation.StringRes; import android.support.annotation.StringRes;
import android.support.annotation.UiThread; import android.support.annotation.UiThread;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder; import android.support.v4.app.TaskStackBuilder;
import android.support.v4.content.ContextCompat; import android.support.v4.content.ContextCompat;
@@ -33,8 +34,10 @@ import org.briarproject.bramble.util.StringUtils;
import org.briarproject.briar.R; import org.briarproject.briar.R;
import org.briarproject.briar.android.contact.ConversationActivity; import org.briarproject.briar.android.contact.ConversationActivity;
import org.briarproject.briar.android.forum.ForumActivity; import org.briarproject.briar.android.forum.ForumActivity;
import org.briarproject.briar.android.login.SignInReminderReceiver;
import org.briarproject.briar.android.navdrawer.NavDrawerActivity; import org.briarproject.briar.android.navdrawer.NavDrawerActivity;
import org.briarproject.briar.android.privategroup.conversation.GroupActivity; import org.briarproject.briar.android.privategroup.conversation.GroupActivity;
import org.briarproject.briar.android.splash.SplashScreenActivity;
import org.briarproject.briar.android.util.BriarNotificationBuilder; import org.briarproject.briar.android.util.BriarNotificationBuilder;
import org.briarproject.briar.api.android.AndroidNotificationManager; import org.briarproject.briar.api.android.AndroidNotificationManager;
import org.briarproject.briar.api.blog.event.BlogPostAddedEvent; import org.briarproject.briar.api.blog.event.BlogPostAddedEvent;
@@ -64,11 +67,14 @@ import static android.app.Notification.DEFAULT_SOUND;
import static android.app.Notification.DEFAULT_VIBRATE; import static android.app.Notification.DEFAULT_VIBRATE;
import static android.app.Notification.VISIBILITY_SECRET; import static android.app.Notification.VISIBILITY_SECRET;
import static android.app.NotificationManager.IMPORTANCE_DEFAULT; import static android.app.NotificationManager.IMPORTANCE_DEFAULT;
import static android.app.NotificationManager.IMPORTANCE_LOW;
import static android.content.Context.NOTIFICATION_SERVICE; import static android.content.Context.NOTIFICATION_SERVICE;
import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP; 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.os.Build.VERSION.SDK_INT;
import static android.support.v4.app.NotificationCompat.CATEGORY_MESSAGE; import static android.support.v4.app.NotificationCompat.CATEGORY_MESSAGE;
import static android.support.v4.app.NotificationCompat.CATEGORY_SOCIAL; import static android.support.v4.app.NotificationCompat.CATEGORY_SOCIAL;
import static android.support.v4.app.NotificationCompat.PRIORITY_LOW;
import static java.util.logging.Level.WARNING; import static java.util.logging.Level.WARNING;
import static org.briarproject.bramble.util.LogUtils.logException; import static org.briarproject.bramble.util.LogUtils.logException;
import static org.briarproject.briar.android.activity.BriarActivity.GROUP_ID; import static org.briarproject.briar.android.activity.BriarActivity.GROUP_ID;
@@ -107,6 +113,7 @@ class AndroidNotificationManagerImpl implements AndroidNotificationManager,
private int nextRequestId = 0; private int nextRequestId = 0;
private ContactId blockedContact = null; private ContactId blockedContact = null;
private GroupId blockedGroup = null; private GroupId blockedGroup = null;
private boolean blockSignInReminder = false;
private boolean blockContacts = false, blockGroups = false; private boolean blockContacts = false, blockGroups = false;
private boolean blockForums = false, blockBlogs = false; private boolean blockForums = false, blockBlogs = false;
private boolean blockIntroductions = false; private boolean blockIntroductions = false;
@@ -612,6 +619,58 @@ class AndroidNotificationManagerImpl implements AndroidNotificationManager,
this::clearIntroductionSuccessNotification); this::clearIntroductionSuccessNotification);
} }
@Override
public void showSignInNotification() {
if (blockSignInReminder) return;
if (SDK_INT >= 26) {
NotificationChannel channel =
new NotificationChannel(REMINDER_CHANNEL_ID, appContext
.getString(
R.string.reminder_notification_channel_title),
IMPORTANCE_LOW);
channel.setLockscreenVisibility(
NotificationCompat.VISIBILITY_SECRET);
notificationManager.createNotificationChannel(channel);
}
NotificationCompat.Builder b =
new NotificationCompat.Builder(appContext, REMINDER_CHANNEL_ID);
b.setSmallIcon(R.drawable.ic_signout);
b.setColor(ContextCompat.getColor(appContext, R.color.briar_primary));
b.setContentTitle(
appContext.getText(R.string.reminder_notification_title));
b.setContentText(
appContext.getText(R.string.reminder_notification_text));
b.setAutoCancel(true);
b.setWhen(0); // Don't show the time
b.setPriority(PRIORITY_LOW);
// Add a 'Dismiss' action
String actionTitle =
appContext.getString(R.string.reminder_notification_dismiss);
Intent i1 = new Intent(appContext, SignInReminderReceiver.class);
i1.setAction(ACTION_DISMISS_REMINDER);
PendingIntent actionIntent =
PendingIntent.getBroadcast(appContext, 0, i1, 0);
b.addAction(0, actionTitle, actionIntent);
Intent i = new Intent(appContext, SplashScreenActivity.class);
i.setFlags(FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TOP);
b.setContentIntent(PendingIntent.getActivity(appContext, 0, i, 0));
notificationManager.notify(REMINDER_NOTIFICATION_ID, b.build());
}
@Override
public void clearSignInNotification() {
notificationManager.cancel(REMINDER_NOTIFICATION_ID);
}
@Override
public void blockSignInNotification() {
blockSignInReminder = true;
}
@Override @Override
public void blockNotification(GroupId g) { public void blockNotification(GroupId g) {
androidExecutor.runOnUiThread((Runnable) () -> blockedGroup = g); androidExecutor.runOnUiThread((Runnable) () -> blockedGroup = g);

View File

@@ -1,6 +1,5 @@
package org.briarproject.briar.android.login; package org.briarproject.briar.android.login;
import android.app.NotificationManager;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
@@ -20,6 +19,7 @@ import org.briarproject.briar.android.activity.BaseActivity;
import org.briarproject.briar.android.controller.BriarController; import org.briarproject.briar.android.controller.BriarController;
import org.briarproject.briar.android.controller.handler.UiResultHandler; import org.briarproject.briar.android.controller.handler.UiResultHandler;
import org.briarproject.briar.android.util.UiUtils; import org.briarproject.briar.android.util.UiUtils;
import org.briarproject.briar.api.android.AndroidNotificationManager;
import javax.inject.Inject; import javax.inject.Inject;
@@ -29,13 +29,15 @@ import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TASK;
import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK; import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
import static android.view.View.INVISIBLE; import static android.view.View.INVISIBLE;
import static android.view.View.VISIBLE; import static android.view.View.VISIBLE;
import static org.briarproject.briar.api.android.AndroidNotificationManager.REMINDER_NOTIFICATION_ID;
public class PasswordActivity extends BaseActivity { public class PasswordActivity extends BaseActivity {
@Inject @Inject
AccountManager accountManager; AccountManager accountManager;
@Inject
AndroidNotificationManager notificationManager;
@Inject @Inject
PasswordController passwordController; PasswordController passwordController;
@@ -95,10 +97,8 @@ public class PasswordActivity extends BaseActivity {
setResult(RESULT_OK); setResult(RESULT_OK);
finish(); finish();
} else { } else {
// Remove sign-in reminder notification notificationManager.blockSignInNotification();
NotificationManager nm = (NotificationManager) notificationManager.clearSignInNotification();
getSystemService(NOTIFICATION_SERVICE);
nm.cancel(REMINDER_NOTIFICATION_ID);
} }
} }

View File

@@ -1,43 +1,29 @@
package org.briarproject.briar.android.login; package org.briarproject.briar.android.login;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.support.v4.app.NotificationCompat;
import android.support.v4.content.ContextCompat;
import org.briarproject.bramble.api.account.AccountManager; import org.briarproject.bramble.api.account.AccountManager;
import org.briarproject.briar.R;
import org.briarproject.briar.android.AndroidComponent; import org.briarproject.briar.android.AndroidComponent;
import org.briarproject.briar.android.BriarApplication; import org.briarproject.briar.android.BriarApplication;
import org.briarproject.briar.android.navdrawer.NavDrawerActivity; import org.briarproject.briar.api.android.AndroidNotificationManager;
import javax.inject.Inject; import javax.inject.Inject;
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.ACTION_BOOT_COMPLETED;
import static android.content.Intent.ACTION_MY_PACKAGE_REPLACED; import static android.content.Intent.ACTION_MY_PACKAGE_REPLACED;
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.android.TestingConstants.FEATURE_FLAG_SIGN_IN_REMINDER; import static org.briarproject.briar.android.TestingConstants.FEATURE_FLAG_SIGN_IN_REMINDER;
import static org.briarproject.briar.android.settings.SettingsFragment.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.ACTION_DISMISS_REMINDER;
import static org.briarproject.briar.api.android.AndroidNotificationManager.REMINDER_NOTIFICATION_ID;
public class SignInReminderReceiver extends BroadcastReceiver { public class SignInReminderReceiver extends BroadcastReceiver {
public static final String DISMISS_REMINDER = "dismissReminder";
@Inject @Inject
AccountManager accountManager; AccountManager accountManager;
@Inject
AndroidNotificationManager notificationManager;
@Override @Override
public void onReceive(Context ctx, Intent intent) { public void onReceive(Context ctx, Intent intent) {
@@ -55,58 +41,12 @@ public class SignInReminderReceiver extends BroadcastReceiver {
!accountManager.hasDatabaseKey()) { !accountManager.hasDatabaseKey()) {
SharedPreferences prefs = app.getDefaultSharedPreferences(); SharedPreferences prefs = app.getDefaultSharedPreferences();
if (prefs.getBoolean(NOTIFY_SIGN_IN, true)) { if (prefs.getBoolean(NOTIFY_SIGN_IN, true)) {
showSignInNotification(ctx); notificationManager.showSignInNotification();
} }
} }
} else if (action.equals(DISMISS_REMINDER)) { } else if (action.equals(ACTION_DISMISS_REMINDER)) {
dismissReminder(ctx); notificationManager.clearSignInNotification();
} }
} }
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.ic_signout);
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);
// Add a 'Dismiss' action
String actionTitle =
ctx.getString(R.string.reminder_notification_dismiss);
Intent i1 = new Intent(ctx, SignInReminderReceiver.class);
i1.setAction(DISMISS_REMINDER);
PendingIntent actionIntent = PendingIntent.getBroadcast(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));
nm.notify(REMINDER_NOTIFICATION_ID, b.build());
}
private void dismissReminder(Context ctx) {
NotificationManager nm = (NotificationManager)
ctx.getSystemService(NOTIFICATION_SERVICE);
if (nm == null) return;
nm.cancel(REMINDER_NOTIFICATION_ID);
}
} }

View File

@@ -49,6 +49,9 @@ public interface AndroidNotificationManager {
String BLOG_URI = "content://org.briarproject.briar/blog"; String BLOG_URI = "content://org.briarproject.briar/blog";
String INTRODUCTION_URI = "content://org.briarproject.briar/introduction"; String INTRODUCTION_URI = "content://org.briarproject.briar/introduction";
// Actions for pending intents
String ACTION_DISMISS_REMINDER = "dismissReminder";
void clearContactNotification(ContactId c); void clearContactNotification(ContactId c);
void clearAllContactNotifications(); void clearAllContactNotifications();
@@ -67,6 +70,12 @@ public interface AndroidNotificationManager {
void clearAllIntroductionNotifications(); void clearAllIntroductionNotifications();
void showSignInNotification();
void clearSignInNotification();
void blockSignInNotification();
void blockContactNotification(ContactId c); void blockContactNotification(ContactId c);
void unblockContactNotification(ContactId c); void unblockContactNotification(ContactId c);