Merge "hap: Fix missing preset change reason" am: 3115ab2e3c am: c060d82fc6 am: c712b67089

Original change: https://android-review.googlesource.com/c/platform/packages/modules/Bluetooth/+/2020205

Change-Id: I486ae231962c5bac01c79c3aa6bc726def7a46c8
diff --git a/android/app/src/com/android/bluetooth/hap/HapClientService.java b/android/app/src/com/android/bluetooth/hap/HapClientService.java
index 14412b2..b6d4c8b 100644
--- a/android/app/src/com/android/bluetooth/hap/HapClientService.java
+++ b/android/app/src/com/android/bluetooth/hap/HapClientService.java
@@ -785,12 +785,14 @@
         }
     }
 
-    private void notifyActivePresetChanged(BluetoothDevice device, int presetIndex) {
+    private void notifyActivePresetChanged(BluetoothDevice device, int presetIndex,
+            int reasonCode) {
         if (mCallbacks != null) {
             int n = mCallbacks.beginBroadcast();
             for (int i = 0; i < n; i++) {
                 try {
-                    mCallbacks.getBroadcastItem(i).onPresetSelected(device, presetIndex);
+                    mCallbacks.getBroadcastItem(i).onPresetSelected(device, presetIndex,
+                            reasonCode);
                 } catch (RemoteException e) {
                     continue;
                 }
@@ -799,10 +801,10 @@
         }
     }
 
-    private void notifyActivePresetChangedForGroup(int groupId, int presetIndex) {
+    private void notifyActivePresetChangedForGroup(int groupId, int presetIndex, int reasonCode) {
         List<BluetoothDevice> all_group_devices = getGroupDevices(groupId);
         for (BluetoothDevice dev : all_group_devices) {
-            notifyActivePresetChanged(dev, presetIndex);
+            notifyActivePresetChanged(dev, presetIndex, reasonCode);
         }
     }
 
@@ -1103,14 +1105,18 @@
 
                 if (device != null) {
                     mDeviceCurrentPresetMap.put(device, currentPresetIndex);
-                    notifyActivePresetChanged(device, currentPresetIndex);
+                    // FIXME: Add app request queueing to support other reasons
+                    int reasonCode = BluetoothStatusCodes.REASON_LOCAL_STACK_REQUEST;
+                    notifyActivePresetChanged(device, currentPresetIndex, reasonCode);
 
                 } else if (groupId != BluetoothCsipSetCoordinator.GROUP_ID_INVALID) {
                     List<BluetoothDevice> all_group_devices = getGroupDevices(groupId);
                     for (BluetoothDevice dev : all_group_devices) {
                         mDeviceCurrentPresetMap.put(dev, currentPresetIndex);
                     }
-                    notifyActivePresetChangedForGroup(groupId, currentPresetIndex);
+                    // FIXME: Add app request queueing to support other reasons
+                    int reasonCode = BluetoothStatusCodes.REASON_LOCAL_STACK_REQUEST;
+                    notifyActivePresetChangedForGroup(groupId, currentPresetIndex, reasonCode);
                 }
             } return;
 
diff --git a/android/app/tests/unit/src/com/android/bluetooth/hap/HapClientTest.java b/android/app/tests/unit/src/com/android/bluetooth/hap/HapClientTest.java
index 9b78f40..6b31044 100644
--- a/android/app/tests/unit/src/com/android/bluetooth/hap/HapClientTest.java
+++ b/android/app/tests/unit/src/com/android/bluetooth/hap/HapClientTest.java
@@ -697,7 +697,7 @@
      * Test that native callback generates proper callback call.
      */
     @Test
-    public void testOtackEventonPresetSelected() {
+    public void testStackEventOnPresetSelected() {
         doReturn(new ParcelUuid[]{BluetoothUuid.HAS}).when(mAdapterService)
                 .getRemoteUuids(any(BluetoothDevice.class));
 
@@ -705,7 +705,7 @@
 
         try {
             verify(mCallback, after(TIMEOUT_MS).times(1)).onPresetSelected(eq(mDevice),
-                    eq(0x01));
+                    eq(0x01), eq(BluetoothStatusCodes.REASON_LOCAL_STACK_REQUEST));
         } catch (RemoteException e) {
             throw e.rethrowFromSystemServer();
         }
@@ -988,7 +988,7 @@
 
         try {
             verify(mCallback, after(TIMEOUT_MS).times(1)).onPresetSelected(eq(device),
-                    eq(evt.valueInt1));
+                    eq(evt.valueInt1), eq(BluetoothStatusCodes.REASON_LOCAL_STACK_REQUEST));
         } catch (RemoteException e) {
             throw e.rethrowFromSystemServer();
         }
diff --git a/framework/api/system-current.txt b/framework/api/system-current.txt
index 13a9ed3..79e3844 100644
--- a/framework/api/system-current.txt
+++ b/framework/api/system-current.txt
@@ -271,7 +271,7 @@
 
   public static interface BluetoothHapClient.Callback {
     method public void onPresetInfoChanged(@NonNull android.bluetooth.BluetoothDevice, @NonNull java.util.List<android.bluetooth.BluetoothHapPresetInfo>, int);
-    method public void onPresetSelected(@NonNull android.bluetooth.BluetoothDevice, int);
+    method public void onPresetSelected(@NonNull android.bluetooth.BluetoothDevice, int, int);
     method public void onPresetSelectionFailed(@NonNull android.bluetooth.BluetoothDevice, int);
     method public void onPresetSelectionForGroupFailed(int, int);
     method public void onSetPresetNameFailed(@NonNull android.bluetooth.BluetoothDevice, int);
diff --git a/framework/java/android/bluetooth/BluetoothHapClient.java b/framework/java/android/bluetooth/BluetoothHapClient.java
index a306601..4d1bd6a 100644
--- a/framework/java/android/bluetooth/BluetoothHapClient.java
+++ b/framework/java/android/bluetooth/BluetoothHapClient.java
@@ -94,11 +94,13 @@
          *
          * @param device remote device,
          * @param presetIndex the currently active preset index.
+         * @param statusCode reason for the selected preset change
          *
          * @hide
          */
         @SystemApi
-        void onPresetSelected(@NonNull BluetoothDevice device, int presetIndex);
+        void onPresetSelected(@NonNull BluetoothDevice device, int presetIndex,
+                @Status int statusCode);
 
         /**
          * Invoked inform about the result of a failed preset change attempt.
@@ -175,13 +177,14 @@
     @SuppressLint("AndroidFrameworkBluetoothPermission")
     private final IBluetoothHapClientCallback mCallback = new IBluetoothHapClientCallback.Stub() {
         @Override
-        public void onPresetSelected(@NonNull BluetoothDevice device, int presetIndex) {
+        public void onPresetSelected(@NonNull BluetoothDevice device, int presetIndex,
+                int reasonCode) {
             Attributable.setAttributionSource(device, mAttributionSource);
             for (Map.Entry<BluetoothHapClient.Callback, Executor> callbackExecutorEntry:
                     mCallbackExecutorMap.entrySet()) {
                 BluetoothHapClient.Callback callback = callbackExecutorEntry.getKey();
                 Executor executor = callbackExecutorEntry.getValue();
-                executor.execute(() -> callback.onPresetSelected(device, presetIndex));
+                executor.execute(() -> callback.onPresetSelected(device, presetIndex, reasonCode));
             }
         }
 
@@ -866,7 +869,7 @@
     /**
      * Selects the currently active preset for a HA device
      *
-     * On success, {@link Callback#onPresetSelected(BluetoothDevice, int)} will be called with
+     * On success, {@link Callback#onPresetSelected(BluetoothDevice, int, int)} will be called with
      * reason code {@link BluetoothStatusCodes#REASON_LOCAL_APP_REQUEST}
      * On failure, {@link Callback#onPresetSelectionFailed(BluetoothDevice, int)} will be called.
      *
@@ -900,11 +903,11 @@
      * <p> This group call may replace multiple device calls if those are part of the
      * valid HAS group. Note that binaural HA devices may or may not support group.
      *
-     * On success, {@link Callback#onPresetSelected(BluetoothDevice, int)} will be called
+     * On success, {@link Callback#onPresetSelected(BluetoothDevice, int, int)} will be called
      * for each device within the group with reason code
      * {@link BluetoothStatusCodes#REASON_LOCAL_APP_REQUEST}
-     * On failure, {@link Callback#onPresetSelectionFailed(BluetoothDevice, int)} will be called
-     * for each device within the group.
+     * On failure, {@link Callback#onPresetSelectionForGroupFailed(int, int)} will be
+     * called for the group.
      *
      * @param groupId is the device group identifier for which want to set the active preset
      * @param presetIndex is an index of one of the available presets
diff --git a/system/binder/android/bluetooth/IBluetoothHapClientCallback.aidl b/system/binder/android/bluetooth/IBluetoothHapClientCallback.aidl
index cd623cd..274c802 100644
--- a/system/binder/android/bluetooth/IBluetoothHapClientCallback.aidl
+++ b/system/binder/android/bluetooth/IBluetoothHapClientCallback.aidl
@@ -27,7 +27,7 @@
  * @hide
  */
 oneway interface IBluetoothHapClientCallback {
-    void onPresetSelected(in BluetoothDevice device, in int presetIndex);
+    void onPresetSelected(in BluetoothDevice device, in int presetIndex, in int reasonCode);
     void onPresetSelectionFailed(in  BluetoothDevice device, in int statusCode);
     void onPresetSelectionForGroupFailed(in int hapGroupId, in int statusCode);
     void onPresetInfoChanged(in  BluetoothDevice device,