Fetch currently loaded bluetooth name

Test: m
ROBOTEST_FILTER="BluetoothMaxConnectedAudioDevicesPreferenceControllerTest"
RunSettingsRoboTests -j40

Test: m ROBOTEST_FILTER="com.android.settings.bluetooth" RunSettingsRoboTests
Test: Open developer settings
Bug: 257158801
Change-Id: Ib40a6264d8d6908103d76b6401ddcfd3ffa7dd88
This commit is contained in:
William Escande
2022-11-03 12:09:55 -07:00
parent 2552ae08ae
commit 723df89aaa
4 changed files with 78 additions and 22 deletions

View File

@@ -24,6 +24,7 @@ import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.PowerManager; import android.os.PowerManager;
import android.os.UserManager; import android.os.UserManager;
import android.util.Log; import android.util.Log;
@@ -125,8 +126,15 @@ public final class BluetoothPermissionRequest extends BroadcastReceiver {
// Create an intent triggered by clicking on the // Create an intent triggered by clicking on the
// "Clear All Notifications" button // "Clear All Notifications" button
String bluetoothName;
try {
bluetoothName = Utils.findBluetoothPackageName(context);
} catch (NameNotFoundException e) {
e.printStackTrace();
return;
}
Intent deleteIntent = new Intent(BluetoothDevice.ACTION_CONNECTION_ACCESS_REPLY); Intent deleteIntent = new Intent(BluetoothDevice.ACTION_CONNECTION_ACCESS_REPLY);
deleteIntent.setPackage("com.android.bluetooth"); deleteIntent.setPackage(bluetoothName);
deleteIntent.putExtra(BluetoothDevice.EXTRA_DEVICE, mDevice); deleteIntent.putExtra(BluetoothDevice.EXTRA_DEVICE, mDevice);
deleteIntent.putExtra(BluetoothDevice.EXTRA_CONNECTION_ACCESS_RESULT, deleteIntent.putExtra(BluetoothDevice.EXTRA_CONNECTION_ACCESS_RESULT,
BluetoothDevice.CONNECTION_ACCESS_NO); BluetoothDevice.CONNECTION_ACCESS_NO);

51
src/com/android/settings/bluetooth/Utils.java Executable file → Normal file
View File

@@ -16,11 +16,18 @@
package com.android.settings.bluetooth; package com.android.settings.bluetooth;
import static android.os.Process.BLUETOOTH_UID;
import android.app.settings.SettingsEnums; import android.app.settings.SettingsEnums;
import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothProfile; import android.bluetooth.BluetoothProfile;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.UserHandle;
import android.provider.DeviceConfig; import android.provider.DeviceConfig;
import android.provider.Settings; import android.provider.Settings;
import android.text.TextUtils; import android.text.TextUtils;
@@ -189,4 +196,48 @@ public final class Utils {
} }
return false; return false;
} }
/**
* Returns the Bluetooth Package name
*/
public static String findBluetoothPackageName(Context context)
throws NameNotFoundException {
// this activity will always be in the package where the rest of Bluetooth lives
String sentinelActivity = "com.android.bluetooth.opp.BluetoothOppLauncherActivity";
PackageManager packageManager = context.createContextAsUser(UserHandle.SYSTEM, 0)
.getPackageManager();
String[] allPackages = packageManager.getPackagesForUid(BLUETOOTH_UID);
String matchedPackage = null;
for (String candidatePackage : allPackages) {
PackageInfo packageInfo;
try {
packageInfo =
packageManager.getPackageInfo(
candidatePackage,
PackageManager.GET_ACTIVITIES
| PackageManager.MATCH_ANY_USER
| PackageManager.MATCH_UNINSTALLED_PACKAGES
| PackageManager.MATCH_DISABLED_COMPONENTS);
} catch (NameNotFoundException e) {
// rethrow
throw e;
}
if (packageInfo.activities == null) {
continue;
}
for (ActivityInfo activity : packageInfo.activities) {
if (sentinelActivity.equals(activity.name)) {
if (matchedPackage == null) {
matchedPackage = candidatePackage;
} else {
throw new NameNotFoundException("multiple main bluetooth packages found");
}
}
}
}
if (matchedPackage != null) {
return matchedPackage;
}
throw new NameNotFoundException("Could not find main bluetooth package");
}
} }

View File

@@ -16,9 +16,8 @@
package com.android.settings.development; package com.android.settings.development;
import android.bluetooth.BluetoothManager;
import android.content.Context; import android.content.Context;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.os.SystemProperties; import android.os.SystemProperties;
import androidx.annotation.VisibleForTesting; import androidx.annotation.VisibleForTesting;
@@ -42,18 +41,15 @@ public class BluetoothMaxConnectedAudioDevicesPreferenceController extends
private int mDefaultMaxConnectedAudioDevices = 0; private int mDefaultMaxConnectedAudioDevices = 0;
private final BluetoothManager mBluetoothManager;
public BluetoothMaxConnectedAudioDevicesPreferenceController(Context context) { public BluetoothMaxConnectedAudioDevicesPreferenceController(Context context) {
super(context); super(context);
try { mBluetoothManager = context.getSystemService(BluetoothManager.class);
Resources res = context.getPackageManager().getResourcesForApplication(
"com.android.bluetooth"); mDefaultMaxConnectedAudioDevices =
mDefaultMaxConnectedAudioDevices = res.getInteger(res.getIdentifier( mBluetoothManager.getAdapter().getMaxConnectedAudioDevices();
"config_bluetooth_max_connected_audio_devices",
"integer", "com.android.bluetooth"));
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
} }
@Override @Override

View File

@@ -24,9 +24,9 @@ import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothManager;
import android.content.Context; import android.content.Context;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.os.SystemProperties; import android.os.SystemProperties;
@@ -54,6 +54,11 @@ public class BluetoothMaxConnectedAudioDevicesPreferenceControllerTest {
@Spy @Spy
private Context mSpyContext = RuntimeEnvironment.application; private Context mSpyContext = RuntimeEnvironment.application;
@Mock
private BluetoothManager mBluetoothManager;
@Mock
private BluetoothAdapter mBluetoothAdapter;
private ListPreference mPreference; private ListPreference mPreference;
private BluetoothMaxConnectedAudioDevicesPreferenceController mController; private BluetoothMaxConnectedAudioDevicesPreferenceController mController;
@@ -63,19 +68,15 @@ public class BluetoothMaxConnectedAudioDevicesPreferenceControllerTest {
@Before @Before
public void setup() { public void setup() {
MockitoAnnotations.initMocks(this); MockitoAnnotations.initMocks(this);
doReturn(mBluetoothManager).when(mSpyContext).getSystemService(BluetoothManager.class);
doReturn(mBluetoothAdapter).when(mBluetoothManager).getAdapter();
// Get XML values without mock // Get XML values without mock
// Setup test list preference using XML values // Setup test list preference using XML values
mPreference = new ListPreference(mSpyContext); mPreference = new ListPreference(mSpyContext);
mPreference.setEntries(R.array.bluetooth_max_connected_audio_devices); mPreference.setEntries(R.array.bluetooth_max_connected_audio_devices);
mPreference.setEntryValues(R.array.bluetooth_max_connected_audio_devices_values); mPreference.setEntryValues(R.array.bluetooth_max_connected_audio_devices_values);
// Retrieve default max connected audio devices to a test controlled value doReturn(TEST_MAX_CONNECTED_AUDIO_DEVICES).when(mBluetoothAdapter)
try { .getMaxConnectedAudioDevices();
Resources res = mSpyContext.getPackageManager().getResourcesForApplication("com.android.bluetooth");
TEST_MAX_CONNECTED_AUDIO_DEVICES = res.getInteger(res.getIdentifier("config_bluetooth_max_connected_audio_devices", "integer", "com.android.bluetooth"));
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
// Init the actual controller // Init the actual controller
mController = new BluetoothMaxConnectedAudioDevicesPreferenceController(mSpyContext); mController = new BluetoothMaxConnectedAudioDevicesPreferenceController(mSpyContext);
// Construct preference in the controller via a mocked preference screen object // Construct preference in the controller via a mocked preference screen object