diff options
| author | 2020-05-13 15:34:29 -0700 | |
|---|---|---|
| committer | 2020-05-15 10:01:52 -0700 | |
| commit | 276e384e4c677ff6cb6c5f08edacc3e47a51e9b5 (patch) | |
| tree | 0b94666b5b8157d0998b1000880db1a560ccdbbb | |
| parent | 85f2c7946119342e54e8b4a4542089cbd811a51a (diff) | |
De-duplicate with callerTargets(added by using Intent.EXTRA_INITIAL_INTENTS) when adding DisplayResolveInfo.
Bug: 154611479
Test: atest CtsSharesheetTestCases:android.sharesheet.cts.CtsSharesheetDeviceTest
Change-Id: I812c8275fc6f12ad52f56419eedd0c580530704a
3 files changed, 65 insertions, 12 deletions
diff --git a/core/java/com/android/internal/app/ChooserListAdapter.java b/core/java/com/android/internal/app/ChooserListAdapter.java index f426bc0ecbb5..f9462a8f3bfc 100644 --- a/core/java/com/android/internal/app/ChooserListAdapter.java +++ b/core/java/com/android/internal/app/ChooserListAdapter.java @@ -103,7 +103,7 @@ public class ChooserListAdapter extends ResolverListAdapter { private Set<ComponentName> mPendingChooserTargetService = new HashSet<>(); private Set<ComponentName> mShortcutComponents = new HashSet<>(); private final List<ChooserTargetInfo> mServiceTargets = new ArrayList<>(); - private final List<TargetInfo> mCallerTargets = new ArrayList<>(); + private final List<DisplayResolveInfo> mCallerTargets = new ArrayList<>(); private final ChooserActivity.BaseChooserTargetComparator mBaseTargetComparator = new ChooserActivity.BaseChooserTargetComparator(); @@ -257,10 +257,13 @@ public class ChooserListAdapter extends ResolverListAdapter { void updateAlphabeticalList() { mSortedList.clear(); + List<DisplayResolveInfo> tempList = new ArrayList<>(); + tempList.addAll(mDisplayList); + tempList.addAll(mCallerTargets); if (mEnableStackedApps) { // Consolidate multiple targets from same app. Map<String, DisplayResolveInfo> consolidated = new HashMap<>(); - for (DisplayResolveInfo info : mDisplayList) { + for (DisplayResolveInfo info : tempList) { String packageName = info.getResolvedComponentName().getPackageName(); if (consolidated.get(packageName) != null) { // create consolidated target @@ -274,7 +277,7 @@ public class ChooserListAdapter extends ResolverListAdapter { } mSortedList.addAll(consolidated.values()); } else { - mSortedList.addAll(mDisplayList); + mSortedList.addAll(tempList); } Collections.sort(mSortedList, new ChooserActivity.AzInfoComparator(mContext)); } @@ -326,7 +329,10 @@ public class ChooserListAdapter extends ResolverListAdapter { return standardCount > mChooserListCommunicator.getMaxRankedTargets() ? standardCount : 0; } - int getRankedTargetCount() { + /** + * Fetch ranked app target count + */ + public int getRankedTargetCount() { int spacesAvailable = mChooserListCommunicator.getMaxRankedTargets() - getCallerTargetCount(); return Math.min(spacesAvailable, super.getCount()); @@ -411,6 +417,19 @@ public class ChooserListAdapter extends ResolverListAdapter { return null; } + // Check whether {@code dri} should be added into mDisplayList. + @Override + protected boolean shouldAddResolveInfo(DisplayResolveInfo dri) { + // Checks if this info is already listed in callerTargets. + for (TargetInfo existingInfo : mCallerTargets) { + if (mResolverListCommunicator + .resolveInfoMatch(dri.getResolveInfo(), existingInfo.getResolveInfo())) { + return false; + } + } + return super.shouldAddResolveInfo(dri); + } + /** * Fetch surfaced direct share target info */ diff --git a/core/java/com/android/internal/app/ResolverListAdapter.java b/core/java/com/android/internal/app/ResolverListAdapter.java index 24bf98b6502c..af9c0408ccaa 100644 --- a/core/java/com/android/internal/app/ResolverListAdapter.java +++ b/core/java/com/android/internal/app/ResolverListAdapter.java @@ -85,7 +85,7 @@ public class ResolverListAdapter extends BaseAdapter { private int mLastChosenPosition = -1; private boolean mFilterLastUsed; - private final ResolverListCommunicator mResolverListCommunicator; + final ResolverListCommunicator mResolverListCommunicator; private Runnable mPostListReadyRunnable; private final boolean mIsAudioCaptureDevice; private boolean mIsTabLoaded; @@ -443,15 +443,22 @@ public class ResolverListAdapter extends BaseAdapter { // TODO(arangelov): Is that UserHandle.USER_CURRENT check okay? if (dri != null && dri.getResolveInfo() != null && dri.getResolveInfo().targetUserId == UserHandle.USER_CURRENT) { - // Checks if this info is already listed in display. - for (DisplayResolveInfo existingInfo : mDisplayList) { - if (mResolverListCommunicator - .resolveInfoMatch(dri.getResolveInfo(), existingInfo.getResolveInfo())) { - return; - } + if (shouldAddResolveInfo(dri)) { + mDisplayList.add(dri); + } + } + } + + // Check whether {@code dri} should be added into mDisplayList. + protected boolean shouldAddResolveInfo(DisplayResolveInfo dri) { + // Checks if this info is already listed in display. + for (DisplayResolveInfo existingInfo : mDisplayList) { + if (mResolverListCommunicator + .resolveInfoMatch(dri.getResolveInfo(), existingInfo.getResolveInfo())) { + return false; } - mDisplayList.add(dri); } + return true; } @Nullable diff --git a/core/tests/coretests/src/com/android/internal/app/ChooserActivityTest.java b/core/tests/coretests/src/com/android/internal/app/ChooserActivityTest.java index e23a3cad914b..ed2c28372906 100644 --- a/core/tests/coretests/src/com/android/internal/app/ChooserActivityTest.java +++ b/core/tests/coretests/src/com/android/internal/app/ChooserActivityTest.java @@ -1872,6 +1872,33 @@ public class ChooserActivityTest { .check(matches(isDisplayed())); } + @Test + public void testDeduplicateCallerTargetRankedTarget() { + // Create 4 ranked app targets. + List<ResolvedComponentInfo> personalResolvedComponentInfos = + createResolvedComponentsForTest(4); + when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(), + Mockito.anyBoolean(), + Mockito.isA(List.class))) + .thenReturn(new ArrayList<>(personalResolvedComponentInfos)); + // Create caller target which is duplicate with one of app targets + Intent chooserIntent = createChooserIntent(createSendTextIntent(), + new Intent[] {new Intent("action.fake")}); + sOverrides.packageManager = mock(PackageManager.class); + ResolveInfo ri = ResolverDataProvider.createResolveInfo(0, + UserHandle.USER_CURRENT); + when(sOverrides.packageManager.resolveActivity(any(Intent.class), anyInt())).thenReturn(ri); + waitForIdle(); + + ChooserWrapperActivity activity = mActivityRule.launchActivity(chooserIntent); + waitForIdle(); + + // Total 4 targets (1 caller target, 3 ranked targets) + assertThat(activity.getAdapter().getCount(), is(4)); + assertThat(activity.getAdapter().getCallerTargetCount(), is(1)); + assertThat(activity.getAdapter().getRankedTargetCount(), is(3)); + } + private Intent createChooserIntent(Intent intent, Intent[] initialIntents) { Intent chooserIntent = new Intent(); chooserIntent.setAction(Intent.ACTION_CHOOSER); |