Files
app_Settings/src/com/android/settings/notification/VolumeSeekBarPreferenceController.java
Jason Chiu f447cbbcb5 Fix the ANR in panel when changing volume continuously
When users open volume panel and keep on changing the volume slider for
a while, the panel starts to defer the slider updating, and finally gets
stuck and causes an ANR.

Root cause:
Volume panel has four volume adjusting slices. Each of them registers
a broadcast receiver to listen to the volume changed and muted events.
However, when the media volume changes, AudioManager will send four
broadcasts (music, assistant, accessibility, tts) to every receiver, and
each of them will reload slice four times. Thus, one media volume
changed event will lead to 16 (4*4) UI updates. Consequently, keeping on
sliding the volume bar will trigger hundreds of broadcasts and UI
updates, which makes the system busy and getting stuck.

Solution:
Introduce a VolumeSliceHelper to integrate the broadcasts of the volume
slices specifically.
1. Only register one broadcast receiver to reduce the broadcast loading
   since the four slices are listening to the same signal.
2. Filter the only one eligible broadcast among the multiple concurrent
   ones, and then relay it to the registered slice.
3. Listen to one more action STREAM_DEVICES_CHANGED_ACTION to update the
   volume panel when audio output device changes.

Test: robotest, visual
Fixes: 144134209
Fixes: 160489394
Change-Id: I780b9eee35802b19a5f0ab0a7d07bd3e081f5556
Merged-In: I780b9eee35802b19a5f0ab0a7d07bd3e081f5556
(cherry picked from commit 2c7b77dad7)
2020-09-22 02:58:01 +00:00

118 lines
3.4 KiB
Java

/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.notification;
import android.content.Context;
import androidx.annotation.VisibleForTesting;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;
import androidx.preference.PreferenceScreen;
import com.android.settings.notification.VolumeSeekBarPreference.Callback;
import com.android.settingslib.core.lifecycle.Lifecycle;
/**
* Base class for preference controller that handles VolumeSeekBarPreference
*/
public abstract class VolumeSeekBarPreferenceController extends
AdjustVolumeRestrictedPreferenceController implements LifecycleObserver {
protected VolumeSeekBarPreference mPreference;
protected VolumeSeekBarPreference.Callback mVolumePreferenceCallback;
protected AudioHelper mHelper;
public VolumeSeekBarPreferenceController(Context context, String key) {
super(context, key);
setAudioHelper(new AudioHelper(context));
}
@VisibleForTesting
void setAudioHelper(AudioHelper helper) {
mHelper = helper;
}
public void setCallback(Callback callback) {
mVolumePreferenceCallback = callback;
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
if (isAvailable()) {
mPreference = screen.findPreference(getPreferenceKey());
mPreference.setCallback(mVolumePreferenceCallback);
mPreference.setStream(getAudioStream());
mPreference.setMuteIcon(getMuteIcon());
}
}
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
public void onResume() {
if (mPreference != null) {
mPreference.onActivityResume();
}
}
@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
public void onPause() {
if (mPreference != null) {
mPreference.onActivityPause();
}
}
@Override
public int getSliderPosition() {
if (mPreference != null) {
return mPreference.getProgress();
}
return mHelper.getStreamVolume(getAudioStream());
}
@Override
public boolean setSliderPosition(int position) {
if (mPreference != null) {
mPreference.setProgress(position);
}
return mHelper.setStreamVolume(getAudioStream(), position);
}
@Override
public int getMax() {
if (mPreference != null) {
return mPreference.getMax();
}
return mHelper.getMaxVolume(getAudioStream());
}
@Override
public int getMin() {
if (mPreference != null) {
return mPreference.getMin();
}
return mHelper.getMinVolume(getAudioStream());
}
/**
* @return the audio stream type
*/
public abstract int getAudioStream();
protected abstract int getMuteIcon();
}