1 /*
2  * 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.details2
18 
19 import android.content.Context
20 import android.net.wifi.WifiManager
21 import androidx.compose.material3.Icon
22 import androidx.compose.runtime.Composable
23 import androidx.compose.ui.graphics.vector.ImageVector
24 import androidx.compose.ui.res.stringResource
25 import androidx.compose.ui.res.vectorResource
26 import com.android.settings.R
27 import com.android.settings.spa.SpaActivity.Companion.startSpaActivity
28 import com.android.settings.spa.preference.ComposePreferenceController
29 import com.android.settingslib.spa.widget.preference.Preference
30 import com.android.settingslib.spa.widget.preference.PreferenceModel
31 import com.android.wifi.flags.Flags
32 
33 class WifiPrivacyPreferenceController(context: Context, preferenceKey: String) :
34     ComposePreferenceController(context, preferenceKey) {
35 
36     private var wifiEntryKey: String? = null
37 
38     var wifiManager = context.getSystemService(WifiManager::class.java)!!
39 
setWifiEntryKeynull40     fun setWifiEntryKey(key: String?) {
41         wifiEntryKey = key
42     }
43 
getAvailabilityStatusnull44     override fun getAvailabilityStatus() =
45         if (Flags.androidVWifiApi() && wifiManager.isConnectedMacRandomizationSupported) AVAILABLE
46         else CONDITIONALLY_UNAVAILABLE
47 
48     @Composable
49     override fun Content() {
50         Preference(object : PreferenceModel {
51             override val title = stringResource(R.string.wifi_privacy_settings)
52             override val icon = @Composable {
53                 Icon(
54                     ImageVector.vectorResource(R.drawable.ic_wifi_privacy_24dp),
55                     contentDescription = null
56                 )
57             }
58             override val onClick: () -> Unit =
59                 {
60                     wifiEntryKey?.let {
61                         mContext.startSpaActivity(WifiPrivacyPageProvider.getRoute(it))
62                     }
63                 }
64         })
65     }
66 
67     companion object {
68         private const val PREF_SEND_DHCP_HOST_NAME_ENABLE = 0
69         private const val PREF_SEND_DHCP_HOST_NAME_DISABLE = 1
70 
71         /**
72          * Returns preference index value.
73          *
74          * @param isSendDhcpHostnameEnabled determines whether device name can be sent.
75          * @return index value of preference
76          */
translateSendDhcpHostnameEnabledToPrefValuenull77         fun translateSendDhcpHostnameEnabledToPrefValue(
78             isSendDhcpHostnameEnabled: Boolean
79         ): Int {
80             return if (isSendDhcpHostnameEnabled) PREF_SEND_DHCP_HOST_NAME_ENABLE
81             else PREF_SEND_DHCP_HOST_NAME_DISABLE
82         }
83 
84         /**
85          * Returns whether device name can be sent.
86          *
87          * @param prefDhcpRandomized is preference index value
88          * @return is send dhcp host name enabled
89          */
translatePrefValueToSendDhcpHostnameEnablednull90         fun translatePrefValueToSendDhcpHostnameEnabled(prefDhcpRandomized: Int): Boolean {
91             return prefDhcpRandomized == PREF_SEND_DHCP_HOST_NAME_ENABLE
92         }
93     }
94 }