Merge "[BT Broadcast Sink] Add the function for leave and scan QRcode buttons" into tm-dev am: 286682ec8a

Original change: https://googleplex-android-review.googlesource.com/c/platform/packages/apps/Settings/+/18181646

Change-Id: If732e278300370c10c61b52e347f79c8d9656a0b
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
diff --git a/src/com/android/settings/bluetooth/BluetoothFindBroadcastsFragment.java b/src/com/android/settings/bluetooth/BluetoothFindBroadcastsFragment.java
index 9a26a57..07a3156 100644
--- a/src/com/android/settings/bluetooth/BluetoothFindBroadcastsFragment.java
+++ b/src/com/android/settings/bluetooth/BluetoothFindBroadcastsFragment.java
@@ -75,6 +75,7 @@
     CachedBluetoothDevice mCachedDevice;
     @VisibleForTesting
     PreferenceCategory mBroadcastSourceListCategory;
+    BluetoothFindBroadcastsHeaderController mBluetoothFindBroadcastsHeaderController;
     private LocalBluetoothLeBroadcastAssistant mLeBroadcastAssistant;
     private BluetoothBroadcastSourcePreference mSelectedPreference;
     private Executor mExecutor;
@@ -93,7 +94,6 @@
                 @Override
                 public void onSearchStartFailed(int reason) {
                     Log.d(TAG, "onSearchStartFailed: " + reason);
-
                 }
 
                 @Override
@@ -240,6 +240,28 @@
         return SettingsEnums.PAGE_UNKNOWN;
     }
 
+    /**
+     * Starts to scan broadcast source by the BluetoothLeBroadcastAssistant.
+     */
+    public void scanBroadcastSource() {
+        if (mLeBroadcastAssistant == null) {
+            Log.w(TAG, "scanBroadcastSource: LeBroadcastAssistant is null!");
+            return;
+        }
+        mLeBroadcastAssistant.startSearchingForSources(getScanFilter());
+    }
+
+    /**
+     * Leaves the broadcast source by the BluetoothLeBroadcastAssistant.
+     */
+    public void leaveBroadcastSession() {
+        if (mLeBroadcastAssistant == null || mCachedDevice == null) {
+            Log.w(TAG, "leaveBroadcastSession: LeBroadcastAssistant or CachedDevice is null!");
+            return;
+        }
+        mLeBroadcastAssistant.removeSource(mCachedDevice.getDevice(), getSourceId());
+    }
+
     @Override
     protected String getLogTag() {
         return TAG;
@@ -256,13 +278,18 @@
 
         if (mCachedDevice != null) {
             Lifecycle lifecycle = getSettingsLifecycle();
-            controllers.add(new BluetoothFindBroadcastsHeaderController(context, this,
-                    mCachedDevice, lifecycle, mManager));
+            mBluetoothFindBroadcastsHeaderController = new BluetoothFindBroadcastsHeaderController(
+                    context, this, mCachedDevice, lifecycle, mManager);
+            controllers.add(mBluetoothFindBroadcastsHeaderController);
         }
         return controllers;
     }
 
-    private LocalBluetoothLeBroadcastAssistant getLeBroadcastAssistant() {
+    /**
+     * Gets the LocalBluetoothLeBroadcastAssistant
+     * @return the LocalBluetoothLeBroadcastAssistant
+     */
+    public LocalBluetoothLeBroadcastAssistant getLeBroadcastAssistant() {
         if (mManager == null) {
             Log.w(TAG, "getLeBroadcastAssistant: LocalBluetoothManager is null!");
             return null;
@@ -292,6 +319,11 @@
         }
         item.updateMetadataAndRefreshUi(source, isConnected);
         item.setOrder(isConnected ? 0 : 1);
+
+        //refresh the header
+        if (mBluetoothFindBroadcastsHeaderController != null) {
+            mBluetoothFindBroadcastsHeaderController.refreshUi();
+        }
     }
 
     private BluetoothBroadcastSourcePreference createBluetoothBroadcastSourcePreference(
diff --git a/src/com/android/settings/bluetooth/BluetoothFindBroadcastsHeaderController.java b/src/com/android/settings/bluetooth/BluetoothFindBroadcastsHeaderController.java
index dfdcae0..1527f21 100644
--- a/src/com/android/settings/bluetooth/BluetoothFindBroadcastsHeaderController.java
+++ b/src/com/android/settings/bluetooth/BluetoothFindBroadcastsHeaderController.java
@@ -16,22 +16,24 @@
 
 package com.android.settings.bluetooth;
 
+import android.bluetooth.BluetoothProfile;
 import android.content.Context;
-import android.util.Log;
+import android.content.Intent;
 import android.view.View;
 import android.widget.Button;
 import android.widget.LinearLayout;
 import android.widget.TextView;
 
 import androidx.preference.PreferenceCategory;
-import androidx.preference.PreferenceFragmentCompat;
 import androidx.preference.PreferenceScreen;
 
 import com.android.settings.R;
+import com.android.settingslib.bluetooth.BluetoothBroadcastUtils;
 import com.android.settingslib.bluetooth.CachedBluetoothDevice;
+import com.android.settingslib.bluetooth.LocalBluetoothLeBroadcastAssistant;
 import com.android.settingslib.bluetooth.LocalBluetoothManager;
-import com.android.settingslib.bluetooth.LocalBluetoothProfileManager;
 import com.android.settingslib.core.lifecycle.Lifecycle;
+import com.android.settingslib.qrcode.QrCodeScanModeActivity;
 import com.android.settingslib.widget.LayoutPreference;
 
 /**
@@ -52,11 +54,12 @@
     LinearLayout mBtnBroadcastLayout;
     Button mBtnLeaveBroadcast;
     Button mBtnScanQrCode;
-
+    BluetoothFindBroadcastsFragment mBluetoothFindBroadcastsFragment;
     public BluetoothFindBroadcastsHeaderController(Context context,
-            PreferenceFragmentCompat fragment, CachedBluetoothDevice device, Lifecycle lifecycle,
-            LocalBluetoothManager bluetoothManager) {
+            BluetoothFindBroadcastsFragment fragment, CachedBluetoothDevice device,
+            Lifecycle lifecycle, LocalBluetoothManager bluetoothManager) {
         super(context, fragment, device, lifecycle);
+        mBluetoothFindBroadcastsFragment = fragment;
     }
 
     @Override
@@ -101,20 +104,41 @@
             mBtnFindBroadcast.setVisibility(View.VISIBLE);
             mBtnBroadcastLayout.setVisibility(View.GONE);
         }
+
+        mBtnLeaveBroadcast.setEnabled(false);
+        if (mBluetoothFindBroadcastsFragment != null && mCachedDevice != null) {
+            LocalBluetoothLeBroadcastAssistant broadcastAssistant =
+                    mBluetoothFindBroadcastsFragment.getLeBroadcastAssistant();
+            if (broadcastAssistant != null
+                    && broadcastAssistant.getConnectionStatus(mCachedDevice.getDevice())
+                    == BluetoothProfile.STATE_CONNECTED) {
+                mBtnLeaveBroadcast.setEnabled(true);
+            }
+        }
     }
 
     private void scanBroadcastSource() {
-        // TODO(b/228258236) : Call the LocalBluetoothLeBroadcastAssistant
-        //  to start searching for source
+        // TODO(b/231543455) : Using the BluetoothDeviceUpdater to refactor it.
+        if (mBluetoothFindBroadcastsFragment == null) {
+            return;
+        }
+        mBluetoothFindBroadcastsFragment.scanBroadcastSource();
     }
 
     private void leaveBroadcastSession() {
-        // TODO(b/228258236) : Call the LocalBluetoothLeBroadcastAssistant
-        //  to leave the broadcast session
+        if (mBluetoothFindBroadcastsFragment == null) {
+            return;
+        }
+        mBluetoothFindBroadcastsFragment.leaveBroadcastSession();
     }
 
     private void launchQrCodeScanner() {
-        // TODO(b/228259065) : Launch the QR code scanner page by intent
+        final Intent intent = new Intent(mContext, QrCodeScanModeActivity.class);
+        intent.setAction(BluetoothBroadcastUtils.ACTION_BLUETOOTH_LE_AUDIO_QR_CODE_SCANNER)
+                .putExtra(BluetoothBroadcastUtils.EXTRA_BLUETOOTH_SINK_IS_GROUP, false)
+                .putExtra(BluetoothBroadcastUtils.EXTRA_BLUETOOTH_DEVICE_SINK,
+                        mCachedDevice.getDevice());
+        mContext.startActivity(intent);
     }
 
     @Override
@@ -128,4 +152,11 @@
     public String getPreferenceKey() {
         return KEY_BROADCAST_HEADER;
     }
+
+    /**
+     * Updates the UI
+     */
+    public void refreshUi() {
+        updateHeaderLayout();
+    }
 }