• 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.network.telephony.MobileNetworkSettingsSearchIndex.MobileNetworkSettingsSearchItem
29 import com.android.settings.network.telephony.MobileNetworkSettingsSearchIndex.MobileNetworkSettingsSearchResult
30 import com.android.settings.spa.preference.ComposePreferenceController
31 import com.android.settingslib.spa.widget.preference.SwitchPreference
32 import com.android.settingslib.spa.widget.preference.SwitchPreferenceModel
33 import kotlinx.coroutines.launch
34 
35 /** Preference controller for "Voice over NR". */
36 class NrAdvancedCallingPreferenceController
37 @JvmOverloads
38 constructor(
39     context: Context,
40     key: String,
41     private val voNrRepository: VoNrRepository = VoNrRepository(context),
42     private val callStateRepository: CallStateRepository = CallStateRepository(context),
43 ) : ComposePreferenceController(context, key) {
44     private var subId: Int = SubscriptionManager.INVALID_SUBSCRIPTION_ID
45     private val searchItem = NrAdvancedCallingSearchItem(context)
46 
47     /** Initial this PreferenceController. */
48     fun init(subId: Int) {
49         this.subId = subId
50     }
51 
52     override fun getAvailabilityStatus() =
53         if (searchItem.isAvailable(subId)) 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
59             remember { callStateRepository.isInCallFlow() }
60                 .collectAsStateWithLifecycle(initialValue = false)
61         val isVoNrEnabled by
62             remember { voNrRepository.isVoNrEnabledFlow(subId) }
63                 .collectAsStateWithLifecycle(initialValue = false)
64         val coroutineScope = rememberCoroutineScope()
65         SwitchPreference(
66             object : SwitchPreferenceModel {
67                 override val title = stringResource(R.string.nr_advanced_calling_title)
68                 override val summary = { summary }
69                 override val changeable = { !isInCall }
70                 override val checked = { isVoNrEnabled }
71                 override val onCheckedChange: (Boolean) -> Unit = { newChecked ->
72                     coroutineScope.launch { voNrRepository.setVoNrEnabled(subId, newChecked) }
73                 }
74             }
75         )
76     }
77 
78     companion object {
79         class NrAdvancedCallingSearchItem(private val context: Context) :
80             MobileNetworkSettingsSearchItem {
81             private val voNrRepository = VoNrRepository(context)
82 
83             fun isAvailable(subId: Int): Boolean = voNrRepository.isVoNrAvailable(subId)
84 
85             override fun getSearchResult(subId: Int): MobileNetworkSettingsSearchResult? {
86                 if (!isAvailable(subId)) return null
87                 return MobileNetworkSettingsSearchResult(
88                     key = "nr_advanced_calling",
89                     title = context.getString(R.string.nr_advanced_calling_title),
90                     keywords = context.getString(R.string.keywords_nr_advanced_calling),
91                 )
92             }
93         }
94     }
95 }
96