Added no clear flag to ongoing notifications

pull/207/head
M66B 3 years ago
parent cc088cbc85
commit 7f92626ee4

@ -23,6 +23,7 @@ import static android.app.Activity.RESULT_OK;
import static androidx.recyclerview.widget.RecyclerView.NO_POSITION; import static androidx.recyclerview.widget.RecyclerView.NO_POSITION;
import android.app.Dialog; import android.app.Dialog;
import android.app.Notification;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.content.ContentResolver; import android.content.ContentResolver;
import android.content.Context; import android.content.Context;
@ -1174,12 +1175,12 @@ public class FragmentFolders extends FragmentBase {
.setSmallIcon(R.drawable.baseline_get_app_white_24) .setSmallIcon(R.drawable.baseline_get_app_white_24)
.setContentTitle(getString(R.string.title_export_messages)) .setContentTitle(getString(R.string.title_export_messages))
.setAutoCancel(false) .setAutoCancel(false)
.setOngoing(true)
.setShowWhen(false) .setShowWhen(false)
.setLocalOnly(true)
.setPriority(NotificationCompat.PRIORITY_DEFAULT) .setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setCategory(NotificationCompat.CATEGORY_PROGRESS) .setCategory(NotificationCompat.CATEGORY_PROGRESS)
.setVisibility(NotificationCompat.VISIBILITY_SECRET); .setVisibility(NotificationCompat.VISIBILITY_SECRET)
.setLocalOnly(true)
.setOngoing(true);
DB db = DB.getInstance(context); DB db = DB.getInstance(context);
List<Long> ids = db.message().getMessageIdsByFolder(fid); List<Long> ids = db.message().getMessageIdsByFolder(fid);
@ -1206,7 +1207,9 @@ public class FragmentFolders extends FragmentBase {
if (now - last > EXPORT_PROGRESS_INTERVAL) { if (now - last > EXPORT_PROGRESS_INTERVAL) {
last = now; last = now;
builder.setProgress(ids.size(), i, false); 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); long id = ids.get(i);

@ -19,6 +19,7 @@ package eu.faircode.email;
Copyright 2018-2022 by Marcel Bokhorst (M66B) Copyright 2018-2022 by Marcel Bokhorst (M66B)
*/ */
import android.app.Notification;
import android.app.Service; import android.app.Service;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
@ -58,8 +59,7 @@ public class ServiceExternal extends Service {
public void onCreate() { public void onCreate() {
Log.i("Service external create"); Log.i("Service external create");
super.onCreate(); super.onCreate();
startForeground(NotificationHelper.NOTIFICATION_EXTERNAL, startForeground(NotificationHelper.NOTIFICATION_EXTERNAL, getNotification());
getNotification().build());
} }
@Override @Override
@ -76,8 +76,7 @@ public class ServiceExternal extends Service {
Log.logExtras(intent); Log.logExtras(intent);
super.onStartCommand(intent, flags, startId); super.onStartCommand(intent, flags, startId);
startForeground(NotificationHelper.NOTIFICATION_EXTERNAL, startForeground(NotificationHelper.NOTIFICATION_EXTERNAL, getNotification());
getNotification().build());
if (intent == null) if (intent == null)
return START_NOT_STICKY; return START_NOT_STICKY;
@ -126,7 +125,7 @@ public class ServiceExternal extends Service {
return null; return null;
} }
private NotificationCompat.Builder getNotification() { private Notification getNotification() {
NotificationCompat.Builder builder = NotificationCompat.Builder builder =
new NotificationCompat.Builder(this, "service") new NotificationCompat.Builder(this, "service")
.setSmallIcon(R.drawable.baseline_compare_arrows_white_24) .setSmallIcon(R.drawable.baseline_compare_arrows_white_24)
@ -140,7 +139,9 @@ public class ServiceExternal extends Service {
.setLocalOnly(true) .setLocalOnly(true)
.setOngoing(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) { private static void poll(Context context, Intent intent) {

@ -20,6 +20,7 @@ package eu.faircode.email;
*/ */
import android.app.AlarmManager; import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
@ -89,8 +90,7 @@ public class ServiceSend extends ServiceBase implements SharedPreferences.OnShar
public void onCreate() { public void onCreate() {
EntityLog.log(this, "Service send create"); EntityLog.log(this, "Service send create");
super.onCreate(); super.onCreate();
startForeground(NotificationHelper.NOTIFICATION_SEND, startForeground(NotificationHelper.NOTIFICATION_SEND, getNotificationService(false));
getNotificationService(false).build());
owner = new TwoStateOwner(this, "send"); owner = new TwoStateOwner(this, "send");
@ -108,8 +108,7 @@ public class ServiceSend extends ServiceBase implements SharedPreferences.OnShar
try { try {
NotificationManager nm = Helper.getSystemService(ServiceSend.this, NotificationManager.class); NotificationManager nm = Helper.getSystemService(ServiceSend.this, NotificationManager.class);
nm.notify(NotificationHelper.NOTIFICATION_SEND, nm.notify(NotificationHelper.NOTIFICATION_SEND, getNotificationService(false));
getNotificationService(false).build());
} catch (Throwable ex) { } catch (Throwable ex) {
Log.w(ex); Log.w(ex);
} }
@ -202,8 +201,7 @@ public class ServiceSend extends ServiceBase implements SharedPreferences.OnShar
@Override @Override
public int onStartCommand(Intent intent, int flags, int startId) { public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId); super.onStartCommand(intent, flags, startId);
startForeground(NotificationHelper.NOTIFICATION_SEND, startForeground(NotificationHelper.NOTIFICATION_SEND, getNotificationService(false));
getNotificationService(false).build());
Log.i("Send intent=" + intent); Log.i("Send intent=" + intent);
Log.logExtras(intent); Log.logExtras(intent);
@ -211,7 +209,7 @@ public class ServiceSend extends ServiceBase implements SharedPreferences.OnShar
return START_STICKY; return START_STICKY;
} }
NotificationCompat.Builder getNotificationService(boolean alert) { private Notification getNotificationService(boolean alert) {
NotificationCompat.Builder builder = NotificationCompat.Builder builder =
new NotificationCompat.Builder(this, "send") new NotificationCompat.Builder(this, "send")
.setSmallIcon(R.drawable.baseline_send_white_24) .setSmallIcon(R.drawable.baseline_send_white_24)
@ -221,11 +219,11 @@ public class ServiceSend extends ServiceBase implements SharedPreferences.OnShar
.setShowWhen(true) .setShowWhen(true)
.setOnlyAlertOnce(!alert) .setOnlyAlertOnce(!alert)
.setDefaults(0) // disable sound on pre Android 8 .setDefaults(0) // disable sound on pre Android 8
.setLocalOnly(true)
.setOngoing(true)
.setPriority(NotificationCompat.PRIORITY_MIN) .setPriority(NotificationCompat.PRIORITY_MIN)
.setCategory(NotificationCompat.CATEGORY_SERVICE) .setCategory(NotificationCompat.CATEGORY_SERVICE)
.setVisibility(NotificationCompat.VISIBILITY_SECRET); .setVisibility(NotificationCompat.VISIBILITY_SECRET)
.setLocalOnly(true)
.setOngoing(true);
if (lastUnsent != null && lastUnsent.count != null) if (lastUnsent != null && lastUnsent.count != null)
builder.setContentText(getResources().getQuantityString( builder.setContentText(getResources().getQuantityString(
@ -237,7 +235,9 @@ public class ServiceSend extends ServiceBase implements SharedPreferences.OnShar
if (lastProgress >= 0) if (lastProgress >= 0)
builder.setProgress(100, lastProgress, false); 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) { NotificationCompat.Builder getNotificationError(String recipient, Throwable ex, int tries_left) {
@ -331,8 +331,7 @@ public class ServiceSend extends ServiceBase implements SharedPreferences.OnShar
try { try {
NotificationManager nm = Helper.getSystemService(ServiceSend.this, NotificationManager.class); NotificationManager nm = Helper.getSystemService(ServiceSend.this, NotificationManager.class);
nm.notify(NotificationHelper.NOTIFICATION_SEND, nm.notify(NotificationHelper.NOTIFICATION_SEND, getNotificationService(false));
getNotificationService(false).build());
} catch (Throwable ex) { } catch (Throwable ex) {
Log.w(ex); Log.w(ex);
} }
@ -518,7 +517,7 @@ public class ServiceSend extends ServiceBase implements SharedPreferences.OnShar
} }
NotificationManager nm = Helper.getSystemService(this, NotificationManager.class); 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); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
boolean reply_move = prefs.getBoolean("reply_move", false); 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) { if (now > last + PROGRESS_UPDATE_INTERVAL) {
last = now; last = now;
lastProgress = progress; 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(); iservice.close();
if (lastProgress >= 0) { if (lastProgress >= 0) {
lastProgress = -1; lastProgress = -1;
nm.notify(NotificationHelper.NOTIFICATION_SEND, getNotificationService(false).build()); nm.notify(NotificationHelper.NOTIFICATION_SEND, getNotificationService(false));
} }
db.identity().setIdentityState(ident.id, null); db.identity().setIdentityState(ident.id, null);
} }

@ -23,6 +23,7 @@ import static android.os.Process.THREAD_PRIORITY_BACKGROUND;
import static eu.faircode.email.ServiceAuthenticator.AUTH_TYPE_PASSWORD; import static eu.faircode.email.ServiceAuthenticator.AUTH_TYPE_PASSWORD;
import android.app.AlarmManager; import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
@ -183,7 +184,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
stopForeground(true); stopForeground(true);
else else
startForeground(NotificationHelper.NOTIFICATION_SYNCHRONIZE, startForeground(NotificationHelper.NOTIFICATION_SYNCHRONIZE,
getNotificationService(null, null).build()); getNotificationService(null, null));
// Listen for network changes // Listen for network changes
ConnectivityManager cm = Helper.getSystemService(this, ConnectivityManager.class); ConnectivityManager cm = Helper.getSystemService(this, ConnectivityManager.class);
@ -405,7 +406,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
try { try {
NotificationManager nm = Helper.getSystemService(ServiceSynchronize.this, NotificationManager.class); NotificationManager nm = Helper.getSystemService(ServiceSynchronize.this, NotificationManager.class);
nm.notify(NotificationHelper.NOTIFICATION_SYNCHRONIZE, nm.notify(NotificationHelper.NOTIFICATION_SYNCHRONIZE,
getNotificationService(lastAccounts, lastOperations).build()); getNotificationService(lastAccounts, lastOperations));
} catch (Throwable ex) { } catch (Throwable ex) {
/* /*
java.lang.NullPointerException: Attempt to invoke interface method 'java.util.Iterator java.lang.Iterable.iterator()' on a null object reference 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); stopForeground(true);
else else
startForeground(NotificationHelper.NOTIFICATION_SYNCHRONIZE, startForeground(NotificationHelper.NOTIFICATION_SYNCHRONIZE,
getNotificationService(null, null).build()); getNotificationService(null, null));
if (action != null) { if (action != null) {
switch (action.split(":")[0]) { switch (action.split(":")[0]) {
@ -1290,7 +1291,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
scheduleWatchdog(this); scheduleWatchdog(this);
} }
private NotificationCompat.Builder getNotificationService(Integer accounts, Integer operations) { private Notification getNotificationService(Integer accounts, Integer operations) {
if (accounts != null) if (accounts != null)
this.lastAccounts = accounts; this.lastAccounts = accounts;
if (operations != null) if (operations != null)
@ -1310,8 +1311,8 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
.setContentIntent(piWhy) .setContentIntent(piWhy)
.setAutoCancel(false) .setAutoCancel(false)
.setShowWhen(false) .setShowWhen(false)
.setPriority(NotificationCompat.PRIORITY_MIN)
.setDefaults(0) // disable sound on pre Android 8 .setDefaults(0) // disable sound on pre Android 8
.setPriority(NotificationCompat.PRIORITY_MIN)
.setCategory(NotificationCompat.CATEGORY_SERVICE) .setCategory(NotificationCompat.CATEGORY_SERVICE)
.setVisibility(NotificationCompat.VISIBILITY_SECRET) .setVisibility(NotificationCompat.VISIBILITY_SECRET)
.setLocalOnly(true) .setLocalOnly(true)
@ -1330,7 +1331,9 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
if (lastSuitable == null || !lastSuitable) if (lastSuitable == null || !lastSuitable)
builder.setSubText(getString(R.string.title_notification_waiting)); 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) { private NotificationCompat.Builder getNotificationAlert(EntityAccount account, String message) {
@ -2762,7 +2765,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
try { try {
NotificationManager nm = Helper.getSystemService(ServiceSynchronize.this, NotificationManager.class); NotificationManager nm = Helper.getSystemService(ServiceSynchronize.this, NotificationManager.class);
nm.notify(NotificationHelper.NOTIFICATION_SYNCHRONIZE, nm.notify(NotificationHelper.NOTIFICATION_SYNCHRONIZE,
getNotificationService(lastAccounts, lastOperations).build()); getNotificationService(lastAccounts, lastOperations));
} catch (Throwable ex) { } catch (Throwable ex) {
Log.w(ex); Log.w(ex);
} }

Loading…
Cancel
Save