diff --git a/core/java/com/android/internal/app/ChooserActivity.java b/core/java/com/android/internal/app/ChooserActivity.java index 7eb3dab9f344d..ee998371c941c 100644 --- a/core/java/com/android/internal/app/ChooserActivity.java +++ b/core/java/com/android/internal/app/ChooserActivity.java @@ -1434,6 +1434,22 @@ public class ChooserActivity extends ResolverActivity { List resultList, List driList, @Nullable List appTargets) { + if (appTargets != null && appTargets.size() != resultList.size()) { + throw new RuntimeException("resultList and appTargets must have the same size." + + " resultList.size()=" + resultList.size() + + " appTargets.size()=" + appTargets.size()); + } + + for (int i = resultList.size() - 1; i >= 0; i--) { + final String packageName = resultList.get(i).getTargetComponent().getPackageName(); + if (!isPackageEnabled(packageName)) { + resultList.remove(i); + if (appTargets != null) { + appTargets.remove(i); + } + } + } + // Match ShareShortcutInfos with DisplayResolveInfos to be able to use the old code path // for direct share targets. After ShareSheet is refactored we should use the // ShareShortcutInfos directly. @@ -1447,7 +1463,6 @@ public class ChooserActivity extends ResolverActivity { ChooserTarget chooserTarget = convertToChooserTarget(shareShortcutInfo); chooserTargets.add(chooserTarget); if (mDirectShareAppTargetCache != null && appTargets != null) { - // Note that appTargets.size() == resultList.size() is always true. mDirectShareAppTargetCache.put(chooserTarget, appTargets.get(j)); } } @@ -1473,6 +1488,24 @@ public class ChooserActivity extends ResolverActivity { mChooserHandler.sendMessage(msg); } + private boolean isPackageEnabled(String packageName) { + if (TextUtils.isEmpty(packageName)) { + return false; + } + ApplicationInfo appInfo; + try { + appInfo = getPackageManager().getApplicationInfo(packageName, 0); + } catch (NameNotFoundException e) { + return false; + } + + if (appInfo != null && appInfo.enabled + && (appInfo.flags & ApplicationInfo.FLAG_SUSPENDED) == 0) { + return true; + } + return false; + } + private ChooserTarget convertToChooserTarget(ShortcutManager.ShareShortcutInfo shareShortcut) { ShortcutInfo shortcutInfo = shareShortcut.getShortcutInfo(); Bundle extras = new Bundle();