From 402fedaf781b325f69f7a2ef0c64ddce3a5edd1b Mon Sep 17 00:00:00 2001 From: adbenitez Date: Thu, 2 Feb 2023 13:05:03 -0500 Subject: [PATCH] allow notifications for all accounts --- .../securesms/ConversationActivity.java | 17 ++- .../securesms/ConversationListActivity.java | 14 +- .../securesms/ConversationListFragment.java | 3 +- .../securesms/connect/AccountManager.java | 3 - .../securesms/connect/DcEventCenter.java | 15 +- .../notifications/MarkReadReceiver.java | 8 +- .../notifications/NotificationCenter.java | 136 +++++++++++------- .../notifications/RemoteReplyReceiver.java | 25 ++-- 8 files changed, 135 insertions(+), 86 deletions(-) diff --git a/src/org/thoughtcrime/securesms/ConversationActivity.java b/src/org/thoughtcrime/securesms/ConversationActivity.java index eb4d0828a..47c431fb9 100644 --- a/src/org/thoughtcrime/securesms/ConversationActivity.java +++ b/src/org/thoughtcrime/securesms/ConversationActivity.java @@ -85,6 +85,7 @@ import org.thoughtcrime.securesms.components.camera.QuickAttachmentDrawer.Attach import org.thoughtcrime.securesms.components.camera.QuickAttachmentDrawer.DrawerState; import org.thoughtcrime.securesms.components.emoji.EmojiKeyboardProvider; import org.thoughtcrime.securesms.components.emoji.MediaKeyboard; +import org.thoughtcrime.securesms.connect.AccountManager; import org.thoughtcrime.securesms.connect.DcEventCenter; import org.thoughtcrime.securesms.connect.DcHelper; import org.thoughtcrime.securesms.connect.DirectShareUtil; @@ -157,6 +158,7 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity { private static final String TAG = ConversationActivity.class.getSimpleName(); + public static final String ACCOUNT_ID_EXTRA = "account_id"; public static final String CHAT_ID_EXTRA = "chat_id"; public static final String FROM_ARCHIVED_CHATS_EXTRA = "from_archived"; public static final String TEXT_EXTRA = "draft_text"; @@ -203,6 +205,7 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity private QuickAttachmentDrawer quickAttachmentDrawer; private InputPanel inputPanel; + private ApplicationContext context; private Recipient recipient; private DcContext dcContext; private DcChat dcChat = new DcChat(0); @@ -226,7 +229,7 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity @Override protected void onCreate(Bundle state, boolean ready) { - final Context context = getApplicationContext(); + this.context = ApplicationContext.getInstance(getApplicationContext()); this.dcContext = DcHelper.getContext(context); supportRequestWindowFeature(WindowCompat.FEATURE_ACTION_BAR_OVERLAY); @@ -327,7 +330,7 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity titleView.setTitle(glideRequests, dcChat); - DcHelper.getNotificationCenter(this).updateVisibleChat(chatId); + DcHelper.getNotificationCenter(this).updateVisibleChat(dcContext.getAccountId(), chatId); if (doReinitializeDraft) { initializeDraft(); @@ -347,7 +350,7 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity doReinitializeDraft = true; } - DcHelper.getNotificationCenter(this).updateVisibleChat(0); + DcHelper.getNotificationCenter(this).updateVisibleChat(0, 0); if (isFinishing()) overridePendingTransition(R.anim.fade_scale_in, R.anim.slide_to_right); quickAttachmentDrawer.onPause(); inputPanel.onPause(); @@ -962,6 +965,12 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity } private void initializeResources() { + int accountId = getIntent().getIntExtra(ACCOUNT_ID_EXTRA, dcContext.getAccountId()); + if (accountId != dcContext.getAccountId()) { + AccountManager.getInstance().switchAccount(context, accountId); + dcContext = context.dcContext; + fragment.dcContext = context.dcContext; + } chatId = getIntent().getIntExtra(CHAT_ID_EXTRA, -1); if(chatId == DcChat.DC_CHAT_NO_CHAT) throw new IllegalStateException("can't display a conversation for no chat."); @@ -1211,7 +1220,7 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity if (refreshFragment) { fragment.reload(recipient, chatId); - DcHelper.getNotificationCenter(this).updateVisibleChat(chatId); + DcHelper.getNotificationCenter(this).updateVisibleChat(dcContext.getAccountId(), chatId); } fragment.scrollToBottom(); diff --git a/src/org/thoughtcrime/securesms/ConversationListActivity.java b/src/org/thoughtcrime/securesms/ConversationListActivity.java index 630422b87..77a371082 100644 --- a/src/org/thoughtcrime/securesms/ConversationListActivity.java +++ b/src/org/thoughtcrime/securesms/ConversationListActivity.java @@ -76,6 +76,7 @@ public class ConversationListActivity extends PassphraseRequiredActionBarActivit private static final String OPENPGP4FPR = "openpgp4fpr"; private static final String NDK_ARCH_WARNED = "ndk_arch_warned"; public static final String CLEAR_NOTIFICATIONS = "clear_notifications"; + public static final String ACCOUNT_ID_EXTRA = "account_id"; private final DynamicTheme dynamicTheme = new DynamicNoActionBarTheme(); private final DynamicLanguage dynamicLanguage = new DynamicLanguage(); @@ -201,6 +202,15 @@ public class ConversationListActivity extends PassphraseRequiredActionBarActivit } private void refresh() { + DcContext dcContext = DcHelper.getContext(this); + int accountId = getIntent().getIntExtra(ACCOUNT_ID_EXTRA, dcContext.getAccountId()); + if (getIntent().getBooleanExtra(CLEAR_NOTIFICATIONS, false)) { + DcHelper.getNotificationCenter(this).removeAllNotifiations(accountId); + } + if (accountId != dcContext.getAccountId()) { + AccountManager.getInstance().switchAccountAndStartActivity(this, accountId, null); + } + refreshAvatar(); refreshTitle(); handleOpenpgp4fpr(); @@ -211,10 +221,6 @@ public class ConversationListActivity extends PassphraseRequiredActionBarActivit if (isDirectSharing(this)) { openConversation(getDirectSharingChatId(this), -1); } - - if (getIntent().getBooleanExtra(CLEAR_NOTIFICATIONS, false)) { - DcHelper.getNotificationCenter(this).removeAllNotifiations(); - } } public void refreshTitle() { diff --git a/src/org/thoughtcrime/securesms/ConversationListFragment.java b/src/org/thoughtcrime/securesms/ConversationListFragment.java index 46a217543..8148e85fa 100644 --- a/src/org/thoughtcrime/securesms/ConversationListFragment.java +++ b/src/org/thoughtcrime/securesms/ConversationListFragment.java @@ -350,8 +350,9 @@ public class ConversationListFragment extends Fragment @Override protected Void doInBackground(Void... params) { + int accountId = dcContext.getAccountId(); for (long chatId : selectedConversations) { - DcHelper.getNotificationCenter(getContext()).removeNotifications((int) chatId); + DcHelper.getNotificationCenter(getContext()).removeNotifications(accountId, (int) chatId); dcContext.deleteChat((int) chatId); DirectShareUtil.clearShortcut(getContext(), (int) chatId); } diff --git a/src/org/thoughtcrime/securesms/connect/AccountManager.java b/src/org/thoughtcrime/securesms/connect/AccountManager.java index e175a4f42..8371268c3 100644 --- a/src/org/thoughtcrime/securesms/connect/AccountManager.java +++ b/src/org/thoughtcrime/securesms/connect/AccountManager.java @@ -30,10 +30,7 @@ public class AccountManager { private void resetDcContext(Context context) { ApplicationContext appContext = (ApplicationContext)context.getApplicationContext(); - DcHelper.getNotificationCenter(context).removeAllNotifiations(); appContext.dcContext = appContext.dcAccounts.getSelectedAccount(); - appContext.notificationCenter = new NotificationCenter(context); - appContext.eventCenter = new DcEventCenter(context); DcHelper.setStockTranslations(context); DirectShareUtil.resetAllShortcuts(appContext); } diff --git a/src/org/thoughtcrime/securesms/connect/DcEventCenter.java b/src/org/thoughtcrime/securesms/connect/DcEventCenter.java index 957ec600a..1c0fecd9c 100644 --- a/src/org/thoughtcrime/securesms/connect/DcEventCenter.java +++ b/src/org/thoughtcrime/securesms/connect/DcEventCenter.java @@ -137,11 +137,22 @@ public class DcEventCenter { public long handleEvent(@NonNull DcEvent event) { int accountId = event.getAccountId(); + int id = event.getId(); + + switch (id) { + case DcContext.DC_EVENT_INCOMING_MSG: + DcHelper.getNotificationCenter(context).addNotification(accountId, event.getData1Int(), event.getData2Int()); + break; + + case DcContext.DC_EVENT_MSGS_NOTICED: + DcHelper.getNotificationCenter(context).removeNotifications(accountId, event.getData1Int()); + break; + } + if (accountId != context.dcContext.getAccountId()) { return 0; } - int id = event.getId(); switch (id) { case DcContext.DC_EVENT_INFO: Log.i("DeltaChat", event.getData2Str()); @@ -160,12 +171,10 @@ public class DcEventCenter { break; case DcContext.DC_EVENT_INCOMING_MSG: - DcHelper.getNotificationCenter(context).addNotification(event.getData1Int(), event.getData2Int()); sendToObservers(event); break; case DcContext.DC_EVENT_MSGS_NOTICED: - DcHelper.getNotificationCenter(context).removeNotifications(event.getData1Int()); sendToObservers(event); break; diff --git a/src/org/thoughtcrime/securesms/notifications/MarkReadReceiver.java b/src/org/thoughtcrime/securesms/notifications/MarkReadReceiver.java index 3c0df2378..641b5e9b4 100644 --- a/src/org/thoughtcrime/securesms/notifications/MarkReadReceiver.java +++ b/src/org/thoughtcrime/securesms/notifications/MarkReadReceiver.java @@ -12,6 +12,7 @@ import org.thoughtcrime.securesms.util.Util; public class MarkReadReceiver extends BroadcastReceiver { public static final String MARK_NOTICED_ACTION = "org.thoughtcrime.securesms.notifications.MARK_NOTICED"; public static final String CANCEL_ACTION = "org.thoughtcrime.securesms.notifications.CANCEL"; + public static final String ACCOUNT_ID_EXTRA = "account_id"; public static final String CHAT_ID_EXTRA = "chat_id"; @Override @@ -21,15 +22,16 @@ public class MarkReadReceiver extends BroadcastReceiver { return; } + final int accountId = intent.getIntExtra(ACCOUNT_ID_EXTRA, 0); final int chatId = intent.getIntExtra(CHAT_ID_EXTRA, 0); - if (chatId==0) { + if (accountId == 0 || chatId == 0) { return; } Util.runOnAnyBackgroundThread(() -> { - DcHelper.getNotificationCenter(context).removeNotifications(chatId); + DcHelper.getNotificationCenter(context).removeNotifications(accountId, chatId); if (markNoticed) { - DcHelper.getContext(context).marknoticedChat(chatId); + DcHelper.getAccounts(context).getAccount(accountId).marknoticedChat(chatId); } }); } diff --git a/src/org/thoughtcrime/securesms/notifications/NotificationCenter.java b/src/org/thoughtcrime/securesms/notifications/NotificationCenter.java index 50663f591..fcd124929 100644 --- a/src/org/thoughtcrime/securesms/notifications/NotificationCenter.java +++ b/src/org/thoughtcrime/securesms/notifications/NotificationCenter.java @@ -15,6 +15,7 @@ import android.net.Uri; import android.os.Build; import android.text.TextUtils; import android.util.Log; +import android.util.Pair; import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -28,10 +29,10 @@ import com.b44t.messenger.DcContext; import com.b44t.messenger.DcMsg; import com.bumptech.glide.load.engine.DiskCacheStrategy; +import org.thoughtcrime.securesms.ApplicationContext; import org.thoughtcrime.securesms.ConversationActivity; import org.thoughtcrime.securesms.ConversationListActivity; import org.thoughtcrime.securesms.R; -import org.thoughtcrime.securesms.connect.DcHelper; import org.thoughtcrime.securesms.contacts.avatars.ContactPhoto; import org.thoughtcrime.securesms.mms.GlideApp; import org.thoughtcrime.securesms.preferences.widgets.NotificationPrivacyPreference; @@ -50,16 +51,14 @@ import java.util.concurrent.TimeUnit; public class NotificationCenter { private static final String TAG = NotificationCenter.class.getSimpleName(); - @NonNull private DcContext dcContext; - @NonNull private Context context; - private volatile int visibleChatId = 0; + @NonNull private ApplicationContext context; + private volatile Pair visibleChat = new Pair<>(0, 0); private volatile long lastAudibleNotification = 0; private static final long MIN_AUDIBLE_PERIOD_MILLIS = TimeUnit.SECONDS.toMillis(2); - private final HashMap> inboxes = new HashMap<>(); // contains the last lines of each chat + private final HashMap>> inboxes = new HashMap<>(); // contains the last lines of each chat public NotificationCenter(Context context) { - this.context = context.getApplicationContext(); - this.dcContext = DcHelper.getContext(context); + this.context = ApplicationContext.getInstance(context); } private @Nullable Uri effectiveSound(int chatId) { // chatId=0: return app-global setting @@ -101,14 +100,16 @@ public class NotificationCenter { return argb; } - private PendingIntent getOpenChatlistIntent() { + private PendingIntent getOpenChatlistIntent(int accountId) { Intent intent = new Intent(context, ConversationListActivity.class); + intent.putExtra(ConversationListActivity.ACCOUNT_ID_EXTRA, accountId); intent.putExtra(ConversationListActivity.CLEAR_NOTIFICATIONS, true); return PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT | IntentUtils.FLAG_MUTABLE()); } - private PendingIntent getOpenChatIntent(int chatId) { + private PendingIntent getOpenChatIntent(int accountId, int chatId) { Intent intent = new Intent(context, ConversationActivity.class); + intent.putExtra(ConversationActivity.ACCOUNT_ID_EXTRA, accountId); intent.putExtra(ConversationActivity.CHAT_ID_EXTRA, chatId); intent.setData((Uri.parse("custom://"+System.currentTimeMillis()))); return TaskStackBuilder.create(context) @@ -116,19 +117,21 @@ public class NotificationCenter { .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT | IntentUtils.FLAG_MUTABLE()); } - private PendingIntent getRemoteReplyIntent(int chatId) { + private PendingIntent getRemoteReplyIntent(int accountId, int chatId) { Intent intent = new Intent(RemoteReplyReceiver.REPLY_ACTION); intent.setClass(context, RemoteReplyReceiver.class); intent.setData((Uri.parse("custom://"+System.currentTimeMillis()))); + intent.putExtra(RemoteReplyReceiver.ACCOUNT_ID_EXTRA, accountId); intent.putExtra(RemoteReplyReceiver.CHAT_ID_EXTRA, chatId); intent.setPackage(context.getPackageName()); return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT | IntentUtils.FLAG_MUTABLE()); } - private PendingIntent getMarkAsReadIntent(int chatId, boolean markNoticed) { + private PendingIntent getMarkAsReadIntent(int accountId, int chatId, boolean markNoticed) { Intent intent = new Intent(markNoticed? MarkReadReceiver.MARK_NOTICED_ACTION : MarkReadReceiver.CANCEL_ACTION); intent.setClass(context, MarkReadReceiver.class); intent.setData((Uri.parse("custom://"+System.currentTimeMillis()))); + intent.putExtra(MarkReadReceiver.ACCOUNT_ID_EXTRA, accountId); intent.putExtra(MarkReadReceiver.CHAT_ID_EXTRA, chatId); intent.setPackage(context.getPackageName()); return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT | IntentUtils.FLAG_MUTABLE()); @@ -182,23 +185,19 @@ public class NotificationCenter { return Build.VERSION.SDK_INT >= 26; } - // full name is "ch_msgV_HASH" or "ch_msgV_HASH.CHATID" - private String computeChannelId(String ledColor, boolean vibrate, @Nullable Uri ringtone, int chatId) { + // full name is "ch_msgV_HASH" or "ch_msgV_HASH.ACCOUNTID.CHATID" + private String computeChannelId(String ledColor, boolean vibrate, @Nullable Uri ringtone, int accountId, int chatId) { String channelId = CH_MSG_PREFIX; try { MessageDigest md = MessageDigest.getInstance("SHA-256"); md.update(ledColor.getBytes()); md.update(vibrate ? (byte) 1 : (byte) 0); md.update((ringtone != null ? ringtone.toString() : "").getBytes()); - if (chatId!=0) { - // for multi-account, force different channelIds for maybe the same chatIds in multiple accounts - md.update(dcContext.getConfig("addr").getBytes()); - } String hash = String.format("%X", new BigInteger(1, md.digest())).substring(0, 16); channelId = CH_MSG_PREFIX + CH_MSG_VERSION + "_" + hash; if (chatId!=0) { - channelId += String.format(".%d", chatId); + channelId += String.format(".%d.%d", accountId, chatId); } } catch(Exception e) { @@ -207,15 +206,21 @@ public class NotificationCenter { return channelId; } - // return chatId from "ch_msgV_HASH.CHATID" or 0 - private int parseNotificationChannelChatId(String channelId) { + // return Pair from "ch_msgV_HASH.ACCOUNTID.CHATID" or Pair<0, 0> + private Pair parseNotificationChannelChat(String channelId) { try { int point = channelId.lastIndexOf("."); if (point>0) { - return Integer.parseInt(channelId.substring(point + 1)); + int chatId = Integer.parseInt(channelId.substring(point + 1)); + channelId = channelId.substring(0, point); + point = channelId.lastIndexOf("."); + if (point>0) { + int accountId = Integer.parseInt(channelId.substring(point + 1)); + return new Pair<>(accountId, chatId); + } } } catch(Exception e) { } - return 0; + return new Pair<>(0, 0); } private String getNotificationChannelGroup(NotificationManagerCompat notificationManager) { @@ -226,7 +231,7 @@ public class NotificationCenter { return CH_GRP_MSG; } - private String getNotificationChannel(NotificationManagerCompat notificationManager, DcChat dcChat) { + private String getNotificationChannel(NotificationManagerCompat notificationManager, int accountId, DcChat dcChat) { int chatId = dcChat.getId(); String channelId = CH_MSG_PREFIX; @@ -239,7 +244,7 @@ public class NotificationCenter { boolean isIndependent = requiresIndependentChannel(chatId); // get channel id from these settings - channelId = computeChannelId(ledColor, defaultVibrate, ringtone, isIndependent? chatId : 0); + channelId = computeChannelId(ledColor, defaultVibrate, ringtone, accountId, isIndependent? chatId : 0); // user-visible name of the channel - // we just use the name of the chat or "Default" @@ -263,8 +268,8 @@ public class NotificationCenter { channels.get(i).setName(name); } else { // ... another message channel, delete if it is not in use. - int currChatId = parseNotificationChannelChatId(currChannelId); - if (!currChannelId.equals(computeChannelId(ledColor, effectiveVibrate(currChatId), effectiveSound(currChatId), currChatId))) { + Pair currChat = parseNotificationChannelChat(currChannelId); + if (!currChannelId.equals(computeChannelId(ledColor, effectiveVibrate(currChat.second), effectiveSound(currChat.second), currChat.first, currChat.second))) { notificationManager.deleteNotificationChannel(currChannelId); } } @@ -311,16 +316,17 @@ public class NotificationCenter { // add notifications & co. // -------------------------------------------------------------------------------------------- - public void addNotification(int chatId, int msgId) { + public void addNotification(int accountId, int chatId, int msgId) { Util.runOnAnyBackgroundThread(() -> { + DcContext dcContext = context.dcAccounts.getAccount(accountId); DcChat dcChat = dcContext.getChat(chatId); if (!Prefs.isNotificationsEnabled(context) || dcChat.isMuted()) { return; } - if (chatId == visibleChatId) { + if (visibleChat.equals(Pair.create(accountId, chatId))) { if (Prefs.isInChatNotifications(context)) { InChatSounds.getInstance(context).playIncomingSound(); } @@ -349,7 +355,7 @@ public class NotificationCenter { // even without a name or message displayed, // it makes sense to use separate notification channels and to open the respective chat directly - // the user may eg. have chosen a different sound - String notificationChannel = getNotificationChannel(notificationManager, dcChat); + String notificationChannel = getNotificationChannel(notificationManager, accountId, dcChat); NotificationCompat.Builder builder = new NotificationCompat.Builder(context, notificationChannel) .setSmallIcon(R.drawable.icon_notification) .setColor(context.getResources().getColor(R.color.delta_primary)) @@ -357,15 +363,17 @@ public class NotificationCenter { .setCategory(NotificationCompat.CATEGORY_MESSAGE) .setOnlyAlertOnce(!signal) .setContentText(line) - .setDeleteIntent(getMarkAsReadIntent(chatId, false)) - .setContentIntent(getOpenChatIntent(chatId)); + .setDeleteIntent(getMarkAsReadIntent(accountId, chatId, false)) + .setContentIntent(getOpenChatIntent(accountId, chatId)); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { - builder.setGroup(GRP_MSG); + builder.setGroup(GRP_MSG + "." + accountId); } + String accountAddr = dcContext.getConfig("addr"); if (privacy.isDisplayContact()) { builder.setContentTitle(dcChat.getName()); + builder.setSubText(accountAddr); } // if privacy allows, for better accessibility, @@ -427,8 +435,8 @@ public class NotificationCenter { if (privacy.isDisplayContact() && privacy.isDisplayMessage() && !Prefs.isScreenLockEnabled(context)) { try { - PendingIntent inNotificationReplyIntent = getRemoteReplyIntent(chatId); - PendingIntent markReadIntent = getMarkAsReadIntent(chatId, true); + PendingIntent inNotificationReplyIntent = getRemoteReplyIntent(accountId, chatId); + PendingIntent markReadIntent = getMarkAsReadIntent(accountId, chatId, true); NotificationCompat.Action markAsReadAction = new NotificationCompat.Action(R.drawable.check, context.getString(R.string.notify_dismiss), @@ -460,10 +468,15 @@ public class NotificationCenter { try { NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle(); synchronized (inboxes) { - ArrayList lines = inboxes.get(chatId); + HashMap> accountInbox = inboxes.get(accountId); + if (accountInbox == null) { + accountInbox = new HashMap<>(); + inboxes.put(accountId, accountInbox); + } + ArrayList lines = accountInbox.get(chatId); if (lines == null) { lines = new ArrayList<>(); - inboxes.put(chatId, lines); + accountInbox.put(chatId, lines); } lines.add(line); @@ -486,7 +499,7 @@ public class NotificationCenter { // add notification, we use one notification per chat, // esp. older android are not that great at grouping try { - notificationManager.notify(ID_MSG_OFFSET + chatId, builder.build()); + notificationManager.notify(String.valueOf(accountId), ID_MSG_OFFSET + chatId, builder.build()); } catch (Exception e) { Log.e(TAG, "cannot add notification", e); } @@ -497,15 +510,18 @@ public class NotificationCenter { if (Build.VERSION.SDK_INT >= 24) { try { NotificationCompat.Builder summary = new NotificationCompat.Builder(context, notificationChannel) - .setGroup(GRP_MSG) + .setGroup(GRP_MSG + "." + accountId) .setGroupSummary(true) .setSmallIcon(R.drawable.icon_notification) .setColor(context.getResources().getColor(R.color.delta_primary)) .setCategory(NotificationCompat.CATEGORY_MESSAGE) .setContentTitle("Delta Chat") // content title would only be used on SDK <24 .setContentText("New messages") // content text would only be used on SDK <24 - .setContentIntent(getOpenChatlistIntent()); - notificationManager.notify(ID_MSG_SUMMARY, summary.build()); + .setContentIntent(getOpenChatlistIntent(accountId)); + if (privacy.isDisplayContact()) { + summary.setSubText(accountAddr); + } + notificationManager.notify(String.valueOf(accountId), ID_MSG_SUMMARY, summary.build()); } catch (Exception e) { Log.e(TAG, "cannot add notification summary", e); } @@ -513,38 +529,50 @@ public class NotificationCenter { }); } - public void removeNotifications(int chatId) { + public void removeNotifications(int accountId, int chatId) { boolean removeSummary; synchronized (inboxes) { - inboxes.remove(chatId); - removeSummary = inboxes.isEmpty(); + HashMap> accountInbox = inboxes.get(accountId); + if (accountInbox == null) { + accountInbox = new HashMap<>(); + } + accountInbox.remove(chatId); + removeSummary = accountInbox.isEmpty(); } // cancel notification independently of inboxes array, // due to restarts, the app may have notification even when inboxes is empty. try { NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context); - notificationManager.cancel(ID_MSG_OFFSET + chatId); + String tag = String.valueOf(accountId); + notificationManager.cancel(tag, ID_MSG_OFFSET + chatId); if (removeSummary) { - notificationManager.cancel(ID_MSG_SUMMARY); + notificationManager.cancel(tag, ID_MSG_SUMMARY); } } catch (Exception e) { Log.w(TAG, e); } } - public void removeAllNotifiations() { - synchronized (inboxes) { - inboxes.clear(); - } + public void removeAllNotifiations(int accountId) { NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context); - notificationManager.cancelAll(); + String tag = String.valueOf(accountId); + synchronized (inboxes) { + HashMap> accountInbox = inboxes.get(accountId); + notificationManager.cancel(tag, ID_MSG_SUMMARY); + if (accountInbox != null) { + for (Integer chatId : accountInbox.keySet()) { + notificationManager.cancel(tag, chatId); + } + accountInbox.clear(); + } + } } - public void updateVisibleChat(int chatId) { + public void updateVisibleChat(int accountId, int chatId) { Util.runOnAnyBackgroundThread(() -> { - visibleChatId = chatId; - if (chatId!=0) { - removeNotifications(chatId); + visibleChat = new Pair<>(accountId, chatId); + if (accountId != 0 && chatId != 0) { + removeNotifications(accountId, chatId); } }); diff --git a/src/org/thoughtcrime/securesms/notifications/RemoteReplyReceiver.java b/src/org/thoughtcrime/securesms/notifications/RemoteReplyReceiver.java index b60456558..9122cfcd4 100644 --- a/src/org/thoughtcrime/securesms/notifications/RemoteReplyReceiver.java +++ b/src/org/thoughtcrime/securesms/notifications/RemoteReplyReceiver.java @@ -21,13 +21,13 @@ import android.annotation.SuppressLint; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; -import android.os.AsyncTask; import android.os.Bundle; import androidx.core.app.RemoteInput; import com.b44t.messenger.DcContext; import org.thoughtcrime.securesms.connect.DcHelper; +import org.thoughtcrime.securesms.util.Util; /** @@ -37,6 +37,7 @@ public class RemoteReplyReceiver extends BroadcastReceiver { public static final String TAG = RemoteReplyReceiver.class.getSimpleName(); public static final String REPLY_ACTION = "org.thoughtcrime.securesms.notifications.WEAR_REPLY"; + public static final String ACCOUNT_ID_EXTRA = "account_id"; public static final String CHAT_ID_EXTRA = "chat_id"; public static final String EXTRA_REMOTE_REPLY = "extra_remote_reply"; @@ -45,26 +46,22 @@ public class RemoteReplyReceiver extends BroadcastReceiver { public void onReceive(final Context context, Intent intent) { if (!REPLY_ACTION.equals(intent.getAction())) return; Bundle remoteInput = RemoteInput.getResultsFromIntent(intent); + final int accountId = intent.getIntExtra(ACCOUNT_ID_EXTRA, 0); final int chatId = intent.getIntExtra(CHAT_ID_EXTRA, 0); - if (remoteInput == null || chatId == 0) return; + if (remoteInput == null || chatId == 0 || accountId == 0) return; final CharSequence responseText = remoteInput.getCharSequence(EXTRA_REMOTE_REPLY); if (responseText != null) { - new AsyncTask() { - @Override - protected Void doInBackground(Void... params) { - DcContext dcContext = DcHelper.getContext(context); - if (dcContext.getChat(chatId).isContactRequest()) { - dcContext.acceptChat(chatId); - } - dcContext.sendTextMsg(chatId, responseText.toString()); - DcHelper.getNotificationCenter(context).removeNotifications(chatId); - return null; + Util.runOnAnyBackgroundThread(() -> { + DcContext dcContext = DcHelper.getAccounts(context).getAccount(accountId); + if (dcContext.getChat(chatId).isContactRequest()) { + dcContext.acceptChat(chatId); } - }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + dcContext.sendTextMsg(chatId, responseText.toString()); + DcHelper.getNotificationCenter(context).removeNotifications(accountId, chatId); + }); } - } }