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