Filter disabled/suspended direct share targets in sharesheet

Bug: 123961239
Test: Manual test by suspending test app using Digital Wellbeing
Change-Id: Id26465c484a708714ba955a1657fb1c3be402734
This commit is contained in:
Mehdi Alizadeh
2019-05-27 17:56:51 -07:00
parent ece7a57af5
commit 3e3216f111

View File

@@ -1327,6 +1327,22 @@ public class ChooserActivity extends ResolverActivity {
List<ShortcutManager.ShareShortcutInfo> resultList,
List<DisplayResolveInfo> driList,
@Nullable List<AppTarget> 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.
@@ -1340,7 +1356,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));
}
}
@@ -1366,6 +1381,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();