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