Merge "AudioService: async rotation/fold update" into tm-d1-dev

This commit is contained in:
Jean-Michel Trivi
2022-06-29 22:34:28 +00:00
committed by Android (Google) Code Review
2 changed files with 52 additions and 9 deletions

View File

@@ -346,6 +346,8 @@ public class AudioService extends IAudioService.Stub
private static final int MSG_REMOVE_ASSISTANT_SERVICE_UID = 45; private static final int MSG_REMOVE_ASSISTANT_SERVICE_UID = 45;
private static final int MSG_UPDATE_ACTIVE_ASSISTANT_SERVICE_UID = 46; private static final int MSG_UPDATE_ACTIVE_ASSISTANT_SERVICE_UID = 46;
private static final int MSG_DISPATCH_DEVICE_VOLUME_BEHAVIOR = 47; private static final int MSG_DISPATCH_DEVICE_VOLUME_BEHAVIOR = 47;
private static final int MSG_ROTATION_UPDATE = 48;
private static final int MSG_FOLD_UPDATE = 49;
// start of messages handled under wakelock // start of messages handled under wakelock
// these messages can only be queued, i.e. sent with queueMsgUnderWakeLock(), // these messages can only be queued, i.e. sent with queueMsgUnderWakeLock(),
@@ -1214,7 +1216,9 @@ public class AudioService extends IAudioService.Stub
intentFilter.addAction(Intent.ACTION_CONFIGURATION_CHANGED); intentFilter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
if (mMonitorRotation) { if (mMonitorRotation) {
RotationHelper.init(mContext, mAudioHandler); RotationHelper.init(mContext, mAudioHandler,
rotationParam -> onRotationUpdate(rotationParam),
foldParam -> onFoldUpdate(foldParam));
} }
intentFilter.addAction(AudioEffect.ACTION_OPEN_AUDIO_EFFECT_CONTROL_SESSION); intentFilter.addAction(AudioEffect.ACTION_OPEN_AUDIO_EFFECT_CONTROL_SESSION);
@@ -1360,6 +1364,20 @@ public class AudioService extends IAudioService.Stub
checkMuteAwaitConnection(); checkMuteAwaitConnection();
} }
//-----------------------------------------------------------------
// rotation/fold updates coming from RotationHelper
void onRotationUpdate(String rotationParameter) {
// use REPLACE as only the last rotation matters
sendMsg(mAudioHandler, MSG_ROTATION_UPDATE, SENDMSG_REPLACE, /*arg1*/ 0, /*arg2*/ 0,
/*obj*/ rotationParameter, /*delay*/ 0);
}
void onFoldUpdate(String foldParameter) {
// use REPLACE as only the last fold state matters
sendMsg(mAudioHandler, MSG_FOLD_UPDATE, SENDMSG_REPLACE, /*arg1*/ 0, /*arg2*/ 0,
/*obj*/ foldParameter, /*delay*/ 0);
}
//----------------------------------------------------------------- //-----------------------------------------------------------------
// monitoring requests for volume range initialization // monitoring requests for volume range initialization
@Override // AudioSystemAdapter.OnVolRangeInitRequestListener @Override // AudioSystemAdapter.OnVolRangeInitRequestListener
@@ -8280,6 +8298,16 @@ public class AudioService extends IAudioService.Stub
case MSG_DISPATCH_DEVICE_VOLUME_BEHAVIOR: case MSG_DISPATCH_DEVICE_VOLUME_BEHAVIOR:
dispatchDeviceVolumeBehavior((AudioDeviceAttributes) msg.obj, msg.arg1); dispatchDeviceVolumeBehavior((AudioDeviceAttributes) msg.obj, msg.arg1);
break; break;
case MSG_ROTATION_UPDATE:
// rotation parameter format: "rotation=x" where x is one of 0, 90, 180, 270
mAudioSystem.setParameters((String) msg.obj);
break;
case MSG_FOLD_UPDATE:
// fold parameter format: "device_folded=x" where x is one of on, off
mAudioSystem.setParameters((String) msg.obj);
break;
} }
} }
} }

View File

@@ -21,13 +21,14 @@ import android.hardware.devicestate.DeviceStateManager;
import android.hardware.devicestate.DeviceStateManager.FoldStateListener; import android.hardware.devicestate.DeviceStateManager.FoldStateListener;
import android.hardware.display.DisplayManager; import android.hardware.display.DisplayManager;
import android.hardware.display.DisplayManagerGlobal; import android.hardware.display.DisplayManagerGlobal;
import android.media.AudioSystem;
import android.os.Handler; import android.os.Handler;
import android.os.HandlerExecutor; import android.os.HandlerExecutor;
import android.util.Log; import android.util.Log;
import android.view.Display; import android.view.Display;
import android.view.Surface; import android.view.Surface;
import java.util.function.Consumer;
/** /**
* Class to handle device rotation events for AudioService, and forward device rotation * Class to handle device rotation events for AudioService, and forward device rotation
* and folded state to the audio HALs through AudioSystem. * and folded state to the audio HALs through AudioSystem.
@@ -53,6 +54,10 @@ class RotationHelper {
private static AudioDisplayListener sDisplayListener; private static AudioDisplayListener sDisplayListener;
private static FoldStateListener sFoldStateListener; private static FoldStateListener sFoldStateListener;
/** callback to send rotation updates to AudioSystem */
private static Consumer<String> sRotationUpdateCb;
/** callback to send folded state updates to AudioSystem */
private static Consumer<String> sFoldUpdateCb;
private static final Object sRotationLock = new Object(); private static final Object sRotationLock = new Object();
private static final Object sFoldStateLock = new Object(); private static final Object sFoldStateLock = new Object();
@@ -67,13 +72,16 @@ class RotationHelper {
* - sDisplayListener != null * - sDisplayListener != null
* - sContext != null * - sContext != null
*/ */
static void init(Context context, Handler handler) { static void init(Context context, Handler handler,
Consumer<String> rotationUpdateCb, Consumer<String> foldUpdateCb) {
if (context == null) { if (context == null) {
throw new IllegalArgumentException("Invalid null context"); throw new IllegalArgumentException("Invalid null context");
} }
sContext = context; sContext = context;
sHandler = handler; sHandler = handler;
sDisplayListener = new AudioDisplayListener(); sDisplayListener = new AudioDisplayListener();
sRotationUpdateCb = rotationUpdateCb;
sFoldUpdateCb = foldUpdateCb;
enable(); enable();
} }
@@ -115,21 +123,26 @@ class RotationHelper {
if (DEBUG_ROTATION) { if (DEBUG_ROTATION) {
Log.i(TAG, "publishing device rotation =" + rotation + " (x90deg)"); Log.i(TAG, "publishing device rotation =" + rotation + " (x90deg)");
} }
String rotationParam;
switch (rotation) { switch (rotation) {
case Surface.ROTATION_0: case Surface.ROTATION_0:
AudioSystem.setParameters("rotation=0"); rotationParam = "rotation=0";
break; break;
case Surface.ROTATION_90: case Surface.ROTATION_90:
AudioSystem.setParameters("rotation=90"); rotationParam = "rotation=90";
break; break;
case Surface.ROTATION_180: case Surface.ROTATION_180:
AudioSystem.setParameters("rotation=180"); rotationParam = "rotation=180";
break; break;
case Surface.ROTATION_270: case Surface.ROTATION_270:
AudioSystem.setParameters("rotation=270"); rotationParam = "rotation=270";
break; break;
default: default:
Log.e(TAG, "Unknown device rotation"); Log.e(TAG, "Unknown device rotation");
rotationParam = null;
}
if (rotationParam != null) {
sRotationUpdateCb.accept(rotationParam);
} }
} }
@@ -140,11 +153,13 @@ class RotationHelper {
synchronized (sFoldStateLock) { synchronized (sFoldStateLock) {
if (sDeviceFold != newFolded) { if (sDeviceFold != newFolded) {
sDeviceFold = newFolded; sDeviceFold = newFolded;
String foldParam;
if (newFolded) { if (newFolded) {
AudioSystem.setParameters("device_folded=on"); foldParam = "device_folded=on";
} else { } else {
AudioSystem.setParameters("device_folded=off"); foldParam = "device_folded=off";
} }
sFoldUpdateCb.accept(foldParam);
} }
} }
} }