Refactor AutomaticDataSwitchingPreference
Split into AutomaticDataSwitchingPreference.kt, and use isMobileDataPolicyEnabledFlow. Bug: 329061940 Test: manual - Set Automatic data switching Test: unit test Change-Id: I878ed70328307c0a5dba6dfb461ff5a85efbcf88
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* Copyright (C) 2024 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.spa.network
|
||||
|
||||
import android.telephony.TelephonyManager
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.rememberCoroutineScope
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import com.android.settings.R
|
||||
import com.android.settings.network.telephony.TelephonyRepository
|
||||
import com.android.settingslib.spa.widget.preference.SwitchPreference
|
||||
import com.android.settingslib.spa.widget.preference.SwitchPreferenceModel
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
@Composable
|
||||
fun AutomaticDataSwitchingPreference(
|
||||
isAutoDataEnabled: () -> Boolean?,
|
||||
setAutoDataEnabled: (newEnabled: Boolean) -> Unit,
|
||||
) {
|
||||
val autoDataSummary = stringResource(id = R.string.primary_sim_automatic_data_msg)
|
||||
val coroutineScope = rememberCoroutineScope()
|
||||
SwitchPreference(
|
||||
object : SwitchPreferenceModel {
|
||||
override val title = stringResource(id = R.string.primary_sim_automatic_data_title)
|
||||
override val summary = { autoDataSummary }
|
||||
override val checked = { isAutoDataEnabled() }
|
||||
override val onCheckedChange: (Boolean) -> Unit = { newEnabled ->
|
||||
coroutineScope.launch(Dispatchers.Default) {
|
||||
setAutoDataEnabled(newEnabled)
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
fun TelephonyRepository.setAutomaticData(subId: Int, newEnabled: Boolean) {
|
||||
setMobileDataPolicyEnabled(
|
||||
subId = subId,
|
||||
policy = TelephonyManager.MOBILE_DATA_POLICY_AUTO_DATA_SWITCH,
|
||||
enabled = newEnabled,
|
||||
)
|
||||
//TODO: setup backup calling
|
||||
}
|
||||
@@ -30,7 +30,6 @@ import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.MutableIntState
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableIntStateOf
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.rememberCoroutineScope
|
||||
import androidx.compose.runtime.saveable.rememberSaveable
|
||||
@@ -44,6 +43,7 @@ import androidx.lifecycle.viewmodel.compose.viewModel
|
||||
import com.android.settings.R
|
||||
import com.android.settings.network.SubscriptionInfoListViewModel
|
||||
import com.android.settings.network.telephony.MobileNetworkUtils
|
||||
import com.android.settings.network.telephony.TelephonyRepository
|
||||
import com.android.settings.spa.network.PrimarySimRepository.PrimarySimInfo
|
||||
import com.android.settings.wifi.WifiPickerTrackerHelper
|
||||
import com.android.settingslib.spa.framework.common.SettingsEntryBuilder
|
||||
@@ -53,8 +53,6 @@ import com.android.settingslib.spa.framework.compose.navigator
|
||||
import com.android.settingslib.spa.framework.util.collectLatestWithLifecycle
|
||||
import com.android.settingslib.spa.widget.preference.Preference
|
||||
import com.android.settingslib.spa.widget.preference.PreferenceModel
|
||||
import com.android.settingslib.spa.widget.preference.SwitchPreference
|
||||
import com.android.settingslib.spa.widget.preference.SwitchPreferenceModel
|
||||
import com.android.settingslib.spa.widget.scaffold.RegularScaffold
|
||||
import com.android.settingslib.spa.widget.ui.Category
|
||||
import com.android.settingslib.spaprivileged.framework.common.broadcastReceiverFlow
|
||||
@@ -193,7 +191,6 @@ fun PrimarySimImpl(
|
||||
callsSelectedId: MutableIntState,
|
||||
textsSelectedId: MutableIntState,
|
||||
mobileDataSelectedId: MutableIntState,
|
||||
nonDds: MutableIntState,
|
||||
subscriptionManager: SubscriptionManager? =
|
||||
LocalContext.current.getSystemService(SubscriptionManager::class.java),
|
||||
coroutineScope: CoroutineScope = rememberCoroutineScope(),
|
||||
@@ -223,23 +220,9 @@ fun PrimarySimImpl(
|
||||
)
|
||||
}
|
||||
},
|
||||
actionSetAutoDataSwitch: (Boolean) -> Unit = { newState ->
|
||||
coroutineScope.launch {
|
||||
val telephonyManagerForNonDds: TelephonyManager? =
|
||||
context.getSystemService(TelephonyManager::class.java)
|
||||
?.createForSubscriptionId(nonDds.intValue)
|
||||
Log.d(NetworkCellularGroupProvider.name, "NonDds:${nonDds.intValue} setAutomaticData")
|
||||
setAutomaticData(telephonyManagerForNonDds, newState)
|
||||
}
|
||||
},
|
||||
isAutoDataEnabled: () -> Boolean?,
|
||||
setAutoDataEnabled: (newEnabled: Boolean) -> Unit,
|
||||
) {
|
||||
val telephonyManagerForNonDds: TelephonyManager? =
|
||||
context.getSystemService(TelephonyManager::class.java)
|
||||
?.createForSubscriptionId(nonDds.intValue)
|
||||
val automaticDataChecked = rememberSaveable() {
|
||||
mutableStateOf(false)
|
||||
}
|
||||
|
||||
CreatePrimarySimListPreference(
|
||||
stringResource(id = R.string.primary_sim_calls_title),
|
||||
primarySimInfo.callsAndSmsList,
|
||||
@@ -262,31 +245,7 @@ fun PrimarySimImpl(
|
||||
actionSetMobileData
|
||||
)
|
||||
|
||||
val autoDataTitle = stringResource(id = R.string.primary_sim_automatic_data_title)
|
||||
val autoDataSummary = stringResource(id = R.string.primary_sim_automatic_data_msg)
|
||||
SwitchPreference(
|
||||
object : SwitchPreferenceModel {
|
||||
override val title = autoDataTitle
|
||||
override val summary = { autoDataSummary }
|
||||
override val checked = {
|
||||
if (nonDds.intValue != SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
|
||||
coroutineScope.launch {
|
||||
automaticDataChecked.value = getAutomaticData(telephonyManagerForNonDds)
|
||||
Log.d(
|
||||
NetworkCellularGroupProvider.name,
|
||||
"NonDds:${nonDds.intValue}" +
|
||||
"getAutomaticData:${automaticDataChecked.value}"
|
||||
)
|
||||
}
|
||||
}
|
||||
automaticDataChecked.value
|
||||
}
|
||||
override val onCheckedChange: ((Boolean) -> Unit)? = {
|
||||
automaticDataChecked.value = it
|
||||
actionSetAutoDataSwitch(it)
|
||||
}
|
||||
}
|
||||
)
|
||||
AutomaticDataSwitchingPreference(isAutoDataEnabled, setAutoDataEnabled)
|
||||
}
|
||||
|
||||
@Composable
|
||||
@@ -308,12 +267,21 @@ fun PrimarySimSectionImpl(
|
||||
}.collectAsStateWithLifecycle(initialValue = null).value ?: return
|
||||
|
||||
Category(title = stringResource(id = R.string.primary_sim_title)) {
|
||||
val isAutoDataEnabled by remember(nonDds.intValue) {
|
||||
TelephonyRepository(context).isMobileDataPolicyEnabledFlow(
|
||||
subId = nonDds.intValue,
|
||||
policy = TelephonyManager.MOBILE_DATA_POLICY_AUTO_DATA_SWITCH
|
||||
)
|
||||
}.collectAsStateWithLifecycle(initialValue = null)
|
||||
PrimarySimImpl(
|
||||
primarySimInfo,
|
||||
callsSelectedId,
|
||||
textsSelectedId,
|
||||
mobileDataSelectedId,
|
||||
nonDds
|
||||
isAutoDataEnabled = { isAutoDataEnabled },
|
||||
setAutoDataEnabled = { newEnabled ->
|
||||
TelephonyRepository(context).setAutomaticData(nonDds.intValue, newEnabled)
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -381,23 +349,3 @@ suspend fun setDefaultData(
|
||||
wifiPickerTrackerHelper.setCarrierNetworkEnabled(true)
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun getAutomaticData(telephonyManagerForNonDds: TelephonyManager?): Boolean =
|
||||
withContext(Dispatchers.Default) {
|
||||
telephonyManagerForNonDds != null
|
||||
&& telephonyManagerForNonDds.isMobileDataPolicyEnabled(
|
||||
TelephonyManager.MOBILE_DATA_POLICY_AUTO_DATA_SWITCH)
|
||||
}
|
||||
|
||||
suspend fun setAutomaticData(telephonyManager: TelephonyManager?, newState: Boolean): Unit =
|
||||
withContext(Dispatchers.Default) {
|
||||
Log.d(
|
||||
NetworkCellularGroupProvider.name,
|
||||
"setAutomaticData: MOBILE_DATA_POLICY_AUTO_DATA_SWITCH as $newState"
|
||||
)
|
||||
telephonyManager?.setMobileDataPolicyEnabled(
|
||||
TelephonyManager.MOBILE_DATA_POLICY_AUTO_DATA_SWITCH,
|
||||
newState
|
||||
)
|
||||
//TODO: setup backup calling
|
||||
}
|
||||
@@ -23,6 +23,7 @@ import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.outlined.SignalCellularAlt
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.MutableIntState
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableIntStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.saveable.rememberSaveable
|
||||
@@ -75,9 +76,6 @@ fun SimOnboardingPrimarySimImpl(
|
||||
val mobileDataSelectedId = rememberSaveable {
|
||||
mutableIntStateOf(SubscriptionManager.INVALID_SUBSCRIPTION_ID)
|
||||
}
|
||||
val nonDdsRemember = rememberSaveable {
|
||||
mutableIntStateOf(SubscriptionManager.INVALID_SUBSCRIPTION_ID)
|
||||
}
|
||||
|
||||
Column(Modifier.padding(SettingsDimension.itemPadding)) {
|
||||
SettingsBody(stringResource(id = R.string.sim_onboarding_primary_sim_msg))
|
||||
@@ -94,12 +92,14 @@ fun SimOnboardingPrimarySimImpl(
|
||||
callsSelectedId.intValue = onboardingService.targetPrimarySimCalls
|
||||
textsSelectedId.intValue = onboardingService.targetPrimarySimTexts
|
||||
mobileDataSelectedId.intValue = onboardingService.targetPrimarySimMobileData
|
||||
val isAutoDataEnabled by
|
||||
onboardingService.targetPrimarySimAutoDataSwitch
|
||||
.collectAsStateWithLifecycle(initialValue = null)
|
||||
PrimarySimImpl(
|
||||
primarySimInfo = primarySimInfo,
|
||||
callsSelectedId = callsSelectedId,
|
||||
textsSelectedId = textsSelectedId,
|
||||
mobileDataSelectedId = mobileDataSelectedId,
|
||||
nonDds = nonDdsRemember,
|
||||
actionSetCalls = {
|
||||
callsSelectedId.intValue = it
|
||||
onboardingService.targetPrimarySimCalls = it},
|
||||
@@ -109,8 +109,10 @@ fun SimOnboardingPrimarySimImpl(
|
||||
actionSetMobileData = {
|
||||
mobileDataSelectedId.intValue = it
|
||||
onboardingService.targetPrimarySimMobileData = it},
|
||||
actionSetAutoDataSwitch = {
|
||||
onboardingService.targetPrimarySimAutoDataSwitch = it},
|
||||
isAutoDataEnabled = { isAutoDataEnabled },
|
||||
setAutoDataEnabled = { newEnabled ->
|
||||
onboardingService.targetPrimarySimAutoDataSwitch.value = newEnabled
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user