Introduce NotificationChannelManager

This commit is contained in:
Vincent Breitmoser
2018-07-16 13:24:37 +02:00
parent ffa5b84593
commit 555f335dbc
9 changed files with 75 additions and 49 deletions

View File

@@ -116,11 +116,6 @@ public final class Constants {
public static final int KEYSERVER_SYNC = 3; 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 class Pref {
public static final String PASSPHRASE_CACHE_SUBS = "passphraseCacheSubs"; public static final String PASSPHRASE_CACHE_SUBS = "passphraseCacheSubs";
public static final String PASSPHRASE_CACHE_LAST_TTL = "passphraseCacheLastTtl"; public static final String PASSPHRASE_CACHE_LAST_TTL = "passphraseCacheLastTtl";

View File

@@ -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);
}
}

View File

@@ -1,25 +1,23 @@
package org.sufficientlysecure.keychain.keysync; package org.sufficientlysecure.keychain.keysync;
import android.app.NotificationChannel;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.content.Context; import android.content.Context;
import android.os.Build;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationCompat.Builder; import android.support.v4.app.NotificationCompat.Builder;
import android.support.v4.os.CancellationSignal; import android.support.v4.os.CancellationSignal;
import androidx.work.Worker; import androidx.work.Worker;
import org.sufficientlysecure.keychain.Constants.NotificationChannels;
import org.sufficientlysecure.keychain.Constants.NotificationIds; import org.sufficientlysecure.keychain.Constants.NotificationIds;
import org.sufficientlysecure.keychain.NotificationChannelManager;
import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.daos.KeyWritableRepository;
import org.sufficientlysecure.keychain.network.orbot.OrbotHelper; import org.sufficientlysecure.keychain.network.orbot.OrbotHelper;
import org.sufficientlysecure.keychain.operations.KeySyncOperation; import org.sufficientlysecure.keychain.operations.KeySyncOperation;
import org.sufficientlysecure.keychain.operations.KeySyncParcel; import org.sufficientlysecure.keychain.operations.KeySyncParcel;
import org.sufficientlysecure.keychain.operations.results.ImportKeyResult; import org.sufficientlysecure.keychain.operations.results.ImportKeyResult;
import org.sufficientlysecure.keychain.pgp.Progressable; import org.sufficientlysecure.keychain.pgp.Progressable;
import org.sufficientlysecure.keychain.daos.KeyWritableRepository;
import org.sufficientlysecure.keychain.service.input.CryptoInputParcel; import org.sufficientlysecure.keychain.service.input.CryptoInputParcel;
import org.sufficientlysecure.keychain.ui.OrbotRequiredDialogActivity; import org.sufficientlysecure.keychain.ui.OrbotRequiredDialogActivity;
import org.sufficientlysecure.keychain.util.ResourceUtils; import org.sufficientlysecure.keychain.util.ResourceUtils;
@@ -81,9 +79,9 @@ public class KeyserverSyncWorker extends Worker {
return null; 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) .setSmallIcon(R.drawable.ic_stat_notify_24dp)
.setLargeIcon(ResourceUtils.getDrawableAsNotificationBitmap(context, R.mipmap.ic_launcher)) .setLargeIcon(ResourceUtils.getDrawableAsNotificationBitmap(context, R.mipmap.ic_launcher))
.setContentTitle(context.getString(R.string.notify_title_keysync)) .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 @Override
public void onStopped() { public void onStopped() {
super.onStopped(); super.onStopped();

View File

@@ -17,6 +17,7 @@
package org.sufficientlysecure.keychain.service; package org.sufficientlysecure.keychain.service;
import android.Manifest; import android.Manifest;
import android.accounts.Account; import android.accounts.Account;
import android.app.PendingIntent; import android.app.PendingIntent;
@@ -38,6 +39,7 @@ import android.support.v4.content.ContextCompat;
import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.KeychainApplication; import org.sufficientlysecure.keychain.KeychainApplication;
import org.sufficientlysecure.keychain.NotificationChannelManager;
import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.ui.SettingsActivity; import org.sufficientlysecure.keychain.ui.SettingsActivity;
import org.sufficientlysecure.keychain.util.ContactHelper; import org.sufficientlysecure.keychain.util.ContactHelper;
@@ -73,6 +75,8 @@ public class ContactSyncAdapterService extends Service {
// deactivate sync // deactivate sync
ContentResolver.setSyncAutomatically(account, authority, false); ContentResolver.setSyncAutomatically(account, authority, false);
NotificationChannelManager.getInstance(getContext()).createNotificationChannelsIfNecessary();
// show notification linking to sync settings // show notification linking to sync settings
Intent resultIntent = new Intent(ContactSyncAdapterService.this, SettingsActivity.class); Intent resultIntent = new Intent(ContactSyncAdapterService.this, SettingsActivity.class);
resultIntent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT, resultIntent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT,
@@ -85,7 +89,7 @@ public class ContactSyncAdapterService extends Service {
PendingIntent.FLAG_UPDATE_CURRENT PendingIntent.FLAG_UPDATE_CURRENT
); );
NotificationCompat.Builder mBuilder = NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(ContactSyncAdapterService.this) new NotificationCompat.Builder(ContactSyncAdapterService.this, NotificationChannelManager.PERMISSION_REQUESTS)
.setAutoCancel(true) .setAutoCancel(true)
.setSmallIcon(R.drawable.ic_stat_notify_24dp) .setSmallIcon(R.drawable.ic_stat_notify_24dp)
.setColor(getResources().getColor(R.color.primary)) .setColor(getResources().getColor(R.color.primary))

View File

@@ -22,8 +22,6 @@ import java.util.Date;
import android.app.AlarmManager; import android.app.AlarmManager;
import android.app.Notification; import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.app.Service; import android.app.Service;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
@@ -31,7 +29,6 @@ import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter; import android.content.IntentFilter;
import android.os.Binder; import android.os.Binder;
import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.os.HandlerThread; import android.os.HandlerThread;
@@ -45,11 +42,11 @@ import android.support.v4.app.NotificationCompat.InboxStyle;
import android.support.v4.util.LongSparseArray; import android.support.v4.util.LongSparseArray;
import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.Constants.NotificationChannels;
import org.sufficientlysecure.keychain.Constants.NotificationIds; import org.sufficientlysecure.keychain.Constants.NotificationIds;
import org.sufficientlysecure.keychain.NotificationChannelManager;
import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.pgp.CanonicalizedSecretKey.SecretKeyType;
import org.sufficientlysecure.keychain.daos.KeyRepository; import org.sufficientlysecure.keychain.daos.KeyRepository;
import org.sufficientlysecure.keychain.pgp.CanonicalizedSecretKey.SecretKeyType;
import org.sufficientlysecure.keychain.util.Passphrase; import org.sufficientlysecure.keychain.util.Passphrase;
import org.sufficientlysecure.keychain.util.Preferences; import org.sufficientlysecure.keychain.util.Preferences;
import timber.log.Timber; import timber.log.Timber;
@@ -494,7 +491,6 @@ public class PassphraseCacheService extends Service {
private void updateService() { private void updateService() {
if (mPassphraseCache.size() > 0) { if (mPassphraseCache.size() > 0) {
createNotificationChannelsIfNecessary();
startForeground(NotificationIds.PASSPHRASE_CACHE, getNotification()); startForeground(NotificationIds.PASSPHRASE_CACHE, getNotification());
} else { } else {
// stop whole service if no cached passphrases remaining // stop whole service if no cached passphrases remaining
@@ -505,7 +501,9 @@ public class PassphraseCacheService extends Service {
} }
private Notification getNotification() { 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) builder.setSmallIcon(R.drawable.ic_stat_notify_24dp)
.setColor(getResources().getColor(R.color.primary)) .setColor(getResources().getColor(R.color.primary))
.setContentTitle(getResources().getQuantityString(R.plurals.passp_cache_notif_n_keys, .setContentTitle(getResources().getQuantityString(R.plurals.passp_cache_notif_n_keys,
@@ -547,22 +545,6 @@ public class PassphraseCacheService extends Service {
return builder.build(); 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 @Override
public void onCreate() { public void onCreate() {
super.onCreate(); super.onCreate();

View File

@@ -17,6 +17,9 @@
package org.sufficientlysecure.keychain.ui; package org.sufficientlysecure.keychain.ui;
import java.io.IOException;
import android.app.Activity; import android.app.Activity;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
@@ -43,8 +46,6 @@ import org.sufficientlysecure.keychain.ui.util.PermissionsUtil;
import org.sufficientlysecure.keychain.util.FileHelper; import org.sufficientlysecure.keychain.util.FileHelper;
import timber.log.Timber; import timber.log.Timber;
import java.io.IOException;
public class ImportKeysFileFragment extends Fragment { public class ImportKeysFileFragment extends Fragment {
private Activity mActivity; private Activity mActivity;

View File

@@ -75,7 +75,6 @@ import org.sufficientlysecure.keychain.ui.base.CryptoOperationHelper;
import org.sufficientlysecure.keychain.ui.base.RecyclerFragment; import org.sufficientlysecure.keychain.ui.base.RecyclerFragment;
import org.sufficientlysecure.keychain.ui.keyview.GenericViewModel; import org.sufficientlysecure.keychain.ui.keyview.GenericViewModel;
import org.sufficientlysecure.keychain.ui.keyview.ViewKeyActivity; 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;
import org.sufficientlysecure.keychain.ui.util.Notify.Style; import org.sufficientlysecure.keychain.ui.util.Notify.Style;
import org.sufficientlysecure.keychain.util.FabContainer; import org.sufficientlysecure.keychain.util.FabContainer;

View File

@@ -33,6 +33,7 @@ import android.support.v4.app.NotificationCompat;
import android.view.ContextThemeWrapper; import android.view.ContextThemeWrapper;
import org.sufficientlysecure.keychain.Constants.NotificationIds; import org.sufficientlysecure.keychain.Constants.NotificationIds;
import org.sufficientlysecure.keychain.NotificationChannelManager;
import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.compatibility.DialogFragmentWorkaround; import org.sufficientlysecure.keychain.compatibility.DialogFragmentWorkaround;
import org.sufficientlysecure.keychain.service.input.CryptoInputParcel; import org.sufficientlysecure.keychain.service.input.CryptoInputParcel;
@@ -185,7 +186,9 @@ public class OrbotRequiredDialogActivity extends FragmentActivity
} }
private static Notification createOrbotNotification(Context context) { 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) builder.setSmallIcon(R.drawable.ic_stat_notify_24dp)
.setLargeIcon(ResourceUtils.getDrawableAsNotificationBitmap(context, R.mipmap.ic_launcher)) .setLargeIcon(ResourceUtils.getDrawableAsNotificationBitmap(context, R.mipmap.ic_launcher))
.setContentTitle(context.getString(R.string.keyserver_sync_orbot_notif_title)) .setContentTitle(context.getString(R.string.keyserver_sync_orbot_notif_title))

View File

@@ -2027,7 +2027,9 @@
<string name="snack_keylist_clipboard_action">View</string> <string name="snack_keylist_clipboard_action">View</string>
<string name="notify_channel_keysync">Keyserver update</string> <string name="notify_channel_keysync">Keyserver update</string>
<string name="notify_channel_permission">Permission requests</string>
<string name="notify_channel_passcache">Passphrase cache</string> <string name="notify_channel_passcache">Passphrase cache</string>
<string name="notify_channel_orbot">Orbot status</string>
<string name="notify_title_keysync">Updating keys…</string> <string name="notify_title_keysync">Updating keys…</string>
<string name="notify_title_keysync_finished">Finished updating %d keys</string> <string name="notify_title_keysync_finished">Finished updating %d keys</string>
<string name="notify_content_keysync">Key %d / %d</string> <string name="notify_content_keysync">Key %d / %d</string>