From 0e91537a4d41704d0b22b309ddc57cf2ad170656 Mon Sep 17 00:00:00 2001 From: adbenitez Date: Wed, 10 Sep 2025 20:47:25 +0200 Subject: [PATCH] allow to decline calls and remove call notification --- src/main/AndroidManifest.xml | 8 ++++ .../securesms/connect/DcEventCenter.java | 4 ++ .../notifications/DeclineCallReceiver.java | 37 +++++++++++++++++++ .../notifications/NotificationCenter.java | 31 ++++++++++++++-- .../videochat/VideochatActivity.java | 4 +- .../res/drawable/baseline_call_end_24.xml | 5 +++ 6 files changed, 84 insertions(+), 5 deletions(-) create mode 100644 src/main/java/org/thoughtcrime/securesms/notifications/DeclineCallReceiver.java create mode 100644 src/main/res/drawable/baseline_call_end_24.xml diff --git a/src/main/AndroidManifest.xml b/src/main/AndroidManifest.xml index b987209ec..6b96e120d 100644 --- a/src/main/AndroidManifest.xml +++ b/src/main/AndroidManifest.xml @@ -447,6 +447,14 @@ + + + + + + diff --git a/src/main/java/org/thoughtcrime/securesms/connect/DcEventCenter.java b/src/main/java/org/thoughtcrime/securesms/connect/DcEventCenter.java index d0ae0aec4..bce2904fd 100644 --- a/src/main/java/org/thoughtcrime/securesms/connect/DcEventCenter.java +++ b/src/main/java/org/thoughtcrime/securesms/connect/DcEventCenter.java @@ -187,6 +187,10 @@ public class DcEventCenter { DcHelper.getNotificationCenter(context).notifyCall(accountId, event.getData1Int(), event.getData2Str()); break; + case DcContext.DC_EVENT_CALL_ENDED: + DcHelper.getNotificationCenter(context).removeCallNotification(accountId, event.getData1Int()); + break; + case DcContext.DC_EVENT_MSGS_NOTICED: DcHelper.getNotificationCenter(context).removeNotifications(accountId, event.getData1Int()); break; diff --git a/src/main/java/org/thoughtcrime/securesms/notifications/DeclineCallReceiver.java b/src/main/java/org/thoughtcrime/securesms/notifications/DeclineCallReceiver.java new file mode 100644 index 000000000..b8e1ba6e9 --- /dev/null +++ b/src/main/java/org/thoughtcrime/securesms/notifications/DeclineCallReceiver.java @@ -0,0 +1,37 @@ +// called when the user click the "clear" or "mark read" button in the system notification + +package org.thoughtcrime.securesms.notifications; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; + +import com.b44t.messenger.DcContext; + +import org.thoughtcrime.securesms.connect.DcHelper; +import org.thoughtcrime.securesms.util.Util; + +public class DeclineCallReceiver extends BroadcastReceiver { + public static final String DECLINE_ACTION = "org.thoughtcrime.securesms.notifications.DECLINE_CALL"; + public static final String ACCOUNT_ID_EXTRA = "account_id"; + public static final String CALL_ID_EXTRA = "call_id"; + + @Override + public void onReceive(final Context context, Intent intent) { + if (!DECLINE_ACTION.equals(intent.getAction())) { + return; + } + + final int accountId = intent.getIntExtra(ACCOUNT_ID_EXTRA, 0); + final int callId = intent.getIntExtra(CALL_ID_EXTRA, 0); + if (accountId == 0) { + return; + } + + Util.runOnAnyBackgroundThread(() -> { + DcHelper.getNotificationCenter(context).removeCallNotification(accountId, callId); + DcContext dcContext = DcHelper.getAccounts(context).getAccount(accountId); + dcContext.endCall(callId); + }); + } +} diff --git a/src/main/java/org/thoughtcrime/securesms/notifications/NotificationCenter.java b/src/main/java/org/thoughtcrime/securesms/notifications/NotificationCenter.java index 32967e1ef..96c540a40 100644 --- a/src/main/java/org/thoughtcrime/securesms/notifications/NotificationCenter.java +++ b/src/main/java/org/thoughtcrime/securesms/notifications/NotificationCenter.java @@ -174,6 +174,15 @@ public class NotificationCenter { .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT | IntentUtils.FLAG_MUTABLE()); } + private PendingIntent getDeclineCallIntent(ChatData chatData, int callId) { + Intent intent = new Intent(DeclineCallReceiver.DECLINE_ACTION); + intent.setClass(context, DeclineCallReceiver.class); + intent.putExtra(DeclineCallReceiver.ACCOUNT_ID_EXTRA, chatData.accountId); + intent.putExtra(DeclineCallReceiver.CALL_ID_EXTRA, callId); + intent.setPackage(context.getPackageName()); + return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT | IntentUtils.FLAG_MUTABLE()); + } + // Groups and Notification channel groups // -------------------------------------------------------------------------------------------- @@ -407,6 +416,8 @@ public class NotificationCenter { ChatData chatData = new ChatData(accId, chatId); String notificationChannel = getCallNotificationChannel(notificationManager, chatData, name); + PendingIntent declineCallIntent = getDeclineCallIntent(chatData, callId); + NotificationCompat.Builder builder = new NotificationCompat.Builder(context, notificationChannel) .setSmallIcon(R.drawable.icon_notification) .setColor(context.getResources().getColor(R.color.delta_primary)) @@ -417,9 +428,13 @@ public class NotificationCenter { .setTicker(name) .setContentTitle(name) .setContentText("Incoming Call") - ; - // .setDeleteIntenta(getMarkAsReadIntent(chatData, msgId, false)) - // .setContentIntent(getOpenChatIntent(chatData)); + .setDeleteIntent(declineCallIntent); + + builder.addAction( + new NotificationCompat.Action.Builder( + R.drawable.baseline_call_end_24, + context.getString(R.string.end_call), + declineCallIntent).build()); builder.addAction( new NotificationCompat.Action.Builder( @@ -437,7 +452,7 @@ public class NotificationCenter { // add notification, we use one notification per chat, // esp. older android are not that great at grouping try { - notificationManager.notify(String.valueOf(accId), ID_MSG_OFFSET + chatId, notif); + notificationManager.notify("call-" + accId, callId, notif); } catch (Exception e) { Log.e(TAG, "cannot add notification", e); } @@ -732,6 +747,14 @@ public class NotificationCenter { return null; } + public void removeCallNotification(int accountId, int callId) { + try { + NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context); + String tag = "call-" + accountId; + notificationManager.cancel(tag, callId); + } catch (Exception e) { Log.w(TAG, e); } + } + public void removeNotifications(int accountId, int chatId) { boolean removeSummary; synchronized (inboxes) { diff --git a/src/main/java/org/thoughtcrime/securesms/videochat/VideochatActivity.java b/src/main/java/org/thoughtcrime/securesms/videochat/VideochatActivity.java index 4433145a9..e20d6635d 100644 --- a/src/main/java/org/thoughtcrime/securesms/videochat/VideochatActivity.java +++ b/src/main/java/org/thoughtcrime/securesms/videochat/VideochatActivity.java @@ -44,7 +44,9 @@ public class VideochatActivity extends WebViewActivity implements DcEventCenter. chatId = bundle.getInt(EXTRA_CHAT_ID, 0); callId = bundle.getInt(EXTRA_CALL_ID, 0); int accId = bundle.getInt(EXTRA_ACCOUNT_ID, -1); - this.dcContext = DcHelper.getAccounts(getApplicationContext()).getAccount(accId); + this.dcContext = DcHelper.getAccounts(this).getAccount(accId); + + DcHelper.getNotificationCenter(this).removeCallNotification(accId, callId); WebSettings webSettings = webView.getSettings(); webSettings.setJavaScriptEnabled(true); diff --git a/src/main/res/drawable/baseline_call_end_24.xml b/src/main/res/drawable/baseline_call_end_24.xml new file mode 100644 index 000000000..fa4cd2c10 --- /dev/null +++ b/src/main/res/drawable/baseline_call_end_24.xml @@ -0,0 +1,5 @@ + + + + +