• 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.wifi
18 
19 import android.content.Context
20 import android.net.wifi.WifiManager
21 import androidx.compose.foundation.layout.fillMaxWidth
22 import androidx.compose.material3.Text
23 import androidx.compose.runtime.Composable
24 import androidx.compose.runtime.getValue
25 import androidx.compose.runtime.mutableStateOf
26 import androidx.compose.runtime.saveable.rememberSaveable
27 import androidx.compose.runtime.setValue
28 import androidx.compose.ui.Modifier
29 import androidx.compose.ui.res.stringResource
30 import androidx.compose.ui.text.style.TextAlign
31 import androidx.lifecycle.compose.collectAsStateWithLifecycle
32 import com.android.settings.R
33 import com.android.settings.spa.preference.ComposePreferenceController
34 import com.android.settingslib.spa.framework.compose.OverridableFlow
35 import com.android.settingslib.spa.widget.dialog.AlertDialogButton
36 import com.android.settingslib.spa.widget.dialog.SettingsAlertDialogWithIcon
37 import com.android.settingslib.spa.widget.preference.SwitchPreference
38 import com.android.settingslib.spa.widget.preference.SwitchPreferenceModel
39 import com.android.wifi.flags.Flags
40 import com.android.wifitrackerlib.WifiEntry
41 import kotlinx.coroutines.Dispatchers
42 import kotlinx.coroutines.asExecutor
43 import kotlinx.coroutines.channels.awaitClose
44 import kotlinx.coroutines.flow.callbackFlow
45 
46 /** Controller that controls whether the WEP network can be connected. */
47 class WepNetworksPreferenceController(context: Context, preferenceKey: String) :
48     ComposePreferenceController(context, preferenceKey) {
49 
50     var wifiManager = context.getSystemService(WifiManager::class.java)!!
51 
52     override fun getAvailabilityStatus() = if (Flags.androidVWifiApi()) AVAILABLE
53     else UNSUPPORTED_ON_DEVICE
54 
55     @Composable
56     override fun Content() {
57         val checked by wepAllowedFlow.flow.collectAsStateWithLifecycle(initialValue = null)
58         var openDialog by rememberSaveable { mutableStateOf(false) }
59         val wifiInfo = wifiManager.connectionInfo
60         SwitchPreference(object : SwitchPreferenceModel {
61             override val title = stringResource(R.string.wifi_allow_wep_networks)
62             override val summary = { getSummary() }
63             override val checked = { checked }
64             override val changeable: () -> Boolean
65                 get() = { carrierAllowed }
66             override val onCheckedChange: (Boolean) -> Unit = { newChecked ->
67                 if (!newChecked && wifiInfo.currentSecurityType == WifiEntry.SECURITY_WEP) {
68                     openDialog = true
69                 } else {
70                     wifiManager.setWepAllowed(newChecked)
71                     wepAllowedFlow.override(newChecked)
72                 }
73             }
74         })
75         if (openDialog) {
76             SettingsAlertDialogWithIcon(
77                 onDismissRequest = { openDialog = false },
78                 confirmButton = AlertDialogButton(
79                     stringResource(R.string.sim_action_yes)
80                 ) {
81                     wifiManager.setWepAllowed(false)
82                     wepAllowedFlow.override(false)
83                     openDialog = false
84                 },
85                 dismissButton =
86                 AlertDialogButton(
87                     stringResource(R.string.wifi_cancel)
88                 ) { openDialog = false },
89                 title = stringResource(R.string.wifi_settings_wep_networks_disconnect_title),
90                 text = {
91                     Text(
92                         stringResource(R.string.wifi_settings_wep_networks_disconnect_summary),
93                         modifier = Modifier.fillMaxWidth(),
94                         textAlign = TextAlign.Center
95                     )
96                 })
97         }
98     }
99 
100     override fun getSummary(): String = mContext.getString(
101         if (carrierAllowed) {
102             R.string.wifi_allow_wep_networks_summary
103         } else {
104             R.string.wifi_allow_wep_networks_summary_carrier_not_allow
105         }
106     )
107 
108     private val carrierAllowed: Boolean
109         get() = wifiManager.isWepSupported
110 
111     val wepAllowedFlow = OverridableFlow(callbackFlow {
112         wifiManager.queryWepAllowed(Dispatchers.Default.asExecutor(), ::trySend)
113 
114         awaitClose { }
115     })
116 }