From 996f233d259dec4626cd25bfb4275e2dbaaae7ea Mon Sep 17 00:00:00 2001 From: Mikunj Varsani Date: Thu, 12 Mar 2020 10:42:08 +1100 Subject: [PATCH] Enable quick reply --- .../notifications/MessageNotifier.java | 32 +++++++- .../SingleRecipientNotificationBuilder.java | 74 +++++++++---------- 2 files changed, 61 insertions(+), 45 deletions(-) diff --git a/src/org/thoughtcrime/securesms/notifications/MessageNotifier.java b/src/org/thoughtcrime/securesms/notifications/MessageNotifier.java index 7941014245..3036a79bdd 100644 --- a/src/org/thoughtcrime/securesms/notifications/MessageNotifier.java +++ b/src/org/thoughtcrime/securesms/notifications/MessageNotifier.java @@ -49,6 +49,7 @@ import org.thoughtcrime.securesms.database.model.MediaMmsMessageRecord; import org.thoughtcrime.securesms.database.model.MessageRecord; import org.thoughtcrime.securesms.database.model.MmsMessageRecord; import org.thoughtcrime.securesms.logging.Log; +import org.thoughtcrime.securesms.loki.MultiDeviceUtilities; import org.thoughtcrime.securesms.mms.SlideDeck; import org.thoughtcrime.securesms.recipients.Recipient; import org.thoughtcrime.securesms.service.IncomingMessageObserver; @@ -58,6 +59,8 @@ import org.thoughtcrime.securesms.util.SpanUtil; import org.thoughtcrime.securesms.util.TextSecurePreferences; import org.thoughtcrime.securesms.webrtc.CallNotificationBuilder; import org.whispersystems.signalservice.internal.util.Util; +import org.whispersystems.signalservice.loki.messaging.LokiThreadFriendRequestStatus; +import org.whispersystems.signalservice.loki.utilities.PromiseUtil; import java.util.HashSet; import java.util.List; @@ -70,6 +73,7 @@ import java.util.concurrent.atomic.AtomicBoolean; import me.leolin.shortcutbadger.ShortcutBadger; import network.loki.messenger.R; +import nl.komponents.kovenant.Promise; /** @@ -316,15 +320,35 @@ public class MessageNotifier { long timestamp = notifications.get(0).getTimestamp(); if (timestamp != 0) builder.setWhen(timestamp); + long threadId = notifications.get(0).getThreadId(); + ReplyMethod replyMethod = ReplyMethod.forRecipient(context, recipient); + + // We can only reply if we are friends with the user or we're messaging a group + boolean isGroup = recipient.isGroupRecipient() && !recipient.getAddress().isRSSFeed(); + boolean isFriends = DatabaseFactory.getLokiThreadDatabase(context).getFriendRequestStatus(threadId) == LokiThreadFriendRequestStatus.FRIENDS; + + // If we're not friends then we need to check if we're friends with any of the linked devices + if (!isGroup && !isFriends) { + Promise promise = PromiseUtil.timeout(MultiDeviceUtilities.isFriendsWithAnyLinkedDevice(context, recipient), 5000); + isFriends = PromiseUtil.get(promise, false); + } + + boolean canReply = isGroup || isFriends; + + PendingIntent quickReplyIntent = canReply ? notificationState.getQuickReplyIntent(context, notifications.get(0).getRecipient()) : null; + PendingIntent remoteReplyIntent = canReply ? notificationState.getRemoteReplyIntent(context, notifications.get(0).getRecipient(), replyMethod) : null; + builder.addActions(notificationState.getMarkAsReadIntent(context, notificationId), - /*notificationState.getQuickReplyIntent(context, notifications.get(0).getRecipient()),*/ - notificationState.getRemoteReplyIntent(context, notifications.get(0).getRecipient(), replyMethod), + quickReplyIntent, + remoteReplyIntent, replyMethod); - builder.addAndroidAutoAction(notificationState.getAndroidAutoReplyIntent(context, notifications.get(0).getRecipient()), - notificationState.getAndroidAutoHeardIntent(context, notificationId), notifications.get(0).getTimestamp()); + if (canReply) { + builder.addAndroidAutoAction(notificationState.getAndroidAutoReplyIntent(context, notifications.get(0).getRecipient()), + notificationState.getAndroidAutoHeardIntent(context, notificationId), notifications.get(0).getTimestamp()); + } ListIterator iterator = notifications.listIterator(notifications.size()); diff --git a/src/org/thoughtcrime/securesms/notifications/SingleRecipientNotificationBuilder.java b/src/org/thoughtcrime/securesms/notifications/SingleRecipientNotificationBuilder.java index 0da777e700..a9472ebbf3 100644 --- a/src/org/thoughtcrime/securesms/notifications/SingleRecipientNotificationBuilder.java +++ b/src/org/thoughtcrime/securesms/notifications/SingleRecipientNotificationBuilder.java @@ -28,7 +28,6 @@ import org.thoughtcrime.securesms.contacts.avatars.ContactColors; import org.thoughtcrime.securesms.contacts.avatars.ContactPhoto; import org.thoughtcrime.securesms.contacts.avatars.FallbackContactPhoto; import org.thoughtcrime.securesms.contacts.avatars.GeneratedContactPhoto; -import org.thoughtcrime.securesms.database.DatabaseFactory; import org.thoughtcrime.securesms.logging.Log; import org.thoughtcrime.securesms.mms.DecryptableStreamUriLoader; import org.thoughtcrime.securesms.mms.GlideApp; @@ -39,7 +38,6 @@ import org.thoughtcrime.securesms.recipients.Recipient; import org.thoughtcrime.securesms.util.BitmapUtil; import org.thoughtcrime.securesms.util.TextSecurePreferences; import org.thoughtcrime.securesms.util.Util; -import org.whispersystems.signalservice.loki.api.LokiPublicChat; import java.util.LinkedList; import java.util.List; @@ -156,59 +154,53 @@ public class SingleRecipientNotificationBuilder extends AbstractNotificationBuil } public void addActions(@NonNull PendingIntent markReadIntent, - @NonNull PendingIntent wearableReplyIntent, + @Nullable PendingIntent quickReplyIntent, + @Nullable PendingIntent wearableReplyIntent, @NonNull ReplyMethod replyMethod) { Action markAsReadAction = new Action(R.drawable.check, context.getString(R.string.MessageNotifier_mark_read), markReadIntent); - String actionName = context.getString(R.string.MessageNotifier_reply); - String label = context.getString(replyMethodLongDescription(replyMethod)); + addAction(markAsReadAction); - /* - Action replyAction = new Action(R.drawable.ic_reply_white_36dp, - actionName, - quickReplyIntent); - */ + NotificationCompat.WearableExtender wearableExtender = new NotificationCompat.WearableExtender().addAction(markAsReadAction); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { - /* - replyAction = new Action.Builder(R.drawable.ic_reply_white_36dp, - actionName, - wearableReplyIntent) - .addRemoteInput(new RemoteInput.Builder(MessageNotifier.EXTRA_REMOTE_REPLY) - .setLabel(label).build()) - .build(); - */ + if (quickReplyIntent != null) { + String actionName = context.getString(R.string.MessageNotifier_reply); + String label = context.getString(replyMethodLongDescription(replyMethod)); + + Action replyAction = new Action(R.drawable.ic_reply_white_36dp, + actionName, + quickReplyIntent); + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + replyAction = new Action.Builder(R.drawable.ic_reply_white_36dp, + actionName, + wearableReplyIntent) + .addRemoteInput(new RemoteInput.Builder(MessageNotifier.EXTRA_REMOTE_REPLY) + .setLabel(label).build()) + .build(); + } + + Action wearableReplyAction = new Action.Builder(R.drawable.ic_reply, + actionName, + wearableReplyIntent) + .addRemoteInput(new RemoteInput.Builder(MessageNotifier.EXTRA_REMOTE_REPLY) + .setLabel(label).build()) + .build(); + + + addAction(replyAction); + wearableExtender.addAction(wearableReplyAction); } - Action wearableReplyAction = new Action.Builder(R.drawable.ic_reply, - actionName, - wearableReplyIntent) - .addRemoteInput(new RemoteInput.Builder(MessageNotifier.EXTRA_REMOTE_REPLY) - .setLabel(label).build()) - .build(); - - addAction(markAsReadAction); - // addAction(replyAction); - - extend(new NotificationCompat.WearableExtender().addAction(markAsReadAction) - .addAction(wearableReplyAction)); + extend(wearableExtender); } @StringRes private static int replyMethodLongDescription(@NonNull ReplyMethod replyMethod) { - switch (replyMethod) { - case GroupMessage: - return R.string.MessageNotifier_reply; - case SecureMessage: - return R.string.MessageNotifier_signal_message; - case UnsecuredSmsMessage: - return R.string.MessageNotifier_unsecured_sms; - default: - return R.string.MessageNotifier_reply; - } + return R.string.MessageNotifier_reply; } public void addMessageBody(@NonNull Recipient threadRecipient,