From 6b4f9772a80fe2ba4036fa669b55ec69ff404f97 Mon Sep 17 00:00:00 2001 From: "B. Petersen" Date: Wed, 20 May 2020 17:01:23 +0200 Subject: [PATCH] add remote reply option to notification --- .../notifications/MessageNotifier.java | 4 +- .../notifications/NotificationCenter.java | 40 ++++++++++++++++++- .../notifications/NotificationState.java | 14 ------- .../notifications/RemoteReplyReceiver.java | 17 +++----- 4 files changed, 46 insertions(+), 29 deletions(-) diff --git a/src/org/thoughtcrime/securesms/notifications/MessageNotifier.java b/src/org/thoughtcrime/securesms/notifications/MessageNotifier.java index 0f5230ab9..2bd245a73 100644 --- a/src/org/thoughtcrime/securesms/notifications/MessageNotifier.java +++ b/src/org/thoughtcrime/securesms/notifications/MessageNotifier.java @@ -264,8 +264,8 @@ abstract class MessageNotifier { long timestamp = firstItem.getTimestamp(); if (timestamp != 0) builder.setWhen(timestamp); - builder.addActions(notificationState.getMarkAsReadIntent(context, chatId, notificationId), - notificationState.getRemoteReplyIntent(context, recipient)); + //builder.addActions(notificationState.getMarkAsReadIntent(context, chatId, notificationId), + // notificationState.getRemoteReplyIntent(context, recipient)); ListIterator iterator = notifications.listIterator(notifications.size()); diff --git a/src/org/thoughtcrime/securesms/notifications/NotificationCenter.java b/src/org/thoughtcrime/securesms/notifications/NotificationCenter.java index ae912ffe7..e83cc9d9b 100644 --- a/src/org/thoughtcrime/securesms/notifications/NotificationCenter.java +++ b/src/org/thoughtcrime/securesms/notifications/NotificationCenter.java @@ -19,6 +19,7 @@ import android.util.Log; import androidx.annotation.NonNull; import androidx.core.app.NotificationCompat; import androidx.core.app.NotificationManagerCompat; +import androidx.core.app.RemoteInput; import androidx.core.app.TaskStackBuilder; import com.b44t.messenger.DcChat; @@ -104,8 +105,18 @@ public class NotificationCenter { .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); } + private PendingIntent getRemoteReplyIntent(int chatId) { + Intent intent = new Intent(RemoteReplyReceiver.REPLY_ACTION); + intent.setClass(context, RemoteReplyReceiver.class); + intent.setData((Uri.parse("custom://"+System.currentTimeMillis()))); + intent.putExtra(RemoteReplyReceiver.CHAT_ID_EXTRA, chatId); + intent.setPackage(context.getPackageName()); + return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); + } - // Groups and Notifcation channel groups + + + // Groups and Notification channel groups // -------------------------------------------------------------------------------------------- public static final String GRP_MSG = "chgrp_msg"; @@ -342,10 +353,10 @@ public class NotificationCenter { } // set avatar + Recipient recipient = new Recipient(context, dcChat, null); if (privacy.isDisplayContact()) { try { Drawable drawable; - Recipient recipient = new Recipient(context, dcChat, null); ContactPhoto contactPhoto = recipient.getContactPhoto(context); if (contactPhoto != null) { drawable = GlideApp.with(context.getApplicationContext()) @@ -371,6 +382,31 @@ public class NotificationCenter { } } + // add a reply-button that allows answering without opening Delta Chat. + // the reply-button is useful only if sender+message is displayed and if app-lock is disabled. + if (privacy.isDisplayContact() && privacy.isDisplayMessage() + && !Prefs.isScreenLockEnabled(context)) { + PendingIntent inNotificationReplyIntent = getRemoteReplyIntent(chatId); + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + NotificationCompat.Action replyAction = new NotificationCompat.Action.Builder(R.drawable.ic_reply_white_36dp, + context.getString(R.string.notify_reply_button), + inNotificationReplyIntent) + .addRemoteInput(new RemoteInput.Builder(MessageNotifierCompat.EXTRA_REMOTE_REPLY) + .setLabel(context.getString(R.string.notify_reply_button)).build()) + .build(); + builder.addAction(replyAction); + } + + NotificationCompat.Action wearableReplyAction = new NotificationCompat.Action.Builder(R.drawable.ic_reply, + context.getString(R.string.notify_reply_button), + inNotificationReplyIntent) + .addRemoteInput(new RemoteInput.Builder(MessageNotifierCompat.EXTRA_REMOTE_REPLY) + .setLabel(context.getString(R.string.notify_reply_button)).build()) + .build(); + builder.extend(new NotificationCompat.WearableExtender().addAction(wearableReplyAction)); + } + // messages count, some os make some use of that // - do not use setSubText() as this is displayed together with setContentInfo() eg. on Lollipop // - setNumber() may overwrite setContentInfo(), should be called last diff --git a/src/org/thoughtcrime/securesms/notifications/NotificationState.java b/src/org/thoughtcrime/securesms/notifications/NotificationState.java index ad94b64f9..79bc723ac 100644 --- a/src/org/thoughtcrime/securesms/notifications/NotificationState.java +++ b/src/org/thoughtcrime/securesms/notifications/NotificationState.java @@ -139,18 +139,4 @@ public class NotificationState { return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); } - - PendingIntent getRemoteReplyIntent(Context context, Recipient recipient) { - if (chats.size() != 1) throw new AssertionError("We only support replies to single chat notifications!"); - - Intent intent = new Intent(RemoteReplyReceiver.REPLY_ACTION); - intent.setClass(context, RemoteReplyReceiver.class); - intent.setData((Uri.parse("custom://"+System.currentTimeMillis()))); - intent.putExtra(RemoteReplyReceiver.ADDRESS_EXTRA, recipient.getAddress()); - intent.setPackage(context.getPackageName()); - - return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); - } - - } diff --git a/src/org/thoughtcrime/securesms/notifications/RemoteReplyReceiver.java b/src/org/thoughtcrime/securesms/notifications/RemoteReplyReceiver.java index 3f1096620..e5d2d315f 100644 --- a/src/org/thoughtcrime/securesms/notifications/RemoteReplyReceiver.java +++ b/src/org/thoughtcrime/securesms/notifications/RemoteReplyReceiver.java @@ -27,7 +27,6 @@ import androidx.core.app.RemoteInput; import org.thoughtcrime.securesms.connect.ApplicationDcContext; import org.thoughtcrime.securesms.connect.DcHelper; -import org.thoughtcrime.securesms.database.Address; /** @@ -37,30 +36,26 @@ 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 ADDRESS_EXTRA = "address"; + public static final String CHAT_ID_EXTRA = "chat_id"; @SuppressLint("StaticFieldLeak") @Override public void onReceive(final Context context, Intent intent) { if (!REPLY_ACTION.equals(intent.getAction())) return; Bundle remoteInput = RemoteInput.getResultsFromIntent(intent); + final int chatId = intent.getIntExtra(CHAT_ID_EXTRA, 0); - if (remoteInput == null) return; + if (remoteInput == null || chatId == 0) return; - final Address address = intent.getParcelableExtra(ADDRESS_EXTRA); final CharSequence responseText = remoteInput.getCharSequence(MessageNotifierCompat.EXTRA_REMOTE_REPLY); if (responseText != null) { new AsyncTask() { @Override protected Void doInBackground(Void... params) { - - if(address.isDcChat()) { - ApplicationDcContext dcContext = DcHelper.getContext(context); - dcContext.sendTextMsg(address.getDcChatId(), responseText.toString()); - MessageNotifierCompat.removeNotifications(address.getDcChatId()); - } - + ApplicationDcContext dcContext = DcHelper.getContext(context); + dcContext.sendTextMsg(chatId, responseText.toString()); + dcContext.notificationCenter.removeNotifications(chatId); return null; } }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);