1package eu.siacs.conversations.services;
2
3import android.annotation.TargetApi;
4import android.content.Intent;
5import android.content.pm.ShortcutInfo;
6import android.content.pm.ShortcutManager;
7import android.graphics.Bitmap;
8import android.graphics.drawable.Icon;
9import android.net.Uri;
10import android.os.Build;
11import android.util.Log;
12
13import androidx.annotation.NonNull;
14import androidx.core.content.pm.ShortcutInfoCompat;
15import androidx.core.graphics.drawable.IconCompat;
16
17import java.util.ArrayList;
18import java.util.HashMap;
19import java.util.List;
20
21import eu.siacs.conversations.Config;
22import eu.siacs.conversations.entities.Account;
23import eu.siacs.conversations.entities.Contact;
24import eu.siacs.conversations.entities.Conversation;
25import eu.siacs.conversations.ui.StartConversationActivity;
26import eu.siacs.conversations.ui.ConversationsActivity;
27import eu.siacs.conversations.utils.ReplacingSerialSingleThreadExecutor;
28import eu.siacs.conversations.xmpp.Jid;
29
30public class ShortcutService {
31
32 private final XmppConnectionService xmppConnectionService;
33 private final ReplacingSerialSingleThreadExecutor replacingSerialSingleThreadExecutor = new ReplacingSerialSingleThreadExecutor(ShortcutService.class.getSimpleName());
34
35 public ShortcutService(XmppConnectionService xmppConnectionService) {
36 this.xmppConnectionService = xmppConnectionService;
37 }
38
39 public void refresh() {
40 refresh(false);
41 }
42
43 public void refresh(final boolean forceUpdate) {
44 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
45 final Runnable r = new Runnable() {
46 @Override
47 public void run() {
48 refreshImpl(forceUpdate);
49 }
50 };
51 replacingSerialSingleThreadExecutor.execute(r);
52 }
53 }
54
55 @TargetApi(25)
56 public void report(Contact contact) {
57 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
58 ShortcutManager shortcutManager = xmppConnectionService.getSystemService(ShortcutManager.class);
59 shortcutManager.reportShortcutUsed(getShortcutId(contact));
60 }
61 }
62
63 @TargetApi(25)
64 private void refreshImpl(boolean forceUpdate) {
65 List<FrequentContact> frequentContacts = xmppConnectionService.databaseBackend.getFrequentContacts(30);
66 HashMap<String,Account> accounts = new HashMap<>();
67 for(Account account : xmppConnectionService.getAccounts()) {
68 accounts.put(account.getUuid(),account);
69 }
70 List<Contact> contacts = new ArrayList<>();
71 for(FrequentContact frequentContact : frequentContacts) {
72 Account account = accounts.get(frequentContact.account);
73 if (account != null) {
74 contacts.add(account.getRoster().getContact(frequentContact.contact));
75 }
76 }
77 ShortcutManager shortcutManager = xmppConnectionService.getSystemService(ShortcutManager.class);
78 boolean needsUpdate = forceUpdate || contactsChanged(contacts,shortcutManager.getDynamicShortcuts());
79 if (!needsUpdate) {
80 Log.d(Config.LOGTAG,"skipping shortcut update");
81 return;
82 }
83 List<ShortcutInfo> newDynamicShortCuts = new ArrayList<>();
84 for (Contact contact : contacts) {
85 ShortcutInfo shortcut = getShortcutInfo(contact);
86 newDynamicShortCuts.add(shortcut);
87 }
88 if (shortcutManager.setDynamicShortcuts(newDynamicShortCuts)) {
89 Log.d(Config.LOGTAG,"updated dynamic shortcuts");
90 } else {
91 Log.d(Config.LOGTAG, "unable to update dynamic shortcuts");
92 }
93 }
94
95 public ShortcutInfoCompat getShortcutInfoCompat(Contact contact) {
96 return new ShortcutInfoCompat.Builder(xmppConnectionService, getShortcutId(contact))
97 .setShortLabel(contact.getDisplayName())
98 .setIntent(getShortcutIntent(contact))
99 .setIcon(IconCompat.createFromIcon(Icon.createWithBitmap(xmppConnectionService.getAvatarService().getRoundedShortcut(contact))))
100 .setIsConversation()
101 .build();
102 }
103
104 @TargetApi(Build.VERSION_CODES.N_MR1)
105 private ShortcutInfo getShortcutInfo(Contact contact) {
106 return getShortcutInfoCompat(contact).toShortcutInfo();
107 }
108
109 private static boolean contactsChanged(List<Contact> needles, List<ShortcutInfo> haystack) {
110 for(Contact needle : needles) {
111 if(!contactExists(needle,haystack)) {
112 return true;
113 }
114 }
115 return needles.size() != haystack.size();
116 }
117
118 @TargetApi(25)
119 private static boolean contactExists(Contact needle, List<ShortcutInfo> haystack) {
120 for(ShortcutInfo shortcutInfo : haystack) {
121 if (getShortcutId(needle).equals(shortcutInfo.getId()) && needle.getDisplayName().equals(shortcutInfo.getShortLabel())) {
122 return true;
123 }
124 }
125 return false;
126 }
127
128 private static String getShortcutId(Contact contact) {
129 return contact.getAccount().getJid().asBareJid().toEscapedString()+"#"+contact.getJid().asBareJid().toEscapedString();
130 }
131
132 private Intent getShortcutIntent(Contact contact) {
133 final Conversation conversation = xmppConnectionService.find(contact.getAccount(), contact.getJid());
134
135 if (conversation != null) {
136 Intent intent = new Intent(xmppConnectionService, ConversationsActivity.class);
137 intent.setAction(ConversationsActivity.ACTION_VIEW_CONVERSATION);
138 intent.putExtra(ConversationsActivity.EXTRA_CONVERSATION, conversation.getUuid());
139 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_SINGLE_TOP);
140 return intent;
141 }
142
143 Intent intent = new Intent(xmppConnectionService, StartConversationActivity.class);
144 intent.setAction(Intent.ACTION_VIEW);
145 intent.setData(Uri.parse("xmpp:"+contact.getJid().asBareJid().toEscapedString()));
146 intent.putExtra("account",contact.getAccount().getJid().asBareJid().toString());
147 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_SINGLE_TOP);
148 return intent;
149 }
150
151 @NonNull
152 public Intent createShortcut(Contact contact, boolean legacy) {
153 Intent intent;
154 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && !legacy) {
155 ShortcutInfo shortcut = getShortcutInfo(contact);
156 ShortcutManager shortcutManager = xmppConnectionService.getSystemService(ShortcutManager.class);
157 intent = shortcutManager.createShortcutResultIntent(shortcut);
158 } else {
159 intent = createShortcutResultIntent(contact);
160 }
161 return intent;
162 }
163
164 @NonNull
165 private Intent createShortcutResultIntent(Contact contact) {
166 AvatarService avatarService = xmppConnectionService.getAvatarService();
167 Bitmap icon = avatarService.getRoundedShortcutWithIcon(contact);
168 Intent intent = new Intent();
169 intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, contact.getDisplayName());
170 intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, icon);
171 intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, getShortcutIntent(contact));
172 return intent;
173 }
174
175 public static class FrequentContact {
176 private final String account;
177 private final Jid contact;
178
179 public FrequentContact(String account, Jid contact) {
180 this.account = account;
181 this.contact = contact;
182 }
183 }
184
185}