1 /*
2  * Copyright 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 // @file:JvmName("BleScanSettingListener")
18 
19 package com.android.server.bluetooth
20 
21 import android.content.ContentResolver
22 import android.database.ContentObserver
23 import android.os.Handler
24 import android.os.Looper
25 import android.provider.Settings
26 
27 private const val TAG = "BleScanSettingListener"
28 
29 object BleScanSettingListener {
30     @JvmStatic
31     var isScanAllowed = false
32         private set
33 
34     /**
35      * Listen on Ble Scan setting and trigger the callback when scanning is no longer enabled
36      *
37      * @param callback: The callback to trigger when there is a mode change, pass new mode as
38      *   parameter
39      * @return The initial value of the radio
40      */
41     @JvmStatic
initializenull42     fun initialize(looper: Looper, resolver: ContentResolver, callback: () -> Unit) {
43         val notifyForDescendants = false
44 
45         resolver.registerContentObserver(
46             Settings.Global.getUriFor(Settings.Global.BLE_SCAN_ALWAYS_AVAILABLE),
47             notifyForDescendants,
48             object : ContentObserver(Handler(looper)) {
49                 override fun onChange(selfChange: Boolean) {
50                     val previousValue = isScanAllowed
51                     isScanAllowed = getScanSettingValue(resolver)
52                     if (isScanAllowed) {
53                         Log.i(TAG, "Ble Scan mode is now allowed. Nothing to do")
54                         return
55                     } else if (previousValue == isScanAllowed) {
56                         Log.i(TAG, "Ble Scan mode was already considered as false. Discarding")
57                         return
58                     } else {
59                         Log.i(TAG, "Trigger callback to disable BLE_ONLY mode")
60                         callback()
61                     }
62                 }
63             }
64         )
65         isScanAllowed = getScanSettingValue(resolver)
66     }
67 
68     /**
69      * Check if Bluetooth is impacted by the radio and fetch global mode status
70      *
71      * @return whether Bluetooth should consider this radio or not
72      */
getScanSettingValuenull73     private fun getScanSettingValue(resolver: ContentResolver): Boolean {
74         try {
75             return Settings.Global.getInt(resolver, Settings.Global.BLE_SCAN_ALWAYS_AVAILABLE) != 0
76         } catch (e: Settings.SettingNotFoundException) {
77             Log.i(TAG, "Settings not found. Default to false")
78             return false
79         }
80     }
81 }
82