From 42f91931799e941e2190f530f9a94cfdf521be52 Mon Sep 17 00:00:00 2001 From: M66B Date: Thu, 16 May 2019 07:43:18 +0200 Subject: [PATCH] Simplify account notification management --- .../java/eu/faircode/email/ActivitySetup.java | 6 ++--- .../java/eu/faircode/email/EntityAccount.java | 24 +++++++++---------- .../eu/faircode/email/FragmentAccount.java | 11 ++++----- .../eu/faircode/email/ServiceSynchronize.java | 9 ++++--- 4 files changed, 23 insertions(+), 27 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/ActivitySetup.java b/app/src/main/java/eu/faircode/email/ActivitySetup.java index 0090788486..811d8b3421 100644 --- a/app/src/main/java/eu/faircode/email/ActivitySetup.java +++ b/app/src/main/java/eu/faircode/email/ActivitySetup.java @@ -770,9 +770,9 @@ public class ActivitySetup extends ActivityBilling implements FragmentManager.On account.id = db.account().insertAccount(account); Log.i("Imported account=" + account.name); - if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) - if (account.notify) - account.createNotificationChannel(context); + account.deleteNotificationChannel(context); + if (account.notify) + account.createNotificationChannel(context); JSONArray jidentities = (JSONArray) jaccount.get("identities"); for (int i = 0; i < jidentities.length(); i++) { diff --git a/app/src/main/java/eu/faircode/email/EntityAccount.java b/app/src/main/java/eu/faircode/email/EntityAccount.java index b759471ba8..dce09bbc22 100644 --- a/app/src/main/java/eu/faircode/email/EntityAccount.java +++ b/app/src/main/java/eu/faircode/email/EntityAccount.java @@ -23,10 +23,8 @@ import android.app.Notification; import android.app.NotificationChannel; import android.app.NotificationManager; import android.content.Context; -import android.os.Build; import androidx.annotation.NonNull; -import androidx.annotation.RequiresApi; import androidx.room.Entity; import androidx.room.PrimaryKey; @@ -105,20 +103,22 @@ public class EntityAccount extends EntityOrder implements Serializable { return "notification" + (account == 0 ? "" : "." + account); } - @RequiresApi(api = Build.VERSION_CODES.O) void createNotificationChannel(Context context) { - NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); - NotificationChannel channel = new NotificationChannel( - getNotificationChannelId(id), name, - NotificationManager.IMPORTANCE_HIGH); - channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE); - nm.createNotificationChannel(channel); + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { + NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); + NotificationChannel channel = new NotificationChannel( + getNotificationChannelId(id), name, + NotificationManager.IMPORTANCE_HIGH); + channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE); + nm.createNotificationChannel(channel); + } } - @RequiresApi(api = Build.VERSION_CODES.O) void deleteNotificationChannel(Context context) { - NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); - nm.deleteNotificationChannel(getNotificationChannelId(id)); + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { + NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); + nm.deleteNotificationChannel(getNotificationChannelId(id)); + } } @Override diff --git a/app/src/main/java/eu/faircode/email/FragmentAccount.java b/app/src/main/java/eu/faircode/email/FragmentAccount.java index fa7ab3459e..eb9cdd13de 100644 --- a/app/src/main/java/eu/faircode/email/FragmentAccount.java +++ b/app/src/main/java/eu/faircode/email/FragmentAccount.java @@ -965,13 +965,10 @@ public class FragmentAccount extends FragmentBase { EntityLog.log(context, (update ? "Updated" : "Added") + " account=" + account.name); // Make sure the channel exists on commit - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) - if (account.notify) { - // Add or update notification channel - account.deleteNotificationChannel(context); - account.createNotificationChannel(context); - } else if (!account.synchronize) - account.deleteNotificationChannel(context); + if (account.notify) + account.createNotificationChannel(context); + else if (!account.synchronize) + account.deleteNotificationChannel(context); List folders = new ArrayList<>(); diff --git a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java index 564c5f09d0..43e64527ce 100644 --- a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java +++ b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java @@ -447,11 +447,10 @@ public class ServiceSynchronize extends LifecycleService { // Start monitoring accounts List accounts = db.account().getSynchronizingAccounts(); for (final EntityAccount account : accounts) { - if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) - if (account.notify) - account.createNotificationChannel(ServiceSynchronize.this); - else - account.deleteNotificationChannel(ServiceSynchronize.this); + if (account.notify) + account.createNotificationChannel(ServiceSynchronize.this); + else + account.deleteNotificationChannel(ServiceSynchronize.this); Log.i(account.host + "/" + account.user + " run"); final Core.State astate = new Core.State(state);