use PM on direct reply if last message in notifacation stack is PM

This commit is contained in:
Daniel Gultsch 2021-10-03 16:38:30 +02:00
parent daf1bbfca5
commit f182fe6697
4 changed files with 53 additions and 19 deletions

View File

@ -271,6 +271,17 @@ public class Conversation extends AbstractEntity implements Blockable, Comparabl
return null; return null;
} }
public Message findMessageWithUuid(final String uuid) {
synchronized (this.messages) {
for (final Message message : this.messages) {
if (message.getUuid().equals(uuid)) {
return message;
}
}
}
return null;
}
public boolean markAsDeleted(final List<String> uuids) { public boolean markAsDeleted(final List<String> uuids) {
boolean deleted = false; boolean deleted = false;
final PgpDecryptionService pgpDecryptionService = account.getPgpDecryptionService(); final PgpDecryptionService pgpDecryptionService = account.getPgpDecryptionService();

View File

@ -984,13 +984,28 @@ public class Message extends AbstractEntity implements AvatarService.Avatarable
} }
if (conversation.getMode() == Conversation.MODE_MULTI) { if (conversation.getMode() == Conversation.MODE_MULTI) {
final Jid nextCounterpart = conversation.getNextCounterpart(); final Jid nextCounterpart = conversation.getNextCounterpart();
if (nextCounterpart != null) { return configurePrivateMessage(conversation, message, nextCounterpart, isFile);
message.setCounterpart(nextCounterpart);
message.setTrueCounterpart(conversation.getMucOptions().getTrueCounterpart(nextCounterpart));
message.setType(isFile ? Message.TYPE_PRIVATE_FILE : Message.TYPE_PRIVATE);
return true;
}
} }
return false; return false;
} }
public static boolean configurePrivateMessage(final Message message, final Jid counterpart) {
final Conversation conversation;
if (message.conversation instanceof Conversation) {
conversation = (Conversation) message.conversation;
} else {
return false;
}
return configurePrivateMessage(conversation, message, counterpart, false);
}
private static boolean configurePrivateMessage(final Conversation conversation, final Message message, final Jid counterpart, final boolean isFile) {
if (counterpart == null) {
return false;
}
message.setCounterpart(counterpart);
message.setTrueCounterpart(conversation.getMucOptions().getTrueCounterpart(counterpart));
message.setType(isFile ? Message.TYPE_PRIVATE_FILE : Message.TYPE_PRIVATE);
return true;
}
} }

View File

@ -35,6 +35,7 @@ import androidx.core.content.ContextCompat;
import androidx.core.graphics.drawable.IconCompat; import androidx.core.graphics.drawable.IconCompat;
import com.google.common.base.Strings; import com.google.common.base.Strings;
import com.google.common.collect.Iterables;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
@ -407,7 +408,7 @@ public class NotificationService {
currentInterruptionFilter = 1; //INTERRUPTION_FILTER_ALL currentInterruptionFilter = 1; //INTERRUPTION_FILTER_ALL
} }
if (currentInterruptionFilter != 1) { if (currentInterruptionFilter != 1) {
Log.d(Config.LOGTAG,"do not ring or vibrate because interruption filter has been set to "+currentInterruptionFilter); Log.d(Config.LOGTAG, "do not ring or vibrate because interruption filter has been set to " + currentInterruptionFilter);
return; return;
} }
final ScheduledFuture<?> currentVibrationFuture = this.vibrationFuture; final ScheduledFuture<?> currentVibrationFuture = this.vibrationFuture;
@ -424,13 +425,13 @@ public class NotificationService {
final Resources resources = mXmppConnectionService.getResources(); final Resources resources = mXmppConnectionService.getResources();
final String ringtonePreference = preferences.getString("call_ringtone", resources.getString(R.string.incoming_call_ringtone)); final String ringtonePreference = preferences.getString("call_ringtone", resources.getString(R.string.incoming_call_ringtone));
if (Strings.isNullOrEmpty(ringtonePreference)) { if (Strings.isNullOrEmpty(ringtonePreference)) {
Log.d(Config.LOGTAG,"ringtone has been set to none"); Log.d(Config.LOGTAG, "ringtone has been set to none");
return; return;
} }
final Uri uri = Uri.parse(ringtonePreference); final Uri uri = Uri.parse(ringtonePreference);
this.currentlyPlayingRingtone = RingtoneManager.getRingtone(mXmppConnectionService, uri); this.currentlyPlayingRingtone = RingtoneManager.getRingtone(mXmppConnectionService, uri);
if (this.currentlyPlayingRingtone == null) { if (this.currentlyPlayingRingtone == null) {
Log.d(Config.LOGTAG,"unable to find ringtone for uri "+uri); Log.d(Config.LOGTAG, "unable to find ringtone for uri " + uri);
return; return;
} }
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
@ -790,17 +791,18 @@ public class NotificationService {
.setSemanticAction(NotificationCompat.Action.SEMANTIC_ACTION_MARK_AS_READ) .setSemanticAction(NotificationCompat.Action.SEMANTIC_ACTION_MARK_AS_READ)
.setShowsUserInterface(false) .setShowsUserInterface(false)
.build(); .build();
String replyLabel = mXmppConnectionService.getString(R.string.reply); final String replyLabel = mXmppConnectionService.getString(R.string.reply);
NotificationCompat.Action replyAction = new NotificationCompat.Action.Builder( final String lastMessageUuid = Iterables.getLast(messages).getUuid();
final NotificationCompat.Action replyAction = new NotificationCompat.Action.Builder(
R.drawable.ic_send_text_offline, R.drawable.ic_send_text_offline,
replyLabel, replyLabel,
createReplyIntent(conversation, false)) createReplyIntent(conversation, lastMessageUuid, false))
.setSemanticAction(NotificationCompat.Action.SEMANTIC_ACTION_REPLY) .setSemanticAction(NotificationCompat.Action.SEMANTIC_ACTION_REPLY)
.setShowsUserInterface(false) .setShowsUserInterface(false)
.addRemoteInput(remoteInput).build(); .addRemoteInput(remoteInput).build();
NotificationCompat.Action wearReplyAction = new NotificationCompat.Action.Builder(R.drawable.ic_wear_reply, final NotificationCompat.Action wearReplyAction = new NotificationCompat.Action.Builder(R.drawable.ic_wear_reply,
replyLabel, replyLabel,
createReplyIntent(conversation, true)).addRemoteInput(remoteInput).build(); createReplyIntent(conversation, lastMessageUuid, true)).addRemoteInput(remoteInput).build();
mBuilder.extend(new NotificationCompat.WearableExtender().addAction(wearReplyAction)); mBuilder.extend(new NotificationCompat.WearableExtender().addAction(wearReplyAction));
int addedActionsCount = 1; int addedActionsCount = 1;
mBuilder.addAction(markReadAction); mBuilder.addAction(markReadAction);
@ -1066,13 +1068,14 @@ public class NotificationService {
return PendingIntent.getService(mXmppConnectionService, 0, intent, 0); return PendingIntent.getService(mXmppConnectionService, 0, intent, 0);
} }
private PendingIntent createReplyIntent(Conversation conversation, boolean dismissAfterReply) { private PendingIntent createReplyIntent(final Conversation conversation, final String lastMessageUuid, final boolean dismissAfterReply) {
final Intent intent = new Intent(mXmppConnectionService, XmppConnectionService.class); final Intent intent = new Intent(mXmppConnectionService, XmppConnectionService.class);
intent.setAction(XmppConnectionService.ACTION_REPLY_TO_CONVERSATION); intent.setAction(XmppConnectionService.ACTION_REPLY_TO_CONVERSATION);
intent.putExtra("uuid", conversation.getUuid()); intent.putExtra("uuid", conversation.getUuid());
intent.putExtra("dismiss_notification", dismissAfterReply); intent.putExtra("dismiss_notification", dismissAfterReply);
intent.putExtra("last_message_uuid", lastMessageUuid);
final int id = generateRequestCode(conversation, dismissAfterReply ? 12 : 14); final int id = generateRequestCode(conversation, dismissAfterReply ? 12 : 14);
return PendingIntent.getService(mXmppConnectionService, id, intent, 0); return PendingIntent.getService(mXmppConnectionService, id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
} }
private PendingIntent createReadPendingIntent(Conversation conversation) { private PendingIntent createReadPendingIntent(Conversation conversation) {

View File

@ -726,6 +726,7 @@ public class XmppConnectionService extends Service {
} }
final CharSequence body = remoteInput.getCharSequence("text_reply"); final CharSequence body = remoteInput.getCharSequence("text_reply");
final boolean dismissNotification = intent.getBooleanExtra("dismiss_notification", false); final boolean dismissNotification = intent.getBooleanExtra("dismiss_notification", false);
final String lastMessageUuid = intent.getStringExtra("last_message_uuid");
if (body == null || body.length() <= 0) { if (body == null || body.length() <= 0) {
break; break;
} }
@ -734,7 +735,7 @@ public class XmppConnectionService extends Service {
restoredFromDatabaseLatch.await(); restoredFromDatabaseLatch.await();
final Conversation c = findConversationByUuid(uuid); final Conversation c = findConversationByUuid(uuid);
if (c != null) { if (c != null) {
directReply(c, body.toString(), dismissNotification); directReply(c, body.toString(), lastMessageUuid, dismissNotification);
} }
} catch (InterruptedException e) { } catch (InterruptedException e) {
Log.d(Config.LOGTAG, "unable to process direct reply"); Log.d(Config.LOGTAG, "unable to process direct reply");
@ -932,8 +933,12 @@ public class XmppConnectionService extends Service {
} }
} }
private void directReply(Conversation conversation, String body, final boolean dismissAfterReply) { private void directReply(final Conversation conversation, final String body, final String lastMessageUuid, final boolean dismissAfterReply) {
Message message = new Message(conversation, body, conversation.getNextEncryption()); final Message inReplyTo = lastMessageUuid == null ? null : conversation.findMessageWithUuid(lastMessageUuid);
final Message message = new Message(conversation, body, conversation.getNextEncryption());
if (inReplyTo != null && inReplyTo.isPrivateMessage()) {
Message.configurePrivateMessage(message, inReplyTo.getCounterpart());
}
message.markUnread(); message.markUnread();
if (message.getEncryption() == Message.ENCRYPTION_PGP) { if (message.getEncryption() == Message.ENCRYPTION_PGP) {
getPgpEngine().encrypt(message, new UiCallback<Message>() { getPgpEngine().encrypt(message, new UiCallback<Message>() {