Merge "De-duplicate with callerTargets(added by using Intent.EXTRA_INITIAL_INTENTS) when adding DisplayResolveInfo." into rvc-dev

This commit is contained in:
TreeHugger Robot
2020-05-15 23:10:41 +00:00
committed by Android (Google) Code Review
3 changed files with 65 additions and 12 deletions

View File

@@ -104,7 +104,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();
@@ -268,10 +268,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();
DisplayResolveInfo multiDri = consolidated.get(packageName);
if (multiDri == null) {
@@ -288,7 +291,7 @@ public class ChooserListAdapter extends ResolverListAdapter {
}
mSortedList.addAll(consolidated.values());
} else {
mSortedList.addAll(mDisplayList);
mSortedList.addAll(tempList);
}
Collections.sort(mSortedList, new ChooserActivity.AzInfoComparator(mContext));
}
@@ -340,7 +343,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());
@@ -425,6 +431,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
*/

View File

@@ -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,17 +443,24 @@ 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);
}
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;
}
}
return true;
}
@Nullable
public ResolveInfo resolveInfoForPosition(int position, boolean filtered) {
TargetInfo target = targetInfoForPosition(position, filtered);

View File

@@ -1926,6 +1926,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);