private Intent mReferrerFillInIntent;
private long mChooserShownTime;
- private boolean mIsSuccessfullySelected;
+ protected boolean mIsSuccessfullySelected;
private ChooserListAdapter mChooserListAdapter;
private ChooserRowAdapter mChooserRowAdapter;
}
}
}
- updateChooserCounts(target, mContentType);
+ updateChooserCounts(target);
return super.onTargetSelected(target, alwaysCheck);
}
// Do nothing. We'll send the voice stuff ourselves.
}
- void updateChooserCounts(TargetInfo info, String annotation) {
+ void updateChooserCounts(TargetInfo info) {
if (info != null) {
UsageStatsManager usageStatsManager =
(UsageStatsManager) getSystemService(Context.USAGE_STATS_SERVICE);
return;
}
final ResolveInfo ri = info.getResolveInfo();
- if (ri != null && ri.activityInfo != null) {
+ Intent targetIntent = getTargetIntent();
+ if (ri != null && ri.activityInfo != null && targetIntent != null) {
usageStatsManager.reportChooserSelection(ri.activityInfo.packageName, getUserId(),
- annotation, null, info.getResolvedIntent().getAction());
+ targetIntent.getType(), null, targetIntent.getAction());
if (mAdapter != null) {
mAdapter.updateModel(info.getResolvedComponentName());
}
if (DEBUG) {
- Log.d(TAG, "ResolveInfo Package is" + ri.activityInfo.packageName);
+ Log.d(TAG, "ResolveInfo Package is " + ri.activityInfo.packageName);
+ Log.d(TAG, "Annotation to be updated is " + targetIntent.getType());
+ Log.d(TAG, "Action to be updated is " + targetIntent.getAction());
}
} else if(DEBUG) {
Log.d(TAG, "Can not log Chooser Counts of null ResovleInfo");
} else {
TargetInfo clonedTarget = selectedTarget.cloneFilledIn(matchingIntent, 0);
if (super.onTargetSelected(clonedTarget, false)) {
- updateChooserCounts(clonedTarget, mContentType);
+ updateChooserCounts(clonedTarget);
finish();
return;
}
import org.junit.runner.RunWith;
import org.mockito.Mockito;
+import android.app.usage.UsageStats;
+import android.app.usage.UsageStatsManager;
import android.content.Intent;
import android.content.pm.ResolveInfo;
+import android.os.UserHandle;
import android.support.test.InstrumentationRegistry;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import java.util.ArrayList;
import java.util.List;
+import java.util.Map;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.Mockito.when;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.times;
/**
* Chooser activity instrumentation tests
}
@Test
+ public void updateChooserCountsAfterUserSelection() throws InterruptedException {
+ Intent sendIntent = createSendImageIntent();
+ List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2);
+
+ when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(),
+ Mockito.anyBoolean(),
+ Mockito.isA(List.class))).thenReturn(resolvedComponentInfos);
+
+ final ChooserWrapperActivity activity = mActivityRule
+ .launchActivity(Intent.createChooser(sendIntent, null));
+ waitForIdle();
+ UsageStatsManager usm = activity.getUsageStatsManager();
+ verify(sOverrides.resolverListController, times(1))
+ .sort(Mockito.any(List.class));
+ assertThat(activity.getIsSelected(), is(false));
+ sOverrides.onSafelyStartCallback = targetInfo -> {
+ return true;
+ };
+ String action = sendIntent.getAction();
+ String annotation = sendIntent.getType();
+ ResolveInfo toChoose = resolvedComponentInfos.get(0).getResolveInfoAt(0);
+ String packageName = toChoose.activityInfo.packageName;
+ long toChooseCount = getCount(usm, packageName, action, annotation);
+ onView(withText(toChoose.activityInfo.name))
+ .perform(click());
+ waitForIdle();
+ verify(sOverrides.resolverListController, times(1))
+ .updateModel(toChoose.activityInfo.getComponentName());
+ assertThat(activity.getIsSelected(), is(true));
+ long updatedCount = getCount(usm, packageName, action, annotation);
+ assertThat(updatedCount, is(toChooseCount + 1l));
+ }
+
+ @Test
+ public void reportChooserSelection() throws InterruptedException {
+ Intent sendIntent = createSendImageIntent();
+ final ChooserWrapperActivity activity = mActivityRule
+ .launchActivity(Intent.createChooser(sendIntent, null));
+ waitForIdle();
+ UsageStatsManager usm = activity.getUsageStatsManager();
+ String packageName = "test_package";
+ String action = "test_action";
+ String annotation = "test_annotation";
+ long beforeReport = getCount(usm, packageName, action, annotation);
+ usm.reportChooserSelection(packageName, activity.getUserId(), annotation, null, action);
+ long afterReport = getCount(usm, packageName, action, annotation);
+ assertThat(afterReport, is(beforeReport + 1l));
+ }
+
+ @Test
public void noResultsFromPackageManager() {
when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(),
Mockito.anyBoolean(),
private void waitForIdle() {
InstrumentationRegistry.getInstrumentation().waitForIdleSync();
}
+
+ private Integer getCount(
+ UsageStatsManager usm, String packageName, String action, String annotation) {
+ if (usm == null) {
+ return 0;
+ }
+ Map<String, UsageStats> stats =
+ usm.queryAndAggregateUsageStats(Long.MIN_VALUE, Long.MAX_VALUE);
+ UsageStats packageStats = stats.get(packageName);
+ if (packageStats == null || packageStats.mChooserCounts == null
+ || packageStats.mChooserCounts.get(action) == null) {
+ return 0;
+ }
+ return packageStats.mChooserCounts.get(action).getOrDefault(annotation, 0);
+ }
}
\ No newline at end of file