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

View file

@ -447,6 +447,14 @@
</intent-filter> </intent-filter>
</receiver> </receiver>
<receiver android:name=".notifications.DeclineCallReceiver"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="org.thoughtcrime.securesms.notifications.DECLINE_CALL_NOTICED"/>
</intent-filter>
</receiver>
<receiver android:name=".notifications.RemoteReplyReceiver" <receiver android:name=".notifications.RemoteReplyReceiver"
android:enabled="true" android:enabled="true"
android:exported="false"> android:exported="false">

View file

@ -187,6 +187,10 @@ public class DcEventCenter {
DcHelper.getNotificationCenter(context).notifyCall(accountId, event.getData1Int(), event.getData2Str()); DcHelper.getNotificationCenter(context).notifyCall(accountId, event.getData1Int(), event.getData2Str());
break; break;
case DcContext.DC_EVENT_CALL_ENDED:
DcHelper.getNotificationCenter(context).removeCallNotification(accountId, event.getData1Int());
break;
case DcContext.DC_EVENT_MSGS_NOTICED: case DcContext.DC_EVENT_MSGS_NOTICED:
DcHelper.getNotificationCenter(context).removeNotifications(accountId, event.getData1Int()); DcHelper.getNotificationCenter(context).removeNotifications(accountId, event.getData1Int());
break; break;

View file

@ -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);
});
}
}

View file

@ -174,6 +174,15 @@ public class NotificationCenter {
.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT | IntentUtils.FLAG_MUTABLE()); .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 // Groups and Notification channel groups
// -------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------
@ -407,6 +416,8 @@ public class NotificationCenter {
ChatData chatData = new ChatData(accId, chatId); ChatData chatData = new ChatData(accId, chatId);
String notificationChannel = getCallNotificationChannel(notificationManager, chatData, name); String notificationChannel = getCallNotificationChannel(notificationManager, chatData, name);
PendingIntent declineCallIntent = getDeclineCallIntent(chatData, callId);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, notificationChannel) NotificationCompat.Builder builder = new NotificationCompat.Builder(context, notificationChannel)
.setSmallIcon(R.drawable.icon_notification) .setSmallIcon(R.drawable.icon_notification)
.setColor(context.getResources().getColor(R.color.delta_primary)) .setColor(context.getResources().getColor(R.color.delta_primary))
@ -417,9 +428,13 @@ public class NotificationCenter {
.setTicker(name) .setTicker(name)
.setContentTitle(name) .setContentTitle(name)
.setContentText("Incoming Call") .setContentText("Incoming Call")
; .setDeleteIntent(declineCallIntent);
// .setDeleteIntenta(getMarkAsReadIntent(chatData, msgId, false))
// .setContentIntent(getOpenChatIntent(chatData)); builder.addAction(
new NotificationCompat.Action.Builder(
R.drawable.baseline_call_end_24,
context.getString(R.string.end_call),
declineCallIntent).build());
builder.addAction( builder.addAction(
new NotificationCompat.Action.Builder( new NotificationCompat.Action.Builder(
@ -437,7 +452,7 @@ public class NotificationCenter {
// add notification, we use one notification per chat, // add notification, we use one notification per chat,
// esp. older android are not that great at grouping // esp. older android are not that great at grouping
try { try {
notificationManager.notify(String.valueOf(accId), ID_MSG_OFFSET + chatId, notif); notificationManager.notify("call-" + accId, callId, notif);
} catch (Exception e) { } catch (Exception e) {
Log.e(TAG, "cannot add notification", e); Log.e(TAG, "cannot add notification", e);
} }
@ -732,6 +747,14 @@ public class NotificationCenter {
return null; 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) { public void removeNotifications(int accountId, int chatId) {
boolean removeSummary; boolean removeSummary;
synchronized (inboxes) { synchronized (inboxes) {

View file

@ -44,7 +44,9 @@ public class VideochatActivity extends WebViewActivity implements DcEventCenter.
chatId = bundle.getInt(EXTRA_CHAT_ID, 0); chatId = bundle.getInt(EXTRA_CHAT_ID, 0);
callId = bundle.getInt(EXTRA_CALL_ID, 0); callId = bundle.getInt(EXTRA_CALL_ID, 0);
int accId = bundle.getInt(EXTRA_ACCOUNT_ID, -1); 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 webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true); webSettings.setJavaScriptEnabled(true);

View file

@ -0,0 +1,5 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="24dp" android:tint="#FFFFFF" android:viewportHeight="24" android:viewportWidth="24" android:width="24dp">
<path android:fillColor="@android:color/white" android:pathData="M12,9c-1.6,0 -3.15,0.25 -4.6,0.72v3.1c0,0.39 -0.23,0.74 -0.56,0.9 -0.98,0.49 -1.87,1.12 -2.66,1.85 -0.18,0.18 -0.43,0.28 -0.7,0.28 -0.28,0 -0.53,-0.11 -0.71,-0.29L0.29,13.08c-0.18,-0.17 -0.29,-0.42 -0.29,-0.7 0,-0.28 0.11,-0.53 0.29,-0.71C3.34,8.78 7.46,7 12,7s8.66,1.78 11.71,4.67c0.18,0.18 0.29,0.43 0.29,0.71 0,0.28 -0.11,0.53 -0.29,0.71l-2.48,2.48c-0.18,0.18 -0.43,0.29 -0.71,0.29 -0.27,0 -0.52,-0.11 -0.7,-0.28 -0.79,-0.74 -1.69,-1.36 -2.67,-1.85 -0.33,-0.16 -0.56,-0.5 -0.56,-0.9v-3.1C15.15,9.25 13.6,9 12,9z"/>
</vector>