1 /*
<lambda>null2 * 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.spa.network
18
19 import androidx.annotation.VisibleForTesting
20 import androidx.compose.runtime.Composable
21 import androidx.compose.runtime.getValue
22 import androidx.compose.runtime.remember
23 import androidx.compose.runtime.rememberCoroutineScope
24 import androidx.compose.ui.platform.LocalContext
25 import androidx.compose.ui.res.stringResource
26 import androidx.lifecycle.compose.collectAsStateWithLifecycle
27 import com.android.settings.R
28 import com.android.settings.network.telephony.MobileDataRepository
29 import com.android.settings.network.telephony.SubscriptionActivationRepository
30 import com.android.settings.network.telephony.subscriptionManager
31 import com.android.settingslib.spa.framework.compose.rememberContext
32 import com.android.settingslib.spa.widget.preference.SwitchPreference
33 import com.android.settingslib.spa.widget.preference.SwitchPreferenceModel
34 import kotlinx.coroutines.Dispatchers
35 import kotlinx.coroutines.launch
36
37 @Composable
38 fun MobileDataSwitchPreference(subId: Int) {
39 MobileDataSwitchPreference(
40 subId = subId,
41 mobileDataRepository = rememberContext(::MobileDataRepository),
42 subscriptionActivationRepository = rememberContext(::SubscriptionActivationRepository),
43 setMobileData = setMobileDataImpl(subId),
44 )
45 }
46
47 @VisibleForTesting
48 @Composable
MobileDataSwitchPreferencenull49 fun MobileDataSwitchPreference(
50 subId: Int,
51 mobileDataRepository: MobileDataRepository,
52 subscriptionActivationRepository: SubscriptionActivationRepository,
53 setMobileData: (newChecked: Boolean) -> Unit,
54 ) {
55 val mobileDataSummary = stringResource(id = R.string.mobile_data_settings_summary)
56 val isMobileDataEnabled by
57 remember(subId) { mobileDataRepository.isMobileDataEnabledFlow(subId) }
58 .collectAsStateWithLifecycle(initialValue = null)
59 val changeable by remember {
60 subscriptionActivationRepository.isActivationChangeableFlow()
61 }.collectAsStateWithLifecycle(initialValue = true)
62 SwitchPreference(
63 object : SwitchPreferenceModel {
64 override val title = stringResource(id = R.string.mobile_data_settings_title)
65 override val summary = { mobileDataSummary }
66 override val checked = { isMobileDataEnabled }
67 override val onCheckedChange = setMobileData
68 override val changeable: () -> Boolean
69 get() = { changeable }
70 }
71 )
72 }
73
74 @Composable
setMobileDataImplnull75 private fun setMobileDataImpl(subId: Int): (newChecked: Boolean) -> Unit {
76 val context = LocalContext.current
77 val coroutineScope = rememberCoroutineScope()
78 val wifiPickerTrackerHelper = rememberWifiPickerTrackerHelper()
79 return { newEnabled ->
80 coroutineScope.launch(Dispatchers.Default) {
81 setMobileData(
82 context = context,
83 subscriptionManager = context.subscriptionManager,
84 wifiPickerTrackerHelper = wifiPickerTrackerHelper,
85 subId = subId,
86 enabled = newEnabled,
87 )
88 }
89 }
90 }
91