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.os.HandlerThread
21 import android.os.UserManager
22 import com.android.server.SystemService
23 import com.android.server.SystemService.TargetUser
24 
25 class BluetoothService(context: Context) : SystemService(context) {
26     private val mHandlerThread: HandlerThread
27     private val mBluetoothManagerService: BluetoothManagerService
28     private var mInitialized = false
29 
30     init {
31         mHandlerThread = HandlerThread("BluetoothManagerService")
32         mHandlerThread.start()
33         mBluetoothManagerService = BluetoothManagerService(context, mHandlerThread.getLooper())
34     }
35 
initializenull36     private fun initialize(user: TargetUser) {
37         if (!mInitialized) {
38             mBluetoothManagerService.handleOnBootPhase(user.userHandle)
39             mInitialized = true
40         }
41     }
42 
onStartnull43     override fun onStart() {}
44 
onBootPhasenull45     override fun onBootPhase(phase: Int) {
46         if (phase == SystemService.PHASE_SYSTEM_SERVICES_READY) {
47             publishBinderService(
48                 BluetoothAdapter.BLUETOOTH_MANAGER_SERVICE,
49                 mBluetoothManagerService.getBinder()
50             )
51         }
52     }
53 
onUserStartingnull54     override fun onUserStarting(user: TargetUser) {
55         if (!UserManager.isHeadlessSystemUserMode()) {
56             initialize(user)
57         }
58     }
59 
onUserSwitchingnull60     override fun onUserSwitching(_from: TargetUser?, to: TargetUser) {
61         if (!mInitialized) {
62             initialize(to)
63         } else {
64             mBluetoothManagerService.onSwitchUser(to.userHandle)
65         }
66     }
67 
onUserUnlockingnull68     override fun onUserUnlocking(user: TargetUser) {
69         mBluetoothManagerService.handleOnUnlockUser(user.userHandle)
70     }
71 }
72