Avoid AudioManager#getMode in isFilterMatched
AudioManager#getMode is a slow binder call which should not be called on UI thread. isFilterMatched will be frequently triggered on UI thread when updating the Connected devices page. Cache and update the audio mode when receive onModeChanged callback in this change. For long term, we should better separate the UI/background thread tasks in those classes. Also send request to Audio team to improve the API latency. Flag: EXEMPT small fix Bug: 380993178 Test: atest Change-Id: I054f3fa62f0fdf03b9a436a532ac1fb4738aaf58
This commit is contained in:
@@ -124,24 +124,17 @@ public class AvailableMediaBluetoothDeviceUpdaterTest {
|
||||
when(mCachedBluetoothDevice.getDrawableWithDescription()).thenReturn(pairs);
|
||||
when(mCachedBluetoothDevice.getMemberDevice()).thenReturn(ImmutableSet.of());
|
||||
|
||||
mBluetoothDeviceUpdater =
|
||||
spy(
|
||||
new AvailableMediaBluetoothDeviceUpdater(
|
||||
mContext, mDevicePreferenceCallback, /* metricsCategory= */ 0));
|
||||
mBluetoothDeviceUpdater.setPrefContext(mContext);
|
||||
mPreference =
|
||||
new BluetoothDevicePreference(
|
||||
mContext,
|
||||
mCachedBluetoothDevice,
|
||||
false,
|
||||
BluetoothDevicePreference.SortType.TYPE_DEFAULT);
|
||||
doNothing().when(mBluetoothDeviceUpdater).addPreference(any());
|
||||
doNothing().when(mBluetoothDeviceUpdater).removePreference(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onAudioModeChanged_hfpDeviceConnected_inCall_addPreference() {
|
||||
mAudioManager.setMode(AudioManager.MODE_IN_CALL);
|
||||
setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_IN_CALL);
|
||||
when(mBluetoothDeviceUpdater.isDeviceConnected(any(CachedBluetoothDevice.class)))
|
||||
.thenReturn(true);
|
||||
when(mCachedBluetoothDevice.isConnectedHfpDevice()).thenReturn(true);
|
||||
@@ -153,7 +146,7 @@ public class AvailableMediaBluetoothDeviceUpdaterTest {
|
||||
|
||||
@Test
|
||||
public void onAudioModeChanged_hfpDeviceConnected_notInCall_removePreference() {
|
||||
mAudioManager.setMode(AudioManager.MODE_NORMAL);
|
||||
setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_NORMAL);
|
||||
when(mBluetoothDeviceUpdater.isDeviceConnected(any(CachedBluetoothDevice.class)))
|
||||
.thenReturn(true);
|
||||
when(mCachedBluetoothDevice.isConnectedHfpDevice()).thenReturn(true);
|
||||
@@ -165,7 +158,7 @@ public class AvailableMediaBluetoothDeviceUpdaterTest {
|
||||
|
||||
@Test
|
||||
public void onAudioModeChanged_a2dpDeviceConnected_inCall_removePreference() {
|
||||
mAudioManager.setMode(AudioManager.MODE_IN_CALL);
|
||||
setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_IN_CALL);
|
||||
when(mBluetoothDeviceUpdater.isDeviceConnected(any(CachedBluetoothDevice.class)))
|
||||
.thenReturn(true);
|
||||
when(mCachedBluetoothDevice.isConnectedA2dpDevice()).thenReturn(true);
|
||||
@@ -177,7 +170,7 @@ public class AvailableMediaBluetoothDeviceUpdaterTest {
|
||||
|
||||
@Test
|
||||
public void onAudioModeChanged_a2dpDeviceConnected_notInCall_addPreference() {
|
||||
mAudioManager.setMode(AudioManager.MODE_NORMAL);
|
||||
setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_NORMAL);
|
||||
when(mBluetoothDeviceUpdater.isDeviceConnected(any(CachedBluetoothDevice.class)))
|
||||
.thenReturn(true);
|
||||
when(mCachedBluetoothDevice.isConnectedA2dpDevice()).thenReturn(true);
|
||||
@@ -189,7 +182,7 @@ public class AvailableMediaBluetoothDeviceUpdaterTest {
|
||||
|
||||
@Test
|
||||
public void onProfileConnectionStateChanged_a2dpDeviceConnected_notInCall_addPreference() {
|
||||
mAudioManager.setMode(AudioManager.MODE_NORMAL);
|
||||
setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_NORMAL);
|
||||
when(mBluetoothDeviceUpdater.isDeviceConnected(any(CachedBluetoothDevice.class)))
|
||||
.thenReturn(true);
|
||||
when(mCachedBluetoothDevice.isConnectedA2dpDevice()).thenReturn(true);
|
||||
@@ -202,7 +195,7 @@ public class AvailableMediaBluetoothDeviceUpdaterTest {
|
||||
|
||||
@Test
|
||||
public void onProfileConnectionStateChanged_a2dpDeviceConnected_inCall_removePreference() {
|
||||
mAudioManager.setMode(AudioManager.MODE_IN_CALL);
|
||||
setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_IN_CALL);
|
||||
when(mBluetoothDeviceUpdater.isDeviceConnected(any(CachedBluetoothDevice.class)))
|
||||
.thenReturn(true);
|
||||
when(mCachedBluetoothDevice.isConnectedA2dpDevice()).thenReturn(true);
|
||||
@@ -215,7 +208,7 @@ public class AvailableMediaBluetoothDeviceUpdaterTest {
|
||||
|
||||
@Test
|
||||
public void onProfileConnectionStateChanged_hfpDeviceConnected_notInCall_removePreference() {
|
||||
mAudioManager.setMode(AudioManager.MODE_NORMAL);
|
||||
setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_NORMAL);
|
||||
when(mBluetoothDeviceUpdater.isDeviceConnected(any(CachedBluetoothDevice.class)))
|
||||
.thenReturn(true);
|
||||
when(mCachedBluetoothDevice.isConnectedHfpDevice()).thenReturn(true);
|
||||
@@ -228,7 +221,7 @@ public class AvailableMediaBluetoothDeviceUpdaterTest {
|
||||
|
||||
@Test
|
||||
public void onProfileConnectionStateChanged_hfpDeviceConnected_inCall_addPreference() {
|
||||
mAudioManager.setMode(AudioManager.MODE_IN_CALL);
|
||||
setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_IN_CALL);
|
||||
when(mBluetoothDeviceUpdater.isDeviceConnected(any(CachedBluetoothDevice.class)))
|
||||
.thenReturn(true);
|
||||
when(mCachedBluetoothDevice.isConnectedHfpDevice()).thenReturn(true);
|
||||
@@ -241,7 +234,7 @@ public class AvailableMediaBluetoothDeviceUpdaterTest {
|
||||
|
||||
@Test
|
||||
public void onProfileConnectionStateChanged_ashaHearingAidConnected_notInCall_addPreference() {
|
||||
mAudioManager.setMode(AudioManager.MODE_NORMAL);
|
||||
setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_NORMAL);
|
||||
when(mBluetoothDeviceUpdater.isDeviceConnected(any(CachedBluetoothDevice.class)))
|
||||
.thenReturn(true);
|
||||
when(mCachedBluetoothDevice.isConnectedAshaHearingAidDevice()).thenReturn(true);
|
||||
@@ -256,7 +249,7 @@ public class AvailableMediaBluetoothDeviceUpdaterTest {
|
||||
|
||||
@Test
|
||||
public void onProfileConnectionStateChanged_ashaHearingAidConnected_inCall_addPreference() {
|
||||
mAudioManager.setMode(AudioManager.MODE_IN_CALL);
|
||||
setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_IN_CALL);
|
||||
when(mBluetoothDeviceUpdater.isDeviceConnected(any(CachedBluetoothDevice.class)))
|
||||
.thenReturn(true);
|
||||
when(mCachedBluetoothDevice.isConnectedAshaHearingAidDevice()).thenReturn(true);
|
||||
@@ -272,7 +265,7 @@ public class AvailableMediaBluetoothDeviceUpdaterTest {
|
||||
@Test
|
||||
public void onProfileConnectionStateChanged_leaConnected_notInCallSharingFlagOff_addPref() {
|
||||
mSetFlagsRule.disableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING);
|
||||
mAudioManager.setMode(AudioManager.MODE_NORMAL);
|
||||
setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_NORMAL);
|
||||
when(mBluetoothDeviceUpdater.isDeviceConnected(any(CachedBluetoothDevice.class)))
|
||||
.thenReturn(true);
|
||||
when(mCachedBluetoothDevice.isConnectedLeAudioDevice()).thenReturn(true);
|
||||
@@ -292,7 +285,7 @@ public class AvailableMediaBluetoothDeviceUpdaterTest {
|
||||
@Test
|
||||
public void onProfileConnectionStateChanged_leaConnected_notInCallNotInSharing_addPref() {
|
||||
mSetFlagsRule.enableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING);
|
||||
mAudioManager.setMode(AudioManager.MODE_NORMAL);
|
||||
setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_NORMAL);
|
||||
when(mBluetoothDeviceUpdater.isDeviceConnected(any(CachedBluetoothDevice.class)))
|
||||
.thenReturn(true);
|
||||
when(mCachedBluetoothDevice.isConnectedLeAudioDevice()).thenReturn(true);
|
||||
@@ -309,7 +302,7 @@ public class AvailableMediaBluetoothDeviceUpdaterTest {
|
||||
@Test
|
||||
public void onProfileConnectionStateChanged_leaConnected_inCallSharingFlagOff_addPref() {
|
||||
mSetFlagsRule.disableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING);
|
||||
mAudioManager.setMode(AudioManager.MODE_IN_CALL);
|
||||
setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_IN_CALL);
|
||||
when(mBluetoothDeviceUpdater.isDeviceConnected(any(CachedBluetoothDevice.class)))
|
||||
.thenReturn(true);
|
||||
when(mCachedBluetoothDevice.isConnectedLeAudioDevice()).thenReturn(true);
|
||||
@@ -326,7 +319,7 @@ public class AvailableMediaBluetoothDeviceUpdaterTest {
|
||||
@Test
|
||||
public void onProfileConnectionStateChanged_leaConnected_inCallNotInSharing_addPref() {
|
||||
mSetFlagsRule.enableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING);
|
||||
mAudioManager.setMode(AudioManager.MODE_IN_CALL);
|
||||
setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_IN_CALL);
|
||||
when(mBluetoothDeviceUpdater.isDeviceConnected(any(CachedBluetoothDevice.class)))
|
||||
.thenReturn(true);
|
||||
when(mCachedBluetoothDevice.isConnectedLeAudioDevice()).thenReturn(true);
|
||||
@@ -344,7 +337,7 @@ public class AvailableMediaBluetoothDeviceUpdaterTest {
|
||||
public void onProfileConnectionStateChanged_leaConnected_notInCallInSharing_removePref() {
|
||||
mSetFlagsRule.enableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING);
|
||||
mSetFlagsRule.disableFlags(Flags.FLAG_AUDIO_SHARING_HYSTERESIS_MODE_FIX);
|
||||
mAudioManager.setMode(AudioManager.MODE_NORMAL);
|
||||
setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_NORMAL);
|
||||
when(mBluetoothDeviceUpdater.isDeviceConnected(any(CachedBluetoothDevice.class)))
|
||||
.thenReturn(true);
|
||||
when(mCachedBluetoothDevice.isConnectedLeAudioDevice()).thenReturn(true);
|
||||
@@ -367,7 +360,7 @@ public class AvailableMediaBluetoothDeviceUpdaterTest {
|
||||
onProfileConnectionStateChanged_leaConnected_noInCallInSharing_hysteresis_removePref() {
|
||||
mSetFlagsRule.enableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING);
|
||||
mSetFlagsRule.enableFlags(Flags.FLAG_AUDIO_SHARING_HYSTERESIS_MODE_FIX);
|
||||
mAudioManager.setMode(AudioManager.MODE_NORMAL);
|
||||
setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_NORMAL);
|
||||
when(mBluetoothDeviceUpdater.isDeviceConnected(any(CachedBluetoothDevice.class)))
|
||||
.thenReturn(true);
|
||||
when(mCachedBluetoothDevice.isConnectedLeAudioDevice()).thenReturn(true);
|
||||
@@ -388,7 +381,7 @@ public class AvailableMediaBluetoothDeviceUpdaterTest {
|
||||
public void onProfileConnectionStateChanged_leaConnected_inCallSharing_removePref() {
|
||||
mSetFlagsRule.enableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING);
|
||||
mSetFlagsRule.disableFlags(Flags.FLAG_AUDIO_SHARING_HYSTERESIS_MODE_FIX);
|
||||
mAudioManager.setMode(AudioManager.MODE_NORMAL);
|
||||
setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_NORMAL);
|
||||
when(mBluetoothDeviceUpdater.isDeviceConnected(any(CachedBluetoothDevice.class)))
|
||||
.thenReturn(true);
|
||||
when(mCachedBluetoothDevice.isConnectedLeAudioDevice()).thenReturn(true);
|
||||
@@ -410,7 +403,7 @@ public class AvailableMediaBluetoothDeviceUpdaterTest {
|
||||
public void onProfileConnectionStateChanged_leaConnected_inCallSharing_hysteresis_removePref() {
|
||||
mSetFlagsRule.enableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING);
|
||||
mSetFlagsRule.enableFlags(Flags.FLAG_AUDIO_SHARING_HYSTERESIS_MODE_FIX);
|
||||
mAudioManager.setMode(AudioManager.MODE_NORMAL);
|
||||
setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_NORMAL);
|
||||
when(mBluetoothDeviceUpdater.isDeviceConnected(any(CachedBluetoothDevice.class)))
|
||||
.thenReturn(true);
|
||||
when(mCachedBluetoothDevice.isConnectedLeAudioDevice()).thenReturn(true);
|
||||
@@ -430,7 +423,7 @@ public class AvailableMediaBluetoothDeviceUpdaterTest {
|
||||
@Test
|
||||
public void
|
||||
onProfileConnectionStateChanged_deviceIsNotInList_notInCall_invokesRemovePreference() {
|
||||
mAudioManager.setMode(AudioManager.MODE_NORMAL);
|
||||
setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_NORMAL);
|
||||
when(mBluetoothDeviceUpdater.isDeviceConnected(any(CachedBluetoothDevice.class)))
|
||||
.thenReturn(true);
|
||||
when(mCachedBluetoothDevice.isConnectedLeAudioDevice()).thenReturn(true);
|
||||
@@ -446,7 +439,7 @@ public class AvailableMediaBluetoothDeviceUpdaterTest {
|
||||
|
||||
@Test
|
||||
public void onProfileConnectionStateChanged_deviceIsNotInList_inCall_invokesRemovePreference() {
|
||||
mAudioManager.setMode(AudioManager.MODE_IN_CALL);
|
||||
setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_IN_CALL);
|
||||
when(mBluetoothDeviceUpdater.isDeviceConnected(any(CachedBluetoothDevice.class)))
|
||||
.thenReturn(true);
|
||||
when(mCachedBluetoothDevice.isConnectedLeAudioDevice()).thenReturn(true);
|
||||
@@ -462,6 +455,7 @@ public class AvailableMediaBluetoothDeviceUpdaterTest {
|
||||
|
||||
@Test
|
||||
public void onProfileConnectionStateChanged_deviceDisconnected_removePreference() {
|
||||
setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_NORMAL);
|
||||
mBluetoothDeviceUpdater.onProfileConnectionStateChanged(
|
||||
mCachedBluetoothDevice, BluetoothProfile.STATE_DISCONNECTED, BluetoothProfile.A2DP);
|
||||
|
||||
@@ -470,8 +464,19 @@ public class AvailableMediaBluetoothDeviceUpdaterTest {
|
||||
|
||||
@Test
|
||||
public void onClick_Preference_setActive() {
|
||||
setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_NORMAL);
|
||||
mBluetoothDeviceUpdater.onPreferenceClick(mPreference);
|
||||
|
||||
verify(mDevicePreferenceCallback).onDeviceClick(mPreference);
|
||||
}
|
||||
|
||||
private void setUpDeviceUpdaterWithAudioMode(int audioMode) {
|
||||
mAudioManager.setMode(audioMode);
|
||||
mBluetoothDeviceUpdater =
|
||||
spy(new AvailableMediaBluetoothDeviceUpdater(
|
||||
mContext, mDevicePreferenceCallback, /* metricsCategory= */ 0));
|
||||
mBluetoothDeviceUpdater.setPrefContext(mContext);
|
||||
doNothing().when(mBluetoothDeviceUpdater).addPreference(any());
|
||||
doNothing().when(mBluetoothDeviceUpdater).removePreference(any());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user