Merge "Settings change for device-wide unknown sources block."

This commit is contained in:
TreeHugger Robot
2018-10-02 13:07:24 +00:00
committed by Android (Google) Code Review
2 changed files with 148 additions and 13 deletions

View File

@@ -80,21 +80,20 @@ public class ExternalSourcesDetails extends AppInfoWithHeader
}
public static CharSequence getPreferenceSummary(Context context, AppEntry entry) {
final UserHandle userHandle = UserHandle.getUserHandleForUid(entry.info.uid);
final UserManager um = UserManager.get(context);
final int userRestrictionSource = um.getUserRestrictionSource(
UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES,
UserHandle.getUserHandleForUid(entry.info.uid));
switch (userRestrictionSource) {
case UserManager.RESTRICTION_SOURCE_DEVICE_OWNER:
case UserManager.RESTRICTION_SOURCE_PROFILE_OWNER:
return context.getString(R.string.disabled_by_admin);
case UserManager.RESTRICTION_SOURCE_SYSTEM:
return context.getString(R.string.disabled);
UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES, userHandle)
| um.getUserRestrictionSource(
UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES_GLOBALLY,
userHandle);
if ((userRestrictionSource & UserManager.RESTRICTION_SOURCE_SYSTEM) != 0) {
return context.getString(R.string.disabled_by_admin);
} else if (userRestrictionSource != 0) {
return context.getString(R.string.disabled);
}
final InstallAppsState appsState = new AppStateInstallAppsBridge(context, null, null)
.createInstallAppsStateFor(entry.info.packageName, entry.info.uid);
return context.getString(appsState.canInstallApps()
? R.string.app_permission_summary_allowed
: R.string.app_permission_summary_not_allowed);
@@ -119,6 +118,10 @@ public class ExternalSourcesDetails extends AppInfoWithHeader
return true;
}
mSwitchPref.checkRestrictionAndSetDisabled(UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES);
if (!mSwitchPref.isDisabledByAdmin()) {
mSwitchPref.checkRestrictionAndSetDisabled(
UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES_GLOBALLY);
}
if (mSwitchPref.isDisabledByAdmin()) {
return true;
}

View File

@@ -17,29 +17,36 @@
package com.android.settings.applications.appinfo;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.nullable;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.os.UserHandle;
import android.os.UserManager;
import com.android.settings.applications.AppStateInstallAppsBridge;
import com.android.settings.applications.AppStateInstallAppsBridge.InstallAppsState;
import com.android.settings.testutils.shadow.ShadowUserManager;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settingslib.RestrictedPreferenceHelper;
import com.android.settingslib.RestrictedSwitchPreference;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import org.robolectric.shadow.api.Shadow;
import org.robolectric.util.ReflectionHelpers;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(shadows = {ShadowUserManager.class})
public class ExternalSourcesDetailsTest {
@Mock
@@ -47,6 +54,8 @@ public class ExternalSourcesDetailsTest {
@Mock
private RestrictedSwitchPreference mSwitchPref;
@Mock
private RestrictedPreferenceHelper mHelper;
@Mock
private PackageInfo mPackageInfo;
private ExternalSourcesDetails mFragment;
@@ -90,5 +99,128 @@ public class ExternalSourcesDetailsTest {
mFragment.refreshUi();
assertThat(mFragment.refreshUi()).isTrue();
assertThat(mSwitchPref.isDisabledByAdmin()).isFalse();
}
@Test
public void refreshUi_userRestrictionsUnknownSources_disablesSwitchPreference() {
// Mocks set up
final ExternalSourcesDetails fragment = new ExternalSourcesDetails();
final ContextWrapper context = RuntimeEnvironment.application;
final UserManager userManager = (UserManager) context.getSystemService(
Context.USER_SERVICE);
final ShadowUserManager shadowUserManager = Shadow.extract(userManager);
ReflectionHelpers.setField(fragment, "mSwitchPref", mSwitchPref);
ReflectionHelpers.setField(fragment, "mPackageInfo", mPackageInfo);
mPackageInfo.applicationInfo = new ApplicationInfo();
ReflectionHelpers.setField(fragment, "mUserManager", userManager);
ReflectionHelpers.setField(mSwitchPref, "mHelper", mHelper);
final AppStateInstallAppsBridge appBridge = mock(AppStateInstallAppsBridge.class);
ReflectionHelpers.setField(fragment, "mAppBridge", appBridge);
when(appBridge.createInstallAppsStateFor(nullable(String.class), anyInt()))
.thenReturn(mock(InstallAppsState.class));
// Test restriction set up
shadowUserManager.setUserRestriction(UserHandle.of(UserHandle.myUserId()),
UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES, true);
doAnswer((answer) -> {
when(mSwitchPref.isDisabledByAdmin()).thenReturn(true);
return null;
}).when(mSwitchPref).checkRestrictionAndSetDisabled(
UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES);
// Code execution
assertThat(fragment.refreshUi()).isTrue();
// Assertions
assertThat(shadowUserManager.hasUserRestriction(
UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES,
UserHandle.of(UserHandle.myUserId()))).isTrue();
assertThat(mSwitchPref.isDisabledByAdmin()).isTrue();
}
@Test
public void refreshUi_userRestrictionsUnknownSourcesGlobally_disablesSwitchPreference() {
// Mocks set up
final ExternalSourcesDetails fragment = new ExternalSourcesDetails();
final ContextWrapper context = RuntimeEnvironment.application;
final UserManager userManager = (UserManager) context.getSystemService(
Context.USER_SERVICE);
final ShadowUserManager shadowUserManager = Shadow.extract(userManager);
ReflectionHelpers.setField(fragment, "mSwitchPref", mSwitchPref);
ReflectionHelpers.setField(fragment, "mPackageInfo", mPackageInfo);
mPackageInfo.applicationInfo = new ApplicationInfo();
ReflectionHelpers.setField(fragment, "mUserManager", userManager);
ReflectionHelpers.setField(mSwitchPref, "mHelper", mHelper);
final AppStateInstallAppsBridge appBridge = mock(AppStateInstallAppsBridge.class);
ReflectionHelpers.setField(fragment, "mAppBridge", appBridge);
when(appBridge.createInstallAppsStateFor(nullable(String.class), anyInt()))
.thenReturn(mock(InstallAppsState.class));
// Test restriction set up
shadowUserManager.setUserRestriction(UserHandle.of(UserHandle.myUserId()),
UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES_GLOBALLY, true);
doAnswer((answer) -> {
when(mSwitchPref.isDisabledByAdmin()).thenReturn(true);
return null;
}).when(mSwitchPref).checkRestrictionAndSetDisabled(
UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES_GLOBALLY);
// Code execution
assertThat(fragment.refreshUi()).isTrue();
// Assertions
assertThat(shadowUserManager.hasUserRestriction(
UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES_GLOBALLY,
UserHandle.of(UserHandle.myUserId()))).isTrue();
assertThat(mSwitchPref.isDisabledByAdmin()).isTrue();
}
@Test
public void refreshUi_bothUnknownSourcesUserRestrictions_disableSwitchPreference() {
// Mocks set up
final ExternalSourcesDetails fragment = new ExternalSourcesDetails();
final ContextWrapper context = RuntimeEnvironment.application;
final UserManager userManager = (UserManager) context.getSystemService(
Context.USER_SERVICE);
final ShadowUserManager shadowUserManager = Shadow.extract(userManager);
ReflectionHelpers.setField(fragment, "mSwitchPref", mSwitchPref);
ReflectionHelpers.setField(fragment, "mPackageInfo", mPackageInfo);
mPackageInfo.applicationInfo = new ApplicationInfo();
ReflectionHelpers.setField(fragment, "mUserManager", userManager);
ReflectionHelpers.setField(mSwitchPref, "mHelper", mHelper);
final AppStateInstallAppsBridge appBridge = mock(AppStateInstallAppsBridge.class);
ReflectionHelpers.setField(fragment, "mAppBridge", appBridge);
when(appBridge.createInstallAppsStateFor(nullable(String.class), anyInt()))
.thenReturn(mock(InstallAppsState.class));
// Test restriction set up
shadowUserManager.setUserRestriction(UserHandle.of(UserHandle.myUserId()),
UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES_GLOBALLY, true);
shadowUserManager.setUserRestriction(UserHandle.of(UserHandle.myUserId()),
UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES, true);
doAnswer((answer) -> {
when(mSwitchPref.isDisabledByAdmin()).thenReturn(true);
return null;
}).when(mSwitchPref).checkRestrictionAndSetDisabled(
UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES_GLOBALLY);
// Code execution
assertThat(fragment.refreshUi()).isTrue();
// Assertions
assertThat(shadowUserManager.hasUserRestriction(
UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES_GLOBALLY,
UserHandle.of(UserHandle.myUserId()))).isTrue();
assertThat(shadowUserManager.hasUserRestriction(
UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES,
UserHandle.of(UserHandle.myUserId()))).isTrue();
assertThat(mSwitchPref.isDisabledByAdmin()).isTrue();
}
}