From 6221a5b8d2138e564fa297e62d5ca9a05f43f824 Mon Sep 17 00:00:00 2001 From: Suprabh Shukla Date: Mon, 13 Mar 2017 15:11:08 -0700 Subject: [PATCH] Disabling switch preference when unknown sources are blocked When the user restriction DISALLOW_INSTALL_UNKNOWN_SOURCES is set, changing the preferences in settings is meaningless as package installer will not let the install proceed. Disabling the settings switch and updated the summaries to reflect when a switch is disabled. Removing robolectric test as the logic is no longer testable due to missing stubs for UserManager and UserHandle. Test: CtsVerifier -> Device Owner Tests -> Policy Transparency Test Bug: 34688413 Change-Id: I7cf6cc09a5d29721186a0df9405d3c7e486295ee Merged-In: I7cf6cc09a5d29721186a0df9405d3c7e486295ee --- res/xml/external_sources_details.xml | 10 ++- .../applications/ExternalSourcesDetails.java | 48 +++++++++----- .../ExternalSourcesDetailsTest.java | 65 ------------------- 3 files changed, 39 insertions(+), 84 deletions(-) delete mode 100644 tests/robotests/src/com/android/settings/applications/ExternalSourcesDetailsTest.java diff --git a/res/xml/external_sources_details.xml b/res/xml/external_sources_details.xml index fb443f49977..6bc73905c2d 100644 --- a/res/xml/external_sources_details.xml +++ b/res/xml/external_sources_details.xml @@ -14,13 +14,17 @@ limitations under the License. --> - + - + diff --git a/src/com/android/settings/applications/ExternalSourcesDetails.java b/src/com/android/settings/applications/ExternalSourcesDetails.java index 6441437ea9c..fe51a87ab77 100644 --- a/src/com/android/settings/applications/ExternalSourcesDetails.java +++ b/src/com/android/settings/applications/ExternalSourcesDetails.java @@ -24,27 +24,26 @@ import android.app.AlertDialog; import android.app.AppOpsManager; import android.content.Context; import android.os.Bundle; -import android.support.v14.preference.SwitchPreference; +import android.os.UserHandle; +import android.os.UserManager; import android.support.v7.preference.Preference; import android.support.v7.preference.Preference.OnPreferenceChangeListener; import com.android.settings.R; import com.android.settings.Settings; import com.android.settings.applications.AppStateInstallAppsBridge.InstallAppsState; +import com.android.settingslib.RestrictedSwitchPreference; import com.android.settingslib.applications.ApplicationsState.AppEntry; public class ExternalSourcesDetails extends AppInfoWithHeader implements OnPreferenceChangeListener { - private static final String KEY_EXTERNAL_SOURCES_SETTINGS_SWITCH = - "external_sources_settings_switch"; - private static final String KEY_EXTERNAL_SOURCES_SETTINGS_DESC = - "external_sources_settings_description"; + private static final String KEY_EXTERNAL_SOURCE_SWITCH = "external_sources_settings_switch"; private AppStateInstallAppsBridge mAppBridge; private AppOpsManager mAppOpsManager; - private SwitchPreference mSwitchPref; - private Preference mExternalSourcesSettingsDesc; + private UserManager mUserManager; + private RestrictedSwitchPreference mSwitchPref; private InstallAppsState mInstallAppsState; @Override @@ -54,15 +53,10 @@ public class ExternalSourcesDetails extends AppInfoWithHeader final Context context = getActivity(); mAppBridge = new AppStateInstallAppsBridge(context, mState, null); mAppOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE); + mUserManager = UserManager.get(context); addPreferencesFromResource(R.xml.external_sources_details); - mSwitchPref = (SwitchPreference) findPreference(KEY_EXTERNAL_SOURCES_SETTINGS_SWITCH); - mExternalSourcesSettingsDesc = findPreference(KEY_EXTERNAL_SOURCES_SETTINGS_DESC); - - getPreferenceScreen().setTitle(R.string.install_other_apps); - mSwitchPref.setTitle(R.string.external_source_switch_title); - mExternalSourcesSettingsDesc.setSummary(R.string.install_all_warning); - + mSwitchPref = (RestrictedSwitchPreference) findPreference(KEY_EXTERNAL_SOURCE_SWITCH); mSwitchPref.setOnPreferenceChangeListener(this); } @@ -84,6 +78,18 @@ public class ExternalSourcesDetails extends AppInfoWithHeader } static CharSequence getPreferenceSummary(Context context, AppEntry entry) { + 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); + } + final InstallAppsState appsState; if (entry.extraInfo instanceof InstallAppsState) { appsState = (InstallAppsState) entry.extraInfo; @@ -103,6 +109,17 @@ public class ExternalSourcesDetails extends AppInfoWithHeader @Override protected boolean refreshUi() { + if (mUserManager.hasBaseUserRestriction(UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES, + UserHandle.of(UserHandle.myUserId()))) { + mSwitchPref.setChecked(false); + mSwitchPref.setSummary(R.string.disabled); + mSwitchPref.setEnabled(false); + return true; + } + mSwitchPref.checkRestrictionAndSetDisabled(UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES); + if (mSwitchPref.isDisabledByAdmin()) { + return true; + } mInstallAppsState = mAppBridge.createInstallAppsStateFor(mPackageName, mPackageInfo.applicationInfo.uid); if (!mInstallAppsState.isPotentialAppSource()) { @@ -110,8 +127,7 @@ public class ExternalSourcesDetails extends AppInfoWithHeader mSwitchPref.setEnabled(false); return true; } - final boolean canInstallApps = mInstallAppsState.canInstallApps(); - mSwitchPref.setChecked(canInstallApps); + mSwitchPref.setChecked(mInstallAppsState.canInstallApps()); return true; } diff --git a/tests/robotests/src/com/android/settings/applications/ExternalSourcesDetailsTest.java b/tests/robotests/src/com/android/settings/applications/ExternalSourcesDetailsTest.java deleted file mode 100644 index 5b2f8e07272..00000000000 --- a/tests/robotests/src/com/android/settings/applications/ExternalSourcesDetailsTest.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Copyright (C) 2017 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License - */ - -package com.android.settings.applications; - -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -import android.content.Context; - -import com.android.settings.R; -import com.android.settings.SettingsRobolectricTestRunner; -import com.android.settings.TestConfig; -import com.android.settingslib.applications.ApplicationsState; - -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; -import org.robolectric.annotation.Config; - -@RunWith(SettingsRobolectricTestRunner.class) -@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) -public class ExternalSourcesDetailsTest { - - @Mock - private Context mContext; - @Mock - private AppStateInstallAppsBridge.InstallAppsState mInstallAppsStateAllowed; - @Mock - private AppStateInstallAppsBridge.InstallAppsState mInstallAppsStateBlocked; - - @Before - public void setUp() { - MockitoAnnotations.initMocks(this); - when(mInstallAppsStateAllowed.canInstallApps()).thenReturn(true); - when(mInstallAppsStateBlocked.canInstallApps()).thenReturn(false); - } - - @Test - public void testGetPreferenceSummary() { - ApplicationsState.AppEntry appEntry = mock(ApplicationsState.AppEntry.class); - appEntry.extraInfo = mInstallAppsStateBlocked; - ExternalSourcesDetails.getPreferenceSummary(mContext, appEntry); - verify(mContext).getString(R.string.external_source_untrusted); - appEntry.extraInfo = mInstallAppsStateAllowed; - ExternalSourcesDetails.getPreferenceSummary(mContext, appEntry); - verify(mContext).getString(R.string.external_source_trusted); - } -}