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