From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Pratyush <codelab@pratyush.dev>
Date: Wed, 20 Oct 2021 05:40:11 +0530
Subject: [PATCH] add toggle to set captiveportal settings

Access is disallowed for users that are disallowed from configuring
Private DNS settings. This restriction provides administrative control
over this feature in a way that's compatible with existing device
manager implementations. Private DNS is the closest fit from the
existing user management restrictions that are available.

Co-authored-by: stuux <stuux@posteo.net>
Co-authored-by: Renlord <me@renlord.com>
Co-authored-by: Danny Lin <danny@kdrag0n.dev>
Co-authored-by: anupritaisno1 <www.anuprita804@gmail.com>
Signed-off-by: empratyush <codelab@pratyush.dev>
[tad@spotco.us]: removed the option for GrapheneOS servers
Change-Id: I8a762d0f29ac42fce3dcfc7189e8ff216a8f8d1a
---
 res/values/arrays.xml                         |  10 +
 res/values/strings.xml                        |   3 +
 ...ConnectivityCheckPreferenceController.java | 176 ++++++++++++++++++
 .../network/NetworkDashboardFragment.java     |   3 +
 4 files changed, 192 insertions(+)
 create mode 100644 src/com/android/settings/network/ConnectivityCheckPreferenceController.java

diff --git a/res/values/arrays.xml b/res/values/arrays.xml
index cfc7671652..7fac175770 100644
--- a/res/values/arrays.xml
+++ b/res/values/arrays.xml
@@ -210,6 +210,16 @@
         <item>@string/dark_ui_auto_mode_custom_bedtime</item>
     </string-array>
 
+    <string-array name="connectivity_check_entries">
+        <item>Standard (Google)</item>
+        <item>Disabled</item>
+    </string-array>
+
+    <string-array name="connectivity_check_values" translatable="false">
+        <item>0</item>
+        <item>1</item>
+    </string-array>
+
     <!-- Security settings.  The delay after screen is turned off until device locks.
          These are shown in a list dialog. -->
     <string-array name="lock_after_timeout_entries">
diff --git a/res/values/strings.xml b/res/values/strings.xml
index d9f4a37f2d..bba0900122 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -14254,4 +14254,7 @@
     <!-- [CHAR LIMIT=NONE] Hint for QR code process failure -->
     <string name="bt_le_audio_qr_code_is_not_valid_format">QR code isn\u0027t a valid format</string>
 
+    <string name="connectivity_check_title">Internet connectivity check</string>
+    <string name="connectivity_check_summary">HTTP endpoints to use for performing internet connectivity checks.</string>
+
 </resources>
diff --git a/src/com/android/settings/network/ConnectivityCheckPreferenceController.java b/src/com/android/settings/network/ConnectivityCheckPreferenceController.java
new file mode 100644
index 0000000000..bf16eb825a
--- /dev/null
+++ b/src/com/android/settings/network/ConnectivityCheckPreferenceController.java
@@ -0,0 +1,176 @@
+/*
+ * Copyright (C) 2020 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.network;
+
+import android.content.ContentResolver;
+import android.content.Context;
+import android.content.res.Resources;
+import android.database.ContentObserver;
+import android.net.LinkProperties;
+import android.net.Network;
+import android.os.Handler;
+import android.os.Looper;
+import android.os.UserHandle;
+import android.os.UserManager;
+import android.provider.Settings;
+import androidx.preference.ListPreference;
+import androidx.preference.Preference;
+import androidx.preference.PreferenceScreen;
+import com.android.internal.util.ArrayUtils;
+import com.android.settings.R;
+import com.android.settings.core.BasePreferenceController;
+import com.android.settings.core.PreferenceControllerMixin;
+import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
+import com.android.settingslib.RestrictedLockUtilsInternal;
+import com.android.settingslib.core.lifecycle.events.OnResume;
+
+public class ConnectivityCheckPreferenceController
+        extends BasePreferenceController
+        implements PreferenceControllerMixin, Preference.OnPreferenceChangeListener,
+        OnResume {
+
+    // imported defaults from AOSP NetworkStack
+    private static final String STANDARD_HTTPS_URL =
+            "https://www.google.com/generate_204";
+    private static final String STANDARD_HTTP_URL =
+            "http://connectivitycheck.gstatic.com/generate_204";
+    private static final String STANDARD_FALLBACK_URL =
+            "http://www.google.com/gen_204";
+    private static final String STANDARD_OTHER_FALLBACK_URLS =
+            "http://play.googleapis.com/generate_204";
+
+    private static final int STANDARD_CAPTIVE_PORTAL_HTTP_URL_INTVAL = 0;
+    private static final int DISABLED_CAPTIVE_PORTAL_INTVAL = 1;
+
+    private static final String KEY_CONNECTIVITY_CHECK_SETTINGS =
+            "connectivity_check_settings";
+
+    private ListPreference mConnectivityPreference;
+
+    public ConnectivityCheckPreferenceController(Context context) {
+        super(context, KEY_CONNECTIVITY_CHECK_SETTINGS);
+    }
+
+    @Override
+    public int getAvailabilityStatus() {
+        if (isDisabledByAdmin()) {
+            return BasePreferenceController.DISABLED_FOR_USER;
+        }
+        return BasePreferenceController.AVAILABLE;
+    }
+
+    @Override
+    public void displayPreference(PreferenceScreen screen) {
+        ListPreference captiveList = new ListPreference(screen.getContext());
+        captiveList.setKey(KEY_CONNECTIVITY_CHECK_SETTINGS);
+        captiveList.setOrder(30);
+        captiveList.setIcon(R.drawable.ic_settings_language);
+        captiveList.setTitle(R.string.connectivity_check_title);
+        captiveList.setSummary(R.string.connectivity_check_summary);
+        captiveList.setEntries(R.array.connectivity_check_entries);
+        captiveList.setEntryValues(R.array.connectivity_check_values);
+
+        if (mConnectivityPreference == null) {
+            screen.addPreference(captiveList);
+            mConnectivityPreference = captiveList;
+        }
+        super.displayPreference(screen);
+        updatePreferenceState();
+    }
+
+    @Override
+    public String getPreferenceKey() {
+        return KEY_CONNECTIVITY_CHECK_SETTINGS;
+    }
+
+    private void updatePreferenceState() {
+        if (Settings.Global.getInt(mContext.getContentResolver(),
+                Settings.Global.CAPTIVE_PORTAL_MODE, Settings.Global.CAPTIVE_PORTAL_MODE_PROMPT)
+                == Settings.Global.CAPTIVE_PORTAL_MODE_IGNORE) {
+            mConnectivityPreference.setValueIndex(DISABLED_CAPTIVE_PORTAL_INTVAL);
+            return;
+        }
+
+        String pref = Settings.Global.getString(
+                mContext.getContentResolver(), Settings.Global.CAPTIVE_PORTAL_HTTP_URL);
+        if (STANDARD_HTTP_URL.equals(pref)) {
+            mConnectivityPreference.setValueIndex(
+                    STANDARD_CAPTIVE_PORTAL_HTTP_URL_INTVAL);
+        }
+    }
+
+    @Override
+    public void onResume() {
+        updatePreferenceState();
+        if (mConnectivityPreference != null) {
+            setCaptivePortalURLs(
+                    mContext.getContentResolver(),
+                    Integer.parseInt(mConnectivityPreference.getValue()));
+        }
+    }
+
+    private void setCaptivePortalURLs(ContentResolver cr, int mode) {
+        switch (mode) {
+            case STANDARD_CAPTIVE_PORTAL_HTTP_URL_INTVAL:
+                Settings.Global.putString(cr, Settings.Global.CAPTIVE_PORTAL_HTTP_URL,
+                        STANDARD_HTTP_URL);
+                Settings.Global.putString(cr, Settings.Global.CAPTIVE_PORTAL_HTTPS_URL,
+                        STANDARD_HTTPS_URL);
+                Settings.Global.putString(cr, Settings.Global.CAPTIVE_PORTAL_FALLBACK_URL,
+                        STANDARD_FALLBACK_URL);
+                Settings.Global.putString(
+                        cr, Settings.Global.CAPTIVE_PORTAL_OTHER_FALLBACK_URLS,
+                        STANDARD_OTHER_FALLBACK_URLS);
+                Settings.Global.putInt(cr, Settings.Global.CAPTIVE_PORTAL_MODE,
+                        Settings.Global.CAPTIVE_PORTAL_MODE_PROMPT);
+                break;
+            default:
+                // Default URLs as placeholder
+                Settings.Global.putString(cr, Settings.Global.CAPTIVE_PORTAL_HTTP_URL,
+                        STANDARD_HTTP_URL);
+                Settings.Global.putString(cr, Settings.Global.CAPTIVE_PORTAL_HTTPS_URL,
+                        STANDARD_HTTPS_URL);
+                Settings.Global.putString(cr, Settings.Global.CAPTIVE_PORTAL_FALLBACK_URL,
+                        STANDARD_FALLBACK_URL);
+                Settings.Global.putString(
+                        cr, Settings.Global.CAPTIVE_PORTAL_OTHER_FALLBACK_URLS,
+                        STANDARD_OTHER_FALLBACK_URLS);
+                Settings.Global.putInt(cr, Settings.Global.CAPTIVE_PORTAL_MODE,
+                        Settings.Global.CAPTIVE_PORTAL_MODE_IGNORE);
+        }
+    }
+
+    @Override
+    public boolean onPreferenceChange(Preference preference, Object value) {
+        final String key = preference.getKey();
+        if (KEY_CONNECTIVITY_CHECK_SETTINGS.equals(key)) {
+            setCaptivePortalURLs(mContext.getContentResolver(),
+                    Integer.parseInt((String)value));
+            return true;
+        } else {
+            return false;
+        }
+    }
+
+    private EnforcedAdmin getEnforcedAdmin() {
+        return RestrictedLockUtilsInternal.checkIfRestrictionEnforced(
+                mContext, UserManager.DISALLOW_CONFIG_PRIVATE_DNS,
+                UserHandle.myUserId());
+    }
+
+    private boolean isDisabledByAdmin() { return getEnforcedAdmin() != null; }
+}
diff --git a/src/com/android/settings/network/NetworkDashboardFragment.java b/src/com/android/settings/network/NetworkDashboardFragment.java
index 0cd24dc2a2..31295e9f83 100644
--- a/src/com/android/settings/network/NetworkDashboardFragment.java
+++ b/src/com/android/settings/network/NetworkDashboardFragment.java
@@ -121,6 +121,9 @@ public class NetworkDashboardFragment extends DashboardFragment implements
         }
         controllers.add(privateDnsPreferenceController);
         controllers.add(new NetworkProviderCallsSmsController(context, lifecycle));
+        ConnectivityCheckPreferenceController connectivityCheck =
+                new ConnectivityCheckPreferenceController(context);
+        controllers.add(connectivityCheck);
         return controllers;
     }
 
