1 /* 2 * Copyright (C) 2022 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 17 package com.android.bluetooth.bass_client; 18 19 import android.bluetooth.BluetoothAdapter; 20 import android.bluetooth.BluetoothDevice; 21 import android.bluetooth.le.BluetoothLeScanner; 22 import android.os.Looper; 23 import android.util.Log; 24 25 import com.android.bluetooth.Utils; 26 import com.android.bluetooth.btservice.AdapterService; 27 import com.android.internal.annotations.VisibleForTesting; 28 29 /** Factory class for object initialization to help with unit testing */ 30 public class BassObjectsFactory { 31 private static final String TAG = BassObjectsFactory.class.getSimpleName(); 32 33 private static BassObjectsFactory sInstance; 34 private static final Object INSTANCE_LOCK = new Object(); 35 BassObjectsFactory()36 private BassObjectsFactory() {} 37 38 /** 39 * Get the singleton instance of object factory 40 * 41 * @return the singleton instance, guaranteed not null 42 */ getInstance()43 public static BassObjectsFactory getInstance() { 44 synchronized (INSTANCE_LOCK) { 45 if (sInstance == null) { 46 sInstance = new BassObjectsFactory(); 47 } 48 } 49 return sInstance; 50 } 51 52 /** 53 * Allow unit tests to substitute BassObjectsFactory with a test instance 54 * 55 * @param objectsFactory a test instance of the BassObjectsFactory 56 */ 57 @VisibleForTesting setInstanceForTesting(BassObjectsFactory objectsFactory)58 public static void setInstanceForTesting(BassObjectsFactory objectsFactory) { 59 Utils.enforceInstrumentationTestMode(); 60 synchronized (INSTANCE_LOCK) { 61 Log.d(TAG, "setInstanceForTesting(), set to " + objectsFactory); 62 sInstance = objectsFactory; 63 } 64 } 65 66 /** 67 * Make a {@link BassClientStateMachine} 68 * 69 * @param device the remote device associated with this state machine 70 * @param svc the bass client service 71 * @param looper the thread that the state machine is supposed to run on 72 * @return a state machine that is initialized and started, ready to go 73 */ makeStateMachine( BluetoothDevice device, BassClientService svc, AdapterService adapterService, Looper looper)74 public BassClientStateMachine makeStateMachine( 75 BluetoothDevice device, 76 BassClientService svc, 77 AdapterService adapterService, 78 Looper looper) { 79 return BassClientStateMachine.make(device, svc, adapterService, looper); 80 } 81 82 /** 83 * Destroy a state machine 84 * 85 * @param stateMachine to be destroyed. Cannot be used after this call. 86 */ destroyStateMachine(BassClientStateMachine stateMachine)87 public void destroyStateMachine(BassClientStateMachine stateMachine) { 88 BassClientStateMachine.destroy(stateMachine); 89 } 90 91 /** 92 * Get a {@link BluetoothLeScannerWrapper} object 93 * 94 * @param adapter bluetooth adapter 95 * @return a bluetooth LE scanner 96 */ getBluetoothLeScannerWrapper(BluetoothAdapter adapter)97 public BluetoothLeScannerWrapper getBluetoothLeScannerWrapper(BluetoothAdapter adapter) { 98 BluetoothLeScanner bluetoothLeScanner = adapter.getBluetoothLeScanner(); 99 if (bluetoothLeScanner == null) { 100 return null; 101 } else { 102 return new BluetoothLeScannerWrapper(bluetoothLeScanner); 103 } 104 } 105 } 106