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