From 21a8866591e491a5f5109ff87a8ec0e1e0b7be53 Mon Sep 17 00:00:00 2001 From: Shen Lin Date: Tue, 3 Jan 2023 14:05:53 +0800 Subject: [PATCH] Introduce isDeviceClassMatched() to align BluetoothClass comparison getBluetoothClass is nullable that caller needs paying attention to. This CL introduced a unified and safe method to BluetoothUtils to align this comparison. Bug: 264162859 Test: atest CachedBluetoothDeviceTest Change-Id: I740defb7bc369f659103b09643c3e37050ea8bbe --- .../settingslib/bluetooth/BluetoothUtils.java | 12 ++++++++++++ .../settingslib/bluetooth/CachedBluetoothDevice.java | 9 ++++----- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/packages/SettingsLib/src/com/android/settingslib/bluetooth/BluetoothUtils.java b/packages/SettingsLib/src/com/android/settingslib/bluetooth/BluetoothUtils.java index 5c796af84feff..879181f0fbd67 100644 --- a/packages/SettingsLib/src/com/android/settingslib/bluetooth/BluetoothUtils.java +++ b/packages/SettingsLib/src/com/android/settingslib/bluetooth/BluetoothUtils.java @@ -278,6 +278,18 @@ public class BluetoothUtils { return false; } + /** + * Check if a device class matches with a defined BluetoothClass device. + * + * @param device Must be one of the public constants in {@link BluetoothClass.Device} + * @return true if device class matches, false otherwise. + */ + public static boolean isDeviceClassMatched(@NonNull BluetoothDevice bluetoothDevice, + int device) { + return bluetoothDevice.getBluetoothClass() != null + && bluetoothDevice.getBluetoothClass().getDeviceClass() == device; + } + private static boolean isAdvancedHeaderEnabled() { if (!DeviceConfig.getBoolean(DeviceConfig.NAMESPACE_SETTINGS_UI, BT_ADVANCED_HEADER_ENABLED, true)) { diff --git a/packages/SettingsLib/src/com/android/settingslib/bluetooth/CachedBluetoothDevice.java b/packages/SettingsLib/src/com/android/settingslib/bluetooth/CachedBluetoothDevice.java index eb53ea1d44f78..845975d853401 100644 --- a/packages/SettingsLib/src/com/android/settingslib/bluetooth/CachedBluetoothDevice.java +++ b/packages/SettingsLib/src/com/android/settingslib/bluetooth/CachedBluetoothDevice.java @@ -1020,12 +1020,11 @@ public class CachedBluetoothDevice implements Comparable if (BluetoothUuid.containsAnyUuid(uuids, PbapServerProfile.PBAB_CLIENT_UUIDS)) { // The pairing dialog now warns of phone-book access for paired devices. // No separate prompt is displayed after pairing. - final BluetoothClass bluetoothClass = mDevice.getBluetoothClass(); if (mDevice.getPhonebookAccessPermission() == BluetoothDevice.ACCESS_UNKNOWN) { - if (bluetoothClass != null && (bluetoothClass.getDeviceClass() - == BluetoothClass.Device.AUDIO_VIDEO_HANDSFREE - || bluetoothClass.getDeviceClass() - == BluetoothClass.Device.AUDIO_VIDEO_WEARABLE_HEADSET)) { + if (BluetoothUtils.isDeviceClassMatched(mDevice, + BluetoothClass.Device.AUDIO_VIDEO_HANDSFREE) + || BluetoothUtils.isDeviceClassMatched(mDevice, + BluetoothClass.Device.AUDIO_VIDEO_WEARABLE_HEADSET)) { EventLog.writeEvent(0x534e4554, "138529441", -1, ""); } }