From 9366320529436c3f64185166c244cb26e2d88b3b Mon Sep 17 00:00:00 2001 From: M66B Date: Sat, 25 Jul 2020 19:54:28 +0200 Subject: [PATCH] Added option to disable notifying in the foreground --- .../java/eu/faircode/email/ActivityView.java | 5 +++++ app/src/main/java/eu/faircode/email/Core.java | 14 +++++++++++--- .../email/FragmentOptionsNotifications.java | 13 ++++++++++++- .../eu/faircode/email/ServiceSynchronize.java | 18 +++++++++++++++++- .../layout/fragment_options_notifications.xml | 13 ++++++++++++- app/src/main/res/values/strings.xml | 1 + 6 files changed, 58 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/ActivityView.java b/app/src/main/java/eu/faircode/email/ActivityView.java index 530f56772e..78354d8e29 100644 --- a/app/src/main/java/eu/faircode/email/ActivityView.java +++ b/app/src/main/java/eu/faircode/email/ActivityView.java @@ -625,6 +625,8 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB iff.addAction(ACTION_EDIT_RULE); lbm.registerReceiver(receiver, iff); + ServiceSynchronize.state(this, true); + checkUpdate(false); checkIntent(); } @@ -632,8 +634,11 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB @Override protected void onPause() { super.onPause(); + LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this); lbm.unregisterReceiver(receiver); + + ServiceSynchronize.state(this, false); } diff --git a/app/src/main/java/eu/faircode/email/Core.java b/app/src/main/java/eu/faircode/email/Core.java index 3912aa5d91..ad7a02b5fc 100644 --- a/app/src/main/java/eu/faircode/email/Core.java +++ b/app/src/main/java/eu/faircode/email/Core.java @@ -3163,7 +3163,7 @@ class Core { } } - static void notifyMessages(Context context, List messages, Map> groupNotifying) { + static void notifyMessages(Context context, List messages, Map> groupNotifying, boolean foreground) { if (messages == null) messages = new ArrayList<>(); @@ -3171,7 +3171,10 @@ class Core { if (nm == null) return; + DB db = DB.getInstance(context); + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + boolean notify_background_only = prefs.getBoolean("notify_background_only", false); boolean notify_summary = prefs.getBoolean("notify_summary", false); boolean notify_preview = prefs.getBoolean("notify_preview", true); boolean notify_preview_only = prefs.getBoolean("notify_preview_only", false); @@ -3208,6 +3211,13 @@ class Core { if (notify_preview && notify_preview_only && !message.content) continue; + if (foreground && notify_background_only && message.notifying >= 0) { + Log.i("Notify foreground=" + message.id + " notifying=" + message.notifying); + if (message.notifying == 0) + db.message().setMessageNotifying(message.id, 1); + continue; + } + long group = (pro && message.accountNotify ? message.account : 0); if (!message.folderUnified) group = -message.folder; @@ -3275,8 +3285,6 @@ class Core { Log.i("Notify group=" + group + " count=" + notifications.size() + " added=" + add.size() + " removed=" + remove.size()); - DB db = DB.getInstance(context); - if (notifications.size() == 0) { String tag = "unseen." + group + "." + 0; Log.i("Notify cancel tag=" + tag); diff --git a/app/src/main/java/eu/faircode/email/FragmentOptionsNotifications.java b/app/src/main/java/eu/faircode/email/FragmentOptionsNotifications.java index 31088d8a55..3ef805c8b3 100644 --- a/app/src/main/java/eu/faircode/email/FragmentOptionsNotifications.java +++ b/app/src/main/java/eu/faircode/email/FragmentOptionsNotifications.java @@ -70,6 +70,7 @@ public class FragmentOptionsNotifications extends FragmentBase implements Shared private SwitchCompat swBadge; private SwitchCompat swUnseenIgnored; + private SwitchCompat swNotifyBackgroundOnly; private SwitchCompat swNotifyKnown; private TextView tvNotifyKnownPro; private SwitchCompat swNotifySummary; @@ -93,7 +94,7 @@ public class FragmentOptionsNotifications extends FragmentBase implements Shared "notify_flag", "notify_seen", "notify_snooze", "light", "sound", "badge", "unseen_ignored", - "notify_known", "notify_summary", "notify_remove", "notify_clear", + "notify_background_only", "notify_known", "notify_summary", "notify_remove", "notify_clear", "notify_preview", "notify_preview_all", "notify_preview_only", "wearable_preview", "biometrics_notify", "alert_once" @@ -130,6 +131,7 @@ public class FragmentOptionsNotifications extends FragmentBase implements Shared swBadge = view.findViewById(R.id.swBadge); swUnseenIgnored = view.findViewById(R.id.swUnseenIgnored); + swNotifyBackgroundOnly = view.findViewById(R.id.swNotifyBackgroundOnly); swNotifyKnown = view.findViewById(R.id.swNotifyKnown); tvNotifyKnownPro = view.findViewById(R.id.tvNotifyKnownPro); swNotifySummary = view.findViewById(R.id.swNotifySummary); @@ -308,6 +310,14 @@ public class FragmentOptionsNotifications extends FragmentBase implements Shared } }); + swNotifyBackgroundOnly.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { + @Override + public void onCheckedChanged(CompoundButton compoundButton, boolean checked) { + prefs.edit().putBoolean("notify_background_only", checked).apply(); + enableOptions(); + } + }); + swNotifyKnown.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean checked) { @@ -457,6 +467,7 @@ public class FragmentOptionsNotifications extends FragmentBase implements Shared swBadge.setChecked(prefs.getBoolean("badge", true)); swUnseenIgnored.setChecked(prefs.getBoolean("unseen_ignored", false)); + swNotifyBackgroundOnly.setChecked(prefs.getBoolean("notify_background_only", false)); swNotifyKnown.setChecked(prefs.getBoolean("notify_known", false)); swNotifySummary.setChecked(prefs.getBoolean("notify_summary", false)); swNotifyRemove.setChecked(prefs.getBoolean("notify_remove", true)); diff --git a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java index ee67817021..41f17a227b 100644 --- a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java +++ b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java @@ -102,6 +102,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences private int lastAccounts = 0; private int lastOperations = 0; + private boolean foreground = false; private Map coreStates = new Hashtable<>(); private MutableLiveData liveNetworkState = new MutableLiveData<>(); private MutableLiveData> liveAccountState = new MutableLiveData<>(); @@ -509,7 +510,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences @Override public void run() { try { - Core.notifyMessages(ServiceSynchronize.this, messages, groupNotifying); + Core.notifyMessages(ServiceSynchronize.this, messages, groupNotifying, foreground); } catch (SecurityException ex) { Log.w(ex); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ServiceSynchronize.this); @@ -717,6 +718,10 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences onWakeup(intent); break; + case "state": + onState(intent); + break; + case "alarm": onAlarm(intent); break; @@ -772,6 +777,10 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences } } + private void onState(Intent intent) { + foreground = intent.getBooleanExtra("foreground", false); + } + private void onAlarm(Intent intent) { Bundle command = new Bundle(); schedule(this); @@ -2145,6 +2154,13 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences .setAction("alarm")); } + static void state(Context context, boolean foreground) { + start(context, + new Intent(context, ServiceSynchronize.class) + .setAction("state") + .putExtra("foreground", foreground)); + } + static void watchdog(Context context) { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); boolean enabled = prefs.getBoolean("enabled", true); diff --git a/app/src/main/res/layout/fragment_options_notifications.xml b/app/src/main/res/layout/fragment_options_notifications.xml index 6c1cb6ed04..5f5ae2e335 100644 --- a/app/src/main/res/layout/fragment_options_notifications.xml +++ b/app/src/main/res/layout/fragment_options_notifications.xml @@ -328,6 +328,17 @@ app:layout_constraintTop_toBottomOf="@id/tvBadgeHint" app:switchPadding="12dp" /> + + Show launcher icon with number of new messages Let the number of new messages match the number of notifications + Show notifications when on the background only Show notifications for contacts only Show summary notification only Show message preview in notifications