1 /* 2 * Copyright 2023 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.server.bluetooth; 18 19 import android.content.ContentResolver; 20 import android.os.HandlerThread; 21 import android.provider.Settings; 22 import android.util.Log; 23 24 import com.android.internal.annotations.VisibleForTesting; 25 26 /** 27 * Proxy class for method calls to help with unit testing 28 */ 29 public class BluetoothServerProxy { 30 private static final String TAG = BluetoothServerProxy.class.getSimpleName(); 31 private static final Object INSTANCE_LOCK = new Object(); 32 private static BluetoothServerProxy sInstance; 33 BluetoothServerProxy()34 private BluetoothServerProxy() { 35 } 36 37 /** 38 * Get the singleton instance of proxy 39 * 40 * @return the singleton instance, guaranteed not null 41 */ getInstance()42 public static BluetoothServerProxy getInstance() { 43 synchronized (INSTANCE_LOCK) { 44 if (sInstance == null) { 45 sInstance = new BluetoothServerProxy(); 46 } 47 } 48 return sInstance; 49 } 50 51 /** 52 * Allow unit tests to substitute BluetoothPbapMethodCallProxy with a test instance 53 * 54 * @param proxy a test instance of the BluetoothPbapMethodCallProxy 55 */ 56 @VisibleForTesting setInstanceForTesting(BluetoothServerProxy proxy)57 public static void setInstanceForTesting(BluetoothServerProxy proxy) { 58 synchronized (INSTANCE_LOCK) { 59 Log.d(TAG, "setInstanceForTesting(), set to " + proxy); 60 sInstance = proxy; 61 } 62 } 63 64 /** 65 * Proxies {@link com.android.server.bluetooth.BluetoothManagerService.BluetoothHandler}. 66 */ createBluetoothHandler( BluetoothManagerService.BluetoothHandler bluetoothHandler)67 public BluetoothManagerService.BluetoothHandler createBluetoothHandler( 68 BluetoothManagerService.BluetoothHandler bluetoothHandler) { 69 return bluetoothHandler; 70 } 71 72 /** 73 * Proxies {@link com.android.server.bluetooth.BluetoothManagerService.BluetoothHandler}. 74 */ newBluetoothHandler( BluetoothManagerService.BluetoothHandler bluetoothHandler)75 public BluetoothManagerService.BluetoothHandler newBluetoothHandler( 76 BluetoothManagerService.BluetoothHandler bluetoothHandler) { 77 return bluetoothHandler; 78 } 79 80 /** 81 * Proxies {@link HandlerThread(String)}. 82 */ createHandlerThread(String name)83 public HandlerThread createHandlerThread(String name) { 84 return new HandlerThread(name); 85 } 86 87 /** 88 * Proxies {@link android.provider.Settings.Secure.getString}. 89 */ settingsSecureGetString(ContentResolver contentResolver, String name)90 public String settingsSecureGetString(ContentResolver contentResolver, String name) { 91 return Settings.Secure.getString(contentResolver, name); 92 } 93 94 /** 95 * Proxies 96 * {@link com.android.server.bluetooth.BluetoothManagerService.BluetoothHandler.sendMessage}. 97 */ handlerSendWhatMessage( com.android.server.bluetooth.BluetoothManagerService.BluetoothHandler handler, int what)98 public boolean handlerSendWhatMessage( 99 com.android.server.bluetooth.BluetoothManagerService.BluetoothHandler handler, 100 int what) { 101 return handler.sendMessage(handler.obtainMessage(what)); 102 } 103 } 104