Fix SysUI volume controls not appearing
SysUI listens to volume change events to trigger showing its volume control overlay UI. Normally when a hardware volume key is pressed, a volume (either locally or for a session corresponding to a remote playback) is changed, SysUI receives an event and the UI is displayed. In the case that a remote session doesn't allow volume adjustment, the key event is still routed to the session if that session is top of the stack. However, since the volume is not adjusted and an event is not dispatched, the SysUI volume control overlay doesn't appear. To the user it seems like the volume hardware key is broken, neither adjusting a volume or displaying the volume control overlay UI. This change fires volume change events for sessions that don't support actually changing the volume, to ensure that the SysUI is still displayed. It also seems more consistent, since there are other existing cases where the volume doesn't actually change for which an event is still fired (e.g., adjusting up when already at max volume). An alternative would be to change MediaSessionRecord's canHandleVolumeKey to return false for sessions that don't support volume adjustment, which would mean the key event would be handled by the next session in the stack (or, in most cases, to make a local volume adjustment). This is likely more confusing, since the user will generally expect the top session to be adjusted (surfacing that it could not is less confusing than just adjusting some other volume instead). Bug: 197586676 Bug: 202500642 Test: manual Change-Id: Ic44b48bb5605699b476af05407b90f2be5d70cd3
This commit is contained in:
@@ -291,15 +291,14 @@ public class MediaSessionRecord implements IBinder.DeathRecipient, MediaSessionR
|
|||||||
asSystemService, useSuggested, previousFlagPlaySound);
|
asSystemService, useSuggested, previousFlagPlaySound);
|
||||||
} else {
|
} else {
|
||||||
if (mVolumeControlType == VolumeProvider.VOLUME_CONTROL_FIXED) {
|
if (mVolumeControlType == VolumeProvider.VOLUME_CONTROL_FIXED) {
|
||||||
// Nothing to do, the volume cannot be changed
|
if (DEBUG) {
|
||||||
return;
|
Log.d(TAG, "Session does not support volume adjustment");
|
||||||
}
|
}
|
||||||
if (direction == AudioManager.ADJUST_TOGGLE_MUTE
|
} else if (direction == AudioManager.ADJUST_TOGGLE_MUTE
|
||||||
|| direction == AudioManager.ADJUST_MUTE
|
|| direction == AudioManager.ADJUST_MUTE
|
||||||
|| direction == AudioManager.ADJUST_UNMUTE) {
|
|| direction == AudioManager.ADJUST_UNMUTE) {
|
||||||
Log.w(TAG, "Muting remote playback is not supported");
|
Log.w(TAG, "Muting remote playback is not supported");
|
||||||
return;
|
} else {
|
||||||
}
|
|
||||||
if (DEBUG) {
|
if (DEBUG) {
|
||||||
Log.w(TAG, "adjusting volume, pkg=" + packageName + ", asSystemService="
|
Log.w(TAG, "adjusting volume, pkg=" + packageName + ", asSystemService="
|
||||||
+ asSystemService + ", dir=" + direction);
|
+ asSystemService + ", dir=" + direction);
|
||||||
@@ -314,13 +313,18 @@ public class MediaSessionRecord implements IBinder.DeathRecipient, MediaSessionR
|
|||||||
if (volumeBefore != mOptimisticVolume) {
|
if (volumeBefore != mOptimisticVolume) {
|
||||||
pushVolumeUpdate();
|
pushVolumeUpdate();
|
||||||
}
|
}
|
||||||
mService.notifyRemoteVolumeChanged(flags, this);
|
|
||||||
|
|
||||||
if (DEBUG) {
|
if (DEBUG) {
|
||||||
Log.d(TAG, "Adjusted optimistic volume to " + mOptimisticVolume + " max is "
|
Log.d(TAG, "Adjusted optimistic volume to " + mOptimisticVolume + " max is "
|
||||||
+ mMaxVolume);
|
+ mMaxVolume);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// Always notify, even if the volume hasn't changed. This is important to ensure that
|
||||||
|
// System UI receives an event if a hardware volume key is pressed but the session that
|
||||||
|
// handles it does not allow volume adjustment. Without such an event, System UI would
|
||||||
|
// not show volume controls to the user.
|
||||||
|
mService.notifyRemoteVolumeChanged(flags, this);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setVolumeTo(String packageName, String opPackageName, int pid, int uid, int value,
|
private void setVolumeTo(String packageName, String opPackageName, int pid, int uid, int value,
|
||||||
@@ -343,9 +347,10 @@ public class MediaSessionRecord implements IBinder.DeathRecipient, MediaSessionR
|
|||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
if (mVolumeControlType != VolumeProvider.VOLUME_CONTROL_ABSOLUTE) {
|
if (mVolumeControlType != VolumeProvider.VOLUME_CONTROL_ABSOLUTE) {
|
||||||
// Nothing to do. The volume can't be set directly.
|
if (DEBUG) {
|
||||||
return;
|
Log.d(TAG, "Session does not support setting volume");
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
value = Math.max(0, Math.min(value, mMaxVolume));
|
value = Math.max(0, Math.min(value, mMaxVolume));
|
||||||
mSessionCb.setVolumeTo(packageName, pid, uid, value);
|
mSessionCb.setVolumeTo(packageName, pid, uid, value);
|
||||||
|
|
||||||
@@ -356,13 +361,15 @@ public class MediaSessionRecord implements IBinder.DeathRecipient, MediaSessionR
|
|||||||
if (volumeBefore != mOptimisticVolume) {
|
if (volumeBefore != mOptimisticVolume) {
|
||||||
pushVolumeUpdate();
|
pushVolumeUpdate();
|
||||||
}
|
}
|
||||||
mService.notifyRemoteVolumeChanged(flags, this);
|
|
||||||
|
|
||||||
if (DEBUG) {
|
if (DEBUG) {
|
||||||
Log.d(TAG, "Set optimistic volume to " + mOptimisticVolume + " max is "
|
Log.d(TAG, "Set optimistic volume to " + mOptimisticVolume + " max is "
|
||||||
+ mMaxVolume);
|
+ mMaxVolume);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// Always notify, even if the volume hasn't changed.
|
||||||
|
mService.notifyRemoteVolumeChanged(flags, this);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user