• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 android.bluetooth;
17 
18 import android.annotation.NonNull;
19 import android.annotation.SystemApi;
20 import android.annotation.SystemApi.Client;
21 import android.app.SystemServiceRegistry;
22 import android.content.Context;
23 import android.os.BluetoothServiceManager;
24 
25 import java.util.function.Consumer;
26 
27 /**
28  * Class for performing registration for Bluetooth service.
29  *
30  * @hide
31  */
32 @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
33 public class BluetoothFrameworkInitializer {
BluetoothFrameworkInitializer()34   private BluetoothFrameworkInitializer() {}
35 
36   private static volatile BluetoothServiceManager sBluetoothServiceManager;
37   private static volatile Consumer<Context> sBinderCallsStatsInitializer;
38 
39   /**
40    * Sets an instance of {@link BluetoothServiceManager} that allows
41    * the bluetooth mainline module to register/obtain bluetooth binder services. This is called
42    * by the platform during the system initialization.
43    *
44    * @param bluetoothServiceManager instance of {@link BluetoothServiceManager} that allows
45    * the bluetooth mainline module to register/obtain bluetoothd binder services.
46    */
setBluetoothServiceManager( @onNull BluetoothServiceManager bluetoothServiceManager)47   public static void setBluetoothServiceManager(
48       @NonNull BluetoothServiceManager bluetoothServiceManager) {
49     if (sBluetoothServiceManager != null) {
50       throw new IllegalStateException("setBluetoothServiceManager called twice!");
51     }
52 
53     if (bluetoothServiceManager == null) {
54       throw new IllegalArgumentException("bluetoothServiceManager must not be null");
55     }
56 
57     sBluetoothServiceManager = bluetoothServiceManager;
58   }
59 
60   /** @hide */
getBluetoothServiceManager()61   public static BluetoothServiceManager getBluetoothServiceManager() {
62     return sBluetoothServiceManager;
63   }
64 
65   /**
66    * Called by {@link ActivityThread}'s static initializer to set the callback enabling Bluetooth
67    * {@link BinderCallsStats} registeration.
68    *
69    * @param binderCallsStatsConsumer called by bluetooth service to create a new binder calls
70    *        stats observer
71    */
setBinderCallsStatsInitializer( @onNull Consumer<Context> binderCallsStatsConsumer)72   public static void setBinderCallsStatsInitializer(
73       @NonNull Consumer<Context> binderCallsStatsConsumer) {
74     if (sBinderCallsStatsInitializer != null) {
75       throw new IllegalStateException("setBinderCallsStatsInitializer called twice!");
76     }
77 
78     if (binderCallsStatsConsumer == null) {
79       throw new IllegalArgumentException("binderCallsStatsConsumer must not be null");
80     }
81 
82     sBinderCallsStatsInitializer = binderCallsStatsConsumer;
83   }
84 
85   /** @hide */
initializeBinderCallsStats(Context context)86   public static void initializeBinderCallsStats(Context context) {
87     if (sBinderCallsStatsInitializer == null) {
88       throw new IllegalStateException("sBinderCallsStatsInitializer has not been set");
89     }
90     sBinderCallsStatsInitializer.accept(context);
91   }
92 
93   /**
94    * Called by {@link SystemServiceRegistry}'s static initializer and registers BT service
95    * to {@link Context}, so that {@link Context#getSystemService} can return them.
96    *
97    * @throws IllegalStateException if this is called from anywhere besides
98    * {@link SystemServiceRegistry}
99    */
registerServiceWrappers()100   public static void registerServiceWrappers() {
101     SystemServiceRegistry.registerContextAwareService(Context.BLUETOOTH_SERVICE,
102         BluetoothManager.class, context -> new BluetoothManager(context));
103   }
104 }
105