Merge "[Audiosharing] Refine sharing join dialog." into main

This commit is contained in:
Yiyi Shen
2023-12-08 06:19:35 +00:00
committed by Android (Google) Code Review
3 changed files with 196 additions and 23 deletions

View File

@@ -19,35 +19,44 @@
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="24dp"
android:orientation="vertical">
android:orientation="vertical"
android:paddingHorizontal="?android:dialogPreferredPadding"
android:paddingBottom="?android:dialogPreferredPadding">
<TextView
android:id="@+id/share_audio_subtitle1"
style="@style/DeviceAudioSharingText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:layout_gravity="center"/>
android:layout_gravity="center"
android:paddingBottom="14dp"
android:textFontWeight="500" />
<TextView
android:id="@+id/share_audio_subtitle2"
style="@style/DeviceAudioSharingText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:layout_gravity="center"/>
android:layout_gravity="center"
android:paddingBottom="24dp"
android:textFontWeight="400" />
<Button
android:id="@+id/share_btn"
style="@style/SettingsLibActionButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text=""/>
android:layout_marginTop="4dp"
android:background="@drawable/audio_sharing_rounded_bg_ripple" />
<Button
android:id="@+id/cancel_btn"
style="@style/SettingsLibActionButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/cancel"/>
android:layout_marginTop="4dp"
android:background="@drawable/audio_sharing_rounded_bg_ripple"
android:text="Not now" />
</LinearLayout>

View File

@@ -22,6 +22,7 @@ import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.appcompat.app.AlertDialog;
@@ -48,7 +49,6 @@ public class AudioSharingJoinDialogFragment extends InstrumentedDialogFragment {
}
private static DialogEventListener sListener;
private View mRootView;
@Override
public int getMetricsCategory() {
@@ -86,14 +86,17 @@ public class AudioSharingJoinDialogFragment extends InstrumentedDialogFragment {
arguments.getParcelableArrayList(BUNDLE_KEY_DEVICE_ITEMS);
String newDeviceName = arguments.getString(BUNDLE_KEY_NEW_DEVICE_NAME);
final AlertDialog.Builder builder =
new AlertDialog.Builder(getActivity())
.setTitle("Share audio?")
.setCancelable(false);
mRootView =
LayoutInflater.from(builder.getContext())
.inflate(R.layout.dialog_audio_sharing_join, null /* parent */);
TextView subtitle1 = mRootView.findViewById(R.id.share_audio_subtitle1);
TextView subtitle2 = mRootView.findViewById(R.id.share_audio_subtitle2);
new AlertDialog.Builder(getActivity()).setCancelable(false);
LayoutInflater inflater = LayoutInflater.from(builder.getContext());
View customTitle =
inflater.inflate(R.layout.dialog_custom_title_audio_sharing, /* parent= */ null);
ImageView icon = customTitle.findViewById(R.id.title_icon);
icon.setImageResource(R.drawable.ic_bt_audio_sharing);
TextView title = customTitle.findViewById(R.id.title_text);
title.setText("Share your audio");
View rootView = inflater.inflate(R.layout.dialog_audio_sharing_join, /* parent= */ null);
TextView subtitle1 = rootView.findViewById(R.id.share_audio_subtitle1);
TextView subtitle2 = rootView.findViewById(R.id.share_audio_subtitle2);
if (deviceItems.isEmpty()) {
subtitle1.setText(newDeviceName);
} else {
@@ -106,10 +109,9 @@ public class AudioSharingJoinDialogFragment extends InstrumentedDialogFragment {
.collect(Collectors.joining(", ")),
newDeviceName));
}
subtitle2.setText(
"Connected eligible headphones will hear videos ad music playing on this phone");
Button shareBtn = mRootView.findViewById(R.id.share_btn);
Button cancelBtn = mRootView.findViewById(R.id.cancel_btn);
subtitle2.setText("This device's music and videos will play on both pairs of headphones");
Button shareBtn = rootView.findViewById(R.id.share_btn);
Button cancelBtn = rootView.findViewById(R.id.cancel_btn);
shareBtn.setOnClickListener(
v -> {
sListener.onShareClick();
@@ -117,7 +119,7 @@ public class AudioSharingJoinDialogFragment extends InstrumentedDialogFragment {
});
shareBtn.setText("Share audio");
cancelBtn.setOnClickListener(v -> dismiss());
Dialog dialog = builder.setView(mRootView).create();
Dialog dialog = builder.setCustomTitle(customTitle).setView(rootView).create();
dialog.setCanceledOnTouchOutside(false);
return dialog;
}

View File

@@ -0,0 +1,162 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.connecteddevice.audiosharing;
import static com.google.common.truth.Truth.assertThat;
import static org.robolectric.shadows.ShadowLooper.shadowMainLooper;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothStatusCodes;
import android.platform.test.annotations.RequiresFlagsDisabled;
import android.platform.test.annotations.RequiresFlagsEnabled;
import android.platform.test.flag.junit.CheckFlagsRule;
import android.platform.test.flag.junit.DeviceFlagsValueProvider;
import android.view.View;
import android.widget.TextView;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import com.android.settings.R;
import com.android.settings.flags.Flags;
import com.android.settings.testutils.shadow.ShadowAlertDialogCompat;
import com.android.settings.testutils.shadow.ShadowBluetoothAdapter;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.shadow.api.Shadow;
import org.robolectric.shadows.androidx.fragment.FragmentController;
import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicBoolean;
@RunWith(RobolectricTestRunner.class)
@Config(
shadows = {
ShadowAlertDialogCompat.class,
ShadowBluetoothAdapter.class,
})
public class AudioSharingJoinDialogFragmentTest {
@Rule public final MockitoRule mocks = MockitoJUnit.rule();
@Rule
public final CheckFlagsRule mCheckFlagsRule = DeviceFlagsValueProvider.createCheckFlagsRule();
private static final String TEST_DEVICE_NAME1 = "test1";
private static final String TEST_DEVICE_NAME2 = "test2";
private static final AudioSharingDeviceItem TEST_DEVICE_ITEM =
new AudioSharingDeviceItem(TEST_DEVICE_NAME1, /* groupId= */ 1, /* isActive= */ true);
private Fragment mParent;
private AudioSharingJoinDialogFragment mFragment;
private ShadowBluetoothAdapter mShadowBluetoothAdapter;
@Before
public void setUp() {
mShadowBluetoothAdapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter());
mShadowBluetoothAdapter.setEnabled(true);
mShadowBluetoothAdapter.setIsLeAudioBroadcastSourceSupported(
BluetoothStatusCodes.FEATURE_SUPPORTED);
mShadowBluetoothAdapter.setIsLeAudioBroadcastAssistantSupported(
BluetoothStatusCodes.FEATURE_SUPPORTED);
mFragment = new AudioSharingJoinDialogFragment();
mParent = new Fragment();
FragmentController.setupFragment(
mParent, FragmentActivity.class, /* containerViewId= */ 0, /* bundle= */ null);
}
@After
public void tearDown() {
ShadowAlertDialogCompat.reset();
}
@Test
@RequiresFlagsDisabled(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
public void onCreateDialog_flagOff_dialogNotExist() {
mFragment.show(mParent, new ArrayList<>(), TEST_DEVICE_NAME2, () -> {});
shadowMainLooper().idle();
AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog();
assertThat(dialog).isNull();
}
@Test
@RequiresFlagsEnabled(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
public void onCreateDialog_flagOn_dialogShowTextForSingleDevice() {
mFragment.show(mParent, new ArrayList<>(), TEST_DEVICE_NAME2, () -> {});
shadowMainLooper().idle();
AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog();
assertThat(dialog).isNotNull();
assertThat(dialog.isShowing()).isTrue();
ShadowAlertDialogCompat shadowDialog = ShadowAlertDialogCompat.shadowOf(dialog);
View rootView = shadowDialog.getView();
TextView subtitle1 = rootView.findViewById(R.id.share_audio_subtitle1);
assertThat(subtitle1.getText()).isEqualTo(TEST_DEVICE_NAME2);
}
@Test
@RequiresFlagsEnabled(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
public void onCreateDialog_flagOn_dialogShowTextForTwoDevice() {
ArrayList<AudioSharingDeviceItem> list = new ArrayList<>();
list.add(TEST_DEVICE_ITEM);
mFragment.show(mParent, list, TEST_DEVICE_NAME2, () -> {});
shadowMainLooper().idle();
AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog();
assertThat(dialog).isNotNull();
assertThat(dialog.isShowing()).isTrue();
ShadowAlertDialogCompat shadowDialog = ShadowAlertDialogCompat.shadowOf(dialog);
View rootView = shadowDialog.getView();
TextView subtitle1 = rootView.findViewById(R.id.share_audio_subtitle1);
assertThat(subtitle1.getText()).isEqualTo(TEST_DEVICE_NAME1 + " and " + TEST_DEVICE_NAME2);
}
@Test
@RequiresFlagsEnabled(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
public void onCreateDialog_clickCancel_dialogDismiss() {
mFragment.show(mParent, new ArrayList<>(), TEST_DEVICE_NAME2, () -> {});
shadowMainLooper().idle();
AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog();
ShadowAlertDialogCompat shadowDialog = ShadowAlertDialogCompat.shadowOf(dialog);
View rootView = shadowDialog.getView();
rootView.findViewById(R.id.cancel_btn).performClick();
assertThat(dialog.isShowing()).isFalse();
}
@Test
@RequiresFlagsEnabled(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
public void onCreateDialog_clickShare_callbackTriggered() {
AtomicBoolean isShareBtnClicked = new AtomicBoolean(false);
mFragment.show(
mParent, new ArrayList<>(), TEST_DEVICE_NAME2, () -> isShareBtnClicked.set(true));
shadowMainLooper().idle();
AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog();
ShadowAlertDialogCompat shadowDialog = ShadowAlertDialogCompat.shadowOf(dialog);
View rootView = shadowDialog.getView();
rootView.findViewById(R.id.share_btn).performClick();
assertThat(dialog.isShowing()).isFalse();
assertThat(isShareBtnClicked.get()).isTrue();
}
}