add shortcut info only for 'messages' channel

This commit is contained in:
Daniel Gultsch 2024-02-13 18:29:08 +01:00
parent 1cfc5d426e
commit 617dd76d2f
No known key found for this signature in database
GPG key ID: F43D18AD2A0982C2

View file

@ -115,6 +115,7 @@ public class NotificationService {
private long mLastNotification; private long mLastNotification;
private static final String INCOMING_CALLS_NOTIFICATION_CHANNEL = "incoming_calls_channel"; private static final String INCOMING_CALLS_NOTIFICATION_CHANNEL = "incoming_calls_channel";
private static final String MESSAGES_NOTIFICATION_CHANNEL = "messages";
private Ringtone currentlyPlayingRingtone = null; private Ringtone currentlyPlayingRingtone = null;
private ScheduledFuture<?> vibrationFuture; private ScheduledFuture<?> vibrationFuture;
@ -244,7 +245,7 @@ public class NotificationService {
final NotificationChannel messagesChannel = final NotificationChannel messagesChannel =
new NotificationChannel( new NotificationChannel(
"messages", MESSAGES_NOTIFICATION_CHANNEL,
c.getString(R.string.messages_channel_name), c.getString(R.string.messages_channel_name),
NotificationManager.IMPORTANCE_HIGH); NotificationManager.IMPORTANCE_HIGH);
messagesChannel.setShowBadge(true); messagesChannel.setShowBadge(true);
@ -1112,7 +1113,7 @@ public class NotificationService {
final Builder mBuilder = final Builder mBuilder =
new NotificationCompat.Builder( new NotificationCompat.Builder(
mXmppConnectionService, mXmppConnectionService,
quietHours ? "quiet_hours" : (notify ? "messages" : "silent_messages")); quietHours ? "quiet_hours" : (notify ? MESSAGES_NOTIFICATION_CHANNEL : "silent_messages"));
final NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle(); final NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle();
style.setBigContentTitle( style.setBigContentTitle(
mXmppConnectionService mXmppConnectionService
@ -1178,160 +1179,161 @@ public class NotificationService {
private Builder buildSingleConversations( private Builder buildSingleConversations(
final ArrayList<Message> messages, final boolean notify, final boolean quietHours) { final ArrayList<Message> messages, final boolean notify, final boolean quietHours) {
final Builder mBuilder = final var channel = quietHours ? "quiet_hours" : (notify ? MESSAGES_NOTIFICATION_CHANNEL : "silent_messages");
new NotificationCompat.Builder( final Builder notificationBuilder =
mXmppConnectionService, new NotificationCompat.Builder(mXmppConnectionService, channel);
quietHours ? "quiet_hours" : (notify ? "messages" : "silent_messages")); if (messages.isEmpty()) {
if (messages.size() >= 1) { return notificationBuilder;
final Conversation conversation = (Conversation) messages.get(0).getConversation(); }
mBuilder.setLargeIcon( final Conversation conversation = (Conversation) messages.get(0).getConversation();
notificationBuilder.setLargeIcon(
mXmppConnectionService
.getAvatarService()
.get(
conversation,
AvatarService.getSystemUiAvatarSize(mXmppConnectionService)));
notificationBuilder.setContentTitle(conversation.getName());
if (Config.HIDE_MESSAGE_TEXT_IN_NOTIFICATION) {
int count = messages.size();
notificationBuilder.setContentText(
mXmppConnectionService mXmppConnectionService
.getAvatarService() .getResources()
.get( .getQuantityString(R.plurals.x_messages, count, count));
conversation, } else {
AvatarService.getSystemUiAvatarSize(mXmppConnectionService))); final Message message;
mBuilder.setContentTitle(conversation.getName()); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P
if (Config.HIDE_MESSAGE_TEXT_IN_NOTIFICATION) { && (message = getImage(messages)) != null) {
int count = messages.size(); modifyForImage(notificationBuilder, message, messages);
mBuilder.setContentText(
mXmppConnectionService
.getResources()
.getQuantityString(R.plurals.x_messages, count, count));
} else { } else {
Message message; modifyForTextOnly(notificationBuilder, messages);
// TODO starting with Android 9 we might want to put images in MessageStyle }
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P RemoteInput remoteInput =
&& (message = getImage(messages)) != null) { new RemoteInput.Builder("text_reply")
modifyForImage(mBuilder, message, messages); .setLabel(UIHelper.getMessageHint(mXmppConnectionService, conversation))
} else { .build();
modifyForTextOnly(mBuilder, messages); PendingIntent markAsReadPendingIntent = createReadPendingIntent(conversation);
} NotificationCompat.Action markReadAction =
RemoteInput remoteInput = new NotificationCompat.Action.Builder(
new RemoteInput.Builder("text_reply") R.drawable.ic_drafts_white_24dp,
.setLabel( mXmppConnectionService.getString(R.string.mark_as_read),
UIHelper.getMessageHint( markAsReadPendingIntent)
mXmppConnectionService, conversation)) .setSemanticAction(
.build(); NotificationCompat.Action.SEMANTIC_ACTION_MARK_AS_READ)
PendingIntent markAsReadPendingIntent = createReadPendingIntent(conversation); .setShowsUserInterface(false)
NotificationCompat.Action markReadAction = .build();
new NotificationCompat.Action.Builder( final String replyLabel = mXmppConnectionService.getString(R.string.reply);
R.drawable.ic_drafts_white_24dp, final String lastMessageUuid = Iterables.getLast(messages).getUuid();
mXmppConnectionService.getString(R.string.mark_as_read), final NotificationCompat.Action replyAction =
markAsReadPendingIntent) new NotificationCompat.Action.Builder(
.setSemanticAction( R.drawable.ic_send_text_offline,
NotificationCompat.Action.SEMANTIC_ACTION_MARK_AS_READ) replyLabel,
.setShowsUserInterface(false) createReplyIntent(conversation, lastMessageUuid, false))
.build(); .setSemanticAction(NotificationCompat.Action.SEMANTIC_ACTION_REPLY)
final String replyLabel = mXmppConnectionService.getString(R.string.reply); .setShowsUserInterface(false)
final String lastMessageUuid = Iterables.getLast(messages).getUuid(); .addRemoteInput(remoteInput)
final NotificationCompat.Action replyAction = .build();
new NotificationCompat.Action.Builder( final NotificationCompat.Action wearReplyAction =
R.drawable.ic_send_text_offline, new NotificationCompat.Action.Builder(
replyLabel, R.drawable.ic_wear_reply,
createReplyIntent(conversation, lastMessageUuid, false)) replyLabel,
.setSemanticAction(NotificationCompat.Action.SEMANTIC_ACTION_REPLY) createReplyIntent(conversation, lastMessageUuid, true))
.setShowsUserInterface(false) .addRemoteInput(remoteInput)
.addRemoteInput(remoteInput) .build();
.build(); notificationBuilder.extend(
final NotificationCompat.Action wearReplyAction = new NotificationCompat.WearableExtender().addAction(wearReplyAction));
new NotificationCompat.Action.Builder( int addedActionsCount = 1;
R.drawable.ic_wear_reply, notificationBuilder.addAction(markReadAction);
replyLabel, if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
createReplyIntent(conversation, lastMessageUuid, true)) notificationBuilder.addAction(replyAction);
.addRemoteInput(remoteInput) ++addedActionsCount;
.build(); }
mBuilder.extend(
new NotificationCompat.WearableExtender().addAction(wearReplyAction));
int addedActionsCount = 1;
mBuilder.addAction(markReadAction);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
mBuilder.addAction(replyAction);
++addedActionsCount;
}
if (displaySnoozeAction(messages)) { if (displaySnoozeAction(messages)) {
String label = mXmppConnectionService.getString(R.string.snooze); String label = mXmppConnectionService.getString(R.string.snooze);
PendingIntent pendingSnoozeIntent = createSnoozeIntent(conversation); PendingIntent pendingSnoozeIntent = createSnoozeIntent(conversation);
NotificationCompat.Action snoozeAction = NotificationCompat.Action snoozeAction =
new NotificationCompat.Action.Builder( new NotificationCompat.Action.Builder(
R.drawable.ic_notifications_paused_white_24dp, R.drawable.ic_notifications_paused_white_24dp,
label, label,
pendingSnoozeIntent) pendingSnoozeIntent)
.build(); .build();
mBuilder.addAction(snoozeAction); notificationBuilder.addAction(snoozeAction);
++addedActionsCount; ++addedActionsCount;
} }
if (addedActionsCount < 3) { if (addedActionsCount < 3) {
final Message firstLocationMessage = getFirstLocationMessage(messages); final Message firstLocationMessage = getFirstLocationMessage(messages);
if (firstLocationMessage != null) { if (firstLocationMessage != null) {
final PendingIntent pendingShowLocationIntent = final PendingIntent pendingShowLocationIntent =
createShowLocationIntent(firstLocationMessage); createShowLocationIntent(firstLocationMessage);
if (pendingShowLocationIntent != null) { if (pendingShowLocationIntent != null) {
final String label = final String label =
mXmppConnectionService
.getResources()
.getString(R.string.show_location);
NotificationCompat.Action locationAction =
new NotificationCompat.Action.Builder(
R.drawable.ic_room_white_24dp,
label,
pendingShowLocationIntent)
.build();
mBuilder.addAction(locationAction);
++addedActionsCount;
}
}
}
if (addedActionsCount < 3) {
Message firstDownloadableMessage = getFirstDownloadableMessage(messages);
if (firstDownloadableMessage != null) {
String label =
mXmppConnectionService mXmppConnectionService
.getResources() .getResources()
.getString( .getString(R.string.show_location);
R.string.download_x_file, NotificationCompat.Action locationAction =
UIHelper.getFileDescriptionString(
mXmppConnectionService,
firstDownloadableMessage));
PendingIntent pendingDownloadIntent =
createDownloadIntent(firstDownloadableMessage);
NotificationCompat.Action downloadAction =
new NotificationCompat.Action.Builder( new NotificationCompat.Action.Builder(
R.drawable.ic_file_download_white_24dp, R.drawable.ic_room_white_24dp,
label, label,
pendingDownloadIntent) pendingShowLocationIntent)
.build(); .build();
mBuilder.addAction(downloadAction); notificationBuilder.addAction(locationAction);
++addedActionsCount; ++addedActionsCount;
} }
} }
} }
final ShortcutInfoCompat info; if (addedActionsCount < 3) {
if (conversation.getMode() == Conversation.MODE_SINGLE) { Message firstDownloadableMessage = getFirstDownloadableMessage(messages);
final Contact contact = conversation.getContact(); if (firstDownloadableMessage != null) {
final Uri systemAccount = contact.getSystemAccount(); String label =
if (systemAccount != null) { mXmppConnectionService
mBuilder.addPerson(systemAccount.toString()); .getResources()
.getString(
R.string.download_x_file,
UIHelper.getFileDescriptionString(
mXmppConnectionService,
firstDownloadableMessage));
PendingIntent pendingDownloadIntent =
createDownloadIntent(firstDownloadableMessage);
NotificationCompat.Action downloadAction =
new NotificationCompat.Action.Builder(
R.drawable.ic_file_download_white_24dp,
label,
pendingDownloadIntent)
.build();
notificationBuilder.addAction(downloadAction);
++addedActionsCount;
} }
info = mXmppConnectionService.getShortcutService().getShortcutInfoCompat(contact);
} else {
info =
mXmppConnectionService
.getShortcutService()
.getShortcutInfoCompat(conversation.getMucOptions());
} }
mBuilder.setWhen(conversation.getLatestMessage().getTimeSent()); }
mBuilder.setSmallIcon(R.drawable.ic_notification); final ShortcutInfoCompat info;
mBuilder.setDeleteIntent(createDeleteIntent(conversation)); if (conversation.getMode() == Conversation.MODE_SINGLE) {
mBuilder.setContentIntent(createContentIntent(conversation)); final Contact contact = conversation.getContact();
mBuilder.setShortcutInfo(info); final Uri systemAccount = contact.getSystemAccount();
if (systemAccount != null) {
notificationBuilder.addPerson(systemAccount.toString());
}
info = mXmppConnectionService.getShortcutService().getShortcutInfoCompat(contact);
} else {
info =
mXmppConnectionService
.getShortcutService()
.getShortcutInfoCompat(conversation.getMucOptions());
}
notificationBuilder.setWhen(conversation.getLatestMessage().getTimeSent());
notificationBuilder.setSmallIcon(R.drawable.ic_notification);
notificationBuilder.setDeleteIntent(createDeleteIntent(conversation));
notificationBuilder.setContentIntent(createContentIntent(conversation));
if (channel.equals(MESSAGES_NOTIFICATION_CHANNEL)) {
// when do not want 'customized' notifications for silent notifications in their
// respective channels
notificationBuilder.setShortcutInfo(info);
if (Build.VERSION.SDK_INT >= 30) { if (Build.VERSION.SDK_INT >= 30) {
mXmppConnectionService mXmppConnectionService
.getSystemService(ShortcutManager.class) .getSystemService(ShortcutManager.class)
.pushDynamicShortcut(info.toShortcutInfo()); .pushDynamicShortcut(info.toShortcutInfo());
} }
} }
return mBuilder; return notificationBuilder;
} }
private void modifyForImage( private void modifyForImage(