Merge "Add Wi-Fi and adaptive mobile network (5G PM) toggle event in Adaptive Connectivity UX" into main

This commit is contained in:
Mir Noshin Jahan
2025-03-16 23:18:27 -07:00
committed by Android (Google) Code Review
7 changed files with 533 additions and 25 deletions

View File

@@ -37,6 +37,10 @@ class AdaptiveConnectivityScreen : PreferenceScreenCreator {
override fun getPreferenceHierarchy(context: Context) = preferenceHierarchy(context, this) {
+AdaptiveConnectivityTogglePreference()
if (Flags.enableNestedToggleSwitches()) {
+WifiScorerTogglePreference()
+AdaptiveMobileNetworkTogglePreference()
}
}
override fun hasCompleteHierarchy() = false
@@ -44,4 +48,4 @@ class AdaptiveConnectivityScreen : PreferenceScreenCreator {
companion object {
const val KEY = "adaptive_connectivity"
}
}
}

View File

@@ -15,9 +15,14 @@
*/
package com.android.settings.network;
import static android.provider.Settings.Secure.ADAPTIVE_CONNECTIVITY_MOBILE_NETWORK_ENABLED;
import static android.provider.Settings.Secure.ADAPTIVE_CONNECTIVITY_WIFI_ENABLED;
import android.app.settings.SettingsEnums;
import android.content.Context;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
@@ -31,12 +36,7 @@ import com.android.settingslib.search.SearchIndexable;
/** Adaptive connectivity is a feature which automatically manages network connections. */
@SearchIndexable
public class AdaptiveConnectivitySettings extends DashboardFragment {
private static final String TAG = "AdaptiveConnectivitySettings";
protected static final String ADAPTIVE_CONNECTIVITY_WIFI_ENABLED =
"adaptive_connectivity_wifi_enabled";
protected static final String ADAPTIVE_CONNECTIVITY_MOBILE_NETWORK_ENABLED =
"adaptive_connectivity_mobile_network_enabled";
@Override
public int getMetricsCategory() {
@@ -65,16 +65,25 @@ public class AdaptiveConnectivitySettings extends DashboardFragment {
public void onCreatePreferences(@NonNull Bundle savedInstanceState, @NonNull String rootKey) {
Log.i("Settings", "onCreatePreferences");
super.onCreatePreferences(savedInstanceState, rootKey);
if (Flags.enableNestedToggleSwitches()) {
setSwitchVisibility(ADAPTIVE_CONNECTIVITY_WIFI_ENABLED, true);
setSwitchVisibility(ADAPTIVE_CONNECTIVITY_MOBILE_NETWORK_ENABLED, true);
if (Flags.enableNestedToggleSwitches() && !isCatalystEnabled()) {
setupSwitchPreferenceCompat(ADAPTIVE_CONNECTIVITY_WIFI_ENABLED);
setupSwitchPreferenceCompat(ADAPTIVE_CONNECTIVITY_MOBILE_NETWORK_ENABLED);
}
}
private void setSwitchVisibility(String key, boolean isVisible) {
private void setupSwitchPreferenceCompat(String key) {
SwitchPreferenceCompat switchPreference = findPreference(key);
if (switchPreference != null) {
switchPreference.setVisible(isVisible);
switchPreference.setOnPreferenceChangeListener(
(preference, newValue) -> {
boolean isChecked = (Boolean) newValue;
Settings.Secure.putInt(getContentResolver(), key, isChecked ? 1 : 0);
if (preference.getKey().equals(ADAPTIVE_CONNECTIVITY_WIFI_ENABLED)) {
getSystemService(WifiManager.class).setWifiScoringEnabled(isChecked);
}
return true;
});
switchPreference.setVisible(true);
}
}
}

View File

@@ -0,0 +1,89 @@
/*
* Copyright (C) 2025 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.app.settings.SettingsEnums.ACTION_ADAPTIVE_CONNECTIVITY
import android.content.Context
import android.provider.Settings.Secure.ADAPTIVE_CONNECTIVITY_MOBILE_NETWORK_ENABLED
import com.android.settings.R
import com.android.settings.contract.KEY_ADAPTIVE_CONNECTIVITY
import com.android.settings.metrics.PreferenceActionMetricsProvider
import com.android.settingslib.datastore.KeyValueStore
import com.android.settingslib.datastore.KeyValueStoreDelegate
import com.android.settingslib.datastore.SettingsSecureStore
import com.android.settingslib.datastore.SettingsStore
import com.android.settingslib.metadata.ReadWritePermit
import com.android.settingslib.metadata.SensitivityLevel
import com.android.settingslib.metadata.SwitchPreference
class AdaptiveMobileNetworkTogglePreference() :
SwitchPreference(
KEY,
R.string.adaptive_connectivity_mobile_network_switch_title,
),
PreferenceActionMetricsProvider {
override val preferenceActionMetrics: Int
get() = ACTION_ADAPTIVE_CONNECTIVITY
override val key: String
get() = KEY
override fun tags(context: Context) = arrayOf(KEY_ADAPTIVE_CONNECTIVITY)
override fun storage(context: Context): KeyValueStore =
AdaptiveMobileNetworkToggleStorage(context)
override fun getReadPermissions(context: Context) = SettingsSecureStore.getReadPermissions()
override fun getWritePermissions(context: Context) = SettingsSecureStore.getWritePermissions()
override fun getReadPermit(context: Context, callingPid: Int, callingUid: Int) =
ReadWritePermit.ALLOW
override fun getWritePermit(
context: Context,
value: Boolean?,
callingPid: Int,
callingUid: Int,
) = ReadWritePermit.ALLOW
override val sensitivityLevel
get() = SensitivityLevel.NO_SENSITIVITY
@Suppress("UNCHECKED_CAST")
private class AdaptiveMobileNetworkToggleStorage(
private val context: Context,
private val settingsStore: SettingsStore = SettingsSecureStore.get(context),
) : KeyValueStoreDelegate {
override val keyValueStoreDelegate
get() = settingsStore
override fun <T : Any> getDefaultValue(key: String, valueType: Class<T>) =
DEFAULT_VALUE as T
override fun <T : Any> setValue(key: String, valueType: Class<T>, value: T?) {
settingsStore.setValue(key, valueType, value)
}
}
companion object {
const val KEY = ADAPTIVE_CONNECTIVITY_MOBILE_NETWORK_ENABLED
const val DEFAULT_VALUE = true
}
}

View File

@@ -0,0 +1,101 @@
/*
* Copyright (C) 2025 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.Manifest
import android.app.settings.SettingsEnums.ACTION_ADAPTIVE_CONNECTIVITY
import android.content.Context
import android.net.wifi.WifiManager
import android.provider.Settings.Secure.ADAPTIVE_CONNECTIVITY_WIFI_ENABLED
import androidx.annotation.RequiresPermission
import com.android.settings.R
import com.android.settings.contract.KEY_ADAPTIVE_CONNECTIVITY
import com.android.settings.metrics.PreferenceActionMetricsProvider
import com.android.settingslib.datastore.KeyValueStore
import com.android.settingslib.datastore.KeyValueStoreDelegate
import com.android.settingslib.datastore.SettingsSecureStore
import com.android.settingslib.datastore.SettingsStore
import com.android.settingslib.datastore.and
import com.android.settingslib.metadata.ReadWritePermit
import com.android.settingslib.metadata.SensitivityLevel
import com.android.settingslib.metadata.SwitchPreference
class WifiScorerTogglePreference() :
SwitchPreference(
KEY,
R.string.adaptive_connectivity_wifi_switch_title
),
PreferenceActionMetricsProvider {
override val preferenceActionMetrics: Int
get() = ACTION_ADAPTIVE_CONNECTIVITY
override val key: String
get() = KEY
override fun tags(context: Context) = arrayOf(KEY_ADAPTIVE_CONNECTIVITY)
override fun storage(context: Context): KeyValueStore =
WifiScorerToggleStorage(context)
override fun getReadPermissions(context: Context) = SettingsSecureStore.getReadPermissions()
override fun getWritePermissions(context: Context) =
SettingsSecureStore.getWritePermissions() and Manifest.permission.NETWORK_SETTINGS
override fun getReadPermit(context: Context, callingPid: Int, callingUid: Int) =
ReadWritePermit.ALLOW
override fun getWritePermit(
context: Context,
value: Boolean?,
callingPid: Int,
callingUid: Int,
) = ReadWritePermit.ALLOW
override val sensitivityLevel
get() = SensitivityLevel.NO_SENSITIVITY
@Suppress("UNCHECKED_CAST")
private class WifiScorerToggleStorage(
private val context: Context,
private val settingsStore: SettingsStore = SettingsSecureStore.get(context),
) : KeyValueStoreDelegate {
override val keyValueStoreDelegate
get() = settingsStore
override fun <T : Any> getDefaultValue(key: String, valueType: Class<T>) =
DEFAULT_VALUE as T
@RequiresPermission(Manifest.permission.NETWORK_SETTINGS)
override fun <T : Any> setValue(key: String, valueType: Class<T>, value: T?) {
settingsStore.setValue(key, valueType, value)
context
.getSystemService(WifiManager::class.java)
?.setWifiScoringEnabled(
(value as Boolean?)
?: DEFAULT_VALUE
)
}
}
companion object {
const val KEY = ADAPTIVE_CONNECTIVITY_WIFI_ENABLED
const val DEFAULT_VALUE = true
}
}