/* * Copyright (C) 2022 The Android Open Source Project * * Attribute: AXP GOLDEN ConnectivityCheckPreferenceController (Settings.apk), * cloned for LOS 23.2 without axp.os / captiveportal.axpos.org endpoints. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. */ package com.android.settings.network; import android.content.ContentResolver; import android.content.Context; import android.provider.Settings; import androidx.preference.ListPreference; import androidx.preference.Preference; import androidx.preference.PreferenceScreen; import com.android.settings.R; import com.android.settings.core.BasePreferenceController; import com.android.settings.core.PreferenceControllerMixin; import com.android.settingslib.RestrictedLockUtils; import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin; import com.android.settingslib.RestrictedLockUtilsInternal; import com.android.settingslib.core.lifecycle.events.OnResume; import android.os.UserHandle; import android.os.UserManager; /** Internet connectivity check provider list (captive portal URLs). */ public class ConnectivityCheckPreferenceController extends BasePreferenceController implements PreferenceControllerMixin, Preference.OnPreferenceChangeListener, OnResume { private static final String KEY = "connectivity_check_settings"; private static final int DISABLED = 0; private static final int AMAZON = 1; private static final int CLOUDFLARE = 2; private static final int GOOGLE = 3; private static final int HUAWEI = 4; private static final int KUKETZ = 5; private static final int MICROSOFT = 6; private static final int OPENSUSE = 7; private static final int UBUNTU = 8; private ListPreference mConnectivityPreference; public ConnectivityCheckPreferenceController(Context context) { super(context, KEY); } @Override public int getAvailabilityStatus() { return isDisabledByAdmin() ? DISABLED_FOR_USER : AVAILABLE; } @Override public String getPreferenceKey() { return KEY; } @Override public void displayPreference(PreferenceScreen screen) { ListPreference pref = new ListPreference(screen.getContext()); pref.setKey(KEY); pref.setOrder(30); pref.setIcon(R.drawable.ic_settings_language); pref.setTitle(R.string.connectivity_check_title); pref.setSummary(R.string.connectivity_check_summary); pref.setEntries(R.array.connectivity_check_entries); pref.setEntryValues(R.array.connectivity_check_values); pref.setOnPreferenceChangeListener(this); if (mConnectivityPreference == null) { screen.addPreference(pref); mConnectivityPreference = pref; } super.displayPreference(screen); updatePreferenceState(); } private void updatePreferenceState() { if (mConnectivityPreference == null) { return; } ContentResolver cr = mContext.getContentResolver(); int mode = Settings.Global.getInt(cr, Settings.Global.CAPTIVE_PORTAL_MODE, 1); if (mode == 0) { mConnectivityPreference.setValueIndex(DISABLED); return; } String http = Settings.Global.getString(cr, Settings.Global.CAPTIVE_PORTAL_HTTP_URL); int idx = GOOGLE; if ("http://fireoscaptiveportal.com/generate_204".equals(http)) { idx = AMAZON; } else if ("http://cp.cloudflare.com".equals(http)) { idx = CLOUDFLARE; } else if ("http://connectivitycheck.gstatic.com/generate_204".equals(http)) { idx = GOOGLE; } else if ("http://connectivitycheck.platform.hicloud.com/generate_204".equals(http)) { idx = HUAWEI; } else if ("http://captiveportal.kuketz.de".equals(http)) { idx = KUKETZ; } else if ("http://edge-http.microsoft.com/captiveportal/generate_204".equals(http)) { idx = MICROSOFT; } else if ("http://conncheck.opensuse.org".equals(http)) { idx = OPENSUSE; } else if ("http://connectivity-check.ubuntu.com".equals(http)) { idx = UBUNTU; } mConnectivityPreference.setValueIndex(idx); } private void setCaptivePortalURLs(ContentResolver cr, int value) { final String httpKey = Settings.Global.CAPTIVE_PORTAL_HTTP_URL; final String httpsKey = Settings.Global.CAPTIVE_PORTAL_HTTPS_URL; final String fallbackKey = Settings.Global.CAPTIVE_PORTAL_FALLBACK_URL; final String otherKey = Settings.Global.CAPTIVE_PORTAL_OTHER_FALLBACK_URLS; final String modeKey = Settings.Global.CAPTIVE_PORTAL_MODE; String http; String https; String fallback; String other; int mode = 1; switch (value) { case DISABLED: // Match GOLDEN: write Google URLs then mode=0 http = "http://connectivitycheck.gstatic.com/generate_204"; https = "https://www.google.com/generate_204"; fallback = "http://www.google.com/gen_204"; other = "http://play.googleapis.com/generate_204"; mode = 0; break; case AMAZON: http = "http://fireoscaptiveportal.com/generate_204"; https = "https://fireoscaptiveportal.com/generate_204"; fallback = http; other = http; break; case CLOUDFLARE: http = "http://cp.cloudflare.com"; https = "https://cp.cloudflare.com"; fallback = http; other = http; break; case HUAWEI: http = "http://connectivitycheck.platform.hicloud.com/generate_204"; https = "https://connectivitycheck.platform.hicloud.com/generate_204"; fallback = http; other = http; break; case KUKETZ: http = "http://captiveportal.kuketz.de"; https = "https://captiveportal.kuketz.de"; fallback = http; other = http; break; case MICROSOFT: http = "http://edge-http.microsoft.com/captiveportal/generate_204"; https = http; fallback = http; other = http; break; case OPENSUSE: http = "http://conncheck.opensuse.org"; https = "https://conncheck.opensuse.org"; fallback = http; other = http; break; case UBUNTU: http = "http://connectivity-check.ubuntu.com"; https = "https://connectivity-check.ubuntu.com"; fallback = http; other = http; break; case GOOGLE: default: http = "http://connectivitycheck.gstatic.com/generate_204"; https = "https://www.google.com/generate_204"; fallback = "http://www.google.com/gen_204"; other = "http://play.googleapis.com/generate_204"; break; } Settings.Global.putString(cr, httpKey, http); Settings.Global.putString(cr, httpsKey, https); Settings.Global.putString(cr, fallbackKey, fallback); Settings.Global.putString(cr, otherKey, other); Settings.Global.putInt(cr, modeKey, mode); } @Override public boolean onPreferenceChange(Preference preference, Object newValue) { if (KEY.equals(preference.getKey())) { setCaptivePortalURLs( mContext.getContentResolver(), Integer.parseInt((String) newValue)); return true; } return false; } @Override public void onResume() { updatePreferenceState(); if (mConnectivityPreference != null && mConnectivityPreference.getValue() != null) { setCaptivePortalURLs( mContext.getContentResolver(), Integer.parseInt(mConnectivityPreference.getValue())); } } private EnforcedAdmin getEnforcedAdmin() { return RestrictedLockUtilsInternal.checkIfRestrictionEnforced( mContext, UserManager.DISALLOW_CONFIG_PRIVATE_DNS, UserHandle.myUserId()); } private boolean isDisabledByAdmin() { return getEnforcedAdmin() != null; } }