Commit 9cd81b3d authored by Adrien Béraud's avatar Adrien Béraud

shortcuts: avoid concurrent conversation access, fix exception

Change-Id: I770fe134b0036883ac925e1ea3181530a7a64861
parent 88b49837
......@@ -357,8 +357,14 @@ public class HomeActivity extends AppCompatActivity implements BottomNavigationV
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
mDisposable.add((mAccountService
.getCurrentAccountSubject()
.observeOn(Schedulers.computation())
.subscribe(this::setShareShortcuts)));
.observeOn(Schedulers.io())
.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;
......@@ -766,30 +772,36 @@ public class HomeActivity extends AppCompatActivity implements BottomNavigationV
return mBinding.accountSwitch;
}
private void setShareShortcuts(Account account) {
Collection<Conversation> conversations = account.getConversations();
List<Future<Bitmap>> futureIcons = new ArrayList<>(conversations.size());
private void setShareShortcuts(Collection<Conversation> conversations) {
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) {
CallContact contact = conversation.getContact();
futureIcons.add(AvatarFactory.getBitmapAvatar(this, contact, targetSize)
.subscribeOn(Schedulers.computation())
.toFuture());
if (++i == maxCount)
break;
}
int i = 0;
List<ShortcutInfoCompat> shortcutInfoList = new ArrayList<>(conversations.size());
List<ShortcutInfoCompat> shortcutInfoList = new ArrayList<>(futureIcons.size());
i = 0;
for (Conversation conversation : conversations) {
CallContact contact = conversation.getContact();
IconCompat icon = null;
try {
icon = IconCompat.createWithBitmap(futureIcons.get(i).get());
} 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());
String key = ConversationPath.toKey(account.getAccountID(), contact.getPrimaryNumber());
Bundle bundle = ConversationPath.toBundle(conversation.getAccountId(), contact.getPrimaryNumber());
String key = ConversationPath.toKey(conversation.getAccountId(), contact.getPrimaryNumber());
Person person = new Person.Builder()
.setName(contact.getDisplayName())
......@@ -806,12 +818,17 @@ public class HomeActivity extends AppCompatActivity implements BottomNavigationV
.build();
shortcutInfoList.add(shortcutInfo);
i++;
if (++i == maxCount)
break;
}
Log.w(TAG, "ShortcutManagerCompat.addDynamicShortcuts " + shortcutInfoList.size());
ShortcutManagerCompat.removeAllDynamicShortcuts(this);
ShortcutManagerCompat.addDynamicShortcuts(this, shortcutInfoList);
try {
Log.d(TAG, "Adding shortcuts: " + shortcutInfoList.size());
ShortcutManagerCompat.removeAllDynamicShortcuts(this);
ShortcutManagerCompat.addDynamicShortcuts(this, shortcutInfoList);
} catch (Exception e) {
Log.w(TAG, "Error adding shortcuts", e);
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment