From cadf64a64f71d4a733bbe173d2c06222f0f3718a Mon Sep 17 00:00:00 2001 From: William Escande Date: Wed, 16 Mar 2022 16:01:30 +0100 Subject: API Review: Add ConnectionState Intdef > getConnectionState Please add @intdef for the return value. Bug: 222434921 Test: TH Tag: #refactor Ignore-AOSP-First: merge conflict Change-Id: Idab42db60cff7a1bc346f29685acffee1764a3d3 --- .../java/android/bluetooth/BluetoothAdapter.java | 23 +++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) (limited to 'framework/java/android/bluetooth/BluetoothAdapter.java') diff --git a/framework/java/android/bluetooth/BluetoothAdapter.java b/framework/java/android/bluetooth/BluetoothAdapter.java index a1726307d9..7f3e0dc416 100644 --- a/framework/java/android/bluetooth/BluetoothAdapter.java +++ b/framework/java/android/bluetooth/BluetoothAdapter.java @@ -738,6 +738,16 @@ public final class BluetoothAdapter { public static final int STATE_DISCONNECTING = 3; //BluetoothProtoEnums.CONNECTION_STATE_DISCONNECTING; + /** @hide */ + @Retention(RetentionPolicy.SOURCE) + @IntDef(prefix = { "STATE_" }, value = { + STATE_DISCONNECTED, + STATE_CONNECTING, + STATE_CONNECTED, + STATE_DISCONNECTING, + }) + public @interface ConnectionState {} + /** @hide */ public static final String BLUETOOTH_MANAGER_SERVICE = "bluetooth_manager"; private final IBinder mToken; @@ -2856,13 +2866,12 @@ public final class BluetoothAdapter { *

Use this function along with {@link #ACTION_CONNECTION_STATE_CHANGED} * intent to get the connection state of the adapter. * - * @return One of {@link #STATE_CONNECTED}, {@link #STATE_DISCONNECTED}, {@link - * #STATE_CONNECTING} or {@link #STATE_DISCONNECTED} + * @return the connection state * @hide */ @SystemApi @RequiresNoPermission - public int getConnectionState() { + public @ConnectionState int getConnectionState() { if (getState() != STATE_ON) { return BluetoothAdapter.STATE_DISCONNECTED; } @@ -2918,17 +2927,13 @@ public final class BluetoothAdapter { * is connected to any remote device for a specific profile. * Profile can be one of {@link BluetoothProfile#HEADSET}, {@link BluetoothProfile#A2DP}. * - *

Return value can be one of - * {@link BluetoothProfile#STATE_DISCONNECTED}, - * {@link BluetoothProfile#STATE_CONNECTING}, - * {@link BluetoothProfile#STATE_CONNECTED}, - * {@link BluetoothProfile#STATE_DISCONNECTING} + *

Return the profile connection state */ @RequiresLegacyBluetoothPermission @RequiresBluetoothConnectPermission @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) @SuppressLint("AndroidFrameworkRequiresPermission") - public int getProfileConnectionState(int profile) { + public @ConnectionState int getProfileConnectionState(int profile) { if (getState() != STATE_ON) { return BluetoothProfile.STATE_DISCONNECTED; } -- cgit v1.2.3 From 9c8b03e4b9d13c7d3d14130dd69fe12ccb973339 Mon Sep 17 00:00:00 2001 From: William Escande Date: Thu, 17 Mar 2022 12:47:31 +0100 Subject: GetUuids refactor to call in correct order getUuidsList was calling getUuids (return an Array[]) but the getUuids is already calling a method that return a List By inversing their call we are no longer creating a temporary array Bug: 222434921 Tag: #refactor Test: pre-submit Change-Id: I202d1923d21b795f191e1570cd489e01e78654bd --- .../java/android/bluetooth/BluetoothAdapter.java | 38 ++++++++++------------ 1 file changed, 18 insertions(+), 20 deletions(-) (limited to 'framework/java/android/bluetooth/BluetoothAdapter.java') diff --git a/framework/java/android/bluetooth/BluetoothAdapter.java b/framework/java/android/bluetooth/BluetoothAdapter.java index 716d808a11..2b03261cd6 100644 --- a/framework/java/android/bluetooth/BluetoothAdapter.java +++ b/framework/java/android/bluetooth/BluetoothAdapter.java @@ -1445,25 +1445,8 @@ public final class BluetoothAdapter { @RequiresBluetoothConnectPermission @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) public @NonNull ParcelUuid[] getUuids() { - if (getState() != STATE_ON) { - return new ParcelUuid[0]; - } - try { - mServiceLock.readLock().lock(); - if (mService != null) { - final SynchronousResultReceiver> recv = - new SynchronousResultReceiver(); - mService.getUuids(mAttributionSource, recv); - List parcels = recv.awaitResultNoInterrupt(getSyncTimeout()) - .getValue(new ArrayList<>()); - return parcels.toArray(new ParcelUuid[parcels.size()]); - } - } catch (RemoteException | TimeoutException e) { - Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable())); - } finally { - mServiceLock.readLock().unlock(); - } - return new ParcelUuid[0]; + List parcels = getUuidsList(); + return parcels.toArray(new ParcelUuid[parcels.size()]); } /** @@ -1475,7 +1458,22 @@ public final class BluetoothAdapter { @SystemApi @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) public @NonNull List getUuidsList() { - return Arrays.asList(getUuids()); + List defaultValue = new ArrayList<>(); + if (getState() != STATE_ON || mService == null) { + return defaultValue; + } + mServiceLock.readLock().lock(); + try { + final SynchronousResultReceiver> recv = + new SynchronousResultReceiver(); + mService.getUuids(mAttributionSource, recv); + return recv.awaitResultNoInterrupt(getSyncTimeout()).getValue(defaultValue); + } catch (RemoteException | TimeoutException e) { + Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable())); + } finally { + mServiceLock.readLock().unlock(); + } + return defaultValue; } /** -- cgit v1.2.3