• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.network.telephony
18 
19 import android.content.Context
20 import android.telephony.SubscriptionManager
21 import androidx.compose.runtime.Composable
22 import androidx.compose.runtime.getValue
23 import androidx.compose.runtime.remember
24 import androidx.compose.runtime.rememberCoroutineScope
25 import androidx.compose.ui.res.stringResource
26 import androidx.lifecycle.compose.collectAsStateWithLifecycle
27 import com.android.settings.R
28 import com.android.settings.spa.preference.ComposePreferenceController
29 import com.android.settingslib.spa.widget.preference.SwitchPreference
30 import com.android.settingslib.spa.widget.preference.SwitchPreferenceModel
31 import kotlinx.coroutines.flow.flowOf
32 import kotlinx.coroutines.launch
33 
34 /**
35  * Preference controller for "Voice over NR".
36  */
37 class NrAdvancedCallingPreferenceController @JvmOverloads constructor(
38     context: Context,
39     key: String,
40     private val callStateRepository : CallStateRepository = CallStateRepository(context),
41 ) : ComposePreferenceController(context, key) {
42     private var subId: Int = SubscriptionManager.INVALID_SUBSCRIPTION_ID
43     private var repository: VoNrRepository? = null
44 
45     /** Initial this PreferenceController. */
46     @JvmOverloads
47     fun init(subId: Int, repository: VoNrRepository = VoNrRepository(mContext, subId)) {
48         this.subId = subId
49         this.repository = repository
50     }
51 
52     override fun getAvailabilityStatus() =
53         if (repository?.isVoNrAvailable() == true) AVAILABLE else CONDITIONALLY_UNAVAILABLE
54 
55     @Composable
56     override fun Content() {
57         val summary = stringResource(R.string.nr_advanced_calling_summary)
58         val isInCall by remember { callStateRepository.isInCallFlow() }
59             .collectAsStateWithLifecycle(initialValue = false)
60         val isEnabled by remember {
61             repository?.isVoNrEnabledFlow() ?: flowOf(false)
62         }.collectAsStateWithLifecycle(initialValue = false)
63         val coroutineScope = rememberCoroutineScope()
64         SwitchPreference(object : SwitchPreferenceModel {
65             override val title = stringResource(R.string.nr_advanced_calling_title)
66             override val summary = { summary }
67             override val changeable = { !isInCall }
68             override val checked = { isEnabled }
69             override val onCheckedChange: (Boolean) -> Unit = { newChecked ->
70                 coroutineScope.launch {
71                     repository?.setVoNrEnabled(newChecked)
72                 }
73             }
74         })
75     }
76 }
77