Skip to content

Commit 4859ce8

Browse files
author
维术
committed
[Exposed-UI]: fix the crash caused by shortcuts overflow.
1 parent 5839632 commit 4859ce8

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

VirtualApp/lib/src/main/java/com/lody/virtual/client/core/VirtualCore.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@
4949
import com.lody.virtual.server.interfaces.IUiCallback;
5050

5151
import java.io.IOException;
52-
import java.util.Arrays;
52+
import java.util.Collections;
53+
import java.util.Comparator;
5354
import java.util.List;
5455

5556
import dalvik.system.DexFile;
@@ -509,8 +510,28 @@ private static boolean createShortcutAboveN(Context context, String id, String l
509510
if (shortcutManager == null) {
510511
return false;
511512
}
512-
shortcutManager.addDynamicShortcuts(Arrays.asList(likeShortcut));
513-
return true;
513+
try {
514+
int max = shortcutManager.getMaxShortcutCountPerActivity();
515+
List<ShortcutInfo> dynamicShortcuts = shortcutManager.getDynamicShortcuts();
516+
if (dynamicShortcuts.size() >= max) {
517+
Collections.sort(dynamicShortcuts, new Comparator<ShortcutInfo>() {
518+
@Override
519+
public int compare(ShortcutInfo o1, ShortcutInfo o2) {
520+
long r = o1.getLastChangedTimestamp() - o2.getLastChangedTimestamp();
521+
return r == 0 ? 0 : (r > 0 ? 1 : -1);
522+
}
523+
});
524+
525+
ShortcutInfo remove = dynamicShortcuts.remove(0);// remove old.
526+
shortcutManager.removeDynamicShortcuts(Collections.singletonList(remove.getId()));
527+
}
528+
529+
dynamicShortcuts.add(likeShortcut);
530+
shortcutManager.addDynamicShortcuts(dynamicShortcuts);
531+
return true;
532+
} catch (Throwable e) {
533+
return false;
534+
}
514535
}
515536

516537
@TargetApi(Build.VERSION_CODES.O)

0 commit comments

Comments
 (0)