1 /*
2  * Copyright 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 @file:JvmName("SatelliteModeListener")
17 
18 package com.android.server.bluetooth.satellite
19 
20 import android.content.ContentResolver
21 import android.os.Looper
22 import com.android.server.bluetooth.Log
23 import com.android.server.bluetooth.initializeRadioModeListener
24 
25 /**
26  * constant copied from {@link Settings.Global}
27  *
28  * TODO(b/274636414): Migrate to official API in Android V.
29  */
30 internal const val SETTINGS_SATELLITE_MODE_RADIOS = "satellite_mode_radios"
31 
32 /**
33  * constant copied from {@link Settings.Global}
34  *
35  * TODO(b/274636414): Migrate to official API in Android V.
36  */
37 internal const val SETTINGS_SATELLITE_MODE_ENABLED = "satellite_mode_enabled"
38 
39 private const val TAG = "SatelliteModeListener"
40 
41 public var isOn = false
42     private set
43 
44 /** Listen on satellite mode and trigger the callback if it has changed */
initializenull45 public fun initialize(looper: Looper, resolver: ContentResolver, callback: (m: Boolean) -> Unit) {
46     isOn =
47         initializeRadioModeListener(
48             looper,
49             resolver,
50             SETTINGS_SATELLITE_MODE_RADIOS,
51             SETTINGS_SATELLITE_MODE_ENABLED,
52             fun(newMode: Boolean) {
53                 val previousMode = isOn
54                 isOn = newMode
55                 if (previousMode == isOn) {
56                     Log.d(TAG, "Ignore satellite mode change because is already: " + isOn)
57                     return
58                 }
59                 Log.i(TAG, "Trigger callback with state: $isOn")
60                 callback(isOn)
61             }
62         )
63     Log.i(TAG, "Initialized successfully with state: $isOn")
64 }
65