Merge "[Audiosharing] Add more checks before show notif from receiver" into main

This commit is contained in:
Yiyi Shen
2025-02-25 23:53:12 -08:00
committed by Android (Google) Code Review
2 changed files with 238 additions and 52 deletions

View File

@@ -41,6 +41,7 @@ import android.app.Notification;
import android.app.NotificationManager;
import android.app.settings.SettingsEnums;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothCsipSetCoordinator;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothLeBroadcastMetadata;
import android.bluetooth.BluetoothLeBroadcastReceiveState;
@@ -306,7 +307,8 @@ public class AudioSharingReceiverTest {
}
@Test
@EnableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
@EnableFlags({Flags.FLAG_ENABLE_LE_AUDIO_SHARING,
Flags.FLAG_PROMOTE_AUDIO_SHARING_FOR_SECOND_AUTO_CONNECTED_LEA_DEVICE})
public void broadcastReceiver_receiveAudioSharingDeviceConnected_broadcastDisabled_doNothing() {
mShadowBluetoothAdapter.setIsLeAudioBroadcastSourceSupported(
BluetoothStatusCodes.ERROR_BLUETOOTH_NOT_ENABLED);
@@ -325,6 +327,38 @@ public class AudioSharingReceiverTest {
@Test
@EnableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
@DisableFlags(Flags.FLAG_PROMOTE_AUDIO_SHARING_FOR_SECOND_AUTO_CONNECTED_LEA_DEVICE)
public void broadcastReceiver_receiveAudioSharingDeviceConnected_flagOff_doNothing() {
setAppInForeground(false);
Intent intent = new Intent(ACTION_LE_AUDIO_SHARING_DEVICE_CONNECTED);
intent.setPackage(mContext.getPackageName());
intent.putExtra(EXTRA_BLUETOOTH_DEVICE, mDevice);
AudioSharingReceiver audioSharingReceiver = getAudioSharingReceiver(intent);
audioSharingReceiver.onReceive(mContext, intent);
verify(mNm, never()).notify(
eq(com.android.settings.R.string.share_audio_notification_title),
any(Notification.class));
}
@Test
@EnableFlags({Flags.FLAG_ENABLE_LE_AUDIO_SHARING,
Flags.FLAG_PROMOTE_AUDIO_SHARING_FOR_SECOND_AUTO_CONNECTED_LEA_DEVICE})
public void broadcastReceiver_receiveAudioSharingDeviceConnected_nullArg_doNothing() {
setAppInForeground(false);
Intent intent = new Intent(ACTION_LE_AUDIO_SHARING_DEVICE_CONNECTED);
intent.setPackage(mContext.getPackageName());
AudioSharingReceiver audioSharingReceiver = getAudioSharingReceiver(intent);
audioSharingReceiver.onReceive(mContext, intent);
verify(mNm, never()).notify(
eq(com.android.settings.R.string.share_audio_notification_title),
any(Notification.class));
}
@Test
@EnableFlags({Flags.FLAG_ENABLE_LE_AUDIO_SHARING,
Flags.FLAG_PROMOTE_AUDIO_SHARING_FOR_SECOND_AUTO_CONNECTED_LEA_DEVICE})
public void broadcastReceiver_receiveAudioSharingDeviceConnected_showDialog() {
setAppInForeground(true);
Intent intent = new Intent(ACTION_LE_AUDIO_SHARING_DEVICE_CONNECTED);
@@ -340,9 +374,139 @@ public class AudioSharingReceiverTest {
}
@Test
@EnableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
@EnableFlags({Flags.FLAG_ENABLE_LE_AUDIO_SHARING,
Flags.FLAG_PROMOTE_AUDIO_SHARING_FOR_SECOND_AUTO_CONNECTED_LEA_DEVICE})
public void broadcastReceiver_receiveAudioSharingDeviceConnected_notInBroadcast_noNotif() {
setAppInForeground(false);
when(mBroadcast.isEnabled(null)).thenReturn(false);
Intent intent = new Intent(ACTION_LE_AUDIO_SHARING_DEVICE_CONNECTED);
intent.setPackage(mContext.getPackageName());
intent.putExtra(EXTRA_BLUETOOTH_DEVICE, mDevice);
AudioSharingReceiver audioSharingReceiver = getAudioSharingReceiver(intent);
audioSharingReceiver.onReceive(mContext, intent);
verify(mNm, never()).notify(
eq(com.android.settings.R.string.share_audio_notification_title),
any(Notification.class));
}
@Test
@EnableFlags({Flags.FLAG_ENABLE_LE_AUDIO_SHARING,
Flags.FLAG_PROMOTE_AUDIO_SHARING_FOR_SECOND_AUTO_CONNECTED_LEA_DEVICE})
public void broadcastReceiver_receiveAudioSharingDeviceConnected_invalidGroupId_noNotif() {
setAppInForeground(false);
when(mBroadcast.isEnabled(null)).thenReturn(true);
CachedBluetoothDeviceManager deviceManager = mock(CachedBluetoothDeviceManager.class);
when(mLocalBluetoothManager.getCachedDeviceManager()).thenReturn(deviceManager);
CachedBluetoothDevice cachedDevice = mock(CachedBluetoothDevice.class);
when(deviceManager.findDevice(mDevice)).thenReturn(cachedDevice);
when(cachedDevice.getDevice()).thenReturn(mDevice);
when(cachedDevice.getGroupId()).thenReturn(BluetoothCsipSetCoordinator.GROUP_ID_INVALID);
when(mAssistant.getAllConnectedDevices()).thenReturn(ImmutableList.of(mDevice));
Intent intent = new Intent(ACTION_LE_AUDIO_SHARING_DEVICE_CONNECTED);
intent.setPackage(mContext.getPackageName());
intent.putExtra(EXTRA_BLUETOOTH_DEVICE, mDevice);
AudioSharingReceiver audioSharingReceiver = getAudioSharingReceiver(intent);
audioSharingReceiver.onReceive(mContext, intent);
verify(mNm, never()).notify(
eq(com.android.settings.R.string.share_audio_notification_title),
any(Notification.class));
}
@Test
@EnableFlags({Flags.FLAG_ENABLE_LE_AUDIO_SHARING, Flags.FLAG_AUDIO_SHARING_HYSTERESIS_MODE_FIX,
Flags.FLAG_PROMOTE_AUDIO_SHARING_FOR_SECOND_AUTO_CONNECTED_LEA_DEVICE})
public void broadcastReceiver_receiveAudioSharingDeviceConnected_alreadyTwoSinks_noNotif() {
setAppInForeground(false);
when(mBroadcast.isEnabled(null)).thenReturn(true);
when(mBroadcast.getLatestBroadcastId()).thenReturn(1);
CachedBluetoothDeviceManager deviceManager = mock(CachedBluetoothDeviceManager.class);
when(mLocalBluetoothManager.getCachedDeviceManager()).thenReturn(deviceManager);
CachedBluetoothDevice cachedDevice1 = mock(CachedBluetoothDevice.class);
when(deviceManager.findDevice(mDevice)).thenReturn(cachedDevice1);
BluetoothDevice device2 = mock(BluetoothDevice.class);
CachedBluetoothDevice cachedDevice2 = mock(CachedBluetoothDevice.class);
when(deviceManager.findDevice(device2)).thenReturn(cachedDevice2);
when(cachedDevice1.getGroupId()).thenReturn(1);
when(cachedDevice1.getDevice()).thenReturn(mDevice);
when(cachedDevice1.getName()).thenReturn(TEST_DEVICE_NAME);
when(cachedDevice2.getGroupId()).thenReturn(2);
when(cachedDevice2.getDevice()).thenReturn(device2);
when(cachedDevice2.getName()).thenReturn(TEST_DEVICE_NAME);
when(mAssistant.getAllConnectedDevices()).thenReturn(ImmutableList.of(mDevice, device2));
BluetoothLeBroadcastReceiveState state = mock(BluetoothLeBroadcastReceiveState.class);
when(state.getBroadcastId()).thenReturn(1);
when(mAssistant.getAllSources(any())).thenReturn(ImmutableList.of(state));
Intent intent = new Intent(ACTION_LE_AUDIO_SHARING_DEVICE_CONNECTED);
intent.setPackage(mContext.getPackageName());
intent.putExtra(EXTRA_BLUETOOTH_DEVICE, mDevice);
AudioSharingReceiver audioSharingReceiver = getAudioSharingReceiver(intent);
audioSharingReceiver.onReceive(mContext, intent);
verify(mNm, never()).notify(
eq(com.android.settings.R.string.share_audio_notification_title),
any(Notification.class));
}
@Test
@EnableFlags({Flags.FLAG_ENABLE_LE_AUDIO_SHARING, Flags.FLAG_AUDIO_SHARING_HYSTERESIS_MODE_FIX,
Flags.FLAG_PROMOTE_AUDIO_SHARING_FOR_SECOND_AUTO_CONNECTED_LEA_DEVICE})
public void
broadcastReceiver_receiveAudioSharingDeviceConnected_alreadyHasSource_cancelNotif() {
setAppInForeground(false);
when(mBroadcast.isEnabled(null)).thenReturn(true);
when(mBroadcast.getLatestBroadcastId()).thenReturn(1);
CachedBluetoothDeviceManager deviceManager = mock(CachedBluetoothDeviceManager.class);
when(mLocalBluetoothManager.getCachedDeviceManager()).thenReturn(deviceManager);
CachedBluetoothDevice cachedDevice = mock(CachedBluetoothDevice.class);
when(deviceManager.findDevice(mDevice)).thenReturn(cachedDevice);
when(cachedDevice.getGroupId()).thenReturn(1);
when(cachedDevice.getDevice()).thenReturn(mDevice);
when(mAssistant.getAllConnectedDevices()).thenReturn(ImmutableList.of(mDevice));
BluetoothLeBroadcastReceiveState state = mock(BluetoothLeBroadcastReceiveState.class);
when(state.getBroadcastId()).thenReturn(1);
when(mAssistant.getAllSources(mDevice)).thenReturn(ImmutableList.of(state));
Intent intent = new Intent(ACTION_LE_AUDIO_SHARING_DEVICE_CONNECTED);
intent.setPackage(mContext.getPackageName());
intent.putExtra(EXTRA_BLUETOOTH_DEVICE, mDevice);
AudioSharingReceiver audioSharingReceiver = getAudioSharingReceiver(intent);
audioSharingReceiver.onReceive(mContext, intent);
verify(mNm, never()).notify(
eq(com.android.settings.R.string.share_audio_notification_title),
any(Notification.class));
}
@Test
@EnableFlags({Flags.FLAG_ENABLE_LE_AUDIO_SHARING, Flags.FLAG_AUDIO_SHARING_HYSTERESIS_MODE_FIX,
Flags.FLAG_PROMOTE_AUDIO_SHARING_FOR_SECOND_AUTO_CONNECTED_LEA_DEVICE})
public void broadcastReceiver_receiveAudioSharingDeviceConnected_showNotification() {
setAppInForeground(false);
when(mBroadcast.isEnabled(null)).thenReturn(true);
when(mBroadcast.getLatestBroadcastId()).thenReturn(1);
BluetoothLeBroadcastMetadata metadata = mock(BluetoothLeBroadcastMetadata.class);
when(mBroadcast.getLatestBluetoothLeBroadcastMetadata()).thenReturn(metadata);
CachedBluetoothDeviceManager deviceManager = mock(CachedBluetoothDeviceManager.class);
when(mLocalBluetoothManager.getCachedDeviceManager()).thenReturn(deviceManager);
CachedBluetoothDevice cachedDevice1 = mock(CachedBluetoothDevice.class);
when(deviceManager.findDevice(mDevice)).thenReturn(cachedDevice1);
BluetoothDevice device2 = mock(BluetoothDevice.class);
CachedBluetoothDevice cachedDevice2 = mock(CachedBluetoothDevice.class);
when(deviceManager.findDevice(device2)).thenReturn(cachedDevice2);
when(cachedDevice1.getGroupId()).thenReturn(1);
when(cachedDevice1.getDevice()).thenReturn(mDevice);
when(cachedDevice2.getGroupId()).thenReturn(2);
when(cachedDevice2.getDevice()).thenReturn(device2);
when(mAssistant.getAllConnectedDevices()).thenReturn(ImmutableList.of(mDevice, device2));
BluetoothLeBroadcastReceiveState state = mock(BluetoothLeBroadcastReceiveState.class);
when(state.getBroadcastId()).thenReturn(1);
when(mAssistant.getAllSources(device2)).thenReturn(ImmutableList.of(state));
Intent intent = new Intent(ACTION_LE_AUDIO_SHARING_DEVICE_CONNECTED);
intent.setPackage(mContext.getPackageName());
intent.putExtra(EXTRA_BLUETOOTH_DEVICE, mDevice);
@@ -355,7 +519,8 @@ public class AudioSharingReceiverTest {
}
@Test
@EnableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
@EnableFlags({Flags.FLAG_ENABLE_LE_AUDIO_SHARING,
Flags.FLAG_PROMOTE_AUDIO_SHARING_FOR_SECOND_AUTO_CONNECTED_LEA_DEVICE})
public void broadcastReceiver_receiveAudioSharingAddSource_broadcastDisabled_cancelNotif() {
mShadowBluetoothAdapter.setIsLeAudioBroadcastSourceSupported(
BluetoothStatusCodes.ERROR_BLUETOOTH_NOT_ENABLED);
@@ -372,7 +537,8 @@ public class AudioSharingReceiverTest {
}
@Test
@EnableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
@EnableFlags({Flags.FLAG_ENABLE_LE_AUDIO_SHARING,
Flags.FLAG_PROMOTE_AUDIO_SHARING_FOR_SECOND_AUTO_CONNECTED_LEA_DEVICE})
public void broadcastReceiver_receiveAudioSharingAddSource_nullArg_cancelNotif() {
Intent intent = new Intent(ACTION_LE_AUDIO_SHARING_ADD_SOURCE);
intent.setPackage(mContext.getPackageName());
@@ -385,7 +551,8 @@ public class AudioSharingReceiverTest {
}
@Test
@EnableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
@EnableFlags({Flags.FLAG_ENABLE_LE_AUDIO_SHARING,
Flags.FLAG_PROMOTE_AUDIO_SHARING_FOR_SECOND_AUTO_CONNECTED_LEA_DEVICE})
public void broadcastReceiver_receiveAudioSharingAddSource_notInBroadcast_cancelNotif() {
when(mBroadcast.isEnabled(null)).thenReturn(false);
@@ -401,7 +568,8 @@ public class AudioSharingReceiverTest {
}
@Test
@EnableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
@EnableFlags({Flags.FLAG_ENABLE_LE_AUDIO_SHARING,
Flags.FLAG_PROMOTE_AUDIO_SHARING_FOR_SECOND_AUTO_CONNECTED_LEA_DEVICE})
public void broadcastReceiver_receiveAudioSharingAddSource_notConnected_cancelNotif() {
when(mBroadcast.isEnabled(null)).thenReturn(true);
when(mAssistant.getAllConnectedDevices()).thenReturn(ImmutableList.of());
@@ -418,7 +586,8 @@ public class AudioSharingReceiverTest {
}
@Test
@EnableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
@EnableFlags({Flags.FLAG_ENABLE_LE_AUDIO_SHARING,
Flags.FLAG_PROMOTE_AUDIO_SHARING_FOR_SECOND_AUTO_CONNECTED_LEA_DEVICE})
public void broadcastReceiver_receiveAudioSharingAddSource_invalidGroupId_cancelNotif() {
when(mBroadcast.isEnabled(null)).thenReturn(true);
CachedBluetoothDeviceManager deviceManager = mock(CachedBluetoothDeviceManager.class);
@@ -426,6 +595,7 @@ public class AudioSharingReceiverTest {
CachedBluetoothDevice cachedDevice = mock(CachedBluetoothDevice.class);
when(deviceManager.findDevice(mDevice)).thenReturn(cachedDevice);
when(cachedDevice.getDevice()).thenReturn(mDevice);
when(cachedDevice.getGroupId()).thenReturn(BluetoothCsipSetCoordinator.GROUP_ID_INVALID);
when(mAssistant.getAllConnectedDevices()).thenReturn(ImmutableList.of(mDevice));
Intent intent = new Intent(ACTION_LE_AUDIO_SHARING_ADD_SOURCE);
@@ -440,7 +610,8 @@ public class AudioSharingReceiverTest {
}
@Test
@EnableFlags({Flags.FLAG_ENABLE_LE_AUDIO_SHARING, Flags.FLAG_AUDIO_SHARING_HYSTERESIS_MODE_FIX})
@EnableFlags({Flags.FLAG_ENABLE_LE_AUDIO_SHARING, Flags.FLAG_AUDIO_SHARING_HYSTERESIS_MODE_FIX,
Flags.FLAG_PROMOTE_AUDIO_SHARING_FOR_SECOND_AUTO_CONNECTED_LEA_DEVICE})
public void broadcastReceiver_receiveAudioSharingAddSource_alreadyTwoSinks_cancelNotif() {
when(mBroadcast.isEnabled(null)).thenReturn(true);
when(mBroadcast.getLatestBroadcastId()).thenReturn(1);
@@ -474,7 +645,8 @@ public class AudioSharingReceiverTest {
}
@Test
@EnableFlags({Flags.FLAG_ENABLE_LE_AUDIO_SHARING, Flags.FLAG_AUDIO_SHARING_HYSTERESIS_MODE_FIX})
@EnableFlags({Flags.FLAG_ENABLE_LE_AUDIO_SHARING, Flags.FLAG_AUDIO_SHARING_HYSTERESIS_MODE_FIX,
Flags.FLAG_PROMOTE_AUDIO_SHARING_FOR_SECOND_AUTO_CONNECTED_LEA_DEVICE})
public void broadcastReceiver_receiveAudioSharingAddSource_alreadyHasSource_cancelNotif() {
when(mBroadcast.isEnabled(null)).thenReturn(true);
when(mBroadcast.getLatestBroadcastId()).thenReturn(1);
@@ -501,7 +673,8 @@ public class AudioSharingReceiverTest {
}
@Test
@EnableFlags({Flags.FLAG_ENABLE_LE_AUDIO_SHARING, Flags.FLAG_AUDIO_SHARING_HYSTERESIS_MODE_FIX})
@EnableFlags({Flags.FLAG_ENABLE_LE_AUDIO_SHARING, Flags.FLAG_AUDIO_SHARING_HYSTERESIS_MODE_FIX,
Flags.FLAG_PROMOTE_AUDIO_SHARING_FOR_SECOND_AUTO_CONNECTED_LEA_DEVICE})
public void broadcastReceiver_receiveAudioSharingAddSource_addSource() {
when(mBroadcast.isEnabled(null)).thenReturn(true);
when(mBroadcast.getLatestBroadcastId()).thenReturn(1);