• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 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 package com.android.bluetooth.btservice;
17 
18 import android.bluetooth.BluetoothAdapter;
19 import android.util.Log;
20 
21 import com.android.bluetooth.Utils;
22 import com.android.internal.annotations.VisibleForTesting;
23 
24 /**
25  * A proxy class that facilitates testing of the ScanManager.
26  *
27  * <p>This is necessary due to the "final" attribute of the BluetoothAdapter class. In order to test
28  * the correct functioning of the ScanManager class, the final class must be put into a container
29  * that can be mocked correctly.
30  */
31 public class BluetoothAdapterProxy {
32     private static final String TAG = BluetoothAdapterProxy.class.getSimpleName();
33 
34     private static BluetoothAdapterProxy sInstance;
35     private static final Object INSTANCE_LOCK = new Object();
36 
BluetoothAdapterProxy()37     private BluetoothAdapterProxy() {}
38 
39     /**
40      * Get the singleton instance of proxy.
41      *
42      * @return the singleton instance, guaranteed not null
43      */
getInstance()44     public static BluetoothAdapterProxy getInstance() {
45         synchronized (INSTANCE_LOCK) {
46             if (sInstance == null) {
47                 sInstance = new BluetoothAdapterProxy();
48             }
49             return sInstance;
50         }
51     }
52 
53     /**
54      * Proxy function that calls {@link BluetoothAdapter#isOffloadedFilteringSupported()}.
55      *
56      * @return whether the offloaded scan filtering is supported
57      */
isOffloadedScanFilteringSupported()58     public boolean isOffloadedScanFilteringSupported() {
59         BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
60         return adapter.isOffloadedFilteringSupported();
61     }
62 
63     /**
64      * Allow unit tests to substitute BluetoothAdapterProxy with a test instance
65      *
66      * @param proxy a test instance of the BluetoothAdapterProxy
67      */
68     @VisibleForTesting
setInstanceForTesting(BluetoothAdapterProxy proxy)69     public static void setInstanceForTesting(BluetoothAdapterProxy proxy) {
70         Utils.enforceInstrumentationTestMode();
71         synchronized (INSTANCE_LOCK) {
72             Log.d(TAG, "setInstanceForTesting(), set to " + proxy);
73             sInstance = proxy;
74         }
75     }
76 }
77