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
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 6caaf16..dc6be69 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -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>
diff --git a/res/xml/enterprise_privacy_settings.xml b/res/xml/enterprise_privacy_settings.xml
index 856706e..0358911 100644
--- a/res/xml/enterprise_privacy_settings.xml
+++ b/res/xml/enterprise_privacy_settings.xml
@@ -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">
diff --git a/src/com/android/settings/enterprise/EnterprisePrivacyFeatureProvider.java b/src/com/android/settings/enterprise/EnterprisePrivacyFeatureProvider.java
index dec2d80..43f6903 100644
--- a/src/com/android/settings/enterprise/EnterprisePrivacyFeatureProvider.java
+++ b/src/com/android/settings/enterprise/EnterprisePrivacyFeatureProvider.java
@@ -58,4 +58,9 @@
      * 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();
 }
diff --git a/src/com/android/settings/enterprise/EnterprisePrivacyFeatureProviderImpl.java b/src/com/android/settings/enterprise/EnterprisePrivacyFeatureProviderImpl.java
index a742cc3..9fbb083 100644
--- a/src/com/android/settings/enterprise/EnterprisePrivacyFeatureProviderImpl.java
+++ b/src/com/android/settings/enterprise/EnterprisePrivacyFeatureProviderImpl.java
@@ -96,4 +96,9 @@
         return managedProfileUserId != -1 &&
                 VpnUtils.isAlwaysOnVpnSet(mCm, managedProfileUserId);
     }
+
+    @Override
+    public boolean isGlobalHttpProxySet() {
+        return mCm.getGlobalProxy() != null;
+    }
 }
diff --git a/src/com/android/settings/enterprise/EnterprisePrivacySettings.java b/src/com/android/settings/enterprise/EnterprisePrivacySettings.java
index 478f7fe..8f1f22b 100644
--- a/src/com/android/settings/enterprise/EnterprisePrivacySettings.java
+++ b/src/com/android/settings/enterprise/EnterprisePrivacySettings.java
@@ -63,6 +63,7 @@
         controllers.add(new SecurityLogsPreferenceController(context));
         controllers.add(new AlwaysOnVpnPrimaryUserPreferenceController(context));
         controllers.add(new AlwaysOnVpnManagedProfilePreferenceController(context));
+        controllers.add(new GlobalHttpProxyPreferenceController(context));
         return controllers;
     }
 
diff --git a/src/com/android/settings/enterprise/GlobalHttpProxyPreferenceController.java b/src/com/android/settings/enterprise/GlobalHttpProxyPreferenceController.java
new file mode 100644
index 0000000..e2f2ab9
--- /dev/null
+++ b/src/com/android/settings/enterprise/GlobalHttpProxyPreferenceController.java
@@ -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;
+    }
+}
diff --git a/src/com/android/settings/vpn2/ConnectivityManagerWrapper.java b/src/com/android/settings/vpn2/ConnectivityManagerWrapper.java
index 938db50..9424278 100644
--- a/src/com/android/settings/vpn2/ConnectivityManagerWrapper.java
+++ b/src/com/android/settings/vpn2/ConnectivityManagerWrapper.java
@@ -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 @@
      * @see android.net.ConnectivityManager#getAlwaysOnVpnPackageForUser
      */
    String getAlwaysOnVpnPackageForUser(int userId);
+
+    /**
+     * Calls {@code ConnectivityManager.getGlobalProxy()}.
+     *
+     * @see android.net.ConnectivityManager#getGlobalProxy
+     */
+   ProxyInfo getGlobalProxy();
 }
diff --git a/src/com/android/settings/vpn2/ConnectivityManagerWrapperImpl.java b/src/com/android/settings/vpn2/ConnectivityManagerWrapperImpl.java
index ad1b4eb..d3c17f2 100644
--- a/src/com/android/settings/vpn2/ConnectivityManagerWrapperImpl.java
+++ b/src/com/android/settings/vpn2/ConnectivityManagerWrapperImpl.java
@@ -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 String getAlwaysOnVpnPackageForUser(int userId) {
         return mCm.getAlwaysOnVpnPackageForUser(userId);
     }
+
+    @Override
+    public ProxyInfo getGlobalProxy() {
+        return mCm.getGlobalProxy();
+    }
 }
diff --git a/tests/robotests/src/com/android/settings/enterprise/EnterprisePrivacyFeatureProviderImplTest.java b/tests/robotests/src/com/android/settings/enterprise/EnterprisePrivacyFeatureProviderImplTest.java
index 3fc6f7c..be1296c 100644
--- a/tests/robotests/src/com/android/settings/enterprise/EnterprisePrivacyFeatureProviderImplTest.java
+++ b/tests/robotests/src/com/android/settings/enterprise/EnterprisePrivacyFeatureProviderImplTest.java
@@ -19,6 +19,7 @@
 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 @@
                 .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();
+    }
 }
diff --git a/tests/robotests/src/com/android/settings/enterprise/EnterprisePrivacySettingsTest.java b/tests/robotests/src/com/android/settings/enterprise/EnterprisePrivacySettingsTest.java
index 4d86e61..40222ef 100644
--- a/tests/robotests/src/com/android/settings/enterprise/EnterprisePrivacySettingsTest.java
+++ b/tests/robotests/src/com/android/settings/enterprise/EnterprisePrivacySettingsTest.java
@@ -73,7 +73,7 @@
         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 @@
                 AlwaysOnVpnPrimaryUserPreferenceController.class);
         assertThat(controllers.get(5)).isInstanceOf(
                 AlwaysOnVpnManagedProfilePreferenceController.class);
+        assertThat(controllers.get(6)).isInstanceOf(GlobalHttpProxyPreferenceController.class);
+
     }
 }
diff --git a/tests/robotests/src/com/android/settings/enterprise/GlobalHttpProxyPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/enterprise/GlobalHttpProxyPreferenceControllerTest.java
new file mode 100644
index 0000000..d505bad
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/enterprise/GlobalHttpProxyPreferenceControllerTest.java
@@ -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");
+    }
+}