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