From 555f335dbcd8ece16c27950b2cacfce211902c12 Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Mon, 16 Jul 2018 13:24:37 +0200 Subject: [PATCH] Introduce NotificationChannelManager --- .../keychain/Constants.java | 5 -- .../keychain/NotificationChannelManager.java | 52 +++++++++++++++++++ .../keychain/keysync/KeyserverSyncWorker.java | 20 ++----- .../service/ContactSyncAdapterService.java | 6 ++- .../service/PassphraseCacheService.java | 28 ++-------- .../keychain/ui/ImportKeysFileFragment.java | 5 +- .../keychain/ui/KeyListFragment.java | 1 - .../ui/OrbotRequiredDialogActivity.java | 5 +- OpenKeychain/src/main/res/values/strings.xml | 2 + 9 files changed, 75 insertions(+), 49 deletions(-) create mode 100644 OpenKeychain/src/main/java/org/sufficientlysecure/keychain/NotificationChannelManager.java diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/Constants.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/Constants.java index 4cd7522a4..4d30d115d 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/Constants.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/Constants.java @@ -116,11 +116,6 @@ public final class Constants { public static final int KEYSERVER_SYNC = 3; } - public static final class NotificationChannels { - public static final String KEYSERVER_SYNC = "keyserverSync"; - public static final String PASSPHRASE_CACHE = "passphraseCache"; - } - public static final class Pref { public static final String PASSPHRASE_CACHE_SUBS = "passphraseCacheSubs"; public static final String PASSPHRASE_CACHE_LAST_TTL = "passphraseCacheLastTtl"; diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/NotificationChannelManager.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/NotificationChannelManager.java new file mode 100644 index 000000000..63806e577 --- /dev/null +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/NotificationChannelManager.java @@ -0,0 +1,52 @@ +package org.sufficientlysecure.keychain; + + +import android.app.NotificationChannel; +import android.app.NotificationManager; +import android.content.Context; +import android.os.Build; +import android.os.Build.VERSION_CODES; +import android.support.annotation.RequiresApi; +import android.support.annotation.StringRes; + + +public class NotificationChannelManager { + public static final String KEYSERVER_SYNC = "keyserverSync"; + public static final String PERMISSION_REQUESTS = "permissionRequests"; + public static final String PASSPHRASE_CACHE = "passphraseCache"; + public static final String ORBOT = "orbot"; + + private final Context context; + private final NotificationManager notificationManager; + + public static NotificationChannelManager getInstance(Context context) { + NotificationManager notifyMan = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); + return new NotificationChannelManager(context.getApplicationContext(), notifyMan); + } + + private NotificationChannelManager(Context context, NotificationManager notificationManager) { + this.context = context; + this.notificationManager = notificationManager; + } + + public void createNotificationChannelsIfNecessary() { + if (notificationManager == null) { + return; + } + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { + return; + } + + createNotificationChannel(KEYSERVER_SYNC, R.string.notify_channel_keysync, NotificationManager.IMPORTANCE_MIN); + createNotificationChannel(PERMISSION_REQUESTS, R.string.notify_channel_permission, NotificationManager.IMPORTANCE_MIN); + createNotificationChannel(PASSPHRASE_CACHE, R.string.notify_channel_passcache, NotificationManager.IMPORTANCE_NONE); + createNotificationChannel(ORBOT, R.string.notify_channel_orbot, NotificationManager.IMPORTANCE_DEFAULT); + } + + @RequiresApi(api = VERSION_CODES.O) + private void createNotificationChannel(String channelName, @StringRes int channelDescription, int importance) { + CharSequence descriptionText = context.getString(channelDescription); + NotificationChannel channel = new NotificationChannel(channelName, descriptionText, importance); + notificationManager.createNotificationChannel(channel); + } +} diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keysync/KeyserverSyncWorker.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keysync/KeyserverSyncWorker.java index cad95eb21..7b638baf3 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keysync/KeyserverSyncWorker.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keysync/KeyserverSyncWorker.java @@ -1,25 +1,23 @@ package org.sufficientlysecure.keychain.keysync; -import android.app.NotificationChannel; import android.app.NotificationManager; import android.content.Context; -import android.os.Build; import android.support.annotation.NonNull; import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationCompat.Builder; import android.support.v4.os.CancellationSignal; import androidx.work.Worker; -import org.sufficientlysecure.keychain.Constants.NotificationChannels; import org.sufficientlysecure.keychain.Constants.NotificationIds; +import org.sufficientlysecure.keychain.NotificationChannelManager; import org.sufficientlysecure.keychain.R; +import org.sufficientlysecure.keychain.daos.KeyWritableRepository; import org.sufficientlysecure.keychain.network.orbot.OrbotHelper; import org.sufficientlysecure.keychain.operations.KeySyncOperation; import org.sufficientlysecure.keychain.operations.KeySyncParcel; import org.sufficientlysecure.keychain.operations.results.ImportKeyResult; import org.sufficientlysecure.keychain.pgp.Progressable; -import org.sufficientlysecure.keychain.daos.KeyWritableRepository; import org.sufficientlysecure.keychain.service.input.CryptoInputParcel; import org.sufficientlysecure.keychain.ui.OrbotRequiredDialogActivity; import org.sufficientlysecure.keychain.util.ResourceUtils; @@ -81,9 +79,9 @@ public class KeyserverSyncWorker extends Worker { return null; } - createNotificationChannelsIfNecessary(context, notificationManager); + NotificationChannelManager.getInstance(context).createNotificationChannelsIfNecessary(); - NotificationCompat.Builder builder = new Builder(context, NotificationChannels.KEYSERVER_SYNC) + NotificationCompat.Builder builder = new Builder(context, NotificationChannelManager.KEYSERVER_SYNC) .setSmallIcon(R.drawable.ic_stat_notify_24dp) .setLargeIcon(ResourceUtils.getDrawableAsNotificationBitmap(context, R.mipmap.ic_launcher)) .setContentTitle(context.getString(R.string.notify_title_keysync)) @@ -127,16 +125,6 @@ public class KeyserverSyncWorker extends Worker { }; } - private void createNotificationChannelsIfNecessary(Context context, - NotificationManager notificationManager) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - CharSequence name = context.getString(R.string.notify_channel_keysync); - NotificationChannel channel = new NotificationChannel( - NotificationChannels.KEYSERVER_SYNC, name, NotificationManager.IMPORTANCE_MIN); - notificationManager.createNotificationChannel(channel); - } - } - @Override public void onStopped() { super.onStopped(); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/ContactSyncAdapterService.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/ContactSyncAdapterService.java index 6c81d8fb8..ffd3daec6 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/ContactSyncAdapterService.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/ContactSyncAdapterService.java @@ -17,6 +17,7 @@ package org.sufficientlysecure.keychain.service; + import android.Manifest; import android.accounts.Account; import android.app.PendingIntent; @@ -38,6 +39,7 @@ import android.support.v4.content.ContextCompat; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.KeychainApplication; +import org.sufficientlysecure.keychain.NotificationChannelManager; import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.ui.SettingsActivity; import org.sufficientlysecure.keychain.util.ContactHelper; @@ -73,6 +75,8 @@ public class ContactSyncAdapterService extends Service { // deactivate sync ContentResolver.setSyncAutomatically(account, authority, false); + NotificationChannelManager.getInstance(getContext()).createNotificationChannelsIfNecessary(); + // show notification linking to sync settings Intent resultIntent = new Intent(ContactSyncAdapterService.this, SettingsActivity.class); resultIntent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT, @@ -85,7 +89,7 @@ public class ContactSyncAdapterService extends Service { PendingIntent.FLAG_UPDATE_CURRENT ); NotificationCompat.Builder mBuilder = - new NotificationCompat.Builder(ContactSyncAdapterService.this) + new NotificationCompat.Builder(ContactSyncAdapterService.this, NotificationChannelManager.PERMISSION_REQUESTS) .setAutoCancel(true) .setSmallIcon(R.drawable.ic_stat_notify_24dp) .setColor(getResources().getColor(R.color.primary)) diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/PassphraseCacheService.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/PassphraseCacheService.java index 713919930..c0e20f568 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/PassphraseCacheService.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/PassphraseCacheService.java @@ -22,8 +22,6 @@ import java.util.Date; import android.app.AlarmManager; import android.app.Notification; -import android.app.NotificationChannel; -import android.app.NotificationManager; import android.app.PendingIntent; import android.app.Service; import android.content.BroadcastReceiver; @@ -31,7 +29,6 @@ import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Binder; -import android.os.Build; import android.os.Bundle; import android.os.Handler; import android.os.HandlerThread; @@ -45,11 +42,11 @@ import android.support.v4.app.NotificationCompat.InboxStyle; import android.support.v4.util.LongSparseArray; import org.sufficientlysecure.keychain.Constants; -import org.sufficientlysecure.keychain.Constants.NotificationChannels; import org.sufficientlysecure.keychain.Constants.NotificationIds; +import org.sufficientlysecure.keychain.NotificationChannelManager; import org.sufficientlysecure.keychain.R; -import org.sufficientlysecure.keychain.pgp.CanonicalizedSecretKey.SecretKeyType; import org.sufficientlysecure.keychain.daos.KeyRepository; +import org.sufficientlysecure.keychain.pgp.CanonicalizedSecretKey.SecretKeyType; import org.sufficientlysecure.keychain.util.Passphrase; import org.sufficientlysecure.keychain.util.Preferences; import timber.log.Timber; @@ -494,7 +491,6 @@ public class PassphraseCacheService extends Service { private void updateService() { if (mPassphraseCache.size() > 0) { - createNotificationChannelsIfNecessary(); startForeground(NotificationIds.PASSPHRASE_CACHE, getNotification()); } else { // stop whole service if no cached passphrases remaining @@ -505,7 +501,9 @@ public class PassphraseCacheService extends Service { } private Notification getNotification() { - Builder builder = new Builder(this, NotificationChannels.PASSPHRASE_CACHE); + NotificationChannelManager.getInstance(this).createNotificationChannelsIfNecessary(); + + Builder builder = new Builder(this, NotificationChannelManager.PASSPHRASE_CACHE); builder.setSmallIcon(R.drawable.ic_stat_notify_24dp) .setColor(getResources().getColor(R.color.primary)) .setContentTitle(getResources().getQuantityString(R.plurals.passp_cache_notif_n_keys, @@ -547,22 +545,6 @@ public class PassphraseCacheService extends Service { return builder.build(); } - private void createNotificationChannelsIfNecessary() { - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { - return; - } - - NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); - if (notificationManager == null) { - return; - } - - CharSequence name = getString(R.string.notify_channel_passcache); - NotificationChannel channel = new NotificationChannel( - NotificationChannels.PASSPHRASE_CACHE, name, NotificationManager.IMPORTANCE_LOW); - notificationManager.createNotificationChannel(channel); - } - @Override public void onCreate() { super.onCreate(); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysFileFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysFileFragment.java index a1165f217..853d82e6a 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysFileFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysFileFragment.java @@ -17,6 +17,9 @@ package org.sufficientlysecure.keychain.ui; + +import java.io.IOException; + import android.app.Activity; import android.content.Context; import android.content.Intent; @@ -43,8 +46,6 @@ import org.sufficientlysecure.keychain.ui.util.PermissionsUtil; import org.sufficientlysecure.keychain.util.FileHelper; import timber.log.Timber; -import java.io.IOException; - public class ImportKeysFileFragment extends Fragment { private Activity mActivity; diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java index 5e7519a7c..4b0a17b4a 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java @@ -75,7 +75,6 @@ import org.sufficientlysecure.keychain.ui.base.CryptoOperationHelper; import org.sufficientlysecure.keychain.ui.base.RecyclerFragment; import org.sufficientlysecure.keychain.ui.keyview.GenericViewModel; import org.sufficientlysecure.keychain.ui.keyview.ViewKeyActivity; -import org.sufficientlysecure.keychain.ui.util.KeyInfoFormatter; import org.sufficientlysecure.keychain.ui.util.Notify; import org.sufficientlysecure.keychain.ui.util.Notify.Style; import org.sufficientlysecure.keychain.util.FabContainer; diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/OrbotRequiredDialogActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/OrbotRequiredDialogActivity.java index f96107619..ceee9e8ef 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/OrbotRequiredDialogActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/OrbotRequiredDialogActivity.java @@ -33,6 +33,7 @@ import android.support.v4.app.NotificationCompat; import android.view.ContextThemeWrapper; import org.sufficientlysecure.keychain.Constants.NotificationIds; +import org.sufficientlysecure.keychain.NotificationChannelManager; import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.compatibility.DialogFragmentWorkaround; import org.sufficientlysecure.keychain.service.input.CryptoInputParcel; @@ -185,7 +186,9 @@ public class OrbotRequiredDialogActivity extends FragmentActivity } private static Notification createOrbotNotification(Context context) { - NotificationCompat.Builder builder = new NotificationCompat.Builder(context); + NotificationChannelManager.getInstance(context).createNotificationChannelsIfNecessary(); + + NotificationCompat.Builder builder = new NotificationCompat.Builder(context, NotificationChannelManager.ORBOT); builder.setSmallIcon(R.drawable.ic_stat_notify_24dp) .setLargeIcon(ResourceUtils.getDrawableAsNotificationBitmap(context, R.mipmap.ic_launcher)) .setContentTitle(context.getString(R.string.keyserver_sync_orbot_notif_title)) diff --git a/OpenKeychain/src/main/res/values/strings.xml b/OpenKeychain/src/main/res/values/strings.xml index 88066be66..6ff54cc39 100644 --- a/OpenKeychain/src/main/res/values/strings.xml +++ b/OpenKeychain/src/main/res/values/strings.xml @@ -2027,7 +2027,9 @@ View Keyserver update + Permission requests Passphrase cache + Orbot status Updating keys… Finished updating %d keys Key %d / %d