[LE] Launch the dialog for the user to broadcast or find broadcast

    - Launch the broadcast dialog from entry point in Media Volume Slice

    - Fix the broadcast dialog to follow the dialog style

    - Start broadcast and launch the MediaOutputBroadcastDialog from
      broadcast dialog

Bug: 229577518
Test: manual test
Change-Id: I1329b3f35b03afc441142494df883ae17f373656
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index 4117b2e..60815aa 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -4124,7 +4124,7 @@
                 <category android:name="android.intent.category.DEFAULT" />
             </intent-filter>
             <meta-data android:name="com.android.settings.FRAGMENT_CLASS"
-                android:value="com.android.settings.bluetooth.BluetoothBroadcastsDialog" />
+                android:value="com.android.settings.bluetooth.BluetoothBroadcastDialog" />
             <meta-data android:name="com.android.settings.PRIMARY_PROFILE_CONTROLLED"
                 android:value="true" />
         </activity>
diff --git a/res/values/strings.xml b/res/values/strings.xml
index cc66b3b..f0ea2fa 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -14125,6 +14125,8 @@
     <!-- BT LE Audio Device: Media Broadcast -->
     <!-- The title of the Media Broadcast Dialog [CHAR LIMIT=none] -->
     <string name="bluetooth_broadcast_dialog_title">Broadcast</string>
+    <!-- [CHAR LIMIT=NONE] Le audio broadcast dialog, switch to others app. -->
+    <string name="bluetooth_broadcast_dialog_broadcast_app">Broadcast <xliff:g id="currentApp" example="App Name 2">%1$s</xliff:g></string>
     <!-- The message of the Media Broadcast Dialog for finding broadcast [CHAR LIMIT=none] -->
     <string name="bluetooth_broadcast_dialog_find_message">Listen to broadcasts that are playing near you</string>
     <!-- The message of the Media Broadcast Dialog for broadcast [CHAR LIMIT=none] -->
diff --git a/src/com/android/settings/bluetooth/BluetoothBroadcastDialog.java b/src/com/android/settings/bluetooth/BluetoothBroadcastDialog.java
index c743653..b2636a6 100644
--- a/src/com/android/settings/bluetooth/BluetoothBroadcastDialog.java
+++ b/src/com/android/settings/bluetooth/BluetoothBroadcastDialog.java
@@ -19,105 +19,93 @@
 import android.app.Dialog;
 import android.app.settings.SettingsEnums;
 import android.content.Context;
+import android.content.Intent;
 import android.os.Bundle;
+import android.text.TextUtils;
 import android.util.Log;
-import android.view.LayoutInflater;
 import android.view.View;
-import android.widget.AdapterView;
-import android.widget.ArrayAdapter;
-import android.widget.ListView;
+import android.widget.Button;
+import android.widget.TextView;
 
 import androidx.appcompat.app.AlertDialog;
 
 import com.android.settings.R;
+import com.android.settings.core.SubSettingLauncher;
 import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
-
-import java.util.ArrayList;
+import com.android.settingslib.bluetooth.LocalBluetoothLeBroadcast;
+import com.android.settingslib.bluetooth.LocalBluetoothManager;
+import com.android.settingslib.media.MediaOutputConstants;
 
 /**
  * This Dialog allowed users to do some actions for broadcast media or find the
  * nearby broadcast sources.
  */
 public class BluetoothBroadcastDialog extends InstrumentedDialogFragment {
+    public static final String KEY_APP_LABEL = "app_label";
+    public static final String KEY_DEVICE_ADDRESS =
+            BluetoothFindBroadcastsFragment.KEY_DEVICE_ADDRESS;
 
     private static final String TAG = "BTBroadcastsDialog";
-
+    private static final CharSequence UNKNOWN_APP_LABEL = "unknown";
+    private Context mContext;
+    private CharSequence mCurrentAppLabel = UNKNOWN_APP_LABEL;
+    private String mDeviceAddress;
+    private LocalBluetoothManager mLocalBluetoothManager;
+    private AlertDialog mAlertDialog;
 
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
+        mContext = getActivity();
+        mCurrentAppLabel = getActivity().getIntent().getCharSequenceExtra(KEY_APP_LABEL);
+        mDeviceAddress = getActivity().getIntent().getStringExtra(KEY_DEVICE_ADDRESS);
+        mLocalBluetoothManager = Utils.getLocalBtManager(mContext);
         setShowsDialog(true);
     }
 
     @Override
     public Dialog onCreateDialog(Bundle savedInstanceState) {
-        final Context context = getActivity();
-        final boolean isMediaPlaying = isMediaPlaying();
+        View layout = View.inflate(mContext,
+                com.android.settingslib.R.layout.broadcast_dialog, null);
 
-        final AlertDialog.Builder builder = new AlertDialog.Builder(context);
-        builder.setTitle(isMediaPlaying ? R.string.bluetooth_find_broadcast
-                : R.string.bluetooth_broadcast_dialog_title);
-        builder.setMessage(isMediaPlaying ? R.string.bluetooth_broadcast_dialog_find_message
-                : R.string.bluetooth_broadcast_dialog_broadcast_message);
+        TextView title = layout.findViewById(com.android.settingslib.R.id.dialog_title);
+        TextView subTitle = layout.findViewById(com.android.settingslib.R.id.dialog_subtitle);
+        title.setText(mContext.getString(R.string.bluetooth_broadcast_dialog_title));
+        subTitle.setText(
+                mContext.getString(R.string.bluetooth_broadcast_dialog_broadcast_message));
 
-        ArrayList<String> optionList = new ArrayList<String>();
-        if (!isMediaPlaying) {
-            optionList.add(context.getString(R.string.bluetooth_broadcast_dialog_title));
-        }
-        optionList.add(context.getString(R.string.bluetooth_find_broadcast));
-        optionList.add(context.getString(android.R.string.cancel));
-
-        View content = LayoutInflater.from(context).inflate(
-                R.layout.sim_confirm_dialog_multiple_enabled_profiles_supported, null);
-
-        if (content != null) {
-            Log.i(TAG, "list =" + optionList.toString());
-
-            final ArrayAdapter<String> arrayAdapterItems = new ArrayAdapter<String>(
-                    context,
-                    R.layout.sim_confirm_dialog_item_multiple_enabled_profiles_supported,
-                    optionList);
-            final ListView lvItems = content.findViewById(R.id.carrier_list);
-            if (lvItems != null) {
-                lvItems.setVisibility(View.VISIBLE);
-                lvItems.setAdapter(arrayAdapterItems);
-                lvItems.setOnItemClickListener(new AdapterView.OnItemClickListener() {
-                    @Override
-                    public void onItemClick(AdapterView<?> parent, View view, int position,
-                            long id) {
-                        Log.i(TAG, "list onClick =" + position);
-                        Log.i(TAG, "list item =" + optionList.get(position));
-
-                        if (position == optionList.size() - 1) {
-                            // The last position in the options is the Cancel button. So when
-                            // the user clicks the button, we do nothing but dismiss the dialog.
-                            dismiss();
-                        } else {
-                            if (optionList.get(position).equals(
-                                    context.getString(R.string.bluetooth_find_broadcast))) {
-                                launchFindBroadcastsActivity();
-                            } else {
-                                launchMediaOutputBroadcastDialog();
-                            }
-                        }
-                    }
-                });
-            }
-            builder.setView(content);
+        Button broadcastBtn = layout.findViewById(com.android.settingslib.R.id.positive_btn);
+        if (TextUtils.isEmpty(mCurrentAppLabel)) {
+            broadcastBtn.setText(mContext.getString(R.string.bluetooth_broadcast_dialog_title));
         } else {
-            Log.i(TAG, "optionList is empty");
+            broadcastBtn.setText(mContext.getString(
+                    R.string.bluetooth_broadcast_dialog_broadcast_app,
+                    String.valueOf(mCurrentAppLabel)));
         }
+        broadcastBtn.setOnClickListener((view) -> {
+            launchMediaOutputBroadcastDialog();
+        });
 
-        AlertDialog dialog = builder.create();
-        dialog.setCanceledOnTouchOutside(false);
-        return dialog;
+        Button findBroadcastBtn = layout.findViewById(com.android.settingslib.R.id.negative_btn);
+        findBroadcastBtn.setText(mContext.getString(R.string.bluetooth_find_broadcast));
+        findBroadcastBtn.setOnClickListener((view) -> {
+            launchFindBroadcastsActivity();
+        });
+
+        Button cancelBtn = layout.findViewById(com.android.settingslib.R.id.neutral_btn);
+        cancelBtn.setOnClickListener((view) -> {
+            dismiss();
+            getActivity().finish();
+        });
+
+        mAlertDialog = new AlertDialog.Builder(mContext,
+                com.android.settingslib.R.style.Theme_AlertDialog_SettingsLib)
+            .setView(layout)
+            .create();
+
+        return mAlertDialog;
     }
 
-    private boolean isMediaPlaying() {
-        return true;
-    }
-
-
     @Override
     public void onStart() {
         super.onStart();
@@ -130,10 +118,55 @@
     }
 
     private void launchFindBroadcastsActivity() {
+        Bundle bundle = new Bundle();
+        bundle.putString(KEY_DEVICE_ADDRESS, mDeviceAddress);
 
+        new SubSettingLauncher(mContext)
+                .setTitleRes(R.string.bluetooth_find_broadcast_title)
+                .setDestination(BluetoothFindBroadcastsFragment.class.getName())
+                .setArguments(bundle)
+                .setSourceMetricsCategory(SettingsEnums.PAGE_UNKNOWN)
+                .launch();
+        dismissVolumePanel();
     }
 
     private void launchMediaOutputBroadcastDialog() {
+        if (startBroadcast()) {
+            mContext.sendBroadcast(new Intent()
+                    .setPackage(MediaOutputConstants.SYSTEMUI_PACKAGE_NAME)
+                    .setAction(MediaOutputConstants.ACTION_LAUNCH_MEDIA_OUTPUT_BROADCAST_DIALOG)
+                    .putExtra(MediaOutputConstants.EXTRA_PACKAGE_NAME,
+                            getActivity().getPackageName()));
+            dismissVolumePanel();
+        }
+    }
 
+    private LocalBluetoothLeBroadcast getLEAudioBroadcastProfile() {
+        if (mLocalBluetoothManager != null && mLocalBluetoothManager.getProfileManager() != null) {
+            LocalBluetoothLeBroadcast bluetoothLeBroadcast =
+                    mLocalBluetoothManager.getProfileManager().getLeAudioBroadcastProfile();
+            if (bluetoothLeBroadcast != null) {
+                return bluetoothLeBroadcast;
+            }
+        }
+        Log.d(TAG, "Can not get LE Audio Broadcast Profile");
+        return null;
+    }
+
+    private boolean startBroadcast() {
+        LocalBluetoothLeBroadcast btLeBroadcast = getLEAudioBroadcastProfile();
+        if (btLeBroadcast != null) {
+            btLeBroadcast.startBroadcast(String.valueOf(mCurrentAppLabel), null);
+            return true;
+        }
+        Log.d(TAG, "Can not broadcast successfully");
+        return false;
+    }
+
+    private void dismissVolumePanel() {
+        // Dismiss volume panel
+        mContext.sendBroadcast(new Intent()
+                .setPackage(MediaOutputConstants.SETTINGS_PACKAGE_NAME)
+                .setAction(MediaOutputConstants.ACTION_CLOSE_PANEL));
     }
 }
diff --git a/src/com/android/settings/media/MediaOutputIndicatorWorker.java b/src/com/android/settings/media/MediaOutputIndicatorWorker.java
index a87b0ea..09e0672 100644
--- a/src/com/android/settings/media/MediaOutputIndicatorWorker.java
+++ b/src/com/android/settings/media/MediaOutputIndicatorWorker.java
@@ -161,7 +161,7 @@
         return mLocalMediaManager.getCurrentConnectedDevice();
     }
 
-    String getPackageName() {
+    public String getPackageName() {
         return mPackageName;
     }
 
diff --git a/src/com/android/settings/notification/MediaVolumePreferenceController.java b/src/com/android/settings/notification/MediaVolumePreferenceController.java
index c0d5610..05c8e03 100644
--- a/src/com/android/settings/notification/MediaVolumePreferenceController.java
+++ b/src/com/android/settings/notification/MediaVolumePreferenceController.java
@@ -31,9 +31,12 @@
 
 import com.android.settings.R;
 import com.android.settings.Utils;
+import com.android.settings.bluetooth.BluetoothBroadcastDialog;
 import com.android.settings.media.MediaOutputIndicatorWorker;
 import com.android.settings.slices.CustomSliceRegistry;
 import com.android.settings.slices.SliceBackgroundWorker;
+import com.android.settingslib.bluetooth.CachedBluetoothDevice;
+import com.android.settingslib.media.BluetoothMediaDevice;
 import com.android.settingslib.media.MediaDevice;
 import com.android.settingslib.media.MediaOutputConstants;
 
@@ -42,6 +45,9 @@
     private static final String KEY_MEDIA_VOLUME = "media_volume";
 
     private MediaOutputIndicatorWorker mWorker;
+    private MediaDevice mMediaDevice;
+    private static final String ACTION_LAUNCH_BROADCAST_DIALOG =
+            "android.settings.MEDIA_BROADCAST_DIALOG";
 
     public MediaVolumePreferenceController(Context context) {
         super(context, KEY_MEDIA_VOLUME);
@@ -91,9 +97,9 @@
     }
 
     private boolean isConnectedBLEDevice() {
-        final MediaDevice device = getWorker().getCurrentConnectedMediaDevice();
-        if (device != null) {
-            return device.isBLEDevice();
+        mMediaDevice = getWorker().getCurrentConnectedMediaDevice();
+        if (mMediaDevice != null) {
+            return mMediaDevice.isBLEDevice();
         }
         return false;
     }
@@ -106,17 +112,32 @@
         }
 
         final Intent intent = new Intent();
+        PendingIntent pi = null;
         if (getWorker().isDeviceBroadcasting()) {
             intent.setPackage(MediaOutputConstants.SYSTEMUI_PACKAGE_NAME);
             intent.setAction(MediaOutputConstants.ACTION_LAUNCH_MEDIA_OUTPUT_BROADCAST_DIALOG);
             intent.putExtra(MediaOutputConstants.EXTRA_PACKAGE_NAME,
                     getWorker().getActiveLocalMediaController().getPackageName());
+
+            pi = PendingIntent.getBroadcast(context, 0 /* requestCode */, intent,
+                    PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE);
         } else {
-            // TODO(b/229577518) : Get the intent action of the Bluetooth Broadcast Dialog
-            //  for user to choose the action
+            final CachedBluetoothDevice bluetoothDevice =
+                    ((BluetoothMediaDevice) mMediaDevice).getCachedDevice();
+            if (bluetoothDevice == null) {
+                Log.d(TAG, "The bluetooth device is null");
+                return null;
+            }
+            intent.setAction(ACTION_LAUNCH_BROADCAST_DIALOG);
+            intent.putExtra(BluetoothBroadcastDialog.KEY_APP_LABEL,
+                    Utils.getApplicationLabel(mContext, getWorker().getPackageName()));
+            intent.putExtra(BluetoothBroadcastDialog.KEY_DEVICE_ADDRESS,
+                    bluetoothDevice.getAddress());
+
+            pi = PendingIntent.getActivity(context, 0 /* requestCode */, intent,
+                    PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE);
         }
-        final PendingIntent pi = PendingIntent.getBroadcast(context, 0 /* requestCode */, intent,
-                PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE);
+
         final IconCompat icon = getBroadcastIcon(context);
 
         return SliceAction.createDeeplink(pi, icon, ListBuilder.ICON_IMAGE, getPreferenceKey());