diff --git a/app/src/main/java/eu/faircode/email/DaoAccount.java b/app/src/main/java/eu/faircode/email/DaoAccount.java index 280bf00019..95d13510b1 100644 --- a/app/src/main/java/eu/faircode/email/DaoAccount.java +++ b/app/src/main/java/eu/faircode/email/DaoAccount.java @@ -35,6 +35,9 @@ public interface DaoAccount { @Query("SELECT * FROM account WHERE synchronize = :synchronize") List getAccounts(boolean synchronize); + @Query("SELECT * FROM account WHERE tbd = 1") + List getAccountsTbd(); + @Query("SELECT * FROM account") LiveData> liveAccounts(); diff --git a/app/src/main/java/eu/faircode/email/EntityAccount.java b/app/src/main/java/eu/faircode/email/EntityAccount.java index e96b6fc30d..5037c33ba9 100644 --- a/app/src/main/java/eu/faircode/email/EntityAccount.java +++ b/app/src/main/java/eu/faircode/email/EntityAccount.java @@ -19,10 +19,17 @@ package eu.faircode.email; Copyright 2018 by Marcel Bokhorst (M66B) */ +import android.app.Notification; +import android.app.NotificationChannel; +import android.app.NotificationManager; +import android.content.Context; +import android.os.Build; + import org.json.JSONException; import org.json.JSONObject; import androidx.annotation.NonNull; +import androidx.annotation.RequiresApi; import androidx.room.Entity; import androidx.room.PrimaryKey; @@ -67,6 +74,26 @@ public class EntityAccount { public String error; public Long last_connected; + static String getNotificationChannelName(long account) { + return "notification." + account; + } + + @RequiresApi(api = Build.VERSION_CODES.O) + void createNotificationChannel(Context context) { + NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); + NotificationChannel notification = new NotificationChannel( + getNotificationChannelName(id), name, + NotificationManager.IMPORTANCE_HIGH); + notification.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE); + nm.createNotificationChannel(notification); + } + + @RequiresApi(api = Build.VERSION_CODES.O) + void deleteNotificationChannel(Context context) { + NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); + nm.deleteNotificationChannel(getNotificationChannelName(id)); + } + public JSONObject toJSON() throws JSONException { JSONObject json = new JSONObject(); json.put("name", name); diff --git a/app/src/main/java/eu/faircode/email/FragmentAccount.java b/app/src/main/java/eu/faircode/email/FragmentAccount.java index b9d69f9061..fee495d1c1 100644 --- a/app/src/main/java/eu/faircode/email/FragmentAccount.java +++ b/app/src/main/java/eu/faircode/email/FragmentAccount.java @@ -676,7 +676,7 @@ public class FragmentAccount extends FragmentEx { !user.equals(account.user) || !password.equals(account.password))); boolean reload = (check || account == null || account.synchronize != synchronize || - account.poll_interval.equals(Integer.parseInt(interval))); + !account.poll_interval.equals(Integer.parseInt(interval))); // Check IMAP server if (check) { @@ -750,6 +750,15 @@ public class FragmentAccount extends FragmentEx { else account.id = db.account().insertAccount(account); + // Make sure the channel exists on commit + if (android.os.Build.VERSION.SDK_INT >= android.os.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); + List folders = new ArrayList<>(); EntityFolder inbox = new EntityFolder(); diff --git a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java index 3998f7c970..7e74b33a81 100644 --- a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java +++ b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java @@ -461,12 +461,14 @@ public class ServiceSynchronize extends LifecycleService { clear.setAction("clear"); PendingIntent piClear = PendingIntent.getService(this, PI_CLEAR, clear, PendingIntent.FLAG_UPDATE_CURRENT); + String channelName = (account == 0 ? "notification" : EntityAccount.getNotificationChannelName(account)); + // Build public notification Notification.Builder pbuilder; if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) pbuilder = new Notification.Builder(this); else - pbuilder = new Notification.Builder(this, "notification"); + pbuilder = new Notification.Builder(this, channelName); pbuilder .setSmallIcon(R.drawable.baseline_email_white_24) @@ -487,7 +489,8 @@ public class ServiceSynchronize extends LifecycleService { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) builder = new Notification.Builder(this); else - builder = new Notification.Builder(this, "notification"); + builder = new Notification.Builder(this, channelName); + builder .setSmallIcon(R.drawable.baseline_email_white_24) @@ -587,7 +590,7 @@ public class ServiceSynchronize extends LifecycleService { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) mbuilder = new Notification.Builder(this); else - mbuilder = new Notification.Builder(this, "notification"); + mbuilder = new Notification.Builder(this, channelName); String folderName = message.folderDisplay == null ? Helper.localizeFolderName(this, message.folderName) @@ -2593,6 +2596,12 @@ public class ServiceSynchronize extends LifecycleService { // Start monitoring accounts List accounts = db.account().getAccounts(true); 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); + Log.i(Helper.TAG, account.host + "/" + account.user + " run"); final ServiceState astate = new ServiceState(); astate.runnable(new Runnable() { @@ -2685,6 +2694,13 @@ public class ServiceSynchronize extends LifecycleService { stop(); DB db = DB.getInstance(ServiceSynchronize.this); + + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { + NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); + for (EntityAccount account : db.account().getAccountsTbd()) + nm.deleteNotificationChannel(EntityAccount.getNotificationChannelName(account.id)); + } + int accounts = db.account().deleteAccountsTbd(); int identities = db.identity().deleteIdentitiesTbd(); if (accounts > 0 || identities > 0)