1 /*
2  * Copyright 2024 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.le_scan;
18 
19 import android.content.Context;
20 import android.os.Looper;
21 import android.util.Log;
22 
23 import com.android.bluetooth.Utils;
24 import com.android.bluetooth.btservice.AdapterService;
25 import com.android.bluetooth.btservice.BluetoothAdapterProxy;
26 
27 /** Factory class for object initialization to help with unit testing */
28 public class ScanObjectsFactory {
29     private static final String TAG = ScanObjectsFactory.class.getSimpleName();
30     private static ScanObjectsFactory sInstance;
31     private static final Object INSTANCE_LOCK = new Object();
32 
ScanObjectsFactory()33     private ScanObjectsFactory() {}
34 
35     /**
36      * Get the singleton instance of object factory
37      *
38      * @return the singleton instance, guaranteed not null
39      */
getInstance()40     public static ScanObjectsFactory getInstance() {
41         synchronized (INSTANCE_LOCK) {
42             if (sInstance == null) {
43                 sInstance = new ScanObjectsFactory();
44             }
45         }
46         return sInstance;
47     }
48 
49     /**
50      * Allow unit tests to substitute ScanObjectsFactory with a test instance
51      *
52      * @param objectsFactory a test instance of the ScanObjectsFactory
53      */
setInstanceForTesting(ScanObjectsFactory objectsFactory)54     public static void setInstanceForTesting(ScanObjectsFactory objectsFactory) {
55         Utils.enforceInstrumentationTestMode();
56         synchronized (INSTANCE_LOCK) {
57             Log.d(TAG, "setInstanceForTesting(), set to " + objectsFactory);
58             sInstance = objectsFactory;
59         }
60     }
61 
getScanNativeInterface()62     public ScanNativeInterface getScanNativeInterface() {
63         return ScanNativeInterface.getInstance();
64     }
65 
66     /**
67      * Create an instance of ScanManager
68      *
69      * @param context a Context instance
70      * @param scanHelper a TransitionalScanHelper instance
71      * @param adapterService an AdapterService instance
72      * @param bluetoothAdapterProxy a bluetoothAdapterProxy instance
73      * @param looper the looper to be used for processing messages
74      * @return the created ScanManager instance
75      */
createScanManager( Context context, TransitionalScanHelper scanHelper, AdapterService adapterService, BluetoothAdapterProxy bluetoothAdapterProxy, Looper looper)76     public ScanManager createScanManager(
77             Context context,
78             TransitionalScanHelper scanHelper,
79             AdapterService adapterService,
80             BluetoothAdapterProxy bluetoothAdapterProxy,
81             Looper looper) {
82         return new ScanManager(context, scanHelper, adapterService, bluetoothAdapterProxy, looper);
83     }
84 
createPeriodicScanManager(AdapterService adapterService)85     public PeriodicScanManager createPeriodicScanManager(AdapterService adapterService) {
86         return new PeriodicScanManager(adapterService);
87     }
88 }
89