Merge "Disabling switch preference when unknown sources are blocked" into oc-dev

This commit is contained in:
TreeHugger Robot
2017-04-01 04:59:30 +00:00
committed by Android (Google) Code Review
3 changed files with 39 additions and 84 deletions

View File

@@ -14,13 +14,17 @@
limitations under the License. limitations under the License.
--> -->
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:settings="http://schemas.android.com/apk/res/com.android.settings"
android:title="@string/install_other_apps">
<SwitchPreference <com.android.settingslib.RestrictedSwitchPreference
android:key="external_sources_settings_switch" /> android:key="external_sources_settings_switch"
android:title="@string/external_source_switch_title" />
<Preference <Preference
android:key="external_sources_settings_description" android:key="external_sources_settings_description"
android:summary="@string/install_all_warning"
android:selectable="false" /> android:selectable="false" />
</PreferenceScreen> </PreferenceScreen>

View File

@@ -24,27 +24,26 @@ import android.app.AlertDialog;
import android.app.AppOpsManager; import android.app.AppOpsManager;
import android.content.Context; import android.content.Context;
import android.os.Bundle; 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;
import android.support.v7.preference.Preference.OnPreferenceChangeListener; import android.support.v7.preference.Preference.OnPreferenceChangeListener;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.Settings; import com.android.settings.Settings;
import com.android.settings.applications.AppStateInstallAppsBridge.InstallAppsState; import com.android.settings.applications.AppStateInstallAppsBridge.InstallAppsState;
import com.android.settingslib.RestrictedSwitchPreference;
import com.android.settingslib.applications.ApplicationsState.AppEntry; import com.android.settingslib.applications.ApplicationsState.AppEntry;
public class ExternalSourcesDetails extends AppInfoWithHeader public class ExternalSourcesDetails extends AppInfoWithHeader
implements OnPreferenceChangeListener { implements OnPreferenceChangeListener {
private static final String KEY_EXTERNAL_SOURCES_SETTINGS_SWITCH = private static final String KEY_EXTERNAL_SOURCE_SWITCH = "external_sources_settings_switch";
"external_sources_settings_switch";
private static final String KEY_EXTERNAL_SOURCES_SETTINGS_DESC =
"external_sources_settings_description";
private AppStateInstallAppsBridge mAppBridge; private AppStateInstallAppsBridge mAppBridge;
private AppOpsManager mAppOpsManager; private AppOpsManager mAppOpsManager;
private SwitchPreference mSwitchPref; private UserManager mUserManager;
private Preference mExternalSourcesSettingsDesc; private RestrictedSwitchPreference mSwitchPref;
private InstallAppsState mInstallAppsState; private InstallAppsState mInstallAppsState;
@Override @Override
@@ -54,15 +53,10 @@ public class ExternalSourcesDetails extends AppInfoWithHeader
final Context context = getActivity(); final Context context = getActivity();
mAppBridge = new AppStateInstallAppsBridge(context, mState, null); mAppBridge = new AppStateInstallAppsBridge(context, mState, null);
mAppOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE); mAppOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
mUserManager = UserManager.get(context);
addPreferencesFromResource(R.xml.external_sources_details); addPreferencesFromResource(R.xml.external_sources_details);
mSwitchPref = (SwitchPreference) findPreference(KEY_EXTERNAL_SOURCES_SETTINGS_SWITCH); mSwitchPref = (RestrictedSwitchPreference) findPreference(KEY_EXTERNAL_SOURCE_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.setOnPreferenceChangeListener(this); mSwitchPref.setOnPreferenceChangeListener(this);
} }
@@ -84,6 +78,18 @@ public class ExternalSourcesDetails extends AppInfoWithHeader
} }
static CharSequence getPreferenceSummary(Context context, AppEntry entry) { 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; final InstallAppsState appsState;
if (entry.extraInfo instanceof InstallAppsState) { if (entry.extraInfo instanceof InstallAppsState) {
appsState = (InstallAppsState) entry.extraInfo; appsState = (InstallAppsState) entry.extraInfo;
@@ -103,6 +109,17 @@ public class ExternalSourcesDetails extends AppInfoWithHeader
@Override @Override
protected boolean refreshUi() { 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, mInstallAppsState = mAppBridge.createInstallAppsStateFor(mPackageName,
mPackageInfo.applicationInfo.uid); mPackageInfo.applicationInfo.uid);
if (!mInstallAppsState.isPotentialAppSource()) { if (!mInstallAppsState.isPotentialAppSource()) {
@@ -110,8 +127,7 @@ public class ExternalSourcesDetails extends AppInfoWithHeader
mSwitchPref.setEnabled(false); mSwitchPref.setEnabled(false);
return true; return true;
} }
final boolean canInstallApps = mInstallAppsState.canInstallApps(); mSwitchPref.setChecked(mInstallAppsState.canInstallApps());
mSwitchPref.setChecked(canInstallApps);
return true; return true;
} }

View File

@@ -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);
}
}