From 00ae1ca7623aed99ad95c1629fdeddf51cc59236 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Mon, 11 Sep 2023 16:38:57 +0200 Subject: [PATCH] fix group chat shortcuts --- .../conversations/services/AvatarService.java | 13 ++++- .../services/NotificationService.java | 11 ++-- .../services/ShortcutService.java | 55 +++++++++++++++++-- 3 files changed, 70 insertions(+), 9 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/services/AvatarService.java b/src/main/java/eu/siacs/conversations/services/AvatarService.java index 7baee23f9..e9e827c56 100644 --- a/src/main/java/eu/siacs/conversations/services/AvatarService.java +++ b/src/main/java/eu/siacs/conversations/services/AvatarService.java @@ -124,6 +124,17 @@ public class AvatarService implements OnAdvancedStreamFeaturesLoaded { return avatar; } + public Bitmap getRoundedShortcut(final MucOptions mucOptions) { + final DisplayMetrics metrics = mXmppConnectionService.getResources().getDisplayMetrics(); + final int size = Math.round(metrics.density * 48); + final Bitmap bitmap = get(mucOptions, size, false); + final Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888); + final Canvas canvas = new Canvas(output); + final Paint paint = new Paint(); + drawAvatar(bitmap, canvas, paint); + return output; + } + public Bitmap getRoundedShortcut(final Contact contact) { return getRoundedShortcut(contact, false); } @@ -147,7 +158,7 @@ public class AvatarService implements OnAdvancedStreamFeaturesLoaded { return output; } - private void drawAvatar(Bitmap bitmap, Canvas canvas, Paint paint) { + private static void drawAvatar(Bitmap bitmap, Canvas canvas, Paint paint) { final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); paint.setAntiAlias(true); canvas.drawARGB(0, 0, 0, 0); diff --git a/src/main/java/eu/siacs/conversations/services/NotificationService.java b/src/main/java/eu/siacs/conversations/services/NotificationService.java index af7667313..9fe1f923c 100644 --- a/src/main/java/eu/siacs/conversations/services/NotificationService.java +++ b/src/main/java/eu/siacs/conversations/services/NotificationService.java @@ -1284,21 +1284,24 @@ public class NotificationService { } } } + final ShortcutInfoCompat info; if (conversation.getMode() == Conversation.MODE_SINGLE) { final Contact contact = conversation.getContact(); final Uri systemAccount = contact.getSystemAccount(); if (systemAccount != null) { mBuilder.addPerson(systemAccount.toString()); } + info = mXmppConnectionService.getShortcutService().getShortcutInfoCompat(contact); + } else { + info = + mXmppConnectionService + .getShortcutService() + .getShortcutInfoCompat(conversation.getMucOptions()); } mBuilder.setWhen(conversation.getLatestMessage().getTimeSent()); mBuilder.setSmallIcon(R.drawable.ic_notification); mBuilder.setDeleteIntent(createDeleteIntent(conversation)); mBuilder.setContentIntent(createContentIntent(conversation)); - final ShortcutInfoCompat info = - mXmppConnectionService - .getShortcutService() - .getShortcutInfoCompat(conversation.getContact()); mBuilder.setShortcutInfo(info); if (Build.VERSION.SDK_INT >= 30) { mXmppConnectionService diff --git a/src/main/java/eu/siacs/conversations/services/ShortcutService.java b/src/main/java/eu/siacs/conversations/services/ShortcutService.java index 57b8e9515..c6ae77b63 100644 --- a/src/main/java/eu/siacs/conversations/services/ShortcutService.java +++ b/src/main/java/eu/siacs/conversations/services/ShortcutService.java @@ -22,6 +22,7 @@ import java.util.List; import eu.siacs.conversations.Config; import eu.siacs.conversations.entities.Account; import eu.siacs.conversations.entities.Contact; +import eu.siacs.conversations.entities.MucOptions; import eu.siacs.conversations.ui.StartConversationActivity; import eu.siacs.conversations.utils.ReplacingSerialSingleThreadExecutor; import eu.siacs.conversations.xmpp.Jid; @@ -109,6 +110,24 @@ public class ShortcutService { return builder.build(); } + public ShortcutInfoCompat getShortcutInfoCompat(final MucOptions mucOptions) { + final ShortcutInfoCompat.Builder builder = + new ShortcutInfoCompat.Builder(xmppConnectionService, getShortcutId(mucOptions)) + .setShortLabel(mucOptions.getConversation().getName()) + .setIntent(getShortcutIntent(mucOptions)) + .setIsConversation(); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + builder.setIcon( + IconCompat.createFromIcon( + xmppConnectionService, + Icon.createWithBitmap( + xmppConnectionService + .getAvatarService() + .getRoundedShortcut(mucOptions)))); + } + return builder.build(); + } + @TargetApi(Build.VERSION_CODES.N_MR1) private ShortcutInfo getShortcutInfo(final Contact contact) { return getShortcutInfoCompat(contact).toShortcutInfo(); @@ -137,12 +156,40 @@ public class ShortcutService { return contact.getAccount().getJid().asBareJid().toEscapedString()+"#"+contact.getJid().asBareJid().toEscapedString(); } - private Intent getShortcutIntent(Contact contact) { + private static String getShortcutId(final MucOptions mucOptions) { + final Account account = mucOptions.getAccount(); + final Jid jid = mucOptions.getConversation().getJid(); + return account.getJid().asBareJid().toEscapedString() + + "#" + + jid.asBareJid().toEscapedString(); + } + + private Intent getShortcutIntent(final MucOptions mucOptions) { + final Account account = mucOptions.getAccount(); + return getShortcutIntent( + account, + Uri.parse( + String.format( + "xmpp:%s?join", + mucOptions + .getConversation() + .getJid() + .asBareJid() + .toEscapedString()))); + } + + private Intent getShortcutIntent(final Contact contact) { + return getShortcutIntent( + contact.getAccount(), + Uri.parse("xmpp:" + contact.getJid().asBareJid().toEscapedString())); + } + + private Intent getShortcutIntent(final Account account, final Uri uri) { Intent intent = new Intent(xmppConnectionService, StartConversationActivity.class); intent.setAction(Intent.ACTION_VIEW); - intent.setData(Uri.parse("xmpp:"+contact.getJid().asBareJid().toEscapedString())); - intent.putExtra("account",contact.getAccount().getJid().asBareJid().toString()); - intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_SINGLE_TOP); + intent.setData(uri); + intent.putExtra("account", account.getJid().asBareJid().toString()); + intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); return intent; }