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