Merge "Don't call ShortcutManager APIs if the work profile is paused or locked" into rvc-dev

This commit is contained in:
Antoan Angelov
2020-06-26 18:37:02 +00:00
committed by Android (Google) Code Review
3 changed files with 101 additions and 3 deletions

View File

@@ -819,7 +819,8 @@ public class ChooserActivity extends ResolverActivity implements
if (chooserListAdapter.getCount() == 0) {
return;
}
if (resultList.isEmpty()) {
if (resultList.isEmpty()
&& shouldQueryShortcutManager(chooserListAdapter.getUserHandle())) {
// APS may be disabled, so try querying targets ourselves.
queryDirectShareTargets(chooserListAdapter, true);
return;
@@ -2025,6 +2026,26 @@ public class ChooserActivity extends ResolverActivity implements
});
}
/**
* Returns {@code false} if {@code userHandle} is the work profile and it's either
* in quiet mode or not running.
*/
private boolean shouldQueryShortcutManager(UserHandle userHandle) {
if (!shouldShowTabs()) {
return true;
}
if (!getWorkProfileUserHandle().equals(userHandle)) {
return true;
}
if (!isUserRunning(userHandle)) {
return false;
}
if (isQuietModeEnabled(userHandle)) {
return false;
}
return true;
}
private void sendChooserTargetRankingScore(List<AppTarget> chooserTargetScores,
UserHandle userHandle) {
final Message msg = Message.obtain();
@@ -2839,8 +2860,8 @@ public class ChooserActivity extends ResolverActivity implements
return;
}
// no need to query direct share for work profile when its turned off
if (isQuietModeEnabled(chooserListAdapter.getUserHandle())) {
// no need to query direct share for work profile when its locked or disabled
if (!shouldQueryShortcutManager(chooserListAdapter.getUserHandle())) {
getChooserActivityLogger().logSharesheetAppLoadComplete();
return;
}
@@ -2864,6 +2885,12 @@ public class ChooserActivity extends ResolverActivity implements
getChooserActivityLogger().logSharesheetAppLoadComplete();
}
@VisibleForTesting
protected boolean isUserRunning(UserHandle userHandle) {
UserManager userManager = getSystemService(UserManager.class);
return userManager.isUserRunning(userHandle);
}
@VisibleForTesting
protected boolean isQuietModeEnabled(UserHandle userHandle) {
UserManager userManager = getSystemService(UserManager.class);

View File

@@ -1995,6 +1995,70 @@ public class ChooserActivityTest {
isQueryTargetServicesCalledOnWorkProfile[0]);
}
@Test
public void testWorkTab_selectingWorkTabWithLockedWorkUser_directShareTargetsNotQueried() {
// enable the work tab feature flag
ResolverActivity.ENABLE_TABBED_VIEW = true;
markWorkProfileUserAvailable();
List<ResolvedComponentInfo> personalResolvedComponentInfos =
createResolvedComponentsForTestWithOtherProfile(3, /* userId */ 10);
List<ResolvedComponentInfo> workResolvedComponentInfos =
createResolvedComponentsForTest(3);
setupResolverControllers(personalResolvedComponentInfos, workResolvedComponentInfos);
sOverrides.isWorkProfileUserRunning = false;
boolean[] isQueryDirectShareCalledOnWorkProfile = new boolean[] { false };
sOverrides.onQueryDirectShareTargets = chooserListAdapter -> {
isQueryDirectShareCalledOnWorkProfile[0] =
(chooserListAdapter.getUserHandle().getIdentifier() == 10);
return null;
};
boolean[] isQueryTargetServicesCalledOnWorkProfile = new boolean[] { false };
sOverrides.onQueryTargetServices = chooserListAdapter -> {
isQueryTargetServicesCalledOnWorkProfile[0] =
(chooserListAdapter.getUserHandle().getIdentifier() == 10);
return null;
};
Intent sendIntent = createSendTextIntent();
sendIntent.setType("TestType");
mActivityRule.launchActivity(Intent.createChooser(sendIntent, "work tab test"));
waitForIdle();
onView(withId(R.id.contentPanel))
.perform(swipeUp());
onView(withText(R.string.resolver_work_tab)).perform(click());
waitForIdle();
assertFalse("Direct share targets were queried on a locked work profile user",
isQueryDirectShareCalledOnWorkProfile[0]);
assertFalse("Target services were queried on a locked work profile user",
isQueryTargetServicesCalledOnWorkProfile[0]);
}
@Test
public void testWorkTab_workUserLocked_workTargetsShown() {
// enable the work tab feature flag
ResolverActivity.ENABLE_TABBED_VIEW = true;
markWorkProfileUserAvailable();
List<ResolvedComponentInfo> personalResolvedComponentInfos =
createResolvedComponentsForTestWithOtherProfile(3, /* userId */ 10);
List<ResolvedComponentInfo> workResolvedComponentInfos =
createResolvedComponentsForTest(3);
setupResolverControllers(personalResolvedComponentInfos, workResolvedComponentInfos);
Intent sendIntent = createSendTextIntent();
sendIntent.setType("TestType");
sOverrides.isWorkProfileUserRunning = false;
final ChooserWrapperActivity activity =
mActivityRule.launchActivity(Intent.createChooser(sendIntent, "work tab test"));
waitForIdle();
onView(withId(R.id.contentPanel))
.perform(swipeUp());
onView(withText(R.string.resolver_work_tab)).perform(click());
waitForIdle();
assertEquals(3, activity.getWorkListAdapter().getCount());
}
private Intent createChooserIntent(Intent intent, Intent[] initialIntents) {
Intent chooserIntent = new Intent();
chooserIntent.setAction(Intent.ACTION_CHOOSER);

View File

@@ -227,6 +227,11 @@ public class ChooserWrapperActivity extends ChooserActivity {
return sOverrides.isQuietModeEnabled;
}
@Override
protected boolean isUserRunning(UserHandle userHandle) {
return sOverrides.isWorkProfileUserRunning;
}
/**
* We cannot directly mock the activity created since instrumentation creates it.
* <p>
@@ -252,6 +257,7 @@ public class ChooserWrapperActivity extends ChooserActivity {
public UserHandle workProfileUserHandle;
public boolean hasCrossProfileIntents;
public boolean isQuietModeEnabled;
public boolean isWorkProfileUserRunning;
public AbstractMultiProfilePagerAdapter.Injector multiPagerAdapterInjector;
public PackageManager packageManager;
@@ -274,6 +280,7 @@ public class ChooserWrapperActivity extends ChooserActivity {
workProfileUserHandle = null;
hasCrossProfileIntents = true;
isQuietModeEnabled = false;
isWorkProfileUserRunning = true;
packageManager = null;
multiPagerAdapterInjector = new AbstractMultiProfilePagerAdapter.Injector() {
@Override