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 package com.android.server.bluetooth
17 
18 import android.bluetooth.BluetoothAdapter
19 import android.content.Context
20 import android.content.res.Resources
21 import android.os.HandlerThread
22 import android.os.UserManager
23 import com.android.server.SystemService
24 import com.android.server.SystemService.TargetUser
25 
26 class BluetoothService(context: Context) : SystemService(context) {
27     private val mHandlerThread: HandlerThread
28     private val mBluetoothManagerService: BluetoothManagerService
29     private var mInitialized = false
30 
31     init {
32         mHandlerThread = HandlerThread("BluetoothManagerService")
33         mHandlerThread.start()
34         mBluetoothManagerService = BluetoothManagerService(context, mHandlerThread.getLooper())
35     }
36 
initializenull37     private fun initialize(user: TargetUser) {
38         if (!mInitialized) {
39             mBluetoothManagerService.handleOnBootPhase(user.userHandle)
40             mInitialized = true
41         }
42     }
43 
onStartnull44     override fun onStart() {}
45 
onBootPhasenull46     override fun onBootPhase(phase: Int) {
47         if (phase == SystemService.PHASE_SYSTEM_SERVICES_READY) {
48             publishBinderService(
49                 BluetoothAdapter.BLUETOOTH_MANAGER_SERVICE,
50                 mBluetoothManagerService.getBinder()
51             )
52         }
53     }
54 
onUserStartingnull55     override fun onUserStarting(user: TargetUser) {
56         if (
57             !UserManager.isHeadlessSystemUserMode() ||
58                 Resources.getSystem()
59                     .getBoolean(
60                         Resources.getSystem()
61                             .getIdentifier("config_bootToHeadlessSystemUser", "bool", "android")
62                     )
63         ) {
64             initialize(user)
65         }
66     }
67 
onUserSwitchingnull68     override fun onUserSwitching(_from: TargetUser?, to: TargetUser) {
69         if (!mInitialized) {
70             initialize(to)
71         } else {
72             mBluetoothManagerService.onSwitchUser(to.userHandle)
73         }
74     }
75 
onUserUnlockingnull76     override fun onUserUnlocking(user: TargetUser) {
77         mBluetoothManagerService.handleOnUnlockUser(user.userHandle)
78     }
79 }
80