1package eu.siacs.conversations.services;
2
3import android.annotation.TargetApi;
4import android.content.Intent;
5import android.content.pm.ShortcutManager;
6import android.graphics.Bitmap;
7import android.net.Uri;
8import android.os.Build;
9import android.os.PersistableBundle;
10import android.util.Log;
11import androidx.annotation.NonNull;
12import androidx.core.content.pm.ShortcutInfoCompat;
13import androidx.core.content.pm.ShortcutManagerCompat;
14import androidx.core.graphics.drawable.IconCompat;
15import com.google.common.collect.ImmutableList;
16import com.google.common.collect.ImmutableMap;
17import com.google.common.collect.ImmutableSet;
18import com.google.common.collect.Maps;
19import eu.siacs.conversations.Config;
20import eu.siacs.conversations.entities.Account;
21import eu.siacs.conversations.entities.Contact;
22import eu.siacs.conversations.entities.MucOptions;
23import eu.siacs.conversations.ui.ConversationsActivity;
24import eu.siacs.conversations.ui.StartConversationActivity;
25import eu.siacs.conversations.utils.ReplacingSerialSingleThreadExecutor;
26import eu.siacs.conversations.xmpp.Jid;
27import java.util.Collection;
28import java.util.List;
29
30public class ShortcutService {
31
32 private final XmppConnectionService xmppConnectionService;
33 private final ReplacingSerialSingleThreadExecutor replacingSerialSingleThreadExecutor =
34 new ReplacingSerialSingleThreadExecutor(ShortcutService.class.getSimpleName());
35
36 public ShortcutService(final XmppConnectionService xmppConnectionService) {
37 this.xmppConnectionService = xmppConnectionService;
38 }
39
40 public void refresh() {
41 refresh(false);
42 }
43
44 public void refresh(final boolean forceUpdate) {
45 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
46 final Runnable r = () -> refreshImpl(forceUpdate);
47 replacingSerialSingleThreadExecutor.execute(r);
48 }
49 }
50
51 @TargetApi(25)
52 public void report(Contact contact) {
53 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
54 ShortcutManager shortcutManager =
55 xmppConnectionService.getSystemService(ShortcutManager.class);
56 shortcutManager.reportShortcutUsed(getShortcutId(contact));
57 }
58 }
59
60 @TargetApi(25)
61 private void refreshImpl(final boolean forceUpdate) {
62 final var frequentContacts = xmppConnectionService.databaseBackend.getFrequentContacts(30);
63 final var accounts =
64 ImmutableMap.copyOf(
65 Maps.uniqueIndex(xmppConnectionService.getAccounts(), Account::getUuid));
66 final var contactBuilder = new ImmutableMap.Builder<FrequentContact, Contact>();
67 for (final var frequentContact : frequentContacts) {
68 final Account account = accounts.get(frequentContact.account);
69 if (account != null) {
70 final var contact = account.getRoster().getContact(frequentContact.contact);
71 contactBuilder.put(frequentContact, contact);
72 }
73 }
74 final var contacts = contactBuilder.build();
75 final var current = ShortcutManagerCompat.getDynamicShortcuts(xmppConnectionService);
76 boolean needsUpdate = forceUpdate || contactsChanged(contacts.values(), current);
77 if (!needsUpdate) {
78 Log.d(Config.LOGTAG, "skipping shortcut update");
79 return;
80 }
81 final var newDynamicShortcuts = new ImmutableList.Builder<ShortcutInfoCompat>();
82 for (final var entry : contacts.entrySet()) {
83 final var contact = entry.getValue();
84 final var conversation = entry.getKey().conversation;
85 final var shortcut = getShortcutInfo(contact, conversation);
86 newDynamicShortcuts.add(shortcut);
87 }
88 if (ShortcutManagerCompat.setDynamicShortcuts(
89 xmppConnectionService, newDynamicShortcuts.build())) {
90 Log.d(Config.LOGTAG, "updated dynamic shortcuts");
91 } else {
92 Log.d(Config.LOGTAG, "unable to update dynamic shortcuts");
93 }
94 }
95
96 public ShortcutInfoCompat getShortcutInfo(final Contact contact, final String conversation) {
97 final ShortcutInfoCompat.Builder builder =
98 new ShortcutInfoCompat.Builder(xmppConnectionService, getShortcutId(contact))
99 .setShortLabel(contact.getDisplayName())
100 .setIntent(getShortcutIntent(contact))
101 .setIsConversation();
102 builder.setIcon(
103 IconCompat.createWithBitmap(
104 xmppConnectionService.getAvatarService().getRoundedShortcut(contact)));
105 if (conversation != null) {
106 setConversation(builder, conversation);
107 }
108 return builder.build();
109 }
110
111 public ShortcutInfoCompat getShortcutInfo(final MucOptions mucOptions) {
112 final ShortcutInfoCompat.Builder builder =
113 new ShortcutInfoCompat.Builder(xmppConnectionService, getShortcutId(mucOptions))
114 .setShortLabel(mucOptions.getConversation().getName())
115 .setIntent(getShortcutIntent(mucOptions))
116 .setIsConversation();
117 builder.setIcon(
118 IconCompat.createWithBitmap(
119 xmppConnectionService.getAvatarService().getRoundedShortcut(mucOptions)));
120 setConversation(builder, mucOptions.getConversation().getUuid());
121 return builder.build();
122 }
123
124 private static void setConversation(
125 final ShortcutInfoCompat.Builder builder, @NonNull final String conversation) {
126 builder.setCategories(ImmutableSet.of("eu.siacs.conversations.category.SHARE_TARGET"));
127 final var extras = new PersistableBundle();
128 extras.putString(ConversationsActivity.EXTRA_CONVERSATION, conversation);
129 builder.setExtras(extras);
130 }
131
132 private static boolean contactsChanged(
133 final Collection<Contact> needles, final List<ShortcutInfoCompat> haystack) {
134 for (final Contact needle : needles) {
135 if (!contactExists(needle, haystack)) {
136 return true;
137 }
138 }
139 return needles.size() != haystack.size();
140 }
141
142 @TargetApi(25)
143 private static boolean contactExists(
144 final Contact needle, final List<ShortcutInfoCompat> haystack) {
145 for (final ShortcutInfoCompat shortcutInfo : haystack) {
146 final var label = shortcutInfo.getShortLabel();
147 if (getShortcutId(needle).equals(shortcutInfo.getId())
148 && needle.getDisplayName().equals(label.toString())) {
149 return true;
150 }
151 }
152 return false;
153 }
154
155 private static String getShortcutId(final Contact contact) {
156 return contact.getAccount().getJid().asBareJid().toEscapedString()
157 + "#"
158 + contact.getJid().asBareJid().toEscapedString();
159 }
160
161 private static String getShortcutId(final MucOptions mucOptions) {
162 final Account account = mucOptions.getAccount();
163 final Jid jid = mucOptions.getConversation().getJid();
164 return account.getJid().asBareJid().toEscapedString()
165 + "#"
166 + jid.asBareJid().toEscapedString();
167 }
168
169 private Intent getShortcutIntent(final MucOptions mucOptions) {
170 final Account account = mucOptions.getAccount();
171 return getShortcutIntent(
172 account,
173 Uri.parse(
174 String.format(
175 "xmpp:%s?join",
176 mucOptions
177 .getConversation()
178 .getJid()
179 .asBareJid()
180 .toEscapedString())));
181 }
182
183 private Intent getShortcutIntent(final Contact contact) {
184 return getShortcutIntent(
185 contact.getAccount(),
186 Uri.parse("xmpp:" + contact.getJid().asBareJid().toEscapedString()));
187 }
188
189 private Intent getShortcutIntent(final Account account, final Uri uri) {
190 Intent intent = new Intent(xmppConnectionService, StartConversationActivity.class);
191 intent.setAction(Intent.ACTION_VIEW);
192 intent.setData(uri);
193 intent.putExtra("account", account.getJid().asBareJid().toString());
194 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
195 return intent;
196 }
197
198 @NonNull
199 public Intent createShortcut(final Contact contact, final boolean legacy) {
200 Intent intent;
201 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && !legacy) {
202 final var conversation = xmppConnectionService.find(contact);
203 final var uuid = conversation == null ? null : conversation.getUuid();
204 final var shortcut = getShortcutInfo(contact, uuid);
205 intent =
206 ShortcutManagerCompat.createShortcutResultIntent(
207 xmppConnectionService, shortcut);
208 } else {
209 intent = createShortcutResultIntent(contact);
210 }
211 return intent;
212 }
213
214 @NonNull
215 private Intent createShortcutResultIntent(final Contact contact) {
216 AvatarService avatarService = xmppConnectionService.getAvatarService();
217 Bitmap icon = avatarService.getRoundedShortcutWithIcon(contact);
218 Intent intent = new Intent();
219 intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, contact.getDisplayName());
220 intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, icon);
221 intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, getShortcutIntent(contact));
222 return intent;
223 }
224
225 public static class FrequentContact {
226 private final String conversation;
227 private final String account;
228 private final Jid contact;
229
230 public FrequentContact(final String conversation, final String account, final Jid contact) {
231 this.conversation = conversation;
232 this.account = account;
233 this.contact = contact;
234 }
235 }
236}