From e883ef4cd5045824660973462b70682304b85d6e Mon Sep 17 00:00:00 2001 From: M66B Date: Tue, 12 Nov 2019 10:57:26 +0100 Subject: [PATCH] Notification improvements --- .../java/eu/faircode/email/ActivitySetup.java | 10 +++++- .../eu/faircode/email/AdapterMessage.java | 4 +-- app/src/main/java/eu/faircode/email/Core.java | 35 +++++++++---------- .../java/eu/faircode/email/EntityMessage.java | 9 +++++ 4 files changed, 36 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/ActivitySetup.java b/app/src/main/java/eu/faircode/email/ActivitySetup.java index a213cae4d5..148915cc73 100644 --- a/app/src/main/java/eu/faircode/email/ActivitySetup.java +++ b/app/src/main/java/eu/faircode/email/ActivitySetup.java @@ -737,6 +737,7 @@ public class ActivitySetup extends ActivityBase implements FragmentManager.OnBac if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { account.deleteNotificationChannel(context); + if (account.notify) if (jaccount.has("channel")) { NotificationChannelGroup group = new NotificationChannelGroup("group." + account.id, account.name); @@ -789,12 +790,15 @@ public class ActivitySetup extends ActivityBase implements FragmentManager.OnBac account.move_to = folder.id; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + String channelId = EntityFolder.getNotificationChannelId(folder.id); + nm.deleteNotificationChannel(channelId); + if (jfolder.has("channel")) { NotificationChannelGroup group = new NotificationChannelGroup("group." + account.id, account.name); nm.createNotificationChannelGroup(group); JSONObject jchannel = (JSONObject) jfolder.get("channel"); - jchannel.put("id", EntityFolder.getNotificationChannelId(folder.id)); + jchannel.put("id", channelId); jchannel.put("group", group.getId()); nm.createNotificationChannel(channelFromJSON(context, jchannel)); @@ -928,6 +932,10 @@ public class ActivitySetup extends ActivityBase implements FragmentManager.OnBac JSONArray jchannels = jimport.getJSONArray("channels"); for (int i = 0; i < jchannels.length(); i++) { JSONObject jchannel = (JSONObject) jchannels.get(i); + + String channelId = jchannel.getString("id"); + nm.deleteNotificationChannel(channelId); + nm.createNotificationChannel(channelFromJSON(context, jchannel)); Log.i("Imported contact channel=" + jchannel); diff --git a/app/src/main/java/eu/faircode/email/AdapterMessage.java b/app/src/main/java/eu/faircode/email/AdapterMessage.java index 391ad12f15..4018f5f4b4 100644 --- a/app/src/main/java/eu/faircode/email/AdapterMessage.java +++ b/app/src/main/java/eu/faircode/email/AdapterMessage.java @@ -2318,8 +2318,7 @@ public class AdapterMessage extends RecyclerView.Adapter Build.VERSION_CODES.O && - message.notifying == 0 && message.from != null && message.from.length > 0) { - InternetAddress from = (InternetAddress) message.from[0]; - NotificationChannel channel = nm.getNotificationChannel( - "notification." + from.getAddress().toLowerCase(Locale.ROOT)); - if (channel != null && channel.getImportance() == NotificationManager.IMPORTANCE_NONE) - continue; + if (message.notifying == 0 && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && pro) { + String channelId = message.getNotificationChannelId(); + if (channelId != null) { + NotificationChannel channel = nm.getNotificationChannel(channelId); + if (channel != null && channel.getImportance() == NotificationManager.IMPORTANCE_NONE) + continue; + } } long group = (pro && message.accountNotify ? message.account : 0); @@ -2911,25 +2910,23 @@ class Core { PendingIntent piIgnore = PendingIntent.getService(context, ServiceUI.PI_IGNORED, ignore, PendingIntent.FLAG_UPDATE_CURRENT); // Get channel name - String channelName = null; - if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O) { + String channelName = EntityAccount.getNotificationChannelId(0); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && pro) { NotificationChannel channel = null; - if (message.from != null && message.from.length > 0) { - InternetAddress from = (InternetAddress) message.from[0]; - channel = nm.getNotificationChannel( - "notification." + from.getAddress().toLowerCase(Locale.ROOT)); - } + String channelId = message.getNotificationChannelId(); + if (channelId != null) + channel = nm.getNotificationChannel(channelId); if (channel == null) channel = nm.getNotificationChannel(EntityFolder.getNotificationChannelId(message.folder)); - if (channel != null) + if (channel == null) { + if (message.accountNotify) + channelName = EntityAccount.getNotificationChannelId(message.account); + } else channelName = channel.getId(); } - if (channelName == null) - channelName = EntityAccount.getNotificationChannelId( - pro && message.accountNotify ? message.account : 0); NotificationCompat.Builder mbuilder = new NotificationCompat.Builder(context, channelName) diff --git a/app/src/main/java/eu/faircode/email/EntityMessage.java b/app/src/main/java/eu/faircode/email/EntityMessage.java index 2a19523e13..cebad26888 100644 --- a/app/src/main/java/eu/faircode/email/EntityMessage.java +++ b/app/src/main/java/eu/faircode/email/EntityMessage.java @@ -37,10 +37,12 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Date; import java.util.List; +import java.util.Locale; import java.util.Objects; import java.util.UUID; import javax.mail.Address; +import javax.mail.internet.InternetAddress; import static androidx.room.ForeignKey.CASCADE; import static androidx.room.ForeignKey.SET_NULL; @@ -201,6 +203,13 @@ public class EntityMessage implements Serializable { return addresses.toArray(new Address[0]); } + String getNotificationChannelId() { + if (from == null || from.length == 0) + return null; + InternetAddress sender = (InternetAddress) from[0]; + return "notification." + sender.getAddress().toLowerCase(Locale.ROOT); + } + static File getFile(Context context, Long id) { File dir = new File(context.getFilesDir(), "messages"); if (!dir.exists())