Merge "[Audiosharing] Avoid dialog flickering for same bt device group." into main

This commit is contained in:
Yiyi Shen
2024-02-06 06:26:30 +00:00
committed by Android (Google) Code Review
8 changed files with 464 additions and 97 deletions

View File

@@ -24,16 +24,7 @@
android:paddingBottom="?android:dialogPreferredPadding"> android:paddingBottom="?android:dialogPreferredPadding">
<TextView <TextView
android:id="@+id/share_audio_subtitle1" android:id="@+id/share_audio_subtitle"
style="@style/DeviceAudioSharingText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingBottom="14dp"
android:textFontWeight="500" />
<TextView
android:id="@+id/share_audio_subtitle2"
style="@style/DeviceAudioSharingText" style="@style/DeviceAudioSharingText"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"

View File

@@ -17,6 +17,7 @@
package com.android.settings.connecteddevice.audiosharing; package com.android.settings.connecteddevice.audiosharing;
import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothCsipSetCoordinator;
import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothLeBroadcast; import android.bluetooth.BluetoothLeBroadcast;
import android.bluetooth.BluetoothLeBroadcastAssistant; import android.bluetooth.BluetoothLeBroadcastAssistant;
@@ -27,6 +28,7 @@ import android.content.Context;
import android.util.Log; import android.util.Log;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment; import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import androidx.lifecycle.DefaultLifecycleObserver; import androidx.lifecycle.DefaultLifecycleObserver;
@@ -43,6 +45,7 @@ import com.android.settings.dashboard.DashboardFragment;
import com.android.settingslib.bluetooth.BluetoothCallback; import com.android.settingslib.bluetooth.BluetoothCallback;
import com.android.settingslib.bluetooth.BluetoothUtils; import com.android.settingslib.bluetooth.BluetoothUtils;
import com.android.settingslib.bluetooth.CachedBluetoothDevice; import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.bluetooth.CachedBluetoothDeviceManager;
import com.android.settingslib.bluetooth.LeAudioProfile; import com.android.settingslib.bluetooth.LeAudioProfile;
import com.android.settingslib.bluetooth.LocalBluetoothLeBroadcast; import com.android.settingslib.bluetooth.LocalBluetoothLeBroadcast;
import com.android.settingslib.bluetooth.LocalBluetoothLeBroadcastAssistant; import com.android.settingslib.bluetooth.LocalBluetoothLeBroadcastAssistant;
@@ -68,9 +71,10 @@ public class AudioSharingDevicePreferenceController extends BasePreferenceContro
"connected_device_audio_sharing_settings"; "connected_device_audio_sharing_settings";
private final LocalBluetoothManager mLocalBtManager; private final LocalBluetoothManager mLocalBtManager;
private final LocalBluetoothLeBroadcast mBroadcast;
private final LocalBluetoothLeBroadcastAssistant mAssistant;
private final Executor mExecutor; private final Executor mExecutor;
private CachedBluetoothDeviceManager mDeviceManager;
private LocalBluetoothLeBroadcast mBroadcast;
private LocalBluetoothLeBroadcastAssistant mAssistant;
private PreferenceGroup mPreferenceGroup; private PreferenceGroup mPreferenceGroup;
private Preference mAudioSharingSettingsPreference; private Preference mAudioSharingSettingsPreference;
private BluetoothDeviceUpdater mBluetoothDeviceUpdater; private BluetoothDeviceUpdater mBluetoothDeviceUpdater;
@@ -165,6 +169,12 @@ public class AudioSharingDevicePreferenceController extends BasePreferenceContro
+ ", reason = " + ", reason = "
+ reason); + reason);
mBluetoothDeviceUpdater.forceUpdate(); mBluetoothDeviceUpdater.forceUpdate();
if (mDeviceManager != null) {
CachedBluetoothDevice cachedDevice = mDeviceManager.findDevice(sink);
if (cachedDevice != null) {
closeOpeningDialogsForLeaDevice(cachedDevice);
}
}
} }
@Override @Override
@@ -241,8 +251,11 @@ public class AudioSharingDevicePreferenceController extends BasePreferenceContro
public AudioSharingDevicePreferenceController(Context context) { public AudioSharingDevicePreferenceController(Context context) {
super(context, KEY); super(context, KEY);
mLocalBtManager = Utils.getLocalBtManager(mContext); mLocalBtManager = Utils.getLocalBtManager(mContext);
if (mLocalBtManager != null) {
mDeviceManager = mLocalBtManager.getCachedDeviceManager();
mBroadcast = mLocalBtManager.getProfileManager().getLeAudioBroadcastProfile(); mBroadcast = mLocalBtManager.getProfileManager().getLeAudioBroadcastProfile();
mAssistant = mLocalBtManager.getProfileManager().getLeAudioBroadcastAssistantProfile(); mAssistant = mLocalBtManager.getProfileManager().getLeAudioBroadcastAssistantProfile();
}
mExecutor = Executors.newSingleThreadExecutor(); mExecutor = Executors.newSingleThreadExecutor();
} }
@@ -344,6 +357,17 @@ public class AudioSharingDevicePreferenceController extends BasePreferenceContro
@NonNull CachedBluetoothDevice cachedDevice, @NonNull CachedBluetoothDevice cachedDevice,
@ConnectionState int state, @ConnectionState int state,
int bluetoothProfile) { int bluetoothProfile) {
if (state == BluetoothAdapter.STATE_DISCONNECTED) {
boolean isLeAudio = isLeAudioSupported(cachedDevice);
if (isLeAudio && bluetoothProfile == BluetoothProfile.LE_AUDIO) {
closeOpeningDialogsForLeaDevice(cachedDevice);
return;
}
if (!isLeAudio && !cachedDevice.isConnected()) {
closeOpeningDialogsForNonLeaDevice(cachedDevice);
return;
}
}
if (state != BluetoothAdapter.STATE_CONNECTED || !cachedDevice.getDevice().isConnected()) { if (state != BluetoothAdapter.STATE_CONNECTED || !cachedDevice.getDevice().isConnected()) {
Log.d(TAG, "Ignore onProfileConnectionStateChanged, not connected state"); Log.d(TAG, "Ignore onProfileConnectionStateChanged, not connected state");
return; return;
@@ -420,10 +444,10 @@ public class AudioSharingDevicePreferenceController extends BasePreferenceContro
// connected during a sharing session. // connected during a sharing session.
postOnMainThread( postOnMainThread(
() -> { () -> {
closeOpeningDialogs(); closeOpeningDialogsOtherThan(AudioSharingStopDialogFragment.tag());
AudioSharingStopDialogFragment.show( AudioSharingStopDialogFragment.show(
mFragment, mFragment,
cachedDevice.getName(), cachedDevice,
() -> mBroadcast.stopBroadcast(mBroadcast.getLatestBroadcastId())); () -> mBroadcast.stopBroadcast(mBroadcast.getLatestBroadcastId()));
}); });
} else { } else {
@@ -466,11 +490,12 @@ public class AudioSharingDevicePreferenceController extends BasePreferenceContro
if (deviceItemsInSharingSession.size() >= 2) { if (deviceItemsInSharingSession.size() >= 2) {
postOnMainThread( postOnMainThread(
() -> { () -> {
closeOpeningDialogs(); closeOpeningDialogsOtherThan(
AudioSharingDisconnectDialogFragment.tag());
AudioSharingDisconnectDialogFragment.show( AudioSharingDisconnectDialogFragment.show(
mFragment, mFragment,
deviceItemsInSharingSession, deviceItemsInSharingSession,
cachedDevice.getName(), cachedDevice,
(AudioSharingDeviceItem item) -> { (AudioSharingDeviceItem item) -> {
// Remove all sources from the device user clicked // Remove all sources from the device user clicked
if (groupedDevices.containsKey(item.getGroupId())) { if (groupedDevices.containsKey(item.getGroupId())) {
@@ -497,11 +522,11 @@ public class AudioSharingDevicePreferenceController extends BasePreferenceContro
// remote device connected during a sharing session. // remote device connected during a sharing session.
postOnMainThread( postOnMainThread(
() -> { () -> {
closeOpeningDialogs(); closeOpeningDialogsOtherThan(AudioSharingJoinDialogFragment.tag());
AudioSharingJoinDialogFragment.show( AudioSharingJoinDialogFragment.show(
mFragment, mFragment,
deviceItemsInSharingSession, deviceItemsInSharingSession,
cachedDevice.getName(), cachedDevice,
() -> { () -> {
// Add current broadcast to the latest connected device // Add current broadcast to the latest connected device
mAssistant.addSource( mAssistant.addSource(
@@ -527,11 +552,11 @@ public class AudioSharingDevicePreferenceController extends BasePreferenceContro
if (deviceItems.size() == 1) { if (deviceItems.size() == 1) {
postOnMainThread( postOnMainThread(
() -> { () -> {
closeOpeningDialogs(); closeOpeningDialogsOtherThan(AudioSharingJoinDialogFragment.tag());
AudioSharingJoinDialogFragment.show( AudioSharingJoinDialogFragment.show(
mFragment, mFragment,
deviceItems, deviceItems,
cachedDevice.getName(), cachedDevice,
() -> { () -> {
mTargetSinks = new ArrayList<>(); mTargetSinks = new ArrayList<>();
for (List<CachedBluetoothDevice> devices : for (List<CachedBluetoothDevice> devices :
@@ -591,17 +616,61 @@ public class AudioSharingDevicePreferenceController extends BasePreferenceContro
} }
} }
private void closeOpeningDialogs() { private void closeOpeningDialogsOtherThan(String tag) {
if (mFragment == null) return; if (mFragment == null) return;
List<Fragment> fragments = mFragment.getChildFragmentManager().getFragments(); List<Fragment> fragments = mFragment.getChildFragmentManager().getFragments();
for (Fragment fragment : fragments) { for (Fragment fragment : fragments) {
if (fragment instanceof DialogFragment) { if (fragment instanceof DialogFragment && !fragment.getTag().equals(tag)) {
Log.d(TAG, "Remove staled opening dialog " + fragment.getTag()); Log.d(TAG, "Remove staled opening dialog " + fragment.getTag());
((DialogFragment) fragment).dismiss(); ((DialogFragment) fragment).dismiss();
} }
} }
} }
private void closeOpeningDialogsForLeaDevice(@NonNull CachedBluetoothDevice cachedDevice) {
if (mFragment == null) return;
int groupId = AudioSharingUtils.getGroupId(cachedDevice);
List<Fragment> fragments = mFragment.getChildFragmentManager().getFragments();
for (Fragment fragment : fragments) {
CachedBluetoothDevice device = getCachedBluetoothDeviceFromDialog(fragment);
if (device != null
&& groupId != BluetoothCsipSetCoordinator.GROUP_ID_INVALID
&& AudioSharingUtils.getGroupId(device) == groupId) {
Log.d(TAG, "Remove staled opening dialog for group " + groupId);
((DialogFragment) fragment).dismiss();
}
}
}
private void closeOpeningDialogsForNonLeaDevice(@NonNull CachedBluetoothDevice cachedDevice) {
if (mFragment == null) return;
String address = cachedDevice.getAddress();
List<Fragment> fragments = mFragment.getChildFragmentManager().getFragments();
for (Fragment fragment : fragments) {
CachedBluetoothDevice device = getCachedBluetoothDeviceFromDialog(fragment);
if (device != null && address != null && address.equals(device.getAddress())) {
Log.d(
TAG,
"Remove staled opening dialog for device "
+ cachedDevice.getDevice().getAnonymizedAddress());
((DialogFragment) fragment).dismiss();
}
}
}
@Nullable
private CachedBluetoothDevice getCachedBluetoothDeviceFromDialog(Fragment fragment) {
CachedBluetoothDevice device = null;
if (fragment instanceof AudioSharingJoinDialogFragment) {
device = ((AudioSharingJoinDialogFragment) fragment).getDevice();
} else if (fragment instanceof AudioSharingStopDialogFragment) {
device = ((AudioSharingStopDialogFragment) fragment).getDevice();
} else if (fragment instanceof AudioSharingDisconnectDialogFragment) {
device = ((AudioSharingDisconnectDialogFragment) fragment).getDevice();
}
return device;
}
private void postOnMainThread(@NonNull Runnable runnable) { private void postOnMainThread(@NonNull Runnable runnable) {
mContext.getMainExecutor().execute(runnable); mContext.getMainExecutor().execute(runnable);
} }

View File

@@ -19,13 +19,17 @@ package com.android.settings.connecteddevice.audiosharing;
import android.app.Dialog; import android.app.Dialog;
import android.app.settings.SettingsEnums; import android.app.settings.SettingsEnums;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.widget.Button; import android.widget.Button;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentManager;
import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.LinearLayoutManager;
@@ -33,8 +37,10 @@ import androidx.recyclerview.widget.RecyclerView;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.core.instrumentation.InstrumentedDialogFragment; import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Locale;
public class AudioSharingDisconnectDialogFragment extends InstrumentedDialogFragment { public class AudioSharingDisconnectDialogFragment extends InstrumentedDialogFragment {
private static final String TAG = "AudioSharingDisconnectDialog"; private static final String TAG = "AudioSharingDisconnectDialog";
@@ -56,6 +62,7 @@ public class AudioSharingDisconnectDialogFragment extends InstrumentedDialogFrag
} }
private static DialogEventListener sListener; private static DialogEventListener sListener;
@Nullable private static CachedBluetoothDevice sNewDevice;
@Override @Override
public int getMetricsCategory() { public int getMetricsCategory() {
@@ -65,35 +72,78 @@ public class AudioSharingDisconnectDialogFragment extends InstrumentedDialogFrag
/** /**
* Display the {@link AudioSharingDisconnectDialogFragment} dialog. * Display the {@link AudioSharingDisconnectDialogFragment} dialog.
* *
* <p>If the dialog is showing for the same group, update the dialog event listener.
*
* @param host The Fragment this dialog will be hosted. * @param host The Fragment this dialog will be hosted.
* @param deviceItems The existing connected device items in audio sharing session. * @param deviceItems The existing connected device items in audio sharing session.
* @param newDeviceName The name of the latest connected device triggered this dialog. * @param newDevice The latest connected device triggered this dialog.
* @param listener The callback to handle the user action on this dialog. * @param listener The callback to handle the user action on this dialog.
*/ */
public static void show( public static void show(
Fragment host, Fragment host,
ArrayList<AudioSharingDeviceItem> deviceItems, ArrayList<AudioSharingDeviceItem> deviceItems,
String newDeviceName, CachedBluetoothDevice newDevice,
DialogEventListener listener) { DialogEventListener listener) {
if (!AudioSharingUtils.isFeatureEnabled()) return; if (!AudioSharingUtils.isFeatureEnabled()) return;
final FragmentManager manager = host.getChildFragmentManager(); final FragmentManager manager = host.getChildFragmentManager();
Fragment dialog = manager.findFragmentByTag(TAG);
if (dialog != null
&& ((DialogFragment) dialog).getDialog() != null
&& ((DialogFragment) dialog).getDialog().isShowing()) {
int newGroupId = AudioSharingUtils.getGroupId(newDevice);
if (sNewDevice != null && newGroupId == AudioSharingUtils.getGroupId(sNewDevice)) {
Log.d(
TAG,
String.format(
Locale.US,
"Dialog is showing for the same device group %d, "
+ "update the content.",
newGroupId));
sListener = listener; sListener = listener;
sNewDevice = newDevice;
return;
} else {
Log.d(
TAG,
String.format(
Locale.US,
"Dialog is showing for new device group %d, "
+ "dismiss current dialog.",
newGroupId));
((DialogFragment) dialog).dismiss();
}
}
sListener = listener;
sNewDevice = newDevice;
Log.d(TAG, "Show up the dialog.");
final Bundle bundle = new Bundle(); final Bundle bundle = new Bundle();
bundle.putParcelableArrayList(BUNDLE_KEY_DEVICE_TO_DISCONNECT_ITEMS, deviceItems); bundle.putParcelableArrayList(BUNDLE_KEY_DEVICE_TO_DISCONNECT_ITEMS, deviceItems);
bundle.putString(BUNDLE_KEY_NEW_DEVICE_NAME, newDeviceName); bundle.putString(BUNDLE_KEY_NEW_DEVICE_NAME, newDevice.getName());
AudioSharingDisconnectDialogFragment dialog = new AudioSharingDisconnectDialogFragment(); AudioSharingDisconnectDialogFragment dialogFrag =
dialog.setArguments(bundle); new AudioSharingDisconnectDialogFragment();
dialog.show(manager, TAG); dialogFrag.setArguments(bundle);
dialogFrag.show(manager, TAG);
}
/** Return the tag of {@link AudioSharingDisconnectDialogFragment} dialog. */
public static @NonNull String tag() {
return TAG;
}
/** Get the latest connected device which triggers the dialog. */
public @Nullable CachedBluetoothDevice getDevice() {
return sNewDevice;
} }
@Override @Override
public Dialog onCreateDialog(Bundle savedInstanceState) { public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
Bundle arguments = requireArguments(); Bundle arguments = requireArguments();
ArrayList<AudioSharingDeviceItem> deviceItems = ArrayList<AudioSharingDeviceItem> deviceItems =
arguments.getParcelableArrayList(BUNDLE_KEY_DEVICE_TO_DISCONNECT_ITEMS); arguments.getParcelableArrayList(BUNDLE_KEY_DEVICE_TO_DISCONNECT_ITEMS);
final AlertDialog.Builder builder = final AlertDialog.Builder builder =
new AlertDialog.Builder(getActivity()).setCancelable(false); new AlertDialog.Builder(getActivity()).setCancelable(false);
LayoutInflater inflater = LayoutInflater.from(builder.getContext()); LayoutInflater inflater = LayoutInflater.from(builder.getContext());
// Set custom title for the dialog.
View customTitle = inflater.inflate(R.layout.dialog_custom_title_audio_sharing, null); View customTitle = inflater.inflate(R.layout.dialog_custom_title_audio_sharing, null);
ImageView icon = customTitle.findViewById(R.id.title_icon); ImageView icon = customTitle.findViewById(R.id.title_icon);
icon.setImageResource(R.drawable.ic_bt_audio_sharing); icon.setImageResource(R.drawable.ic_bt_audio_sharing);
@@ -115,10 +165,7 @@ public class AudioSharingDisconnectDialogFragment extends InstrumentedDialogFrag
recyclerView.setLayoutManager( recyclerView.setLayoutManager(
new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false)); new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
Button cancelBtn = rootView.findViewById(R.id.cancel_btn); Button cancelBtn = rootView.findViewById(R.id.cancel_btn);
cancelBtn.setOnClickListener( cancelBtn.setOnClickListener(v -> dismiss());
v -> {
dismiss();
});
AlertDialog dialog = builder.setCustomTitle(customTitle).setView(rootView).create(); AlertDialog dialog = builder.setCustomTitle(customTitle).setView(rootView).create();
dialog.setCanceledOnTouchOutside(false); dialog.setCanceledOnTouchOutside(false);
return dialog; return dialog;

View File

@@ -18,19 +18,25 @@ package com.android.settings.connecteddevice.audiosharing;
import android.app.Dialog; import android.app.Dialog;
import android.app.settings.SettingsEnums; import android.app.settings.SettingsEnums;
import android.graphics.Typeface;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.widget.Button; import android.widget.Button;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentManager;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.core.instrumentation.InstrumentedDialogFragment; import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Locale; import java.util.Locale;
@@ -49,6 +55,7 @@ public class AudioSharingJoinDialogFragment extends InstrumentedDialogFragment {
} }
private static DialogEventListener sListener; private static DialogEventListener sListener;
private static @Nullable CachedBluetoothDevice sNewDevice;
@Override @Override
public int getMetricsCategory() { public int getMetricsCategory() {
@@ -58,29 +65,54 @@ public class AudioSharingJoinDialogFragment extends InstrumentedDialogFragment {
/** /**
* Display the {@link AudioSharingJoinDialogFragment} dialog. * Display the {@link AudioSharingJoinDialogFragment} dialog.
* *
* <p>If the dialog is showing, update the dialog message and event listener.
*
* @param host The Fragment this dialog will be hosted. * @param host The Fragment this dialog will be hosted.
* @param deviceItems The existing connected device items eligible for audio sharing. * @param deviceItems The existing connected device items eligible for audio sharing.
* @param newDeviceName The name of the latest connected device triggered this dialog. * @param newDevice The latest connected device triggered this dialog.
* @param listener The callback to handle the user action on this dialog. * @param listener The callback to handle the user action on this dialog.
*/ */
public static void show( public static void show(
Fragment host, Fragment host,
ArrayList<AudioSharingDeviceItem> deviceItems, ArrayList<AudioSharingDeviceItem> deviceItems,
String newDeviceName, CachedBluetoothDevice newDevice,
DialogEventListener listener) { DialogEventListener listener) {
if (!AudioSharingUtils.isFeatureEnabled()) return; if (!AudioSharingUtils.isFeatureEnabled()) return;
final FragmentManager manager = host.getChildFragmentManager(); final FragmentManager manager = host.getChildFragmentManager();
sListener = listener; sListener = listener;
sNewDevice = newDevice;
Fragment dialog = manager.findFragmentByTag(TAG);
if (dialog != null
&& ((DialogFragment) dialog).getDialog() != null
&& ((DialogFragment) dialog).getDialog().isShowing()) {
Log.d(TAG, "Dialog is showing, update the content.");
updateDialog(
deviceItems,
newDevice.getName(),
(AlertDialog) ((DialogFragment) dialog).getDialog());
} else {
Log.d(TAG, "Show up the dialog.");
final Bundle bundle = new Bundle(); final Bundle bundle = new Bundle();
bundle.putParcelableArrayList(BUNDLE_KEY_DEVICE_ITEMS, deviceItems); bundle.putParcelableArrayList(BUNDLE_KEY_DEVICE_ITEMS, deviceItems);
bundle.putString(BUNDLE_KEY_NEW_DEVICE_NAME, newDeviceName); bundle.putString(BUNDLE_KEY_NEW_DEVICE_NAME, newDevice.getName());
final AudioSharingJoinDialogFragment dialog = new AudioSharingJoinDialogFragment(); final AudioSharingJoinDialogFragment dialogFrag = new AudioSharingJoinDialogFragment();
dialog.setArguments(bundle); dialogFrag.setArguments(bundle);
dialog.show(manager, TAG); dialogFrag.show(manager, TAG);
}
}
/** Return the tag of {@link AudioSharingJoinDialogFragment} dialog. */
public static @NonNull String tag() {
return TAG;
}
/** Get the latest connected device which triggers the dialog. */
public @Nullable CachedBluetoothDevice getDevice() {
return sNewDevice;
} }
@Override @Override
public Dialog onCreateDialog(Bundle savedInstanceState) { public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
Bundle arguments = requireArguments(); Bundle arguments = requireArguments();
ArrayList<AudioSharingDeviceItem> deviceItems = ArrayList<AudioSharingDeviceItem> deviceItems =
arguments.getParcelableArrayList(BUNDLE_KEY_DEVICE_ITEMS); arguments.getParcelableArrayList(BUNDLE_KEY_DEVICE_ITEMS);
@@ -88,6 +120,7 @@ public class AudioSharingJoinDialogFragment extends InstrumentedDialogFragment {
final AlertDialog.Builder builder = final AlertDialog.Builder builder =
new AlertDialog.Builder(getActivity()).setCancelable(false); new AlertDialog.Builder(getActivity()).setCancelable(false);
LayoutInflater inflater = LayoutInflater.from(builder.getContext()); LayoutInflater inflater = LayoutInflater.from(builder.getContext());
// Set custom title for the dialog.
View customTitle = View customTitle =
inflater.inflate(R.layout.dialog_custom_title_audio_sharing, /* parent= */ null); inflater.inflate(R.layout.dialog_custom_title_audio_sharing, /* parent= */ null);
ImageView icon = customTitle.findViewById(R.id.title_icon); ImageView icon = customTitle.findViewById(R.id.title_icon);
@@ -95,21 +128,8 @@ public class AudioSharingJoinDialogFragment extends InstrumentedDialogFragment {
TextView title = customTitle.findViewById(R.id.title_text); TextView title = customTitle.findViewById(R.id.title_text);
title.setText("Share your audio"); title.setText("Share your audio");
View rootView = inflater.inflate(R.layout.dialog_audio_sharing_join, /* parent= */ null); View rootView = inflater.inflate(R.layout.dialog_audio_sharing_join, /* parent= */ null);
TextView subtitle1 = rootView.findViewById(R.id.share_audio_subtitle1); TextView subtitle = rootView.findViewById(R.id.share_audio_subtitle);
TextView subtitle2 = rootView.findViewById(R.id.share_audio_subtitle2); subtitle.setText("This device's music and videos will play on both pairs of headphones");
if (deviceItems.isEmpty()) {
subtitle1.setText(newDeviceName);
} else {
subtitle1.setText(
String.format(
Locale.US,
"%s and %s",
deviceItems.stream()
.map(AudioSharingDeviceItem::getName)
.collect(Collectors.joining(", ")),
newDeviceName));
}
subtitle2.setText("This device's music and videos will play on both pairs of headphones");
Button shareBtn = rootView.findViewById(R.id.share_btn); Button shareBtn = rootView.findViewById(R.id.share_btn);
Button cancelBtn = rootView.findViewById(R.id.cancel_btn); Button cancelBtn = rootView.findViewById(R.id.cancel_btn);
shareBtn.setOnClickListener( shareBtn.setOnClickListener(
@@ -119,8 +139,37 @@ public class AudioSharingJoinDialogFragment extends InstrumentedDialogFragment {
}); });
shareBtn.setText("Share audio"); shareBtn.setText("Share audio");
cancelBtn.setOnClickListener(v -> dismiss()); cancelBtn.setOnClickListener(v -> dismiss());
Dialog dialog = builder.setCustomTitle(customTitle).setView(rootView).create(); AlertDialog dialog = builder.setCustomTitle(customTitle).setView(rootView).create();
dialog.setCanceledOnTouchOutside(false); dialog.setCanceledOnTouchOutside(false);
updateDialog(deviceItems, newDeviceName, dialog);
dialog.show();
TextView messageView = (TextView) dialog.findViewById(android.R.id.message);
if (messageView != null) {
Typeface typeface = Typeface.create(Typeface.DEFAULT_FAMILY, Typeface.NORMAL);
messageView.setTypeface(typeface);
messageView.setTextDirection(View.TEXT_DIRECTION_LOCALE);
messageView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
} else {
Log.w(TAG, "Fail to update message style: message view is null");
}
return dialog; return dialog;
} }
private static void updateDialog(
ArrayList<AudioSharingDeviceItem> deviceItems,
String newDeviceName,
@NonNull AlertDialog dialog) {
if (deviceItems.isEmpty()) {
dialog.setMessage(newDeviceName);
} else {
dialog.setMessage(
String.format(
Locale.US,
"%s and %s",
deviceItems.stream()
.map(AudioSharingDeviceItem::getName)
.collect(Collectors.joining(", ")),
newDeviceName));
}
}
} }

View File

@@ -18,18 +18,24 @@ package com.android.settings.connecteddevice.audiosharing;
import android.app.Dialog; import android.app.Dialog;
import android.app.settings.SettingsEnums; import android.app.settings.SettingsEnums;
import android.graphics.Typeface;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentManager;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.core.instrumentation.InstrumentedDialogFragment; import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
public class AudioSharingStopDialogFragment extends InstrumentedDialogFragment { public class AudioSharingStopDialogFragment extends InstrumentedDialogFragment {
private static final String TAG = "AudioSharingStopDialog"; private static final String TAG = "AudioSharingStopDialog";
@@ -44,6 +50,7 @@ public class AudioSharingStopDialogFragment extends InstrumentedDialogFragment {
} }
private static DialogEventListener sListener; private static DialogEventListener sListener;
private static @Nullable CachedBluetoothDevice sNewDevice;
@Override @Override
public int getMetricsCategory() { public int getMetricsCategory() {
@@ -53,41 +60,79 @@ public class AudioSharingStopDialogFragment extends InstrumentedDialogFragment {
/** /**
* Display the {@link AudioSharingStopDialogFragment} dialog. * Display the {@link AudioSharingStopDialogFragment} dialog.
* *
* <p>If the dialog is showing, update the dialog message and event listener.
*
* @param host The Fragment this dialog will be hosted. * @param host The Fragment this dialog will be hosted.
* @param newDeviceName The name of the latest connected device triggered this dialog. * @param newDevice The latest connected device triggered this dialog.
* @param listener The callback to handle the user action on this dialog. * @param listener The callback to handle the user action on this dialog.
*/ */
public static void show(Fragment host, String newDeviceName, DialogEventListener listener) { public static void show(
Fragment host, CachedBluetoothDevice newDevice, DialogEventListener listener) {
if (!AudioSharingUtils.isFeatureEnabled()) return; if (!AudioSharingUtils.isFeatureEnabled()) return;
final FragmentManager manager = host.getChildFragmentManager(); final FragmentManager manager = host.getChildFragmentManager();
sListener = listener; sListener = listener;
sNewDevice = newDevice;
Fragment dialog = manager.findFragmentByTag(TAG);
if (dialog != null
&& ((DialogFragment) dialog).getDialog() != null
&& ((DialogFragment) dialog).getDialog().isShowing()) {
Log.d(TAG, "Dialog is showing, update the content.");
updateDialog(newDevice.getName(), (AlertDialog) ((DialogFragment) dialog).getDialog());
} else {
Log.d(TAG, "Show up the dialog.");
final Bundle bundle = new Bundle(); final Bundle bundle = new Bundle();
bundle.putString(BUNDLE_KEY_NEW_DEVICE_NAME, newDeviceName); bundle.putString(BUNDLE_KEY_NEW_DEVICE_NAME, newDevice.getName());
AudioSharingStopDialogFragment dialog = new AudioSharingStopDialogFragment(); AudioSharingStopDialogFragment dialogFrag = new AudioSharingStopDialogFragment();
dialog.setArguments(bundle); dialogFrag.setArguments(bundle);
dialog.show(manager, TAG); dialogFrag.show(manager, TAG);
}
}
/** Return the tag of {@link AudioSharingStopDialogFragment} dialog. */
public static @NonNull String tag() {
return TAG;
}
/** Get the latest connected device which triggers the dialog. */
public @Nullable CachedBluetoothDevice getDevice() {
return sNewDevice;
} }
@Override @Override
public Dialog onCreateDialog(Bundle savedInstanceState) { public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
Bundle arguments = requireArguments(); Bundle arguments = requireArguments();
String newDeviceName = arguments.getString(BUNDLE_KEY_NEW_DEVICE_NAME); String newDeviceName = arguments.getString(BUNDLE_KEY_NEW_DEVICE_NAME);
final AlertDialog.Builder builder = final AlertDialog.Builder builder =
new AlertDialog.Builder(getActivity()).setCancelable(false); new AlertDialog.Builder(getActivity()).setCancelable(false);
LayoutInflater inflater = LayoutInflater.from(builder.getContext()); LayoutInflater inflater = LayoutInflater.from(builder.getContext());
// Set custom title for the dialog.
View customTitle = View customTitle =
inflater.inflate(R.layout.dialog_custom_title_audio_sharing, /* parent= */ null); inflater.inflate(R.layout.dialog_custom_title_audio_sharing, /* parent= */ null);
ImageView icon = customTitle.findViewById(R.id.title_icon); ImageView icon = customTitle.findViewById(R.id.title_icon);
icon.setImageResource(R.drawable.ic_warning_24dp); icon.setImageResource(R.drawable.ic_warning_24dp);
TextView title = customTitle.findViewById(R.id.title_text); TextView title = customTitle.findViewById(R.id.title_text);
title.setText("Stop sharing audio?"); title.setText("Stop sharing audio?");
builder.setMessage(
newDeviceName + " wants to connect, headphones in audio sharing will disconnect.");
builder.setPositiveButton( builder.setPositiveButton(
"Stop sharing", (dialog, which) -> sListener.onStopSharingClick()); "Stop sharing", (dialog, which) -> sListener.onStopSharingClick());
builder.setNegativeButton("Cancel", (dialog, which) -> dismiss()); builder.setNegativeButton("Cancel", (dialog, which) -> dismiss());
AlertDialog dialog = builder.setCustomTitle(customTitle).create(); AlertDialog dialog = builder.setCustomTitle(customTitle).create();
dialog.setCanceledOnTouchOutside(false); dialog.setCanceledOnTouchOutside(false);
updateDialog(newDeviceName, dialog);
dialog.show();
TextView messageView = (TextView) dialog.findViewById(android.R.id.message);
if (messageView != null) {
Typeface typeface = Typeface.create(Typeface.DEFAULT_FAMILY, Typeface.NORMAL);
messageView.setTypeface(typeface);
messageView.setTextDirection(View.TEXT_DIRECTION_LOCALE);
messageView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
} else {
Log.w(TAG, "sssFail to update dialog: message view is null");
}
return dialog; return dialog;
} }
private static void updateDialog(String newDeviceName, @NonNull AlertDialog dialog) {
dialog.setMessage(
newDeviceName + " wants to connect, headphones in audio sharing will disconnect.");
}
} }

View File

@@ -18,15 +18,18 @@ package com.android.settings.connecteddevice.audiosharing;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.when;
import static org.robolectric.shadows.ShadowLooper.shadowMainLooper; import static org.robolectric.shadows.ShadowLooper.shadowMainLooper;
import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothStatusCodes; import android.bluetooth.BluetoothStatusCodes;
import android.platform.test.annotations.RequiresFlagsDisabled; import android.platform.test.annotations.RequiresFlagsDisabled;
import android.platform.test.annotations.RequiresFlagsEnabled; import android.platform.test.annotations.RequiresFlagsEnabled;
import android.platform.test.flag.junit.CheckFlagsRule; import android.platform.test.flag.junit.CheckFlagsRule;
import android.platform.test.flag.junit.DeviceFlagsValueProvider; import android.platform.test.flag.junit.DeviceFlagsValueProvider;
import android.view.View; import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
@@ -37,11 +40,13 @@ import com.android.settings.R;
import com.android.settings.flags.Flags; import com.android.settings.flags.Flags;
import com.android.settings.testutils.shadow.ShadowAlertDialogCompat; import com.android.settings.testutils.shadow.ShadowAlertDialogCompat;
import com.android.settings.testutils.shadow.ShadowBluetoothAdapter; import com.android.settings.testutils.shadow.ShadowBluetoothAdapter;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit; import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule; import org.mockito.junit.MockitoRule;
import org.robolectric.RobolectricTestRunner; import org.robolectric.RobolectricTestRunner;
@@ -50,6 +55,7 @@ import org.robolectric.shadow.api.Shadow;
import org.robolectric.shadows.androidx.fragment.FragmentController; import org.robolectric.shadows.androidx.fragment.FragmentController;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicBoolean;
@RunWith(RobolectricTestRunner.class) @RunWith(RobolectricTestRunner.class)
@Config( @Config(
@@ -67,38 +73,68 @@ public class AudioSharingDisconnectDialogFragmentTest {
private static final String TEST_DEVICE_NAME1 = "test1"; private static final String TEST_DEVICE_NAME1 = "test1";
private static final String TEST_DEVICE_NAME2 = "test2"; private static final String TEST_DEVICE_NAME2 = "test2";
private static final String TEST_DEVICE_NAME3 = "test3"; private static final String TEST_DEVICE_NAME3 = "test3";
private static final int TEST_GROUP_ID1 = 1;
private static final int TEST_GROUP_ID2 = 2;
private static final int TEST_GROUP_ID3 = 3;
private static final AudioSharingDeviceItem TEST_DEVICE_ITEM1 = private static final AudioSharingDeviceItem TEST_DEVICE_ITEM1 =
new AudioSharingDeviceItem(TEST_DEVICE_NAME1, /* groupId= */ 1, /* isActive= */ true); new AudioSharingDeviceItem(TEST_DEVICE_NAME1, TEST_GROUP_ID1, /* isActive= */ true);
private static final AudioSharingDeviceItem TEST_DEVICE_ITEM2 = private static final AudioSharingDeviceItem TEST_DEVICE_ITEM2 =
new AudioSharingDeviceItem(TEST_DEVICE_NAME2, /* groupId= */ 2, /* isActive= */ false); new AudioSharingDeviceItem(TEST_DEVICE_NAME2, TEST_GROUP_ID2, /* isActive= */ false);
private static final AudioSharingDeviceItem TEST_DEVICE_ITEM3 =
new AudioSharingDeviceItem(TEST_DEVICE_NAME3, TEST_GROUP_ID3, /* isActive= */ false);
@Mock private BluetoothDevice mDevice1;
@Mock private BluetoothDevice mDevice2;
@Mock private BluetoothDevice mDevice3;
@Mock private CachedBluetoothDevice mCachedDevice1;
@Mock private CachedBluetoothDevice mCachedDevice2;
@Mock private CachedBluetoothDevice mCachedDevice3;
private Fragment mParent; private Fragment mParent;
private AudioSharingDisconnectDialogFragment mFragment; private AudioSharingDisconnectDialogFragment mFragment;
private ShadowBluetoothAdapter mShadowBluetoothAdapter; private ShadowBluetoothAdapter mShadowBluetoothAdapter;
private ArrayList<AudioSharingDeviceItem> mDeviceItems = new ArrayList<>();
@Before @Before
public void setUp() { public void setUp() {
AlertDialog latestAlertDialog = ShadowAlertDialogCompat.getLatestAlertDialog();
if (latestAlertDialog != null) {
latestAlertDialog.dismiss();
ShadowAlertDialogCompat.reset(); ShadowAlertDialogCompat.reset();
}
mShadowBluetoothAdapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter()); mShadowBluetoothAdapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter());
mShadowBluetoothAdapter.setEnabled(true); mShadowBluetoothAdapter.setEnabled(true);
mShadowBluetoothAdapter.setIsLeAudioBroadcastSourceSupported( mShadowBluetoothAdapter.setIsLeAudioBroadcastSourceSupported(
BluetoothStatusCodes.FEATURE_SUPPORTED); BluetoothStatusCodes.FEATURE_SUPPORTED);
mShadowBluetoothAdapter.setIsLeAudioBroadcastAssistantSupported( mShadowBluetoothAdapter.setIsLeAudioBroadcastAssistantSupported(
BluetoothStatusCodes.FEATURE_SUPPORTED); BluetoothStatusCodes.FEATURE_SUPPORTED);
when(mCachedDevice1.getName()).thenReturn(TEST_DEVICE_NAME1);
when(mCachedDevice1.getDevice()).thenReturn(mDevice1);
when(mCachedDevice1.getGroupId()).thenReturn(TEST_GROUP_ID1);
when(mCachedDevice2.getName()).thenReturn(TEST_DEVICE_NAME2);
when(mCachedDevice2.getDevice()).thenReturn(mDevice2);
when(mCachedDevice2.getGroupId()).thenReturn(TEST_GROUP_ID2);
when(mCachedDevice3.getName()).thenReturn(TEST_DEVICE_NAME3);
when(mCachedDevice3.getDevice()).thenReturn(mDevice3);
when(mCachedDevice3.getGroupId()).thenReturn(TEST_GROUP_ID3);
mFragment = new AudioSharingDisconnectDialogFragment(); mFragment = new AudioSharingDisconnectDialogFragment();
mParent = new Fragment(); mParent = new Fragment();
FragmentController.setupFragment( FragmentController.setupFragment(
mParent, FragmentActivity.class, /* containerViewId= */ 0, /* bundle= */ null); mParent, FragmentActivity.class, /* containerViewId= */ 0, /* bundle= */ null);
ArrayList<AudioSharingDeviceItem> list = new ArrayList<>(); mDeviceItems.add(TEST_DEVICE_ITEM1);
list.add(TEST_DEVICE_ITEM1); mDeviceItems.add(TEST_DEVICE_ITEM2);
list.add(TEST_DEVICE_ITEM2); mFragment.show(mParent, mDeviceItems, mCachedDevice3, (item) -> {});
mFragment.show(mParent, list, TEST_DEVICE_NAME3, (item) -> {});
shadowMainLooper().idle(); shadowMainLooper().idle();
} }
@Test @Test
@RequiresFlagsDisabled(Flags.FLAG_ENABLE_LE_AUDIO_SHARING) @RequiresFlagsDisabled(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
public void onCreateDialog_flagOff_dialogNotExist() { public void onCreateDialog_flagOff_dialogNotExist() {
ArrayList<AudioSharingDeviceItem> list = new ArrayList<>();
list.add(TEST_DEVICE_ITEM1);
list.add(TEST_DEVICE_ITEM2);
mFragment.show(mParent, list, mCachedDevice3, (item) -> {});
shadowMainLooper().idle();
AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog(); AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog();
assertThat(dialog).isNull(); assertThat(dialog).isNull();
} }
@@ -107,16 +143,79 @@ public class AudioSharingDisconnectDialogFragmentTest {
@RequiresFlagsEnabled(Flags.FLAG_ENABLE_LE_AUDIO_SHARING) @RequiresFlagsEnabled(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
public void onCreateDialog_flagOn_dialogShowBtnForTwoDevices() { public void onCreateDialog_flagOn_dialogShowBtnForTwoDevices() {
AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog(); AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog();
assertThat(dialog.isShowing()).isTrue();
ShadowAlertDialogCompat shadowDialog = ShadowAlertDialogCompat.shadowOf(dialog); ShadowAlertDialogCompat shadowDialog = ShadowAlertDialogCompat.shadowOf(dialog);
View rootView = shadowDialog.getView(); View rootView = shadowDialog.getView();
RecyclerView view = rootView.findViewById(R.id.device_btn_list); RecyclerView view = rootView.findViewById(R.id.device_btn_list);
assertThat(view.getAdapter().getItemCount()).isEqualTo(2); assertThat(view.getAdapter().getItemCount()).isEqualTo(2);
} }
@Test
@RequiresFlagsEnabled(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
public void onCreateDialog_dialogIsShowingForSameGroup_updateDialog() {
String prefix = "Disconnect ";
AtomicBoolean isItemBtnClicked = new AtomicBoolean(false);
AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog();
assertThat(dialog.isShowing()).isTrue();
ShadowAlertDialogCompat shadowDialog = ShadowAlertDialogCompat.shadowOf(dialog);
View rootView = shadowDialog.getView();
RecyclerView view = rootView.findViewById(R.id.device_btn_list);
assertThat(view.getAdapter().getItemCount()).isEqualTo(2);
Button btn1 =
view.findViewHolderForAdapterPosition(0).itemView.findViewById(R.id.device_button);
assertThat(btn1.getText().toString()).isEqualTo(prefix + TEST_DEVICE_NAME1);
Button btn2 =
view.findViewHolderForAdapterPosition(1).itemView.findViewById(R.id.device_button);
assertThat(btn2.getText().toString()).isEqualTo(prefix + TEST_DEVICE_NAME2);
btn1.performClick();
assertThat(isItemBtnClicked.get()).isFalse();
// Update dialog content with same group
mFragment.show(mParent, mDeviceItems, mCachedDevice3, (item) -> isItemBtnClicked.set(true));
shadowMainLooper().idle();
dialog = ShadowAlertDialogCompat.getLatestAlertDialog();
assertThat(dialog.isShowing()).isTrue();
btn1 = view.findViewHolderForAdapterPosition(0).itemView.findViewById(R.id.device_button);
btn1.performClick();
assertThat(isItemBtnClicked.get()).isTrue();
}
@Test
@RequiresFlagsEnabled(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
public void onCreateDialog_dialogIsShowingForNewGroup_updateDialog() {
String prefix = "Disconnect ";
AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog();
assertThat(dialog.isShowing()).isTrue();
ShadowAlertDialogCompat shadowDialog = ShadowAlertDialogCompat.shadowOf(dialog);
View rootView = shadowDialog.getView();
RecyclerView view = rootView.findViewById(R.id.device_btn_list);
assertThat(view.getAdapter().getItemCount()).isEqualTo(2);
// Update dialog content with new group
ArrayList<AudioSharingDeviceItem> newDeviceItems = new ArrayList<>();
newDeviceItems.add(TEST_DEVICE_ITEM2);
newDeviceItems.add(TEST_DEVICE_ITEM3);
mFragment.show(mParent, newDeviceItems, mCachedDevice1, (item) -> {});
shadowMainLooper().idle();
dialog = ShadowAlertDialogCompat.getLatestAlertDialog();
assertThat(dialog.isShowing()).isTrue();
shadowDialog = ShadowAlertDialogCompat.shadowOf(dialog);
rootView = shadowDialog.getView();
view = rootView.findViewById(R.id.device_btn_list);
assertThat(view.getAdapter().getItemCount()).isEqualTo(2);
Button btn1 =
view.findViewHolderForAdapterPosition(0).itemView.findViewById(R.id.device_button);
assertThat(btn1.getText().toString()).isEqualTo(prefix + TEST_DEVICE_NAME2);
Button btn2 =
view.findViewHolderForAdapterPosition(1).itemView.findViewById(R.id.device_button);
assertThat(btn2.getText().toString()).isEqualTo(prefix + TEST_DEVICE_NAME3);
}
@Test @Test
@RequiresFlagsEnabled(Flags.FLAG_ENABLE_LE_AUDIO_SHARING) @RequiresFlagsEnabled(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
public void onCreateDialog_clickCancel_dialogDismiss() { public void onCreateDialog_clickCancel_dialogDismiss() {
AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog(); AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog();
assertThat(dialog.isShowing()).isTrue();
ShadowAlertDialogCompat shadowDialog = ShadowAlertDialogCompat.shadowOf(dialog); ShadowAlertDialogCompat shadowDialog = ShadowAlertDialogCompat.shadowOf(dialog);
View rootView = shadowDialog.getView(); View rootView = shadowDialog.getView();
rootView.findViewById(R.id.cancel_btn).performClick(); rootView.findViewById(R.id.cancel_btn).performClick();

View File

@@ -18,6 +18,7 @@ package com.android.settings.connecteddevice.audiosharing;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.when;
import static org.robolectric.shadows.ShadowLooper.shadowMainLooper; import static org.robolectric.shadows.ShadowLooper.shadowMainLooper;
import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothAdapter;
@@ -27,7 +28,6 @@ import android.platform.test.annotations.RequiresFlagsEnabled;
import android.platform.test.flag.junit.CheckFlagsRule; import android.platform.test.flag.junit.CheckFlagsRule;
import android.platform.test.flag.junit.DeviceFlagsValueProvider; import android.platform.test.flag.junit.DeviceFlagsValueProvider;
import android.view.View; import android.view.View;
import android.widget.TextView;
import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
@@ -37,11 +37,13 @@ import com.android.settings.R;
import com.android.settings.flags.Flags; import com.android.settings.flags.Flags;
import com.android.settings.testutils.shadow.ShadowAlertDialogCompat; import com.android.settings.testutils.shadow.ShadowAlertDialogCompat;
import com.android.settings.testutils.shadow.ShadowBluetoothAdapter; import com.android.settings.testutils.shadow.ShadowBluetoothAdapter;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit; import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule; import org.mockito.junit.MockitoRule;
import org.robolectric.RobolectricTestRunner; import org.robolectric.RobolectricTestRunner;
@@ -67,22 +69,32 @@ public class AudioSharingJoinDialogFragmentTest {
private static final String TEST_DEVICE_NAME1 = "test1"; private static final String TEST_DEVICE_NAME1 = "test1";
private static final String TEST_DEVICE_NAME2 = "test2"; private static final String TEST_DEVICE_NAME2 = "test2";
private static final AudioSharingDeviceItem TEST_DEVICE_ITEM = private static final AudioSharingDeviceItem TEST_DEVICE_ITEM1 =
new AudioSharingDeviceItem(TEST_DEVICE_NAME1, /* groupId= */ 1, /* isActive= */ true); new AudioSharingDeviceItem(TEST_DEVICE_NAME1, /* groupId= */ 1, /* isActive= */ true);
private static final AudioSharingDeviceItem TEST_DEVICE_ITEM2 =
new AudioSharingDeviceItem(TEST_DEVICE_NAME2, /* groupId= */ 2, /* isActive= */ false);
@Mock private CachedBluetoothDevice mCachedDevice1;
@Mock private CachedBluetoothDevice mCachedDevice2;
private Fragment mParent; private Fragment mParent;
private AudioSharingJoinDialogFragment mFragment; private AudioSharingJoinDialogFragment mFragment;
private ShadowBluetoothAdapter mShadowBluetoothAdapter; private ShadowBluetoothAdapter mShadowBluetoothAdapter;
@Before @Before
public void setUp() { public void setUp() {
AlertDialog latestAlertDialog = ShadowAlertDialogCompat.getLatestAlertDialog();
if (latestAlertDialog != null) {
latestAlertDialog.dismiss();
ShadowAlertDialogCompat.reset(); ShadowAlertDialogCompat.reset();
}
mShadowBluetoothAdapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter()); mShadowBluetoothAdapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter());
mShadowBluetoothAdapter.setEnabled(true); mShadowBluetoothAdapter.setEnabled(true);
mShadowBluetoothAdapter.setIsLeAudioBroadcastSourceSupported( mShadowBluetoothAdapter.setIsLeAudioBroadcastSourceSupported(
BluetoothStatusCodes.FEATURE_SUPPORTED); BluetoothStatusCodes.FEATURE_SUPPORTED);
mShadowBluetoothAdapter.setIsLeAudioBroadcastAssistantSupported( mShadowBluetoothAdapter.setIsLeAudioBroadcastAssistantSupported(
BluetoothStatusCodes.FEATURE_SUPPORTED); BluetoothStatusCodes.FEATURE_SUPPORTED);
when(mCachedDevice1.getName()).thenReturn(TEST_DEVICE_NAME1);
when(mCachedDevice2.getName()).thenReturn(TEST_DEVICE_NAME2);
mFragment = new AudioSharingJoinDialogFragment(); mFragment = new AudioSharingJoinDialogFragment();
mParent = new Fragment(); mParent = new Fragment();
FragmentController.setupFragment( FragmentController.setupFragment(
@@ -92,7 +104,7 @@ public class AudioSharingJoinDialogFragmentTest {
@Test @Test
@RequiresFlagsDisabled(Flags.FLAG_ENABLE_LE_AUDIO_SHARING) @RequiresFlagsDisabled(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
public void onCreateDialog_flagOff_dialogNotExist() { public void onCreateDialog_flagOff_dialogNotExist() {
mFragment.show(mParent, new ArrayList<>(), TEST_DEVICE_NAME2, () -> {}); mFragment.show(mParent, new ArrayList<>(), mCachedDevice2, () -> {});
shadowMainLooper().idle(); shadowMainLooper().idle();
AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog(); AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog();
assertThat(dialog).isNull(); assertThat(dialog).isNull();
@@ -101,37 +113,58 @@ public class AudioSharingJoinDialogFragmentTest {
@Test @Test
@RequiresFlagsEnabled(Flags.FLAG_ENABLE_LE_AUDIO_SHARING) @RequiresFlagsEnabled(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
public void onCreateDialog_flagOn_dialogShowTextForSingleDevice() { public void onCreateDialog_flagOn_dialogShowTextForSingleDevice() {
mFragment.show(mParent, new ArrayList<>(), TEST_DEVICE_NAME2, () -> {}); mFragment.show(mParent, new ArrayList<>(), mCachedDevice2, () -> {});
shadowMainLooper().idle(); shadowMainLooper().idle();
AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog(); AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog();
assertThat(dialog).isNotNull(); assertThat(dialog).isNotNull();
assertThat(dialog.isShowing()).isTrue(); assertThat(dialog.isShowing()).isTrue();
ShadowAlertDialogCompat shadowDialog = ShadowAlertDialogCompat.shadowOf(dialog); ShadowAlertDialogCompat shadowDialog = ShadowAlertDialogCompat.shadowOf(dialog);
View rootView = shadowDialog.getView(); assertThat(shadowDialog.getMessage().toString()).isEqualTo(TEST_DEVICE_NAME2);
TextView subtitle1 = rootView.findViewById(R.id.share_audio_subtitle1);
assertThat(subtitle1.getText()).isEqualTo(TEST_DEVICE_NAME2);
} }
@Test @Test
@RequiresFlagsEnabled(Flags.FLAG_ENABLE_LE_AUDIO_SHARING) @RequiresFlagsEnabled(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
public void onCreateDialog_flagOn_dialogShowTextForTwoDevice() { public void onCreateDialog_flagOn_dialogShowTextForTwoDevice() {
ArrayList<AudioSharingDeviceItem> list = new ArrayList<>(); ArrayList<AudioSharingDeviceItem> list = new ArrayList<>();
list.add(TEST_DEVICE_ITEM); list.add(TEST_DEVICE_ITEM1);
mFragment.show(mParent, list, TEST_DEVICE_NAME2, () -> {}); mFragment.show(mParent, list, mCachedDevice2, () -> {});
shadowMainLooper().idle(); shadowMainLooper().idle();
AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog(); AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog();
assertThat(dialog).isNotNull(); assertThat(dialog).isNotNull();
assertThat(dialog.isShowing()).isTrue(); assertThat(dialog.isShowing()).isTrue();
ShadowAlertDialogCompat shadowDialog = ShadowAlertDialogCompat.shadowOf(dialog); ShadowAlertDialogCompat shadowDialog = ShadowAlertDialogCompat.shadowOf(dialog);
View rootView = shadowDialog.getView(); assertThat(shadowDialog.getMessage().toString())
TextView subtitle1 = rootView.findViewById(R.id.share_audio_subtitle1); .isEqualTo(TEST_DEVICE_NAME1 + " and " + TEST_DEVICE_NAME2);
assertThat(subtitle1.getText()).isEqualTo(TEST_DEVICE_NAME1 + " and " + TEST_DEVICE_NAME2); }
@Test
@RequiresFlagsEnabled(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
public void onCreateDialog_dialogIsShowing_updateDialog() {
ArrayList<AudioSharingDeviceItem> list = new ArrayList<>();
list.add(TEST_DEVICE_ITEM1);
mFragment.show(mParent, list, mCachedDevice2, () -> {});
shadowMainLooper().idle();
AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog();
assertThat(dialog).isNotNull();
assertThat(dialog.isShowing()).isTrue();
// Update the content
ArrayList<AudioSharingDeviceItem> list2 = new ArrayList<>();
list2.add(TEST_DEVICE_ITEM2);
mFragment.show(mParent, list2, mCachedDevice1, () -> {});
shadowMainLooper().idle();
dialog = ShadowAlertDialogCompat.getLatestAlertDialog();
assertThat(dialog).isNotNull();
assertThat(dialog.isShowing()).isTrue();
ShadowAlertDialogCompat shadowDialog = ShadowAlertDialogCompat.shadowOf(dialog);
assertThat(shadowDialog.getMessage().toString())
.isEqualTo(TEST_DEVICE_NAME2 + " and " + TEST_DEVICE_NAME1);
} }
@Test @Test
@RequiresFlagsEnabled(Flags.FLAG_ENABLE_LE_AUDIO_SHARING) @RequiresFlagsEnabled(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
public void onCreateDialog_clickCancel_dialogDismiss() { public void onCreateDialog_clickCancel_dialogDismiss() {
mFragment.show(mParent, new ArrayList<>(), TEST_DEVICE_NAME2, () -> {}); mFragment.show(mParent, new ArrayList<>(), mCachedDevice2, () -> {});
shadowMainLooper().idle(); shadowMainLooper().idle();
AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog(); AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog();
ShadowAlertDialogCompat shadowDialog = ShadowAlertDialogCompat.shadowOf(dialog); ShadowAlertDialogCompat shadowDialog = ShadowAlertDialogCompat.shadowOf(dialog);
@@ -145,7 +178,7 @@ public class AudioSharingJoinDialogFragmentTest {
public void onCreateDialog_clickShare_callbackTriggered() { public void onCreateDialog_clickShare_callbackTriggered() {
AtomicBoolean isShareBtnClicked = new AtomicBoolean(false); AtomicBoolean isShareBtnClicked = new AtomicBoolean(false);
mFragment.show( mFragment.show(
mParent, new ArrayList<>(), TEST_DEVICE_NAME2, () -> isShareBtnClicked.set(true)); mParent, new ArrayList<>(), mCachedDevice2, () -> isShareBtnClicked.set(true));
shadowMainLooper().idle(); shadowMainLooper().idle();
AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog(); AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog();
ShadowAlertDialogCompat shadowDialog = ShadowAlertDialogCompat.shadowOf(dialog); ShadowAlertDialogCompat shadowDialog = ShadowAlertDialogCompat.shadowOf(dialog);

View File

@@ -18,6 +18,7 @@ package com.android.settings.connecteddevice.audiosharing;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.when;
import static org.robolectric.shadows.ShadowLooper.shadowMainLooper; import static org.robolectric.shadows.ShadowLooper.shadowMainLooper;
import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothAdapter;
@@ -34,11 +35,13 @@ import androidx.fragment.app.FragmentActivity;
import com.android.settings.flags.Flags; import com.android.settings.flags.Flags;
import com.android.settings.testutils.shadow.ShadowAlertDialogCompat; import com.android.settings.testutils.shadow.ShadowAlertDialogCompat;
import com.android.settings.testutils.shadow.ShadowBluetoothAdapter; import com.android.settings.testutils.shadow.ShadowBluetoothAdapter;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit; import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule; import org.mockito.junit.MockitoRule;
import org.robolectric.RobolectricTestRunner; import org.robolectric.RobolectricTestRunner;
@@ -61,21 +64,30 @@ public class AudioSharingStopDialogFragmentTest {
@Rule @Rule
public final CheckFlagsRule mCheckFlagsRule = DeviceFlagsValueProvider.createCheckFlagsRule(); public final CheckFlagsRule mCheckFlagsRule = DeviceFlagsValueProvider.createCheckFlagsRule();
private static final String TEST_DEVICE_NAME = "test"; private static final String TEST_DEVICE_NAME1 = "test1";
private static final String TEST_DEVICE_NAME2 = "test2";
@Mock private CachedBluetoothDevice mCachedDevice1;
@Mock private CachedBluetoothDevice mCachedDevice2;
private Fragment mParent; private Fragment mParent;
private AudioSharingStopDialogFragment mFragment; private AudioSharingStopDialogFragment mFragment;
private ShadowBluetoothAdapter mShadowBluetoothAdapter; private ShadowBluetoothAdapter mShadowBluetoothAdapter;
@Before @Before
public void setUp() { public void setUp() {
AlertDialog latestAlertDialog = ShadowAlertDialogCompat.getLatestAlertDialog();
if (latestAlertDialog != null) {
latestAlertDialog.dismiss();
ShadowAlertDialogCompat.reset(); ShadowAlertDialogCompat.reset();
}
mShadowBluetoothAdapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter()); mShadowBluetoothAdapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter());
mShadowBluetoothAdapter.setEnabled(true); mShadowBluetoothAdapter.setEnabled(true);
mShadowBluetoothAdapter.setIsLeAudioBroadcastSourceSupported( mShadowBluetoothAdapter.setIsLeAudioBroadcastSourceSupported(
BluetoothStatusCodes.FEATURE_SUPPORTED); BluetoothStatusCodes.FEATURE_SUPPORTED);
mShadowBluetoothAdapter.setIsLeAudioBroadcastAssistantSupported( mShadowBluetoothAdapter.setIsLeAudioBroadcastAssistantSupported(
BluetoothStatusCodes.FEATURE_SUPPORTED); BluetoothStatusCodes.FEATURE_SUPPORTED);
when(mCachedDevice1.getName()).thenReturn(TEST_DEVICE_NAME1);
when(mCachedDevice2.getName()).thenReturn(TEST_DEVICE_NAME2);
mFragment = new AudioSharingStopDialogFragment(); mFragment = new AudioSharingStopDialogFragment();
mParent = new Fragment(); mParent = new Fragment();
FragmentController.setupFragment( FragmentController.setupFragment(
@@ -85,16 +97,38 @@ public class AudioSharingStopDialogFragmentTest {
@Test @Test
@RequiresFlagsDisabled(Flags.FLAG_ENABLE_LE_AUDIO_SHARING) @RequiresFlagsDisabled(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
public void onCreateDialog_flagOff_dialogNotExist() { public void onCreateDialog_flagOff_dialogNotExist() {
mFragment.show(mParent, TEST_DEVICE_NAME, () -> {}); mFragment.show(mParent, mCachedDevice1, () -> {});
shadowMainLooper().idle(); shadowMainLooper().idle();
AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog(); AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog();
assertThat(dialog).isNull(); assertThat(dialog).isNull();
} }
@Test
@RequiresFlagsEnabled(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
public void onCreateDialog_dialogIsShowing_updateDialog() {
String postMessage = " wants to connect, headphones in audio sharing will disconnect.";
mFragment.show(mParent, mCachedDevice1, () -> {});
shadowMainLooper().idle();
AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog();
assertThat(dialog).isNotNull();
assertThat(dialog.isShowing()).isTrue();
ShadowAlertDialogCompat shadowDialog = ShadowAlertDialogCompat.shadowOf(dialog);
assertThat(shadowDialog.getMessage().toString()).isEqualTo(TEST_DEVICE_NAME1 + postMessage);
// Update the content
mFragment.show(mParent, mCachedDevice2, () -> {});
shadowMainLooper().idle();
dialog = ShadowAlertDialogCompat.getLatestAlertDialog();
assertThat(dialog).isNotNull();
assertThat(dialog.isShowing()).isTrue();
shadowDialog = ShadowAlertDialogCompat.shadowOf(dialog);
assertThat(shadowDialog.getMessage().toString()).isEqualTo(TEST_DEVICE_NAME2 + postMessage);
}
@Test @Test
@RequiresFlagsEnabled(Flags.FLAG_ENABLE_LE_AUDIO_SHARING) @RequiresFlagsEnabled(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
public void onCreateDialog_clickCancel_dialogDismiss() { public void onCreateDialog_clickCancel_dialogDismiss() {
mFragment.show(mParent, TEST_DEVICE_NAME, () -> {}); mFragment.show(mParent, mCachedDevice1, () -> {});
shadowMainLooper().idle(); shadowMainLooper().idle();
AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog(); AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog();
dialog.findViewById(android.R.id.button2).performClick(); dialog.findViewById(android.R.id.button2).performClick();
@@ -106,7 +140,7 @@ public class AudioSharingStopDialogFragmentTest {
@RequiresFlagsEnabled(Flags.FLAG_ENABLE_LE_AUDIO_SHARING) @RequiresFlagsEnabled(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
public void onCreateDialog_clickShare_callbackTriggered() { public void onCreateDialog_clickShare_callbackTriggered() {
AtomicBoolean isStopBtnClicked = new AtomicBoolean(false); AtomicBoolean isStopBtnClicked = new AtomicBoolean(false);
mFragment.show(mParent, TEST_DEVICE_NAME, () -> isStopBtnClicked.set(true)); mFragment.show(mParent, mCachedDevice1, () -> isStopBtnClicked.set(true));
shadowMainLooper().idle(); shadowMainLooper().idle();
AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog(); AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog();
dialog.findViewById(android.R.id.button1).performClick(); dialog.findViewById(android.R.id.button1).performClick();