• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2022 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.gatt;
18 
19 import android.util.Log;
20 
21 import com.android.bluetooth.Utils;
22 /**
23  * Factory class for object initialization to help with unit testing
24  */
25 public class GattObjectsFactory {
26     private static final String TAG = GattObjectsFactory.class.getSimpleName();
27     private static GattObjectsFactory sInstance;
28     private static final Object INSTANCE_LOCK = new Object();
29 
GattObjectsFactory()30     private GattObjectsFactory() {
31     }
32 
33     /**
34      * Get the singleton instance of object factory
35      *
36      * @return the singleton instance, guaranteed not null
37      */
getInstance()38     public static GattObjectsFactory getInstance() {
39         synchronized (INSTANCE_LOCK) {
40             if (sInstance == null) {
41                 sInstance = new GattObjectsFactory();
42             }
43         }
44         return sInstance;
45     }
46 
47     /**
48      * Allow unit tests to substitute GattObjectsFactory with a test instance
49      *
50      * @param objectsFactory a test instance of the GattObjectsFactory
51      */
setInstanceForTesting(GattObjectsFactory objectsFactory)52     static void setInstanceForTesting(GattObjectsFactory objectsFactory) {
53         Utils.enforceInstrumentationTestMode();
54         synchronized (INSTANCE_LOCK) {
55             Log.d(TAG, "setInstanceForTesting(), set to " + objectsFactory);
56             sInstance = objectsFactory;
57         }
58     }
59 
getNativeInterface()60     public GattNativeInterface getNativeInterface() {
61         return GattNativeInterface.getInstance();
62     }
63 
getScanNativeInterface()64     public ScanNativeInterface getScanNativeInterface() {
65         return ScanNativeInterface.getInstance();
66     }
67 }
68