From 4504953d3656851366c3b1d65a05532a20506de4 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 | 8 ++- .../services/ShortcutService.java | 62 +++++++++++++++---- .../services/XmppConnectionService.java | 8 ++- .../ui/ConversationFragment.java | 7 ++- .../ui/StartConversationActivity.java | 6 +- 6 files changed, 84 insertions(+), 20 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/services/AvatarService.java b/src/main/java/eu/siacs/conversations/services/AvatarService.java index 925e7d893..f2febdec8 100644 --- a/src/main/java/eu/siacs/conversations/services/AvatarService.java +++ b/src/main/java/eu/siacs/conversations/services/AvatarService.java @@ -125,6 +125,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); + Bitmap bitmap = FileBackend.drawDrawable(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); } @@ -148,7 +159,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 b71f9c353..83b2ecbe0 100644 --- a/src/main/java/eu/siacs/conversations/services/NotificationService.java +++ b/src/main/java/eu/siacs/conversations/services/NotificationService.java @@ -1373,12 +1373,19 @@ public class NotificationService { } } } + final ShortcutInfoCompat info; if (conversation.getMode() == Conversation.MODE_SINGLE) { Contact contact = conversation.getContact(); 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); @@ -1388,7 +1395,6 @@ public class NotificationService { mBuilder.setSubText(conversation.getAccount().getJid().asBareJid().toString()); } - ShortcutInfoCompat info = mXmppConnectionService.getShortcutService().getShortcutInfoCompat(conversation.getContact()); mBuilder.setShortcutInfo(info); if (Build.VERSION.SDK_INT >= 30) { mXmppConnectionService.getSystemService(ShortcutManager.class).pushDynamicShortcut(info.toShortcutInfo()); diff --git a/src/main/java/eu/siacs/conversations/services/ShortcutService.java b/src/main/java/eu/siacs/conversations/services/ShortcutService.java index 7182bc3c2..01c7bcad8 100644 --- a/src/main/java/eu/siacs/conversations/services/ShortcutService.java +++ b/src/main/java/eu/siacs/conversations/services/ShortcutService.java @@ -21,7 +21,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.Conversation; +import eu.siacs.conversations.entities.MucOptions; import eu.siacs.conversations.ui.StartConversationActivity; import eu.siacs.conversations.ui.ConversationsActivity; import eu.siacs.conversations.utils.ReplacingSerialSingleThreadExecutor; @@ -101,6 +101,24 @@ public class ShortcutService { .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(Contact contact) { return getShortcutInfoCompat(contact).toShortcutInfo(); @@ -129,22 +147,40 @@ public class ShortcutService { return contact.getAccount().getJid().asBareJid().toEscapedString()+"#"+contact.getJid().asBareJid().toEscapedString(); } - private Intent getShortcutIntent(Contact contact) { - final Conversation conversation = xmppConnectionService.find(contact.getAccount(), contact.getJid()); + 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()))); + } - if (conversation != null) { - Intent intent = new Intent(xmppConnectionService, ConversationsActivity.class); - intent.setAction(ConversationsActivity.ACTION_VIEW_CONVERSATION); - intent.putExtra(ConversationsActivity.EXTRA_CONVERSATION, conversation.getUuid()); - intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_SINGLE_TOP); - return intent; - } + 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; } diff --git a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java index 1dc47fd35..d8b7dcddf 100644 --- a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java @@ -5042,8 +5042,14 @@ public class XmppConnectionService extends Service { } public Conversation findFirstMuc(Jid jid) { + return findFirstMuc(jid, null); + } + + public Conversation findFirstMuc(Jid jid, String accountJid) { for (Conversation conversation : getConversations()) { - if (conversation.getAccount().isEnabled() && conversation.getJid().asBareJid().equals(jid.asBareJid()) && conversation.getMode() == Conversation.MODE_MULTI) { + if ((conversation.getAccount().isEnabled() || accountJid != null) + && (accountJid == null || accountJid.equals(conversation.getAccount().getJid().asBareJid().toString())) + && conversation.getJid().asBareJid().equals(jid.asBareJid()) && conversation.getMode() == Conversation.MODE_MULTI) { return conversation; } } diff --git a/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java b/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java index 85275f83f..4be4c0877 100644 --- a/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java +++ b/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java @@ -1999,7 +1999,12 @@ public class ConversationFragment extends XmppFragment } private void addShortcut() { - ShortcutInfoCompat info = activity.xmppConnectionService.getShortcutService().getShortcutInfoCompat(conversation.getContact()); + ShortcutInfoCompat info; + if (conversation.getMode() == Conversation.MODE_MULTI) { + info = activity.xmppConnectionService.getShortcutService().getShortcutInfoCompat(conversation.getMucOptions()); + } else { + info = activity.xmppConnectionService.getShortcutService().getShortcutInfoCompat(conversation.getContact()); + } ShortcutManagerCompat.requestPinShortcut(activity, info, null); } diff --git a/src/main/java/eu/siacs/conversations/ui/StartConversationActivity.java b/src/main/java/eu/siacs/conversations/ui/StartConversationActivity.java index a73a994e8..7303eb775 100644 --- a/src/main/java/eu/siacs/conversations/ui/StartConversationActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/StartConversationActivity.java @@ -1018,9 +1018,9 @@ public class StartConversationActivity extends XmppActivity implements XmppConne } private boolean handleJid(Invite invite) { - List contacts = xmppConnectionService.findContacts(invite.getJid(), invite.account); - if (invite.isAction(XmppUri.ACTION_JOIN)) { - Conversation muc = xmppConnectionService.findFirstMuc(invite.getJid()); + final List contacts = xmppConnectionService.findContacts(invite.getJid(), invite.account); + final Conversation muc = xmppConnectionService.findFirstMuc(invite.getJid(), invite.account); + if (invite.isAction(XmppUri.ACTION_JOIN) || (contacts.isEmpty() && muc != null)) { if (muc != null && !invite.forceDialog) { switchToConversationDoNotAppend(muc, invite.getBody()); return true; -- 2.45.2