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.annotation.NonNull; 20 import android.content.ContentResolver; 21 import android.os.Binder; 22 import android.os.IBinder; 23 import android.provider.Settings; 24 25 import com.android.internal.annotations.VisibleForTesting; 26 27 /** Proxy class for method calls to help with unit testing */ 28 class BluetoothServerProxy { 29 private static final String TAG = BluetoothServerProxy.class.getSimpleName(); 30 private static final Object INSTANCE_LOCK = new Object(); 31 private static BluetoothServerProxy sInstance; 32 BluetoothServerProxy()33 private BluetoothServerProxy() {} 34 35 /** 36 * Get the singleton instance of proxy 37 * 38 * @return the singleton instance, guaranteed not null 39 */ getInstance()40 static @NonNull BluetoothServerProxy getInstance() { 41 synchronized (INSTANCE_LOCK) { 42 if (sInstance == null) { 43 sInstance = new BluetoothServerProxy(); 44 } 45 } 46 return sInstance; 47 } 48 49 /** Allow unit tests to substitute proxy with a test instance */ 50 @VisibleForTesting setInstanceForTesting(BluetoothServerProxy proxy)51 static void setInstanceForTesting(BluetoothServerProxy proxy) { 52 synchronized (INSTANCE_LOCK) { 53 Log.d(TAG, "setInstanceForTesting(), set to " + proxy); 54 sInstance = proxy; 55 } 56 } 57 createAdapterBinder(IBinder binder)58 AdapterBinder createAdapterBinder(IBinder binder) { 59 return new AdapterBinder(binder); 60 } 61 settingsSecureGetString(ContentResolver contentResolver, String name)62 String settingsSecureGetString(ContentResolver contentResolver, String name) { 63 return Settings.Secure.getString(contentResolver, name); 64 } 65 settingsGlobalGetInt(ContentResolver contentResolver, String name, int def)66 int settingsGlobalGetInt(ContentResolver contentResolver, String name, int def) { 67 return Settings.Global.getInt(contentResolver, name, def); 68 } 69 getBluetoothPersistedState(ContentResolver resolver, int defaultValue)70 int getBluetoothPersistedState(ContentResolver resolver, int defaultValue) { 71 return Settings.Global.getInt(resolver, Settings.Global.BLUETOOTH_ON, defaultValue); 72 } 73 setBluetoothPersistedState(ContentResolver resolver, int state)74 void setBluetoothPersistedState(ContentResolver resolver, int state) { 75 Log.i(TAG, "setBluetoothPersistedState(" + state + ")"); 76 // waive WRITE_SECURE_SETTINGS permission check 77 final long callingIdentity = Binder.clearCallingIdentity(); 78 try { 79 Settings.Global.putInt(resolver, Settings.Global.BLUETOOTH_ON, state); 80 } finally { 81 Binder.restoreCallingIdentity(callingIdentity); 82 } 83 } 84 } 85