diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 32f786a12e2..de4fc607814 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -5456,6 +5456,12 @@ + + + + + + diff --git a/src/com/android/settings/connecteddevice/audiosharing/audiostreams/AudioStreamConfirmDialog.java b/src/com/android/settings/connecteddevice/audiosharing/audiostreams/AudioStreamConfirmDialog.java index df9c26eec01..d2e288f0d0b 100644 --- a/src/com/android/settings/connecteddevice/audiosharing/audiostreams/AudioStreamConfirmDialog.java +++ b/src/com/android/settings/connecteddevice/audiosharing/audiostreams/AudioStreamConfirmDialog.java @@ -17,6 +17,7 @@ package com.android.settings.connecteddevice.audiosharing.audiostreams; import static com.android.settings.connecteddevice.audiosharing.audiostreams.AudioStreamsDashboardFragment.KEY_BROADCAST_METADATA; +import static com.android.settingslib.bluetooth.BluetoothBroadcastUtils.SCHEME_BT_BROADCAST_METADATA; import android.app.Activity; import android.app.Dialog; @@ -48,9 +49,13 @@ public class AudioStreamConfirmDialog extends InstrumentedDialogFragment { static final int DEFAULT_DEVICE_NAME = R.string.audio_streams_dialog_default_device; private Context mContext; - @VisibleForTesting @Nullable Activity mActivity; - @Nullable private BluetoothLeBroadcastMetadata mBroadcastMetadata; - @Nullable private BluetoothDevice mConnectedDevice; + @VisibleForTesting + @Nullable + Activity mActivity; + @Nullable + private BluetoothLeBroadcastMetadata mBroadcastMetadata; + @Nullable + private BluetoothDevice mConnectedDevice; private int mAudioStreamConfirmDialogId = SettingsEnums.PAGE_UNKNOWN; @Override @@ -206,11 +211,22 @@ public class AudioStreamConfirmDialog extends InstrumentedDialogFragment { } private @Nullable BluetoothLeBroadcastMetadata getMetadata(Intent intent) { + // Get the metadata from the intent extras String metadata = intent.getStringExtra(KEY_BROADCAST_METADATA); - if (metadata == null || metadata.isEmpty()) { - return null; + if (metadata != null && !metadata.isEmpty()) { + return BluetoothLeBroadcastMetadataExt.INSTANCE.convertToBroadcastMetadata(metadata); } - return BluetoothLeBroadcastMetadataExt.INSTANCE.convertToBroadcastMetadata(metadata); + // Retrieve the generic data string from the intent + String genericData = intent.getDataString(); + if (genericData != null && !genericData.isEmpty()) { + // Normalize the prefix by replacing lowercase "bluetooth" with uppercase "BLUETOOTH" + genericData = genericData.replaceFirst("bluetooth", "BLUETOOTH"); + if (genericData.startsWith(SCHEME_BT_BROADCAST_METADATA)) { + return BluetoothLeBroadcastMetadataExt.INSTANCE.convertToBroadcastMetadata( + genericData); + } + } + return null; } private int getDialogId(boolean hasMetadata, boolean hasConnectedDevice) { diff --git a/tests/robotests/src/com/android/settings/connecteddevice/audiosharing/audiostreams/AudioStreamConfirmDialogTest.java b/tests/robotests/src/com/android/settings/connecteddevice/audiosharing/audiostreams/AudioStreamConfirmDialogTest.java index 9c83fa6f8ee..d22130a7bbe 100644 --- a/tests/robotests/src/com/android/settings/connecteddevice/audiosharing/audiostreams/AudioStreamConfirmDialogTest.java +++ b/tests/robotests/src/com/android/settings/connecteddevice/audiosharing/audiostreams/AudioStreamConfirmDialogTest.java @@ -36,6 +36,7 @@ import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothStatusCodes; import android.content.Context; import android.content.Intent; +import android.net.Uri; import android.platform.test.flag.junit.SetFlagsRule; import android.view.View; import android.widget.Button; @@ -82,6 +83,9 @@ public class AudioStreamConfirmDialogTest { private static final String VALID_METADATA = "BLUETOOTH:UUID:184F;BN:VGVzdA==;AT:1;AD:00A1A1A1A1A1;BI:1E240;BC:VGVzdENvZGU=;" + "MD:BgNwVGVzdA==;AS:1;PI:A0;NS:1;BS:3;NB:2;SM:BQNUZXN0BARlbmc=;;"; + private static final String VALID_METADATA_LOWERCASE = + "bluetooth:UUID:184F;BN:VGVzdA==;AT:1;AD:00A1A1A1A1A1;BI:1E240;BC:VGVzdENvZGU=;" + + "MD:BgNwVGVzdA==;AS:1;PI:A0;NS:1;BS:3;NB:2;SM:BQNUZXN0BARlbmc=;;"; private static final String DEVICE_NAME = "device_name"; private final Context mContext = ApplicationProvider.getApplicationContext(); @Mock private LocalBluetoothManager mLocalBluetoothManager; @@ -371,4 +375,63 @@ public class AudioStreamConfirmDialogTest { assertThat(dialog.isShowing()).isFalse(); verify(mDialogFragment.mActivity, times(2)).finish(); } + + @Test + public void showDialog_getDataStringFromIntent_confirmListen() { + List devices = new ArrayList<>(); + devices.add(mBluetoothDevice); + when(mAssistant.getAllConnectedDevices()).thenReturn(devices); + when(mBluetoothDevice.getAlias()).thenReturn(""); + + Intent intent = new Intent(); + intent.setData(Uri.parse(VALID_METADATA_LOWERCASE)); + FragmentController.of(mDialogFragment, intent) + .create(/* containerViewId= */ 0, /* bundle= */ null) + .start() + .resume() + .visible() + .get(); + shadowMainLooper().idle(); + + assertThat(mDialogFragment.getMetricsCategory()) + .isEqualTo(DIALOG_AUDIO_STREAM_CONFIRM_LISTEN); + assertThat(mDialogFragment.mActivity).isNotNull(); + mDialogFragment.mActivity = spy(mDialogFragment.mActivity); + + Dialog dialog = mDialogFragment.getDialog(); + assertThat(dialog).isNotNull(); + assertThat(dialog.isShowing()).isTrue(); + TextView title = dialog.findViewById(R.id.dialog_title); + assertThat(title).isNotNull(); + assertThat(title.getText()) + .isEqualTo( + mContext.getString(R.string.audio_streams_dialog_listen_to_audio_stream)); + TextView subtitle1 = dialog.findViewById(R.id.dialog_subtitle); + assertThat(subtitle1).isNotNull(); + assertThat(subtitle1.getVisibility()).isEqualTo(View.VISIBLE); + TextView subtitle2 = dialog.findViewById(R.id.dialog_subtitle_2); + assertThat(subtitle2).isNotNull(); + var defaultName = mContext.getString(DEFAULT_DEVICE_NAME); + assertThat(subtitle2.getText()) + .isEqualTo( + mContext.getString( + R.string.audio_streams_dialog_control_volume, defaultName)); + View leftButton = dialog.findViewById(R.id.left_button); + assertThat(leftButton).isNotNull(); + assertThat(leftButton.getVisibility()).isEqualTo(View.VISIBLE); + assertThat(leftButton.hasOnClickListeners()).isTrue(); + + leftButton.callOnClick(); + assertThat(dialog.isShowing()).isFalse(); + + Button rightButton = dialog.findViewById(R.id.right_button); + assertThat(rightButton).isNotNull(); + assertThat(rightButton.getText()) + .isEqualTo(mContext.getString(R.string.audio_streams_dialog_listen)); + assertThat(rightButton.hasOnClickListeners()).isTrue(); + + rightButton.callOnClick(); + assertThat(dialog.isShowing()).isFalse(); + verify(mDialogFragment.mActivity, times(2)).finish(); + } }