Skip to content
Snippets Groups Projects
Commit 9cd81b3d authored by Adrien Béraud's avatar Adrien Béraud
Browse files

shortcuts: avoid concurrent conversation access, fix exception

Change-Id: I770fe134b0036883ac925e1ea3181530a7a64861
parent 88b49837
No related branches found
No related tags found
No related merge requests found
...@@ -357,8 +357,14 @@ public class HomeActivity extends AppCompatActivity implements BottomNavigationV ...@@ -357,8 +357,14 @@ public class HomeActivity extends AppCompatActivity implements BottomNavigationV
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
mDisposable.add((mAccountService mDisposable.add((mAccountService
.getCurrentAccountSubject() .getCurrentAccountSubject()
.observeOn(Schedulers.computation()) .observeOn(Schedulers.io())
.subscribe(this::setShareShortcuts))); .map(account -> {
Collection<Conversation> conversations = account.getConversations();
synchronized (conversations) {
return new ArrayList<>(conversations);
}
})
.subscribe(this::setShareShortcuts, e -> Log.e(TAG, "Error generating conversation shortcuts", e))));
} }
int newOrientation = getResources().getConfiguration().orientation; int newOrientation = getResources().getConfiguration().orientation;
...@@ -766,30 +772,36 @@ public class HomeActivity extends AppCompatActivity implements BottomNavigationV ...@@ -766,30 +772,36 @@ public class HomeActivity extends AppCompatActivity implements BottomNavigationV
return mBinding.accountSwitch; return mBinding.accountSwitch;
} }
private void setShareShortcuts(Account account) { private void setShareShortcuts(Collection<Conversation> conversations) {
Collection<Conversation> conversations = account.getConversations();
List<Future<Bitmap>> futureIcons = new ArrayList<>(conversations.size());
int targetSize = (int) (AvatarFactory.SIZE_NOTIF * getResources().getDisplayMetrics().density); int targetSize = (int) (AvatarFactory.SIZE_NOTIF * getResources().getDisplayMetrics().density);
int i = 0;
int maxCount = ShortcutManagerCompat.getMaxShortcutCountPerActivity(this);
if (maxCount == 0)
maxCount = 4;
List<Future<Bitmap>> futureIcons = new ArrayList<>(Math.min(conversations.size(),maxCount));
for (Conversation conversation : conversations) { for (Conversation conversation : conversations) {
CallContact contact = conversation.getContact(); CallContact contact = conversation.getContact();
futureIcons.add(AvatarFactory.getBitmapAvatar(this, contact, targetSize) futureIcons.add(AvatarFactory.getBitmapAvatar(this, contact, targetSize)
.subscribeOn(Schedulers.computation()) .subscribeOn(Schedulers.computation())
.toFuture()); .toFuture());
if (++i == maxCount)
break;
} }
int i = 0; List<ShortcutInfoCompat> shortcutInfoList = new ArrayList<>(futureIcons.size());
List<ShortcutInfoCompat> shortcutInfoList = new ArrayList<>(conversations.size());
i = 0;
for (Conversation conversation : conversations) { for (Conversation conversation : conversations) {
CallContact contact = conversation.getContact(); CallContact contact = conversation.getContact();
IconCompat icon = null; IconCompat icon = null;
try { try {
icon = IconCompat.createWithBitmap(futureIcons.get(i).get()); icon = IconCompat.createWithBitmap(futureIcons.get(i).get());
} catch (Exception e) { } catch (Exception e) {
Log.w("RingChooserService", "Failed to load icon", e); Log.w(TAG, "Failed to load icon", e);
} }
Bundle bundle = ConversationPath.toBundle(account.getAccountID(), contact.getPrimaryNumber()); Bundle bundle = ConversationPath.toBundle(conversation.getAccountId(), contact.getPrimaryNumber());
String key = ConversationPath.toKey(account.getAccountID(), contact.getPrimaryNumber()); String key = ConversationPath.toKey(conversation.getAccountId(), contact.getPrimaryNumber());
Person person = new Person.Builder() Person person = new Person.Builder()
.setName(contact.getDisplayName()) .setName(contact.getDisplayName())
...@@ -806,12 +818,17 @@ public class HomeActivity extends AppCompatActivity implements BottomNavigationV ...@@ -806,12 +818,17 @@ public class HomeActivity extends AppCompatActivity implements BottomNavigationV
.build(); .build();
shortcutInfoList.add(shortcutInfo); shortcutInfoList.add(shortcutInfo);
i++; if (++i == maxCount)
break;
} }
Log.w(TAG, "ShortcutManagerCompat.addDynamicShortcuts " + shortcutInfoList.size()); try {
Log.d(TAG, "Adding shortcuts: " + shortcutInfoList.size());
ShortcutManagerCompat.removeAllDynamicShortcuts(this); ShortcutManagerCompat.removeAllDynamicShortcuts(this);
ShortcutManagerCompat.addDynamicShortcuts(this, shortcutInfoList); ShortcutManagerCompat.addDynamicShortcuts(this, shortcutInfoList);
} catch (Exception e) {
Log.w(TAG, "Error adding shortcuts", e);
}
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment