1 /*
<lambda>null2  * 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.bluetooth
18 
19 import android.content.Context
20 import android.os.UserManager
21 import androidx.appcompat.app.AlertDialog
22 import com.android.settings.R
23 import com.android.settingslib.spaprivileged.framework.common.devicePolicyManager
24 import com.android.settingslib.spaprivileged.framework.common.userManager
25 
26 object RequestPermissionHelper {
27     fun requestEnable(
28         context: Context,
29         appLabel: CharSequence?,
30         timeout: Int,
31         onAllow: () -> Unit,
32         onDeny: () -> Unit,
33     ): AlertDialog? {
34         if (context.resources.getBoolean(R.bool.auto_confirm_bluetooth_activation_dialog)) {
35             // Don't even show the dialog if configured this way
36             onAllow()
37             return null
38         }
39         return AlertDialog.Builder(context).apply {
40             setMessage(context.getEnableMessage(timeout, appLabel))
41             setPositiveButton(R.string.allow) { _, _ ->
42                 if (context.isDisallowBluetooth()) onDeny() else onAllow()
43             }
44             setNegativeButton(R.string.deny) { _, _ -> onDeny() }
45             setOnCancelListener { onDeny() }
46         }.create()
47     }
48 
49     fun requestDisable(
50         context: Context,
51         appLabel: CharSequence?,
52         onAllow: () -> Unit,
53         onDeny: () -> Unit,
54     ): AlertDialog? {
55         if (context.resources.getBoolean(R.bool.auto_confirm_bluetooth_activation_dialog)) {
56             // Don't even show the dialog if configured this way
57             onAllow()
58             return null
59         }
60         return AlertDialog.Builder(context).apply {
61             setMessage(context.getDisableMessage(appLabel))
62             setPositiveButton(R.string.allow) { _, _ -> onAllow() }
63             setNegativeButton(R.string.deny) { _, _ -> onDeny() }
64             setOnCancelListener { onDeny() }
65         }.create()
66     }
67 }
68 
69 // If Bluetooth is disallowed, don't try to enable it, show policy transparency message instead.
Contextnull70 private fun Context.isDisallowBluetooth() =
71     if (userManager.hasUserRestriction(UserManager.DISALLOW_BLUETOOTH)) {
72         devicePolicyManager.createAdminSupportIntent(UserManager.DISALLOW_BLUETOOTH)
73             ?.let {
74                 it.setPackage(packageName)
75                 startActivity(it)
76             }
77         true
78     } else false
79 
Contextnull80 private fun Context.getEnableMessage(timeout: Int, name: CharSequence?): String = when {
81     timeout < 0 -> when (name) {
82         null -> getString(R.string.bluetooth_ask_enablement_no_name)
83         else -> getString(R.string.bluetooth_ask_enablement, name)
84     }
85 
86     timeout == BluetoothDiscoverableEnabler.DISCOVERABLE_TIMEOUT_NEVER -> when (name) {
87         null -> getString(R.string.bluetooth_ask_enablement_and_lasting_discovery_no_name)
88         else -> getString(R.string.bluetooth_ask_enablement_and_lasting_discovery, name)
89     }
90 
91     else -> when (name) {
92         null -> getString(R.string.bluetooth_ask_enablement_and_discovery_no_name, timeout)
93         else -> getString(R.string.bluetooth_ask_enablement_and_discovery, name, timeout)
94     }
95 }
96 
Contextnull97 private fun Context.getDisableMessage(name: CharSequence?): String =
98     when (name) {
99         null -> getString(R.string.bluetooth_ask_disablement_no_name)
100         else -> getString(R.string.bluetooth_ask_disablement, name)
101     }
102