From 91387201503fefe72ca5de3218f97e14c0fda036 Mon Sep 17 00:00:00 2001 From: M66B Date: Fri, 12 Jul 2019 11:11:01 +0200 Subject: [PATCH] Workaround notification sound before Android 8 --- app/src/main/java/eu/faircode/email/Core.java | 44 +++++++++++++------ 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/Core.java b/app/src/main/java/eu/faircode/email/Core.java index 3ab02a4226..a63de54b97 100644 --- a/app/src/main/java/eu/faircode/email/Core.java +++ b/app/src/main/java/eu/faircode/email/Core.java @@ -1865,6 +1865,7 @@ class Core { final List add = new ArrayList<>(); final List remove = groupNotifying.get(group); + int updates = 0; for (Notification notification : notifications) { Long id = notification.extras.getLong("id", 0); if (id != 0) @@ -1872,16 +1873,20 @@ class Core { remove.remove(id); Log.i("Notify existing=" + id); } else { - remove.remove(-id); + if (remove.contains(-id)) { + updates++; + remove.remove(-id); + } add.add(id); Log.i("Notify adding=" + id); } } Log.i("Notify group=" + group + " count=" + notifications.size() + - " added=" + add.size() + " removed=" + remove.size()); + " added=" + add.size() + " removed=" + remove.size() + " updates=" + updates); - if (notifications.size() == 0) { + if (notifications.size() == 0 || + (Build.VERSION.SDK_INT < Build.VERSION_CODES.O && add.size() - updates > 0)) { String tag = "unseen." + group + "." + 0; Log.i("Notify cancel tag=" + tag); nm.cancel(tag, 1); @@ -1983,7 +1988,10 @@ class Core { .setVisibility(NotificationCompat.VISIBILITY_PUBLIC) .setGroup(group) .setGroupSummary(true) - .setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_CHILDREN); + .setGroupAlertBehavior(Build.VERSION.SDK_INT < Build.VERSION_CODES.O + ? NotificationCompat.GROUP_ALERT_SUMMARY + : NotificationCompat.GROUP_ALERT_CHILDREN) + .setOnlyAlertOnce(true); Notification pub = builder.build(); builder @@ -2006,6 +2014,19 @@ class Core { .setSummaryText(title)); } + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { + if (light) { + builder.setLights(Color.WHITE, 1000, 1000); + Log.i("Notify light enabled"); + } + + Uri uri = (sound == null ? null : Uri.parse(sound)); + if (uri == null || "file".equals(uri.getScheme())) + uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); + builder.setSound(uri); + Log.i("Notify sound=" + uri); + } + notifications.add(builder.build()); // Message notifications @@ -2060,7 +2081,9 @@ class Core { .setVisibility(NotificationCompat.VISIBILITY_PRIVATE) .setGroup(group) .setGroupSummary(false) - .setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_CHILDREN) + .setGroupAlertBehavior(Build.VERSION.SDK_INT < Build.VERSION_CODES.O + ? NotificationCompat.GROUP_ALERT_SUMMARY + : NotificationCompat.GROUP_ALERT_CHILDREN) .setOnlyAlertOnce(true); if (biometrics) @@ -2174,15 +2197,8 @@ class Core { } } - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { - if (light) - mbuilder.setLights(Color.WHITE, 1000, 1000); - - Uri uri = (sound == null ? null : Uri.parse(sound)); - if (uri == null || "file".equals(uri.getScheme())) - uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); - mbuilder.setSound(uri); - } + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) + mbuilder.setSound(null); notifications.add(mbuilder.build()); }