From 2c9295f28946f2b91d2b78bc01e6728ab5f59fe7 Mon Sep 17 00:00:00 2001 From: Ömer Faruk Yılmaz Date: Tue, 4 Mar 2025 03:26:22 +0000 Subject: Refactor: Consolidate `isValidDevice` implementations in Bluetooth classes Bug: 394958283 Test: m com.android.bt Flag: Exempt refactor no-op Change-Id: I1c1a13b702da16fe50de909ad527f0ac85e49358 --- framework/java/android/bluetooth/BluetoothA2dp.java | 8 +------- framework/java/android/bluetooth/BluetoothA2dpSink.java | 5 +---- framework/java/android/bluetooth/BluetoothAvrcpController.java | 5 +---- framework/java/android/bluetooth/BluetoothHapClient.java | 8 +------- framework/java/android/bluetooth/BluetoothHeadset.java | 5 +---- framework/java/android/bluetooth/BluetoothHeadsetClient.java | 5 +---- framework/java/android/bluetooth/BluetoothHearingAid.java | 8 +------- framework/java/android/bluetooth/BluetoothHidDevice.java | 8 +------- framework/java/android/bluetooth/BluetoothHidHost.java | 5 +---- framework/java/android/bluetooth/BluetoothMap.java | 5 +---- framework/java/android/bluetooth/BluetoothMapClient.java | 5 +---- framework/java/android/bluetooth/BluetoothPan.java | 6 +----- framework/java/android/bluetooth/BluetoothPbap.java | 8 +------- framework/java/android/bluetooth/BluetoothPbapClient.java | 5 +---- framework/java/android/bluetooth/BluetoothSap.java | 5 +---- framework/java/android/bluetooth/BluetoothUtils.java | 4 ++++ 16 files changed, 19 insertions(+), 76 deletions(-) (limited to 'framework/java') diff --git a/framework/java/android/bluetooth/BluetoothA2dp.java b/framework/java/android/bluetooth/BluetoothA2dp.java index 2e3bf9ea92..207167a053 100644 --- a/framework/java/android/bluetooth/BluetoothA2dp.java +++ b/framework/java/android/bluetooth/BluetoothA2dp.java @@ -21,6 +21,7 @@ import static android.Manifest.permission.BLUETOOTH_PRIVILEGED; import static android.bluetooth.BluetoothProfile.CONNECTION_POLICY_ALLOWED; import static android.bluetooth.BluetoothProfile.CONNECTION_POLICY_FORBIDDEN; import static android.bluetooth.BluetoothProfile.STATE_DISCONNECTED; +import static android.bluetooth.BluetoothUtils.isValidDevice; import android.annotation.IntDef; import android.annotation.NonNull; @@ -1108,13 +1109,6 @@ public final class BluetoothA2dp implements BluetoothProfile { } } - private static boolean isValidDevice(BluetoothDevice device) { - if (device == null) return false; - - if (BluetoothAdapter.checkBluetoothAddress(device.getAddress())) return true; - return false; - } - private static void log(String msg) { Log.d(TAG, msg); } diff --git a/framework/java/android/bluetooth/BluetoothA2dpSink.java b/framework/java/android/bluetooth/BluetoothA2dpSink.java index c49bc1ea7a..ac45bcf46e 100644 --- a/framework/java/android/bluetooth/BluetoothA2dpSink.java +++ b/framework/java/android/bluetooth/BluetoothA2dpSink.java @@ -21,6 +21,7 @@ import static android.Manifest.permission.BLUETOOTH_PRIVILEGED; import static android.bluetooth.BluetoothProfile.CONNECTION_POLICY_ALLOWED; import static android.bluetooth.BluetoothProfile.CONNECTION_POLICY_FORBIDDEN; import static android.bluetooth.BluetoothProfile.STATE_DISCONNECTED; +import static android.bluetooth.BluetoothUtils.isValidDevice; import android.annotation.NonNull; import android.annotation.RequiresNoPermission; @@ -428,10 +429,6 @@ public final class BluetoothA2dpSink implements BluetoothProfile { return mAdapter.getState() == BluetoothAdapter.STATE_ON; } - private static boolean isValidDevice(BluetoothDevice device) { - return device != null && BluetoothAdapter.checkBluetoothAddress(device.getAddress()); - } - private static void log(String msg) { Log.d(TAG, msg); } diff --git a/framework/java/android/bluetooth/BluetoothAvrcpController.java b/framework/java/android/bluetooth/BluetoothAvrcpController.java index bb8390f559..2d39b89925 100644 --- a/framework/java/android/bluetooth/BluetoothAvrcpController.java +++ b/framework/java/android/bluetooth/BluetoothAvrcpController.java @@ -18,6 +18,7 @@ package android.bluetooth; import static android.Manifest.permission.BLUETOOTH_CONNECT; import static android.bluetooth.BluetoothProfile.STATE_DISCONNECTED; +import static android.bluetooth.BluetoothUtils.isValidDevice; import android.annotation.RequiresNoPermission; import android.annotation.RequiresPermission; @@ -275,10 +276,6 @@ public final class BluetoothAvrcpController implements BluetoothProfile { return mAdapter.getState() == BluetoothAdapter.STATE_ON; } - private static boolean isValidDevice(BluetoothDevice device) { - return device != null && BluetoothAdapter.checkBluetoothAddress(device.getAddress()); - } - private static void log(String msg) { Log.d(TAG, msg); } diff --git a/framework/java/android/bluetooth/BluetoothHapClient.java b/framework/java/android/bluetooth/BluetoothHapClient.java index 1699c5bf02..e551cca8f5 100644 --- a/framework/java/android/bluetooth/BluetoothHapClient.java +++ b/framework/java/android/bluetooth/BluetoothHapClient.java @@ -23,6 +23,7 @@ import static android.bluetooth.BluetoothProfile.CONNECTION_POLICY_ALLOWED; import static android.bluetooth.BluetoothProfile.CONNECTION_POLICY_FORBIDDEN; import static android.bluetooth.BluetoothProfile.STATE_DISCONNECTED; import static android.bluetooth.BluetoothUtils.callServiceIfEnabled; +import static android.bluetooth.BluetoothUtils.isValidDevice; import static java.util.Objects.requireNonNull; @@ -1087,11 +1088,4 @@ public final class BluetoothHapClient implements BluetoothProfile, AutoCloseable this::getService, s -> s.setPresetNameForGroup(groupId, presetIndex, name, mAttributionSource)); } - - private static boolean isValidDevice(BluetoothDevice device) { - if (device == null) return false; - - if (BluetoothAdapter.checkBluetoothAddress(device.getAddress())) return true; - return false; - } } diff --git a/framework/java/android/bluetooth/BluetoothHeadset.java b/framework/java/android/bluetooth/BluetoothHeadset.java index 448bb05ea0..3e33b42980 100644 --- a/framework/java/android/bluetooth/BluetoothHeadset.java +++ b/framework/java/android/bluetooth/BluetoothHeadset.java @@ -22,6 +22,7 @@ import static android.Manifest.permission.MODIFY_PHONE_STATE; import static android.bluetooth.BluetoothProfile.CONNECTION_POLICY_ALLOWED; import static android.bluetooth.BluetoothProfile.CONNECTION_POLICY_FORBIDDEN; import static android.bluetooth.BluetoothProfile.STATE_DISCONNECTED; +import static android.bluetooth.BluetoothUtils.isValidDevice; import static java.util.Objects.requireNonNull; @@ -1281,10 +1282,6 @@ public final class BluetoothHeadset implements BluetoothProfile { return mAdapter.getState() == BluetoothAdapter.STATE_OFF; } - private static boolean isValidDevice(BluetoothDevice device) { - return device != null && BluetoothAdapter.checkBluetoothAddress(device.getAddress()); - } - private static void log(String msg) { Log.d(TAG, msg); } diff --git a/framework/java/android/bluetooth/BluetoothHeadsetClient.java b/framework/java/android/bluetooth/BluetoothHeadsetClient.java index 14cb62c960..fea8f81c5f 100644 --- a/framework/java/android/bluetooth/BluetoothHeadsetClient.java +++ b/framework/java/android/bluetooth/BluetoothHeadsetClient.java @@ -20,6 +20,7 @@ import static android.Manifest.permission.BLUETOOTH_PRIVILEGED; import static android.bluetooth.BluetoothProfile.CONNECTION_POLICY_ALLOWED; import static android.bluetooth.BluetoothProfile.CONNECTION_POLICY_FORBIDDEN; import static android.bluetooth.BluetoothProfile.STATE_DISCONNECTED; +import static android.bluetooth.BluetoothUtils.isValidDevice; import static java.util.Objects.requireNonNull; @@ -1645,10 +1646,6 @@ public final class BluetoothHeadsetClient implements BluetoothProfile, AutoClose return mAdapter.getState() == BluetoothAdapter.STATE_ON; } - private static boolean isValidDevice(BluetoothDevice device) { - return device != null && BluetoothAdapter.checkBluetoothAddress(device.getAddress()); - } - private static void log(String msg) { Log.d(TAG, msg); } diff --git a/framework/java/android/bluetooth/BluetoothHearingAid.java b/framework/java/android/bluetooth/BluetoothHearingAid.java index d5ed621a37..7e14057546 100644 --- a/framework/java/android/bluetooth/BluetoothHearingAid.java +++ b/framework/java/android/bluetooth/BluetoothHearingAid.java @@ -22,6 +22,7 @@ import static android.Manifest.permission.BLUETOOTH_SCAN; import static android.bluetooth.BluetoothProfile.CONNECTION_POLICY_ALLOWED; import static android.bluetooth.BluetoothProfile.CONNECTION_POLICY_FORBIDDEN; import static android.bluetooth.BluetoothProfile.STATE_DISCONNECTED; +import static android.bluetooth.BluetoothUtils.isValidDevice; import android.annotation.IntDef; import android.annotation.NonNull; @@ -762,11 +763,4 @@ public final class BluetoothHearingAid implements BluetoothProfile { throw new IllegalArgumentException("Device cannot be null"); } } - - private static boolean isValidDevice(BluetoothDevice device) { - if (device == null) return false; - - if (BluetoothAdapter.checkBluetoothAddress(device.getAddress())) return true; - return false; - } } diff --git a/framework/java/android/bluetooth/BluetoothHidDevice.java b/framework/java/android/bluetooth/BluetoothHidDevice.java index 3098c80fdd..92ee4f5fb3 100644 --- a/framework/java/android/bluetooth/BluetoothHidDevice.java +++ b/framework/java/android/bluetooth/BluetoothHidDevice.java @@ -21,6 +21,7 @@ import static android.Manifest.permission.BLUETOOTH_PRIVILEGED; import static android.bluetooth.BluetoothProfile.CONNECTION_POLICY_ALLOWED; import static android.bluetooth.BluetoothProfile.CONNECTION_POLICY_FORBIDDEN; import static android.bluetooth.BluetoothUtils.executeFromBinder; +import static android.bluetooth.BluetoothUtils.isValidDevice; import android.annotation.NonNull; import android.annotation.RequiresNoPermission; @@ -788,13 +789,6 @@ public final class BluetoothHidDevice implements BluetoothProfile { return false; } - private static boolean isValidDevice(BluetoothDevice device) { - if (device == null) return false; - - if (BluetoothAdapter.checkBluetoothAddress(device.getAddress())) return true; - return false; - } - private static void log(String msg) { if (DBG) { Log.d(TAG, msg); diff --git a/framework/java/android/bluetooth/BluetoothHidHost.java b/framework/java/android/bluetooth/BluetoothHidHost.java index 7b67a90862..5eb6895746 100644 --- a/framework/java/android/bluetooth/BluetoothHidHost.java +++ b/framework/java/android/bluetooth/BluetoothHidHost.java @@ -21,6 +21,7 @@ import static android.Manifest.permission.BLUETOOTH_PRIVILEGED; import static android.bluetooth.BluetoothProfile.CONNECTION_POLICY_ALLOWED; import static android.bluetooth.BluetoothProfile.CONNECTION_POLICY_FORBIDDEN; import static android.bluetooth.BluetoothProfile.STATE_DISCONNECTED; +import static android.bluetooth.BluetoothUtils.isValidDevice; import static java.util.Objects.requireNonNull; @@ -604,10 +605,6 @@ public final class BluetoothHidHost implements BluetoothProfile { return mAdapter.getState() == BluetoothAdapter.STATE_ON; } - private static boolean isValidDevice(BluetoothDevice device) { - return device != null && BluetoothAdapter.checkBluetoothAddress(device.getAddress()); - } - /** * Initiate virtual unplug for a HID input device. * diff --git a/framework/java/android/bluetooth/BluetoothMap.java b/framework/java/android/bluetooth/BluetoothMap.java index f88ec35dbb..4c44995921 100644 --- a/framework/java/android/bluetooth/BluetoothMap.java +++ b/framework/java/android/bluetooth/BluetoothMap.java @@ -21,6 +21,7 @@ import static android.Manifest.permission.BLUETOOTH_PRIVILEGED; import static android.bluetooth.BluetoothProfile.CONNECTION_POLICY_ALLOWED; import static android.bluetooth.BluetoothProfile.CONNECTION_POLICY_FORBIDDEN; import static android.bluetooth.BluetoothProfile.STATE_DISCONNECTED; +import static android.bluetooth.BluetoothUtils.isValidDevice; import android.annotation.NonNull; import android.annotation.RequiresNoPermission; @@ -553,8 +554,4 @@ public final class BluetoothMap implements BluetoothProfile, AutoCloseable { private boolean isEnabled() { return mAdapter.isEnabled(); } - - private static boolean isValidDevice(BluetoothDevice device) { - return device != null && BluetoothAdapter.checkBluetoothAddress(device.getAddress()); - } } diff --git a/framework/java/android/bluetooth/BluetoothMapClient.java b/framework/java/android/bluetooth/BluetoothMapClient.java index 2b28da9f0c..a018669858 100644 --- a/framework/java/android/bluetooth/BluetoothMapClient.java +++ b/framework/java/android/bluetooth/BluetoothMapClient.java @@ -23,6 +23,7 @@ import static android.Manifest.permission.SEND_SMS; import static android.bluetooth.BluetoothProfile.CONNECTION_POLICY_ALLOWED; import static android.bluetooth.BluetoothProfile.CONNECTION_POLICY_FORBIDDEN; import static android.bluetooth.BluetoothProfile.STATE_DISCONNECTED; +import static android.bluetooth.BluetoothUtils.isValidDevice; import android.annotation.NonNull; import android.annotation.Nullable; @@ -574,8 +575,4 @@ public final class BluetoothMapClient implements BluetoothProfile, AutoCloseable private boolean isEnabled() { return mAdapter.isEnabled(); } - - private static boolean isValidDevice(BluetoothDevice device) { - return device != null && BluetoothAdapter.checkBluetoothAddress(device.getAddress()); - } } diff --git a/framework/java/android/bluetooth/BluetoothPan.java b/framework/java/android/bluetooth/BluetoothPan.java index 43b1469407..ded188821e 100644 --- a/framework/java/android/bluetooth/BluetoothPan.java +++ b/framework/java/android/bluetooth/BluetoothPan.java @@ -24,6 +24,7 @@ import static android.bluetooth.BluetoothProfile.CONNECTION_POLICY_ALLOWED; import static android.bluetooth.BluetoothProfile.CONNECTION_POLICY_FORBIDDEN; import static android.bluetooth.BluetoothProfile.STATE_DISCONNECTED; import static android.bluetooth.BluetoothUtils.executeFromBinder; +import static android.bluetooth.BluetoothUtils.isValidDevice; import static java.util.Objects.requireNonNull; @@ -609,11 +610,6 @@ public final class BluetoothPan implements BluetoothProfile { return mAdapter.getState() == BluetoothAdapter.STATE_ON; } - @UnsupportedAppUsage - private static boolean isValidDevice(BluetoothDevice device) { - return device != null && BluetoothAdapter.checkBluetoothAddress(device.getAddress()); - } - @UnsupportedAppUsage private static void log(String msg) { Log.d(TAG, msg); diff --git a/framework/java/android/bluetooth/BluetoothPbap.java b/framework/java/android/bluetooth/BluetoothPbap.java index ecbbe0ab23..8da64ddb96 100644 --- a/framework/java/android/bluetooth/BluetoothPbap.java +++ b/framework/java/android/bluetooth/BluetoothPbap.java @@ -21,6 +21,7 @@ import static android.Manifest.permission.BLUETOOTH_PRIVILEGED; import static android.bluetooth.BluetoothProfile.CONNECTION_POLICY_ALLOWED; import static android.bluetooth.BluetoothProfile.CONNECTION_POLICY_FORBIDDEN; import static android.bluetooth.BluetoothProfile.STATE_DISCONNECTED; +import static android.bluetooth.BluetoothUtils.isValidDevice; import android.annotation.NonNull; import android.annotation.RequiresNoPermission; @@ -300,13 +301,6 @@ public class BluetoothPbap implements BluetoothProfile { return false; } - private static boolean isValidDevice(BluetoothDevice device) { - if (device == null) return false; - - if (BluetoothAdapter.checkBluetoothAddress(device.getAddress())) return true; - return false; - } - private static void log(String msg) { if (DBG) { Log.d(TAG, msg); diff --git a/framework/java/android/bluetooth/BluetoothPbapClient.java b/framework/java/android/bluetooth/BluetoothPbapClient.java index 4c0eff490b..391c46da63 100644 --- a/framework/java/android/bluetooth/BluetoothPbapClient.java +++ b/framework/java/android/bluetooth/BluetoothPbapClient.java @@ -21,6 +21,7 @@ import static android.Manifest.permission.BLUETOOTH_PRIVILEGED; import static android.bluetooth.BluetoothProfile.CONNECTION_POLICY_ALLOWED; import static android.bluetooth.BluetoothProfile.CONNECTION_POLICY_FORBIDDEN; import static android.bluetooth.BluetoothProfile.STATE_DISCONNECTED; +import static android.bluetooth.BluetoothUtils.isValidDevice; import android.annotation.NonNull; import android.annotation.RequiresNoPermission; @@ -312,10 +313,6 @@ public final class BluetoothPbapClient implements BluetoothProfile, AutoCloseabl return mAdapter.isEnabled(); } - private static boolean isValidDevice(BluetoothDevice device) { - return device != null && BluetoothAdapter.checkBluetoothAddress(device.getAddress()); - } - /** * Set priority of the profile * diff --git a/framework/java/android/bluetooth/BluetoothSap.java b/framework/java/android/bluetooth/BluetoothSap.java index aa4686d30a..5ec412cb39 100644 --- a/framework/java/android/bluetooth/BluetoothSap.java +++ b/framework/java/android/bluetooth/BluetoothSap.java @@ -21,6 +21,7 @@ import static android.Manifest.permission.BLUETOOTH_PRIVILEGED; import static android.bluetooth.BluetoothProfile.CONNECTION_POLICY_ALLOWED; import static android.bluetooth.BluetoothProfile.CONNECTION_POLICY_FORBIDDEN; import static android.bluetooth.BluetoothProfile.STATE_DISCONNECTED; +import static android.bluetooth.BluetoothUtils.isValidDevice; import static java.util.Objects.requireNonNull; @@ -539,8 +540,4 @@ public final class BluetoothSap implements BluetoothProfile, AutoCloseable { private boolean isEnabled() { return mAdapter.isEnabled(); } - - private static boolean isValidDevice(BluetoothDevice device) { - return device != null && BluetoothAdapter.checkBluetoothAddress(device.getAddress()); - } } diff --git a/framework/java/android/bluetooth/BluetoothUtils.java b/framework/java/android/bluetooth/BluetoothUtils.java index 52fb2160a5..2665af1bac 100644 --- a/framework/java/android/bluetooth/BluetoothUtils.java +++ b/framework/java/android/bluetooth/BluetoothUtils.java @@ -429,4 +429,8 @@ public final class BluetoothUtils { public static void logRemoteException(String tag, RemoteException ex) { Log.w(tag, ex.toString() + ": " + inlineStackTrace()); } + + static boolean isValidDevice(BluetoothDevice device) { + return device != null && BluetoothAdapter.checkBluetoothAddress(device.getAddress()); + } } -- cgit v1.2.3-59-g8ed1b