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 android.telephony.SubscriptionManager
20 import androidx.compose.material.icons.Icons
21 import androidx.compose.material.icons.outlined.SignalCellularAlt
22 import androidx.compose.runtime.Composable
23 import androidx.compose.runtime.MutableIntState
24 import androidx.compose.runtime.getValue
25 import androidx.compose.runtime.mutableIntStateOf
26 import androidx.compose.runtime.remember
27 import androidx.compose.runtime.saveable.rememberSaveable
28 import androidx.compose.ui.graphics.vector.ImageVector
29 import androidx.compose.ui.platform.LocalContext
30 import androidx.compose.ui.res.stringResource
31 import androidx.lifecycle.compose.collectAsStateWithLifecycle
32 import com.android.settings.R
33 import com.android.settings.network.SimOnboardingService
34 import com.android.settingslib.spa.widget.preference.ListPreference
35 import com.android.settingslib.spa.widget.preference.ListPreferenceModel
36 import com.android.settingslib.spa.widget.preference.ListPreferenceOption
37 import com.android.settingslib.spa.widget.scaffold.BottomAppBarButton
38 import com.android.settingslib.spa.widget.scaffold.SuwScaffold
39 import com.android.settingslib.spa.widget.ui.SettingsIcon
40 import kotlinx.coroutines.Dispatchers
41 import kotlinx.coroutines.flow.flow
42 import kotlinx.coroutines.flow.flowOn
43 
44 /**
45  * the sim onboarding primary sim compose
46  */
47 @Composable
48 fun SimOnboardingPrimarySimImpl(
49     nextAction: () -> Unit,
50     cancelAction: () -> Unit,
51     onboardingService: SimOnboardingService
52 ) {
53     SuwScaffold(
54         imageVector = Icons.Outlined.SignalCellularAlt,
55         title = stringResource(id = R.string.sim_onboarding_primary_sim_title),
56         actionButton = BottomAppBarButton(
57             text = stringResource(id = R.string.done),
58             onClick = nextAction
59         ),
60         dismissButton = BottomAppBarButton(
61             text = stringResource(id = R.string.cancel),
62             onClick = cancelAction
63         ),
64     ) {
65         val callsSelectedId = rememberSaveable {
66             mutableIntStateOf(SubscriptionManager.INVALID_SUBSCRIPTION_ID)
67         }
68         val textsSelectedId = rememberSaveable {
69             mutableIntStateOf(SubscriptionManager.INVALID_SUBSCRIPTION_ID)
70         }
71         val mobileDataSelectedId = rememberSaveable {
72             mutableIntStateOf(SubscriptionManager.INVALID_SUBSCRIPTION_ID)
73         }
74 
75         SimOnboardingMessage(stringResource(id = R.string.sim_onboarding_primary_sim_msg))
76 
77         val context = LocalContext.current
78         val primarySimInfo = remember {
79             flow {
80                 val selectableSubInfoList =
81                     onboardingService.getSelectedSubscriptionInfoListWithRenaming()
82                 emit(PrimarySimRepository(context).getPrimarySimInfo(selectableSubInfoList))
83             }.flowOn(Dispatchers.Default)
84         }.collectAsStateWithLifecycle(initialValue = null).value ?: return@SuwScaffold
85         callsSelectedId.intValue = onboardingService.targetPrimarySimCalls
86         textsSelectedId.intValue = onboardingService.targetPrimarySimTexts
87         mobileDataSelectedId.intValue = onboardingService.targetPrimarySimMobileData
88         val isAutoDataEnabled by
89             onboardingService.targetPrimarySimAutoDataSwitch
90                 .collectAsStateWithLifecycle(initialValue = null)
91         PrimarySimImpl(
92             primarySimInfo = primarySimInfo,
93             callsSelectedId = callsSelectedId,
94             textsSelectedId = textsSelectedId,
95             mobileDataSelectedId = mobileDataSelectedId,
96             actionSetCalls = {
97                 callsSelectedId.intValue = it
98                 onboardingService.targetPrimarySimCalls = it
99             },
100             actionSetTexts = {
101                 textsSelectedId.intValue = it
102                 onboardingService.targetPrimarySimTexts = it
103             },
104             actionSetMobileData = {
105                 mobileDataSelectedId.intValue = it
106                 onboardingService.targetPrimarySimMobileData = it
107             }
108         )
109         AutomaticDataSwitchingPreference(isAutoDataEnabled = { isAutoDataEnabled },
110             setAutoDataEnabled = { newEnabled ->
111                 onboardingService.targetPrimarySimAutoDataSwitch.value = newEnabled
112             })
113     }
114 }
115 
116 @Composable
CreatePrimarySimListPreferencenull117 fun CreatePrimarySimListPreference(
118         title: String,
119         list: List<ListPreferenceOption>,
120         selectedId: MutableIntState,
121         icon: ImageVector,
122         onIdSelected: (id: Int) -> Unit
123 ) = ListPreference(
124     object : ListPreferenceModel {
125         override val title = title
126         override val options = list
127         override val selectedId = selectedId
128         override val onIdSelected = onIdSelected
129         override val icon = @Composable {
130             SettingsIcon(icon)
131         }
132 })