From 7f92626ee460016e188c3e1f311341e9ac1a5078 Mon Sep 17 00:00:00 2001 From: M66B Date: Mon, 25 Apr 2022 08:09:39 +0200 Subject: [PATCH] Added no clear flag to ongoing notifications --- .../eu/faircode/email/FragmentFolders.java | 11 ++++--- .../eu/faircode/email/ServiceExternal.java | 13 ++++---- .../java/eu/faircode/email/ServiceSend.java | 31 +++++++++---------- .../eu/faircode/email/ServiceSynchronize.java | 17 +++++----- 4 files changed, 39 insertions(+), 33 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/FragmentFolders.java b/app/src/main/java/eu/faircode/email/FragmentFolders.java index f5218cd501..11b865c406 100644 --- a/app/src/main/java/eu/faircode/email/FragmentFolders.java +++ b/app/src/main/java/eu/faircode/email/FragmentFolders.java @@ -23,6 +23,7 @@ import static android.app.Activity.RESULT_OK; import static androidx.recyclerview.widget.RecyclerView.NO_POSITION; import android.app.Dialog; +import android.app.Notification; import android.app.NotificationManager; import android.content.ContentResolver; import android.content.Context; @@ -1174,12 +1175,12 @@ public class FragmentFolders extends FragmentBase { .setSmallIcon(R.drawable.baseline_get_app_white_24) .setContentTitle(getString(R.string.title_export_messages)) .setAutoCancel(false) - .setOngoing(true) .setShowWhen(false) - .setLocalOnly(true) .setPriority(NotificationCompat.PRIORITY_DEFAULT) .setCategory(NotificationCompat.CATEGORY_PROGRESS) - .setVisibility(NotificationCompat.VISIBILITY_SECRET); + .setVisibility(NotificationCompat.VISIBILITY_SECRET) + .setLocalOnly(true) + .setOngoing(true); DB db = DB.getInstance(context); List ids = db.message().getMessageIdsByFolder(fid); @@ -1206,7 +1207,9 @@ public class FragmentFolders extends FragmentBase { if (now - last > EXPORT_PROGRESS_INTERVAL) { last = now; builder.setProgress(ids.size(), i, false); - nm.notify("export", NotificationHelper.NOTIFICATION_TAGGED, builder.build()); + Notification notification = builder.build(); + notification.flags |= Notification.FLAG_NO_CLEAR; + nm.notify("export", NotificationHelper.NOTIFICATION_TAGGED, notification); } long id = ids.get(i); diff --git a/app/src/main/java/eu/faircode/email/ServiceExternal.java b/app/src/main/java/eu/faircode/email/ServiceExternal.java index 6d214dcbd2..1b079bcbfc 100644 --- a/app/src/main/java/eu/faircode/email/ServiceExternal.java +++ b/app/src/main/java/eu/faircode/email/ServiceExternal.java @@ -19,6 +19,7 @@ package eu.faircode.email; Copyright 2018-2022 by Marcel Bokhorst (M66B) */ +import android.app.Notification; import android.app.Service; import android.content.Context; import android.content.Intent; @@ -58,8 +59,7 @@ public class ServiceExternal extends Service { public void onCreate() { Log.i("Service external create"); super.onCreate(); - startForeground(NotificationHelper.NOTIFICATION_EXTERNAL, - getNotification().build()); + startForeground(NotificationHelper.NOTIFICATION_EXTERNAL, getNotification()); } @Override @@ -76,8 +76,7 @@ public class ServiceExternal extends Service { Log.logExtras(intent); super.onStartCommand(intent, flags, startId); - startForeground(NotificationHelper.NOTIFICATION_EXTERNAL, - getNotification().build()); + startForeground(NotificationHelper.NOTIFICATION_EXTERNAL, getNotification()); if (intent == null) return START_NOT_STICKY; @@ -126,7 +125,7 @@ public class ServiceExternal extends Service { return null; } - private NotificationCompat.Builder getNotification() { + private Notification getNotification() { NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "service") .setSmallIcon(R.drawable.baseline_compare_arrows_white_24) @@ -140,7 +139,9 @@ public class ServiceExternal extends Service { .setLocalOnly(true) .setOngoing(true); - return builder; + Notification notification = builder.build(); + notification.flags |= Notification.FLAG_NO_CLEAR; + return notification; } private static void poll(Context context, Intent intent) { diff --git a/app/src/main/java/eu/faircode/email/ServiceSend.java b/app/src/main/java/eu/faircode/email/ServiceSend.java index 70099704fe..196f5f7a8c 100644 --- a/app/src/main/java/eu/faircode/email/ServiceSend.java +++ b/app/src/main/java/eu/faircode/email/ServiceSend.java @@ -20,6 +20,7 @@ package eu.faircode.email; */ import android.app.AlarmManager; +import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.BroadcastReceiver; @@ -89,8 +90,7 @@ public class ServiceSend extends ServiceBase implements SharedPreferences.OnShar public void onCreate() { EntityLog.log(this, "Service send create"); super.onCreate(); - startForeground(NotificationHelper.NOTIFICATION_SEND, - getNotificationService(false).build()); + startForeground(NotificationHelper.NOTIFICATION_SEND, getNotificationService(false)); owner = new TwoStateOwner(this, "send"); @@ -108,8 +108,7 @@ public class ServiceSend extends ServiceBase implements SharedPreferences.OnShar try { NotificationManager nm = Helper.getSystemService(ServiceSend.this, NotificationManager.class); - nm.notify(NotificationHelper.NOTIFICATION_SEND, - getNotificationService(false).build()); + nm.notify(NotificationHelper.NOTIFICATION_SEND, getNotificationService(false)); } catch (Throwable ex) { Log.w(ex); } @@ -202,8 +201,7 @@ public class ServiceSend extends ServiceBase implements SharedPreferences.OnShar @Override public int onStartCommand(Intent intent, int flags, int startId) { super.onStartCommand(intent, flags, startId); - startForeground(NotificationHelper.NOTIFICATION_SEND, - getNotificationService(false).build()); + startForeground(NotificationHelper.NOTIFICATION_SEND, getNotificationService(false)); Log.i("Send intent=" + intent); Log.logExtras(intent); @@ -211,7 +209,7 @@ public class ServiceSend extends ServiceBase implements SharedPreferences.OnShar return START_STICKY; } - NotificationCompat.Builder getNotificationService(boolean alert) { + private Notification getNotificationService(boolean alert) { NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "send") .setSmallIcon(R.drawable.baseline_send_white_24) @@ -221,11 +219,11 @@ public class ServiceSend extends ServiceBase implements SharedPreferences.OnShar .setShowWhen(true) .setOnlyAlertOnce(!alert) .setDefaults(0) // disable sound on pre Android 8 - .setLocalOnly(true) - .setOngoing(true) .setPriority(NotificationCompat.PRIORITY_MIN) .setCategory(NotificationCompat.CATEGORY_SERVICE) - .setVisibility(NotificationCompat.VISIBILITY_SECRET); + .setVisibility(NotificationCompat.VISIBILITY_SECRET) + .setLocalOnly(true) + .setOngoing(true); if (lastUnsent != null && lastUnsent.count != null) builder.setContentText(getResources().getQuantityString( @@ -237,7 +235,9 @@ public class ServiceSend extends ServiceBase implements SharedPreferences.OnShar if (lastProgress >= 0) builder.setProgress(100, lastProgress, false); - return builder; + Notification notification = builder.build(); + notification.flags |= Notification.FLAG_NO_CLEAR; + return notification; } NotificationCompat.Builder getNotificationError(String recipient, Throwable ex, int tries_left) { @@ -331,8 +331,7 @@ public class ServiceSend extends ServiceBase implements SharedPreferences.OnShar try { NotificationManager nm = Helper.getSystemService(ServiceSend.this, NotificationManager.class); - nm.notify(NotificationHelper.NOTIFICATION_SEND, - getNotificationService(false).build()); + nm.notify(NotificationHelper.NOTIFICATION_SEND, getNotificationService(false)); } catch (Throwable ex) { Log.w(ex); } @@ -518,7 +517,7 @@ public class ServiceSend extends ServiceBase implements SharedPreferences.OnShar } NotificationManager nm = Helper.getSystemService(this, NotificationManager.class); - nm.notify(NotificationHelper.NOTIFICATION_SEND, getNotificationService(true).build()); + nm.notify(NotificationHelper.NOTIFICATION_SEND, getNotificationService(true)); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); boolean reply_move = prefs.getBoolean("reply_move", false); @@ -732,7 +731,7 @@ public class ServiceSend extends ServiceBase implements SharedPreferences.OnShar if (now > last + PROGRESS_UPDATE_INTERVAL) { last = now; lastProgress = progress; - nm.notify(NotificationHelper.NOTIFICATION_SEND, getNotificationService(false).build()); + nm.notify(NotificationHelper.NOTIFICATION_SEND, getNotificationService(false)); } } } @@ -773,7 +772,7 @@ public class ServiceSend extends ServiceBase implements SharedPreferences.OnShar iservice.close(); if (lastProgress >= 0) { lastProgress = -1; - nm.notify(NotificationHelper.NOTIFICATION_SEND, getNotificationService(false).build()); + nm.notify(NotificationHelper.NOTIFICATION_SEND, getNotificationService(false)); } db.identity().setIdentityState(ident.id, null); } diff --git a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java index be59089622..9c8bb11f12 100644 --- a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java +++ b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java @@ -23,6 +23,7 @@ import static android.os.Process.THREAD_PRIORITY_BACKGROUND; import static eu.faircode.email.ServiceAuthenticator.AUTH_TYPE_PASSWORD; import android.app.AlarmManager; +import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.BroadcastReceiver; @@ -183,7 +184,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences stopForeground(true); else startForeground(NotificationHelper.NOTIFICATION_SYNCHRONIZE, - getNotificationService(null, null).build()); + getNotificationService(null, null)); // Listen for network changes ConnectivityManager cm = Helper.getSystemService(this, ConnectivityManager.class); @@ -405,7 +406,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences try { NotificationManager nm = Helper.getSystemService(ServiceSynchronize.this, NotificationManager.class); nm.notify(NotificationHelper.NOTIFICATION_SYNCHRONIZE, - getNotificationService(lastAccounts, lastOperations).build()); + getNotificationService(lastAccounts, lastOperations)); } catch (Throwable ex) { /* java.lang.NullPointerException: Attempt to invoke interface method 'java.util.Iterator java.lang.Iterable.iterator()' on a null object reference @@ -947,7 +948,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences stopForeground(true); else startForeground(NotificationHelper.NOTIFICATION_SYNCHRONIZE, - getNotificationService(null, null).build()); + getNotificationService(null, null)); if (action != null) { switch (action.split(":")[0]) { @@ -1290,7 +1291,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences scheduleWatchdog(this); } - private NotificationCompat.Builder getNotificationService(Integer accounts, Integer operations) { + private Notification getNotificationService(Integer accounts, Integer operations) { if (accounts != null) this.lastAccounts = accounts; if (operations != null) @@ -1310,8 +1311,8 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences .setContentIntent(piWhy) .setAutoCancel(false) .setShowWhen(false) - .setPriority(NotificationCompat.PRIORITY_MIN) .setDefaults(0) // disable sound on pre Android 8 + .setPriority(NotificationCompat.PRIORITY_MIN) .setCategory(NotificationCompat.CATEGORY_SERVICE) .setVisibility(NotificationCompat.VISIBILITY_SECRET) .setLocalOnly(true) @@ -1330,7 +1331,9 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences if (lastSuitable == null || !lastSuitable) builder.setSubText(getString(R.string.title_notification_waiting)); - return builder; + Notification notification = builder.build(); + notification.flags |= Notification.FLAG_NO_CLEAR; + return notification; } private NotificationCompat.Builder getNotificationAlert(EntityAccount account, String message) { @@ -2762,7 +2765,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences try { NotificationManager nm = Helper.getSystemService(ServiceSynchronize.this, NotificationManager.class); nm.notify(NotificationHelper.NOTIFICATION_SYNCHRONIZE, - getNotificationService(lastAccounts, lastOperations).build()); + getNotificationService(lastAccounts, lastOperations)); } catch (Throwable ex) { Log.w(ex); }