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 Bug: 144134209 Bug: 160489394 Change-Id: I780b9eee35802b19a5f0ab0a7d07bd3e081f5556
128 lines
4.4 KiB
Java
128 lines
4.4 KiB
Java
/*
|
|
* Copyright (C) 2020 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.slices;
|
|
|
|
import static com.android.settings.slices.CustomSliceRegistry.VOLUME_SLICES_URI;
|
|
|
|
import android.content.ContentProvider;
|
|
import android.content.Context;
|
|
import android.content.Intent;
|
|
import android.content.IntentFilter;
|
|
import android.media.AudioManager;
|
|
import android.net.Uri;
|
|
import android.util.ArrayMap;
|
|
import android.util.Log;
|
|
|
|
import androidx.annotation.VisibleForTesting;
|
|
|
|
import com.android.settingslib.SliceBroadcastRelay;
|
|
|
|
import java.util.Map;
|
|
|
|
/**
|
|
* This helper is to handle the broadcasts of volume slices
|
|
*/
|
|
public class VolumeSliceHelper {
|
|
|
|
private static final String TAG = "VolumeSliceHelper";
|
|
|
|
@VisibleForTesting
|
|
static Map<Uri, Integer> sRegisteredUri = new ArrayMap<>();
|
|
@VisibleForTesting
|
|
static IntentFilter sIntentFilter;
|
|
|
|
static void registerIntentToUri(Context context, IntentFilter intentFilter, Uri sliceUri,
|
|
int audioStream) {
|
|
Log.d(TAG, "Registering uri for broadcast relay: " + sliceUri);
|
|
synchronized (sRegisteredUri) {
|
|
if (sRegisteredUri.isEmpty()) {
|
|
SliceBroadcastRelay.registerReceiver(context, VOLUME_SLICES_URI,
|
|
VolumeSliceRelayReceiver.class, intentFilter);
|
|
sIntentFilter = intentFilter;
|
|
}
|
|
sRegisteredUri.put(sliceUri, audioStream);
|
|
}
|
|
}
|
|
|
|
static boolean unregisterUri(Context context, Uri sliceUri) {
|
|
if (!sRegisteredUri.containsKey(sliceUri)) {
|
|
return false;
|
|
}
|
|
|
|
Log.d(TAG, "Unregistering uri broadcast relay: " + sliceUri);
|
|
synchronized (sRegisteredUri) {
|
|
sRegisteredUri.remove(sliceUri);
|
|
if (sRegisteredUri.isEmpty()) {
|
|
sIntentFilter = null;
|
|
SliceBroadcastRelay.unregisterReceivers(context, VOLUME_SLICES_URI);
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
static void onReceive(Context context, Intent intent) {
|
|
final String action = intent.getAction();
|
|
if (sIntentFilter == null || action == null || !sIntentFilter.hasAction(action)) {
|
|
return;
|
|
}
|
|
|
|
final String uriString = intent.getStringExtra(SliceBroadcastRelay.EXTRA_URI);
|
|
if (uriString == null) {
|
|
return;
|
|
}
|
|
|
|
final Uri uri = Uri.parse(uriString);
|
|
if (!VOLUME_SLICES_URI.equals(ContentProvider.getUriWithoutUserId(uri))) {
|
|
Log.w(TAG, "Invalid uri: " + uriString);
|
|
return;
|
|
}
|
|
|
|
if (AudioManager.VOLUME_CHANGED_ACTION.equals(action)) {
|
|
handleVolumeChanged(context, intent);
|
|
} else if (AudioManager.STREAM_MUTE_CHANGED_ACTION.equals(action)
|
|
|| AudioManager.STREAM_DEVICES_CHANGED_ACTION.equals(action)) {
|
|
handleStreamChanged(context, intent);
|
|
} else {
|
|
notifyAllStreamsChanged(context);
|
|
}
|
|
}
|
|
|
|
private static void handleVolumeChanged(Context context, Intent intent) {
|
|
final int vol = intent.getIntExtra(AudioManager.EXTRA_VOLUME_STREAM_VALUE, -1);
|
|
final int prevVol = intent.getIntExtra(AudioManager.EXTRA_PREV_VOLUME_STREAM_VALUE, -1);
|
|
if (vol != prevVol) {
|
|
handleStreamChanged(context, intent);
|
|
}
|
|
}
|
|
|
|
private static void handleStreamChanged(Context context, Intent intent) {
|
|
final int inputType = intent.getIntExtra(AudioManager.EXTRA_VOLUME_STREAM_TYPE, -1);
|
|
for (Map.Entry<Uri, Integer> entry : sRegisteredUri.entrySet()) {
|
|
if (entry.getValue() == inputType) {
|
|
context.getContentResolver().notifyChange(entry.getKey(), null /* observer */);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
private static void notifyAllStreamsChanged(Context context) {
|
|
sRegisteredUri.forEach((uri, audioStream) -> {
|
|
context.getContentResolver().notifyChange(uri, null /* observer */);
|
|
});
|
|
}
|
|
}
|