Refactoring

This commit is contained in:
M66B 2021-07-15 18:36:39 +02:00
parent b60da5cf11
commit 34aa1112eb
14 changed files with 89 additions and 56 deletions

View File

@ -312,8 +312,9 @@ public class ActivityCompose extends ActivityBase implements FragmentManager.OnB
ServiceSynchronize.eval(context, "outbox/drafts");
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
nm.cancel("send:" + id, 10);
NotificationManager nm =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
nm.cancel("send:" + id, NotificationHelper.NOTIFICATION_TAGGED);
return message.id;
}

View File

@ -1081,8 +1081,10 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB
}
try {
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(Helper.NOTIFICATION_UPDATE, builder.build());
NotificationManager nm =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(NotificationHelper.NOTIFICATION_UPDATE,
builder.build());
} catch (Throwable ex) {
Log.w(ex);
}

View File

@ -207,7 +207,7 @@ class Core {
if (EntityOperation.HEADERS.equals(op.name) ||
EntityOperation.RAW.equals(op.name))
nm.cancel(op.name + ":" + op.message, 10);
nm.cancel(op.name + ":" + op.message, NotificationHelper.NOTIFICATION_TAGGED);
if (!Objects.equals(folder.id, op.folder))
throw new IllegalArgumentException("Invalid folder=" + folder.id + "/" + op.folder);
@ -628,7 +628,9 @@ class Core {
if (title != null) {
NotificationCompat.Builder builder =
getNotificationError(context, "warning", title, ex);
nm.notify(op.name + ":" + op.message, 10, builder.build());
nm.notify(op.name + ":" + op.message,
NotificationHelper.NOTIFICATION_TAGGED,
builder.build());
}
} else {
@ -4075,7 +4077,7 @@ class Core {
for (Long id : remove) {
String tag = "unseen." + group + "." + Math.abs(id);
EntityLog.log(context, "Notify cancel tag=" + tag + " id=" + id);
nm.cancel(tag, 10);
nm.cancel(tag, NotificationHelper.NOTIFICATION_TAGGED);
data.groupNotifying.get(group).remove(id);
db.message().setMessageNotifying(Math.abs(id), 0);
@ -4084,7 +4086,7 @@ class Core {
if (notifications.size() == 0) {
String tag = "unseen." + group + "." + 0;
EntityLog.log(context, "Notify cancel tag=" + tag);
nm.cancel(tag, 10);
nm.cancel(tag, NotificationHelper.NOTIFICATION_TAGGED);
}
for (Long id : add) {
@ -4114,7 +4116,7 @@ class Core {
: " channel=" + notification.getChannelId()) +
" sort=" + notification.getSortKey());
try {
nm.notify(tag, 10, notification);
nm.notify(tag, NotificationHelper.NOTIFICATION_TAGGED, notification);
// https://github.com/leolin310148/ShortcutBadger/wiki/Xiaomi-Device-Support
if (id == 0 && badge && Helper.isXiaomi())
ShortcutBadger.applyNotification(context, notification, current);

View File

@ -1266,8 +1266,8 @@ public class FragmentAccount extends FragmentBase {
if (!synchronize) {
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
nm.cancel("receive:" + account.id, 10);
nm.cancel("alert:" + account.id, 10);
nm.cancel("receive:" + account.id, NotificationHelper.NOTIFICATION_TAGGED);
nm.cancel("alert:" + account.id, NotificationHelper.NOTIFICATION_TAGGED);
}
args.putBoolean("saved", true);

View File

@ -917,7 +917,7 @@ public class FragmentFolders extends FragmentBase {
if (now - last > EXPORT_PROGRESS_INTERVAL) {
last = now;
builder.setProgress(ids.size(), i, false);
nm.notify("export", 10, builder.build());
nm.notify("export", NotificationHelper.NOTIFICATION_TAGGED, builder.build());
}
long id = ids.get(i);
@ -990,7 +990,7 @@ public class FragmentFolders extends FragmentBase {
Log.e(ex);
}
} finally {
nm.cancel("export", 10);
nm.cancel("export", NotificationHelper.NOTIFICATION_TAGGED);
}
return null;

View File

@ -7571,8 +7571,9 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
ServiceSynchronize.eval(context, "delete");
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
nm.cancel("send:" + id, 10);
NotificationManager nm =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
nm.cancel("send:" + id, NotificationHelper.NOTIFICATION_TAGGED);
return null;
}

View File

@ -448,8 +448,9 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc
prefs.edit().putBoolean("updates", checked).apply();
swCheckWeekly.setEnabled(checked);
if (!checked) {
NotificationManager nm = (NotificationManager) getContext().getSystemService(Context.NOTIFICATION_SERVICE);
nm.cancel(Helper.NOTIFICATION_UPDATE);
NotificationManager nm =
(NotificationManager) getContext().getSystemService(Context.NOTIFICATION_SERVICE);
nm.cancel(NotificationHelper.NOTIFICATION_UPDATE);
}
}
});

View File

@ -623,9 +623,10 @@ public class FragmentPop extends FragmentBase {
ServiceSynchronize.eval(context, "POP3");
if (!synchronize) {
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
nm.cancel("receive:" + account.id, 10);
nm.cancel("alert:" + account.id, 10);
NotificationManager nm =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
nm.cancel("receive:" + account.id, NotificationHelper.NOTIFICATION_TAGGED);
nm.cancel("alert:" + account.id, NotificationHelper.NOTIFICATION_TAGGED);
}
args.putBoolean("saved", true);

View File

@ -144,11 +144,6 @@ public class Helper {
private static Boolean hasPlayStore = null;
private static Boolean hasValidFingerprint = null;
static final int NOTIFICATION_SYNCHRONIZE = 1;
static final int NOTIFICATION_SEND = 2;
static final int NOTIFICATION_EXTERNAL = 3;
static final int NOTIFICATION_UPDATE = 4;
static final float LOW_LIGHT = 0.6f;
static final int BUFFER_SIZE = 8192; // Same as in Files class

View File

@ -37,6 +37,12 @@ import org.json.JSONException;
import org.json.JSONObject;
class NotificationHelper {
static final int NOTIFICATION_SYNCHRONIZE = 100;
static final int NOTIFICATION_SEND = 200;
static final int NOTIFICATION_EXTERNAL = 300;
static final int NOTIFICATION_UPDATE = 400;
static final int NOTIFICATION_TAGGED = 500;
@RequiresApi(api = Build.VERSION_CODES.O)
static void createNotificationChannels(Context context) {
// https://issuetracker.google.com/issues/65108694

View File

@ -58,7 +58,8 @@ public class ServiceExternal extends Service {
public void onCreate() {
Log.i("Service external create");
super.onCreate();
startForeground(Helper.NOTIFICATION_EXTERNAL, getNotification().build());
startForeground(NotificationHelper.NOTIFICATION_EXTERNAL,
getNotification().build());
}
@Override
@ -75,7 +76,8 @@ public class ServiceExternal extends Service {
Log.logExtras(intent);
super.onStartCommand(intent, flags, startId);
startForeground(Helper.NOTIFICATION_EXTERNAL, getNotification().build());
startForeground(NotificationHelper.NOTIFICATION_EXTERNAL,
getNotification().build());
if (intent == null)
return START_NOT_STICKY;

View File

@ -83,7 +83,8 @@ public class ServiceSend extends ServiceBase implements SharedPreferences.OnShar
public void onCreate() {
EntityLog.log(this, "Service send create");
super.onCreate();
startForeground(Helper.NOTIFICATION_SEND, getNotificationService().build());
startForeground(NotificationHelper.NOTIFICATION_SEND,
getNotificationService().build());
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wlOutbox = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, BuildConfig.APPLICATION_ID + ":send");
@ -98,8 +99,10 @@ public class ServiceSend extends ServiceBase implements SharedPreferences.OnShar
EntityLog.log(ServiceSend.this, "Unsent=" + (unsent == null ? null : unsent.count));
try {
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(Helper.NOTIFICATION_SEND, getNotificationService().build());
NotificationManager nm =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(NotificationHelper.NOTIFICATION_SEND,
getNotificationService().build());
} catch (Throwable ex) {
Log.w(ex);
}
@ -177,8 +180,9 @@ public class ServiceSend extends ServiceBase implements SharedPreferences.OnShar
stopForeground(true);
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.cancel(Helper.NOTIFICATION_SEND);
NotificationManager nm =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.cancel(NotificationHelper.NOTIFICATION_SEND);
super.onDestroy();
}
@ -192,7 +196,8 @@ public class ServiceSend extends ServiceBase implements SharedPreferences.OnShar
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
startForeground(Helper.NOTIFICATION_SEND, getNotificationService().build());
startForeground(NotificationHelper.NOTIFICATION_SEND,
getNotificationService().build());
Log.i("Send intent=" + intent);
Log.logExtras(intent);
@ -319,8 +324,10 @@ public class ServiceSend extends ServiceBase implements SharedPreferences.OnShar
EntityLog.log(ServiceSend.this, "Service send suitable=" + suitable);
try {
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(Helper.NOTIFICATION_SEND, getNotificationService().build());
NotificationManager nm =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(NotificationHelper.NOTIFICATION_SEND,
getNotificationService().build());
} catch (Throwable ex) {
Log.w(ex);
}
@ -419,8 +426,10 @@ public class ServiceSend extends ServiceBase implements SharedPreferences.OnShar
try {
int tries_left = (unrecoverable ? 0 : RETRY_MAX - op.tries);
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify("send:" + message.id, 10, getNotificationError(
MessageHelper.formatAddressesShort(message.to), ex, tries_left).build());
nm.notify("send:" + message.id,
NotificationHelper.NOTIFICATION_TAGGED,
getNotificationError(
MessageHelper.formatAddressesShort(message.to), ex, tries_left).build());
} catch (Throwable ex1) {
Log.w(ex1);
}
@ -473,7 +482,7 @@ public class ServiceSend extends ServiceBase implements SharedPreferences.OnShar
}
db.message().setMessageError(message.id, null);
nm.cancel("send:" + message.id, 10);
nm.cancel("send:" + message.id, NotificationHelper.NOTIFICATION_TAGGED);
if (message.ui_snoozed == null)
EntityOperation.queue(this, message, EntityOperation.SEND);
@ -734,8 +743,9 @@ public class ServiceSend extends ServiceBase implements SharedPreferences.OnShar
db.endTransaction();
}
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.cancel("send:" + message.id, 10);
NotificationManager nm =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.cancel("send:" + message.id, NotificationHelper.NOTIFICATION_TAGGED);
// Check sent message
if (sid != null) {

View File

@ -171,7 +171,8 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
if (isBackgroundService(this))
stopForeground(true);
else
startForeground(Helper.NOTIFICATION_SYNCHRONIZE, getNotificationService(null, null).build());
startForeground(NotificationHelper.NOTIFICATION_SYNCHRONIZE,
getNotificationService(null, null).build());
// Listen for network changes
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
@ -367,8 +368,10 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
if (!isBackgroundService(ServiceSynchronize.this))
try {
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(Helper.NOTIFICATION_SYNCHRONIZE, getNotificationService(lastAccounts, lastOperations).build());
NotificationManager nm =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(NotificationHelper.NOTIFICATION_SYNCHRONIZE,
getNotificationService(lastAccounts, lastOperations).build());
} catch (Throwable ex) {
/*
java.lang.NullPointerException: Attempt to invoke interface method 'java.util.Iterator java.lang.Iterable.iterator()' on a null object reference
@ -774,8 +777,9 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
*/
}
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.cancel(Helper.NOTIFICATION_SYNCHRONIZE);
NotificationManager nm =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.cancel(NotificationHelper.NOTIFICATION_SYNCHRONIZE);
super.onDestroy();
}
@ -799,7 +803,8 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
if (isBackgroundService(this))
stopForeground(true);
else
startForeground(Helper.NOTIFICATION_SYNCHRONIZE, getNotificationService(null, null).build());
startForeground(NotificationHelper.NOTIFICATION_SYNCHRONIZE,
getNotificationService(null, null).build());
if (action != null)
try {
@ -1240,7 +1245,8 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
if (!ConnectionHelper.isMaxConnections(message))
try {
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify("alert:" + account.id, 10,
nm.notify("alert:" + account.id,
NotificationHelper.NOTIFICATION_TAGGED,
getNotificationAlert(account.name, message).build());
} catch (Throwable ex) {
Log.w(ex);
@ -1272,7 +1278,8 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
try {
state.setBackoff(2 * CONNECT_BACKOFF_ALARM_MAX * 60);
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify("receive:" + account.id, 10,
nm.notify("receive:" + account.id,
NotificationHelper.NOTIFICATION_TAGGED,
Core.getNotificationError(this, "error", account.name, ex)
.build());
} catch (Throwable ex1) {
@ -1932,9 +1939,10 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
db.account().setAccountConnected(account.id, account.last_connected);
db.account().setAccountWarning(account.id, capIdle ? null : getString(R.string.title_no_idle));
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.cancel("receive:" + account.id, 10);
nm.cancel("alert:" + account.id, 10);
NotificationManager nm =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.cancel("receive:" + account.id, NotificationHelper.NOTIFICATION_TAGGED);
nm.cancel("alert:" + account.id, NotificationHelper.NOTIFICATION_TAGGED);
// Schedule keep alive alarm
Intent intent = new Intent(this, ServiceSynchronize.class);
@ -1992,7 +2000,8 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
.format(account.last_connected)), ex);
try {
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify("receive:" + account.id, 10,
nm.notify("receive:" + account.id,
NotificationHelper.NOTIFICATION_TAGGED,
Core.getNotificationError(this, "warning", account.name, warning)
.build());
} catch (Throwable ex1) {
@ -2351,8 +2360,10 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
if (!isBackgroundService(ServiceSynchronize.this))
try {
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(Helper.NOTIFICATION_SYNCHRONIZE, getNotificationService(lastAccounts, lastOperations).build());
NotificationManager nm =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(NotificationHelper.NOTIFICATION_SYNCHRONIZE,
getNotificationService(lastAccounts, lastOperations).build());
} catch (Throwable ex) {
Log.w(ex);
}

View File

@ -193,8 +193,9 @@ public class ServiceUI extends IntentService {
// https://issuetracker.google.com/issues/159152393
String tag = "unseen." + group + ":" + id;
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.cancel(tag, 10);
NotificationManager nm =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.cancel(tag, NotificationHelper.NOTIFICATION_TAGGED);
}
private void onMove(long id, String folderType) {