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.os.Looper; 22 import android.util.Log; 23 24 import com.android.bluetooth.Utils; 25 import com.android.internal.annotations.VisibleForTesting; 26 27 /** 28 * Factory class for object initialization to help with unit testing 29 */ 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, Looper looper)73 public BassClientStateMachine makeStateMachine(BluetoothDevice device, 74 BassClientService svc, Looper looper) { 75 return BassClientStateMachine.make(device, svc, looper); 76 } 77 78 /** 79 * Destroy a state machine 80 * 81 * @param stateMachine to be destroyed. Cannot be used after this call. 82 */ destroyStateMachine(BassClientStateMachine stateMachine)83 public void destroyStateMachine(BassClientStateMachine stateMachine) { 84 BassClientStateMachine.destroy(stateMachine); 85 } 86 87 /** 88 * Get a {@link BluetoothLeScannerWrapper} object 89 * 90 * @param adapter bluetooth adapter 91 * @return a bluetooth LE scanner 92 */ getBluetoothLeScannerWrapper(BluetoothAdapter adapter)93 public BluetoothLeScannerWrapper getBluetoothLeScannerWrapper(BluetoothAdapter adapter) { 94 return new BluetoothLeScannerWrapper(adapter.getBluetoothLeScanner()); 95 } 96 } 97