Add global HTTP proxy to Privacy Settings page

This CL allows the user to see on the Enterprise Privacy Settings
page whether the admin set a global HTTP proxy.

Test: make RunSettingsRoboTests
Bug: 32692748

Change-Id: I3c7c46f806f39c90425fd8e098a749f3cc1e9278
This commit is contained in:
Bartosz Fabianowski
2017-01-11 13:22:52 +01:00
parent fc018e4672
commit 0d22680807
11 changed files with 182 additions and 1 deletions

View File

@@ -8009,6 +8009,8 @@
<string name="enterprise_privacy_always_on_vpn_personal">Always-on VPN turned on in your personal profile</string>
<!-- Label explaining that an always-on VPN was set by the admin in the work profile. [CHAR LIMIT=NONE] -->
<string name="enterprise_privacy_always_on_vpn_work">Always-on VPN turned on in your work profile</string>
<!-- Label explaining that a global HTTP proxy was set by the admin. [CHAR LIMIT=NONE] -->
<string name="enterprise_privacy_global_http_proxy">Global HTTP proxy set</string>
<!-- Preference label for the Photos & Videos storage section. [CHAR LIMIT=50] -->
<string name="storage_photos_videos">Photos &amp; Videos</string>

View File

@@ -68,6 +68,11 @@
android:title="@string/enterprise_privacy_always_on_vpn_work"
settings:allowDividerBelow="true"
settings:multiLine="true"/>
<com.android.settings.DividerPreference
android:key="global_http_proxy"
android:title="@string/enterprise_privacy_global_http_proxy"
settings:allowDividerBelow="true"
settings:multiLine="true"/>
</PreferenceCategory>
<PreferenceCategory android:title="@string/enterprise_privacy_device_access_category">

View File

@@ -58,4 +58,9 @@ public interface EnterprisePrivacyFeatureProvider {
* Returns whether the Profile Owner in the managed profile (if any) set an always-on VPN.
*/
boolean isAlwaysOnVpnSetInManagedProfile();
/**
* Returns whether the Device Owner set a recommended global HTTP proxy.
*/
boolean isGlobalHttpProxySet();
}

View File

@@ -96,4 +96,9 @@ public class EnterprisePrivacyFeatureProviderImpl implements EnterprisePrivacyFe
return managedProfileUserId != -1 &&
VpnUtils.isAlwaysOnVpnSet(mCm, managedProfileUserId);
}
@Override
public boolean isGlobalHttpProxySet() {
return mCm.getGlobalProxy() != null;
}
}

View File

@@ -63,6 +63,7 @@ public class EnterprisePrivacySettings extends DashboardFragment {
controllers.add(new SecurityLogsPreferenceController(context));
controllers.add(new AlwaysOnVpnPrimaryUserPreferenceController(context));
controllers.add(new AlwaysOnVpnManagedProfilePreferenceController(context));
controllers.add(new GlobalHttpProxyPreferenceController(context));
return controllers;
}

View File

@@ -0,0 +1,47 @@
/*
* 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.enterprise;
import android.content.Context;
import android.support.v7.preference.Preference;
import com.android.settings.core.PreferenceController;
import com.android.settings.overlay.FeatureFactory;
public class GlobalHttpProxyPreferenceController extends PreferenceController {
private static final String KEY_GLOBAL_HTTP_PROXY = "global_http_proxy";
private final EnterprisePrivacyFeatureProvider mFeatureProvider;
public GlobalHttpProxyPreferenceController(Context context) {
super(context);
mFeatureProvider = FeatureFactory.getFactory(context)
.getEnterprisePrivacyFeatureProvider(context);
}
@Override
public void updateState(Preference preference) {
preference.setVisible(mFeatureProvider.isGlobalHttpProxySet());
}
@Override
public boolean isAvailable() {
return true;
}
@Override
public String getPreferenceKey() {
return KEY_GLOBAL_HTTP_PROXY;
}
}

View File

@@ -16,6 +16,8 @@
package com.android.settings.vpn2;
import android.net.ProxyInfo;
/**
* This interface replicates a subset of the android.net.ConnectivityManager (CM). The interface
* exists so that we can use a thin wrapper around the CM in production code and a mock in tests.
@@ -30,4 +32,11 @@ public interface ConnectivityManagerWrapper {
* @see android.net.ConnectivityManager#getAlwaysOnVpnPackageForUser
*/
String getAlwaysOnVpnPackageForUser(int userId);
/**
* Calls {@code ConnectivityManager.getGlobalProxy()}.
*
* @see android.net.ConnectivityManager#getGlobalProxy
*/
ProxyInfo getGlobalProxy();
}

View File

@@ -17,6 +17,7 @@
package com.android.settings.vpn2;
import android.net.ConnectivityManager;
import android.net.ProxyInfo;
public class ConnectivityManagerWrapperImpl implements ConnectivityManagerWrapper {
@@ -30,4 +31,9 @@ public class ConnectivityManagerWrapperImpl implements ConnectivityManagerWrappe
public String getAlwaysOnVpnPackageForUser(int userId) {
return mCm.getAlwaysOnVpnPackageForUser(userId);
}
@Override
public ProxyInfo getGlobalProxy() {
return mCm.getGlobalProxy();
}
}

View File

@@ -19,6 +19,7 @@ package com.android.settings.enterprise;
import android.content.ComponentName;
import android.content.pm.PackageManager;
import android.content.pm.UserInfo;
import android.net.ProxyInfo;
import android.os.UserHandle;
import android.os.UserManager;
@@ -146,4 +147,14 @@ public final class EnterprisePrivacyFeatureProviderImplTest {
.thenReturn(VPN_PACKAGE_ID);
assertThat(mProvider.isAlwaysOnVpnSetInManagedProfile()).isTrue();
}
@Test
public void testIsGlobalHttpProxySet() {
when(mConnectivityManger.getGlobalProxy()).thenReturn(null);
assertThat(mProvider.isGlobalHttpProxySet()).isFalse();
when(mConnectivityManger.getGlobalProxy()).thenReturn(
ProxyInfo.buildDirectProxy("localhost", 123));
assertThat(mProvider.isGlobalHttpProxySet()).isTrue();
}
}

View File

@@ -73,7 +73,7 @@ public final class EnterprisePrivacySettingsTest {
final List<PreferenceController> controllers = mSettings.getPreferenceControllers(
ShadowApplication.getInstance().getApplicationContext());
assertThat(controllers).isNotNull();
assertThat(controllers.size()).isEqualTo(6);
assertThat(controllers.size()).isEqualTo(7);
assertThat(controllers.get(0)).isInstanceOf(InstalledPackagesPreferenceController.class);
assertThat(controllers.get(1)).isInstanceOf(NetworkLogsPreferenceController.class);
assertThat(controllers.get(2)).isInstanceOf(BugReportsPreferenceController.class);
@@ -82,5 +82,7 @@ public final class EnterprisePrivacySettingsTest {
AlwaysOnVpnPrimaryUserPreferenceController.class);
assertThat(controllers.get(5)).isInstanceOf(
AlwaysOnVpnManagedProfilePreferenceController.class);
assertThat(controllers.get(6)).isInstanceOf(GlobalHttpProxyPreferenceController.class);
}
}

View File

@@ -0,0 +1,88 @@
/*
* 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.enterprise;
import android.content.Context;
import android.support.v7.preference.Preference;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import com.android.settings.testutils.FakeFeatureFactory;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.annotation.Config;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.when;
/**
* Tests for {@link GlobalHttpProxyPreferenceController}.
*/
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public final class GlobalHttpProxyPreferenceControllerTest {
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private Context mContext;
private FakeFeatureFactory mFeatureFactory;
private GlobalHttpProxyPreferenceController mController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
FakeFeatureFactory.setupForTest(mContext);
mFeatureFactory = (FakeFeatureFactory) FakeFeatureFactory.getFactory(mContext);
mController = new GlobalHttpProxyPreferenceController(mContext);
}
@Test
public void testUpdateState() {
final Preference preference = new Preference(mContext, null, 0, 0);
preference.setVisible(true);
when(mFeatureFactory.enterprisePrivacyFeatureProvider.isGlobalHttpProxySet())
.thenReturn(false);
mController.updateState(preference);
assertThat(preference.isVisible()).isFalse();
when(mFeatureFactory.enterprisePrivacyFeatureProvider.isGlobalHttpProxySet())
.thenReturn(true);
mController.updateState(preference);
assertThat(preference.isVisible()).isTrue();
}
@Test
public void testIsAvailable() {
assertThat(mController.isAvailable()).isTrue();
}
@Test
public void testHandlePreferenceTreeClick() {
assertThat(mController.handlePreferenceTreeClick(new Preference(mContext, null, 0, 0)))
.isFalse();
}
@Test
public void testGetPreferenceKey() {
assertThat(mController.getPreferenceKey()).isEqualTo("global_http_proxy");
}
}