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:
Yiyi Shen
2024-12-12 15:25:34 +08:00
parent 0ca6d53458
commit 17018dd7e1
4 changed files with 76 additions and 59 deletions

View File

@@ -39,26 +39,29 @@ public class ConnectedBluetoothDeviceUpdater extends BluetoothDeviceUpdater {
private static final String PREF_KEY_PREFIX = "connected_bt_";
private final AudioManager mAudioManager;
private int mAudioMode;
public ConnectedBluetoothDeviceUpdater(Context context,
DevicePreferenceCallback devicePreferenceCallback, int metricsCategory) {
super(context, devicePreferenceCallback, metricsCategory);
mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
mAudioMode = mAudioManager.getMode();
}
@Override
public void onAudioModeChanged() {
// TODO: move to background thread
mAudioMode = mAudioManager.getMode();
forceUpdate();
}
@Override
public boolean isFilterMatched(CachedBluetoothDevice cachedDevice) {
final int audioMode = mAudioManager.getMode();
final int currentAudioProfile;
if (audioMode == AudioManager.MODE_RINGTONE
|| audioMode == AudioManager.MODE_IN_CALL
|| audioMode == AudioManager.MODE_IN_COMMUNICATION) {
if (mAudioMode == AudioManager.MODE_RINGTONE
|| mAudioMode == AudioManager.MODE_IN_CALL
|| mAudioMode == AudioManager.MODE_IN_COMMUNICATION) {
// in phone call
currentAudioProfile = BluetoothProfile.HEADSET;
} else {