allow to decline calls and remove call notification

This commit is contained in:
adbenitez
2025-09-10 20:47:25 +02:00
parent b074bf1819
commit 0e91537a4d
6 changed files with 84 additions and 5 deletions
@@ -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;
@@ -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);
});
}
}
@@ -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) {
@@ -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);