Refactoring

pull/152/head
M66B 6 years ago
parent dc0f6c4caf
commit 8cfd700928

@ -14,7 +14,6 @@ import android.os.Bundle;
import android.os.SystemClock; import android.os.SystemClock;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.LongSparseArray;
import com.sun.mail.iap.ConnectionException; import com.sun.mail.iap.ConnectionException;
import com.sun.mail.iap.Response; import com.sun.mail.iap.Response;
@ -1449,43 +1448,43 @@ class Core {
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
LongSparseArray<List<Long>> notifying = new LongSparseArray<>(); Map<String, List<Long>> notifying = new HashMap<>();
LongSparseArray<String> accountName = new LongSparseArray<>(); Map<String, String> channelSubTitle = new HashMap<>();
LongSparseArray<List<TupleMessageEx>> accountMessages = new LongSparseArray<>(); Map<String, List<TupleMessageEx>> channelMessages = new HashMap<>();
// Previous // Previous
for (String key : prefs.getAll().keySet()) for (String key : prefs.getAll().keySet())
if (key.startsWith("notifying:")) { if (key.startsWith("notifying:")) {
long account = Long.parseLong(key.split(":")[1]); String channelName = key.split(":")[1];
notifying.put(account, new ArrayList<Long>()); notifying.put(channelName, new ArrayList<Long>());
for (String id : prefs.getString(key, null).split(",")) for (String id : prefs.getString(key, null).split(","))
notifying.get(account).add(Long.parseLong(id)); notifying.get(channelName).add(Long.parseLong(id));
editor.remove(key); editor.remove(key);
} }
// Current // Current
for (TupleMessageEx message : messages) { for (TupleMessageEx message : messages) {
long account = (message.accountNotify ? message.account : 0); String channelName = EntityAccount.getNotificationChannelName(message.accountNotify ? message.account : 0);
accountName.put(account, account > 0 ? message.accountName : null); String subTitle = (message.accountNotify ? message.accountName : null);
if (accountMessages.indexOfKey(account) < 0) { channelSubTitle.put(channelName, subTitle);
accountMessages.put(account, new ArrayList<TupleMessageEx>()); if (!channelMessages.containsKey(channelName)) {
if (notifying.indexOfKey(account) < 0) channelMessages.put(channelName, new ArrayList<TupleMessageEx>());
notifying.put(account, new ArrayList<Long>()); if (!notifying.containsKey(channelName))
notifying.put(channelName, new ArrayList<Long>());
} }
accountMessages.get(account).add(message); channelMessages.get(channelName).add(message);
} }
// Difference // Difference
for (int i = 0; i < notifying.size(); i++) { for (String channelName : notifying.keySet()) {
long account = notifying.keyAt(i);
List<Notification> notifications = getNotificationUnseen( List<Notification> notifications = getNotificationUnseen(
context, account, accountName.get(account), accountMessages.get(account)); context, channelName, channelSubTitle.get(channelName), channelMessages.get(channelName));
List<String> all = new ArrayList<>(); List<String> all = new ArrayList<>();
List<Long> add = new ArrayList<>(); List<Long> add = new ArrayList<>();
List<Long> remove = notifying.get(account); List<Long> remove = notifying.get(channelName);
for (Notification notification : notifications) { for (Notification notification : notifications) {
Long id = notification.extras.getLong("id", 0); Long id = notification.extras.getLong("id", 0);
if (id != 0) { if (id != 0) {
@ -1506,24 +1505,24 @@ class Core {
if (id < 0) if (id < 0)
headers++; headers++;
Log.i("Notify account=" + account + " count=" + notifications.size() + Log.i("Notify channel=" + channelName + " count=" + notifications.size() +
" added=" + add.size() + " removed=" + remove.size() + " headers=" + headers); " added=" + add.size() + " removed=" + remove.size() + " headers=" + headers);
if (notifications.size() == 0 || if (notifications.size() == 0 ||
(Build.VERSION.SDK_INT < Build.VERSION_CODES.O && headers > 0)) (Build.VERSION.SDK_INT < Build.VERSION_CODES.O && headers > 0))
nm.cancel("unseen:" + account + ":0", 1); nm.cancel("unseen:0", 1);
for (Long id : remove) for (Long id : remove)
nm.cancel("unseen:" + account + ":" + Math.abs(id), 1); nm.cancel("unseen:" + Math.abs(id), 1);
for (Notification notification : notifications) { for (Notification notification : notifications) {
long id = notification.extras.getLong("id", 0); long id = notification.extras.getLong("id", 0);
if ((id == 0 && add.size() + remove.size() > 0) || add.contains(id)) if ((id == 0 && add.size() + remove.size() > 0) || add.contains(id))
nm.notify("unseen:" + account + ":" + Math.abs(id), 1, notification); nm.notify("unseen:" + Math.abs(id), 1, notification);
} }
if (all.size() > 0) if (all.size() > 0)
editor.putString("notifying:" + account, TextUtils.join(",", all)); editor.putString("notifying:" + channelName, TextUtils.join(",", all));
} }
editor.apply(); editor.apply();
@ -1531,7 +1530,7 @@ class Core {
private static List<Notification> getNotificationUnseen( private static List<Notification> getNotificationUnseen(
Context context, Context context,
long account, String accountName, String channelName, String subTitle,
List<TupleMessageEx> messages) { List<TupleMessageEx> messages) {
List<Notification> notifications = new ArrayList<>(); List<Notification> notifications = new ArrayList<>();
@ -1542,7 +1541,7 @@ class Core {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
// https://developer.android.com/training/notify-user/group // https://developer.android.com/training/notify-user/group
String group = Long.toString(account); String group = channelName;
String title = context.getResources().getQuantityString( String title = context.getResources().getQuantityString(
R.plurals.title_notification_unseen, messages.size(), messages.size()); R.plurals.title_notification_unseen, messages.size(), messages.size());
@ -1561,8 +1560,6 @@ class Core {
clear.setAction("clear"); clear.setAction("clear");
PendingIntent piClear = PendingIntent.getService(context, ServiceUI.PI_CLEAR, clear, PendingIntent.FLAG_UPDATE_CURRENT); PendingIntent piClear = PendingIntent.getService(context, ServiceUI.PI_CLEAR, clear, PendingIntent.FLAG_UPDATE_CURRENT);
String channelName = (account == 0 ? "notification" : EntityAccount.getNotificationChannelName(account));
// Build public notification // Build public notification
NotificationCompat.Builder pbuilder = new NotificationCompat.Builder(context, channelName); NotificationCompat.Builder pbuilder = new NotificationCompat.Builder(context, channelName);
@ -1577,8 +1574,8 @@ class Core {
.setCategory(Notification.CATEGORY_STATUS) .setCategory(Notification.CATEGORY_STATUS)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC); .setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
if (!TextUtils.isEmpty(accountName)) if (!TextUtils.isEmpty(subTitle))
pbuilder.setSubText(accountName); pbuilder.setSubText(subTitle);
// Build notification // Build notification
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, channelName); NotificationCompat.Builder builder = new NotificationCompat.Builder(context, channelName);
@ -1597,8 +1594,8 @@ class Core {
.setGroup(group) .setGroup(group)
.setGroupSummary(true); .setGroupSummary(true);
if (!TextUtils.isEmpty(accountName)) if (!TextUtils.isEmpty(subTitle))
builder.setSubText(accountName); builder.setSubText(subTitle);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
boolean light = prefs.getBoolean("light", false); boolean light = prefs.getBoolean("light", false);
@ -1641,7 +1638,6 @@ class Core {
Bundle args = new Bundle(); Bundle args = new Bundle();
args.putLong("id", message.content ? message.id : -message.id); args.putLong("id", message.content ? message.id : -message.id);
args.putLong("account", message.accountNotify ? message.account : 0);
Intent thread = new Intent(context, ActivityView.class); Intent thread = new Intent(context, ActivityView.class);
thread.setAction("thread:" + message.thread); thread.setAction("thread:" + message.thread);

@ -96,7 +96,7 @@ public class EntityAccount implements Serializable {
} }
static String getNotificationChannelName(long account) { static String getNotificationChannelName(long account) {
return "notification." + account; return "notification" + (account == 0 ? "" : "." + account);
} }
@RequiresApi(api = Build.VERSION_CODES.O) @RequiresApi(api = Build.VERSION_CODES.O)

Loading…
Cancel
Save