add remote reply option to notification

This commit is contained in:
B. Petersen
2020-05-20 17:01:23 +02:00
parent 73f20f5288
commit 6b4f9772a8
4 changed files with 46 additions and 29 deletions
@@ -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<NotificationItem> iterator = notifications.listIterator(notifications.size());
@@ -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
@@ -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);
}
}
@@ -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<Void, Void, Void>() {
@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);