1 /* 2 * Copyright (C) 2024 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.settings.network 18 19 import android.app.settings.SettingsEnums.ACTION_ADAPTIVE_CONNECTIVITY 20 import android.content.Context 21 import android.net.wifi.WifiManager 22 import android.provider.Settings.Secure.ADAPTIVE_CONNECTIVITY_ENABLED 23 import com.android.settings.R 24 import com.android.settings.contract.KEY_ADAPTIVE_CONNECTIVITY 25 import com.android.settings.metrics.PreferenceActionMetricsProvider 26 import com.android.settingslib.datastore.KeyValueStore 27 import com.android.settingslib.datastore.KeyValueStoreDelegate 28 import com.android.settingslib.datastore.SettingsSecureStore 29 import com.android.settingslib.metadata.MainSwitchPreference 30 import com.android.settingslib.metadata.ReadWritePermit 31 import com.android.settingslib.metadata.SensitivityLevel 32 33 // LINT.IfChange 34 class AdaptiveConnectivityTogglePreference : 35 MainSwitchPreference(KEY, R.string.adaptive_connectivity_main_switch_title), 36 PreferenceActionMetricsProvider { 37 38 override val preferenceActionMetrics: Int 39 get() = ACTION_ADAPTIVE_CONNECTIVITY 40 tagsnull41 override fun tags(context: Context) = arrayOf(KEY_ADAPTIVE_CONNECTIVITY) 42 43 override fun storage(context: Context): KeyValueStore = 44 AdaptiveConnectivityToggleStorage(context) 45 46 override fun getReadPermissions(context: Context) = SettingsSecureStore.getReadPermissions() 47 48 override fun getWritePermissions(context: Context) = SettingsSecureStore.getWritePermissions() 49 50 override fun getReadPermit(context: Context, callingPid: Int, callingUid: Int) = 51 ReadWritePermit.ALLOW 52 53 override fun getWritePermit( 54 context: Context, 55 value: Boolean?, 56 callingPid: Int, 57 callingUid: Int, 58 ) = ReadWritePermit.ALLOW 59 60 override val sensitivityLevel 61 get() = SensitivityLevel.NO_SENSITIVITY 62 63 @Suppress("UNCHECKED_CAST") 64 private class AdaptiveConnectivityToggleStorage( 65 private val context: Context, 66 private val settingsStore: KeyValueStore = SettingsSecureStore.get(context), 67 ) : KeyValueStoreDelegate { 68 69 override val keyValueStoreDelegate 70 get() = settingsStore 71 72 override fun <T : Any> getDefaultValue(key: String, valueType: Class<T>) = 73 DEFAULT_VALUE as T 74 75 override fun <T : Any> setValue(key: String, valueType: Class<T>, value: T?) { 76 settingsStore.setValue(key, valueType, value) 77 context 78 .getSystemService(WifiManager::class.java) 79 ?.setWifiScoringEnabled((value as Boolean?) ?: DEFAULT_VALUE) 80 } 81 } 82 83 companion object { 84 const val KEY = ADAPTIVE_CONNECTIVITY_ENABLED 85 const val DEFAULT_VALUE = true 86 } 87 } 88 // LINT.ThenChange(AdaptiveConnectivityTogglePreferenceController.java) 89