1 /**
2  * Copyright (C) 2023 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.content.Context
20 import android.os.Bundle
21 import android.os.UserHandle.myUserId
22 import android.os.UserManager
23 import android.os.UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS
24 import androidx.compose.material.icons.Icons
25 import androidx.compose.material.icons.outlined.Wifi
26 import androidx.compose.runtime.Composable
27 import androidx.compose.runtime.remember
28 import androidx.compose.ui.res.stringResource
29 import com.android.settings.R
30 import com.android.settingslib.RestrictedLockUtilsInternal
31 import com.android.settingslib.Utils
32 import com.android.settingslib.spa.framework.common.SettingsEntryBuilder
33 import com.android.settingslib.spa.framework.common.SettingsPageProvider
34 import com.android.settingslib.spa.framework.common.SpaEnvironmentFactory
35 import com.android.settingslib.spa.framework.common.createSettingsPage
36 import com.android.settingslib.spa.framework.compose.navigator
37 import com.android.settingslib.spa.widget.preference.Preference
38 import com.android.settingslib.spa.widget.preference.PreferenceModel
39 import com.android.settingslib.spa.widget.scaffold.RegularScaffold
40 import com.android.settingslib.spa.widget.ui.SettingsIcon
41 
42 object NetworkAndInternetPageProvider : SettingsPageProvider {
43     override val name = "NetworkAndInternet"
44     private val owner = createSettingsPage()
45 
isEnablednull46     override fun isEnabled(arguments: Bundle?) = false
47 
48     @Composable
49     override fun Page(arguments: Bundle?) {
50         RegularScaffold(title = getTitle(arguments)) {
51             AirplaneModePreference()
52         }
53     }
54 
getTitlenull55     override fun getTitle(arguments: Bundle?): String {
56         return SpaEnvironmentFactory.instance.appContext.getString(R.string.network_dashboard_title)
57     }
58 
buildInjectEntrynull59     fun buildInjectEntry(): SettingsEntryBuilder {
60         return SettingsEntryBuilder.createInject(owner = owner)
61             .setUiLayoutFn {
62                 val summary = stringResource(getSummaryResId())
63                 Preference(object : PreferenceModel {
64                     override val title = stringResource(R.string.network_dashboard_title)
65                     override val summary = { summary }
66                     override val onClick = navigator(name)
67                     override val icon = @Composable {
68                         SettingsIcon(imageVector = Icons.Outlined.Wifi)
69                     }
70                 })
71             }
72     }
73 
74     @Composable
getSummaryResIdnull75     private fun getSummaryResId(): Int {
76         val isMobileAvailable = remember { isMobileAvailable() }
77         var summary = if (isMobileAvailable) {
78             R.string.network_dashboard_summary_mobile
79         } else {
80             R.string.network_dashboard_summary_no_mobile
81         }
82         return summary
83     }
84 
isMobileAvailablenull85     private fun isMobileAvailable(): Boolean {
86         val context = SpaEnvironmentFactory.instance.appContext
87         return !isUserRestricted(context) && !Utils.isWifiOnly(context)
88     }
89 
isUserRestrictednull90     private fun isUserRestricted(context: Context): Boolean {
91         val userManager: UserManager = context.getSystemService(UserManager::class.java)!!
92         return !userManager.isAdminUser || RestrictedLockUtilsInternal.hasBaseUserRestriction(
93             context, DISALLOW_CONFIG_MOBILE_NETWORKS, myUserId()
94         )
95     }
96 }
97