ShortcutService.java

  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.MucOptions;
 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.createWithBitmap(xmppConnectionService.getAvatarService().getRoundedShortcut(contact)))
100                        .setIsConversation()
101                        .build();
102    }
103
104    public ShortcutInfoCompat getShortcutInfoCompat(final MucOptions mucOptions) {
105        final ShortcutInfoCompat.Builder builder =
106                new ShortcutInfoCompat.Builder(xmppConnectionService, getShortcutId(mucOptions))
107                        .setShortLabel(mucOptions.getConversation().getName())
108                        .setIntent(getShortcutIntent(mucOptions))
109                        .setIsConversation();
110        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
111            builder.setIcon(
112                    IconCompat.createFromIcon(
113                            xmppConnectionService,
114                            Icon.createWithBitmap(
115                                    xmppConnectionService
116                                            .getAvatarService()
117                                            .getRoundedShortcut(mucOptions))));
118        }
119        return builder.build();
120    }
121
122    @TargetApi(Build.VERSION_CODES.N_MR1)
123    private ShortcutInfo getShortcutInfo(Contact contact) {
124        return getShortcutInfoCompat(contact).toShortcutInfo();
125    }
126
127    private static boolean contactsChanged(List<Contact> needles, List<ShortcutInfo> haystack) {
128        for(Contact needle : needles) {
129            if(!contactExists(needle,haystack)) {
130                return true;
131            }
132        }
133        return needles.size() != haystack.size();
134    }
135
136    @TargetApi(25)
137    private static boolean contactExists(Contact needle, List<ShortcutInfo> haystack) {
138        for(ShortcutInfo shortcutInfo : haystack) {
139            if (getShortcutId(needle).equals(shortcutInfo.getId()) && needle.getDisplayName().equals(shortcutInfo.getShortLabel())) {
140                return true;
141            }
142        }
143        return false;
144    }
145
146    private static String getShortcutId(Contact contact) {
147        return contact.getAccount().getJid().asBareJid().toEscapedString()+"#"+contact.getJid().asBareJid().toEscapedString();
148    }
149
150    private static String getShortcutId(final MucOptions mucOptions) {
151        final Account account = mucOptions.getAccount();
152        final Jid jid = mucOptions.getConversation().getJid();
153        return account.getJid().asBareJid().toEscapedString()
154                + "#"
155                + jid.asBareJid().toEscapedString();
156    }
157
158    private Intent getShortcutIntent(final MucOptions mucOptions) {
159        final Account account = mucOptions.getAccount();
160        return getShortcutIntent(
161                account,
162                Uri.parse(
163                        String.format(
164                                "xmpp:%s?join",
165                                mucOptions
166                                        .getConversation()
167                                        .getJid()
168                                        .asBareJid()
169                                        .toEscapedString())));
170    }
171
172    private Intent getShortcutIntent(final Contact contact) {
173        return getShortcutIntent(
174                contact.getAccount(),
175                Uri.parse("xmpp:" + contact.getJid().asBareJid().toEscapedString()));
176    }
177
178    private Intent getShortcutIntent(final Account account, final Uri uri) {
179        Intent intent = new Intent(xmppConnectionService, StartConversationActivity.class);
180        intent.setAction(Intent.ACTION_VIEW);
181        intent.setData(uri);
182        intent.putExtra("account", account.getJid().asBareJid().toString());
183        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
184        return intent;
185    }
186
187    @NonNull
188    public Intent createShortcut(Contact contact, boolean legacy) {
189        Intent intent;
190        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && !legacy) {
191            ShortcutInfo shortcut = getShortcutInfo(contact);
192            ShortcutManager shortcutManager = xmppConnectionService.getSystemService(ShortcutManager.class);
193            intent = shortcutManager.createShortcutResultIntent(shortcut);
194        } else {
195            intent = createShortcutResultIntent(contact);
196        }
197        return intent;
198    }
199
200    @NonNull
201    private Intent createShortcutResultIntent(Contact contact) {
202        AvatarService avatarService = xmppConnectionService.getAvatarService();
203        Bitmap icon = avatarService.getRoundedShortcutWithIcon(contact);
204        Intent intent = new Intent();
205        intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, contact.getDisplayName());
206        intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, icon);
207        intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, getShortcutIntent(contact));
208        return intent;
209    }
210
211    public static class FrequentContact {
212        private final String account;
213        private final Jid contact;
214
215        public FrequentContact(String account, Jid contact) {
216            this.account = account;
217            this.contact = contact;
218        }
219    }
220
221}