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.
-->
<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
android:key="external_sources_settings_switch" />
<com.android.settingslib.RestrictedSwitchPreference
android:key="external_sources_settings_switch"
android:title="@string/external_source_switch_title" />
<Preference
android:key="external_sources_settings_description"
android:summary="@string/install_all_warning"
android:selectable="false" />
</PreferenceScreen>

View File

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

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