Route volume key presses to mirroring sessions while mirroring
This change doesn't implement the system ui changes, which will come in a follow up CL. Bug: b/362507305 Bug: b/396394220 Test: atest CtsMediaBetterTogetherTestCases:android.media.router.cts.SystemMediaRoutingTest Flag: com.android.media.flags.enable_mirroring_in_media_router_2 Change-Id: Icd9b1fa299093dded1ebcd52d31ada6d764be022
This commit is contained in:
@@ -54,6 +54,7 @@ import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.content.PermissionChecker;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.media.AudioManager;
|
||||
import android.media.IMediaRouter2;
|
||||
import android.media.IMediaRouter2Manager;
|
||||
import android.media.MediaRoute2Info;
|
||||
@@ -102,6 +103,7 @@ import java.util.Set;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
@@ -129,6 +131,9 @@ class MediaRouter2ServiceImpl {
|
||||
Manifest.permission.BLUETOOTH_CONNECT, Manifest.permission.BLUETOOTH_SCAN
|
||||
};
|
||||
|
||||
private static final AtomicReference<MediaRouter2ServiceImpl> sInstance =
|
||||
new AtomicReference<>();
|
||||
|
||||
private final Context mContext;
|
||||
private final Looper mLooper;
|
||||
private final UserManagerInternal mUserManagerInternal;
|
||||
@@ -241,6 +246,41 @@ class MediaRouter2ServiceImpl {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Conditionally handles a volume key press, and returns true if handled.
|
||||
*
|
||||
* <p>A volume key press will only be handled if a routing session subject to volume key presses
|
||||
* exists.
|
||||
*
|
||||
* @param callerLogTag A tag to include in the log line indicating that the event is handled.
|
||||
* @param direction One of {@link AudioManager#ADJUST_LOWER}, {@link AudioManager#ADJUST_RAISE},
|
||||
* or {@link AudioManager#ADJUST_SAME}. If the direction is not one of these, the key press
|
||||
* will not be handled.
|
||||
* @param suggestedStream The suggested stream to adjust. If the stream is not {@link
|
||||
* AudioManager#USE_DEFAULT_STREAM_TYPE} or {@link AudioManager#STREAM_MUSIC}, the key press
|
||||
* will not be handled.
|
||||
* @return Whether the key press was handled.
|
||||
* @see SystemMediaRoute2Provider2#maybeHandleVolumeKeyEventForSystemMediaSession
|
||||
*/
|
||||
|
||||
/* package */ static boolean maybeHandleVolumeKeyEvent(
|
||||
String callerLogTag, int direction, int suggestedStream) {
|
||||
var service = Flags.enableMirroringInMediaRouter2() ? sInstance.get() : null;
|
||||
var isRelevantStream =
|
||||
suggestedStream == AudioManager.USE_DEFAULT_STREAM_TYPE
|
||||
|| suggestedStream == AudioManager.STREAM_MUSIC;
|
||||
if (service == null || !isRelevantStream) {
|
||||
return false;
|
||||
}
|
||||
var handled = service.maybeHandleVolumeKeyEventInternal(direction);
|
||||
if (handled) {
|
||||
Log.i(
|
||||
TAG,
|
||||
"Volume key press handled by the routing framework. Caller: " + callerLogTag);
|
||||
}
|
||||
return handled;
|
||||
}
|
||||
|
||||
// Start of methods that implement MediaRouter2 operations.
|
||||
|
||||
@NonNull
|
||||
@@ -1095,6 +1135,24 @@ class MediaRouter2ServiceImpl {
|
||||
}
|
||||
}
|
||||
|
||||
private boolean maybeHandleVolumeKeyEventInternal(int direction) {
|
||||
synchronized (mLock) {
|
||||
// TODO: b/396399175 - Replace this for-loop with a singleton system provider when
|
||||
// multi-user support is properly implemented.
|
||||
for (int i = 0; i < mUserRecords.size(); i++) {
|
||||
var userRecord = mUserRecords.valueAt(i);
|
||||
var systemProvider = userRecord.mHandler.getSystemProvider();
|
||||
var handled =
|
||||
systemProvider.maybeHandleVolumeKeyEventForSystemMediaSession(
|
||||
DUMMY_REQUEST_ID, direction);
|
||||
if (handled) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/* package */ void updateRunningUserAndProfiles(int newActiveUserId) {
|
||||
synchronized (mLock) {
|
||||
if (mCurrentActiveUserId != newActiveUserId) {
|
||||
@@ -2264,6 +2322,11 @@ class MediaRouter2ServiceImpl {
|
||||
}
|
||||
}
|
||||
|
||||
/** Invoked when {@link MediaRouterService#systemRunning()} is invoked. */
|
||||
/* package */ void systemRunning() {
|
||||
sInstance.set(this);
|
||||
}
|
||||
|
||||
final class UserRecord {
|
||||
public final int mUserId;
|
||||
//TODO: make records private for thread-safety
|
||||
|
||||
@@ -199,6 +199,7 @@ public final class MediaRouterService extends IMediaRouterService.Stub
|
||||
},
|
||||
TAG);
|
||||
updateRunningUserAndProfiles(ActivityManager.getCurrentUser());
|
||||
mService2.systemRunning();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -782,6 +782,11 @@ public class MediaSessionRecord extends MediaSessionRecordImpl implements IBinde
|
||||
String opPackageName,
|
||||
int uid,
|
||||
int pid) {
|
||||
|
||||
if (MediaRouter2ServiceImpl.maybeHandleVolumeKeyEvent(TAG, direction, stream)) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
if (useSuggested) {
|
||||
if (AudioSystem.isStreamActive(stream, 0)) {
|
||||
|
||||
@@ -2574,6 +2574,11 @@ public class MediaSessionService extends SystemService implements Monitor {
|
||||
return;
|
||||
}
|
||||
|
||||
if (MediaRouter2ServiceImpl.maybeHandleVolumeKeyEvent(
|
||||
TAG, direction, suggestedStream)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Execute mAudioService.adjustSuggestedStreamVolume() on
|
||||
// handler thread of MediaSessionService.
|
||||
// This will release the MediaSessionService.mLock sooner and avoid
|
||||
|
||||
@@ -318,6 +318,23 @@ class SystemMediaRoute2Provider extends MediaRoute2Provider {
|
||||
// Do nothing since the system session persists.
|
||||
}
|
||||
|
||||
/**
|
||||
* Adjusts the volume of {@link MediaRoute2ProviderService service-provided} system media
|
||||
* routing sessions.
|
||||
*
|
||||
* <p>This method does not affect the volume of the system, which is managed by {@link
|
||||
* AudioManager}.
|
||||
*
|
||||
* @param requestId The id of the request, or {@link MediaRoute2ProviderService#REQUEST_ID_NONE}
|
||||
* if there's no associated id.
|
||||
* @param direction One of {@link AudioManager#ADJUST_LOWER}, {@link AudioManager#ADJUST_RAISE},
|
||||
* or {@link AudioManager#ADJUST_SAME}.
|
||||
* @return Whether the volume key pressed was handled.
|
||||
*/
|
||||
public boolean maybeHandleVolumeKeyEventForSystemMediaSession(long requestId, int direction) {
|
||||
return false;
|
||||
}
|
||||
|
||||
public MediaRoute2Info getDefaultRoute() {
|
||||
return mDefaultRoute;
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ package com.android.server.media;
|
||||
|
||||
import static android.media.MediaRoute2Info.FEATURE_LIVE_AUDIO;
|
||||
import static android.media.MediaRoute2Info.FEATURE_LIVE_VIDEO;
|
||||
import static android.media.MediaRoute2Info.PLAYBACK_VOLUME_FIXED;
|
||||
|
||||
import android.annotation.NonNull;
|
||||
import android.annotation.Nullable;
|
||||
@@ -25,6 +26,7 @@ import android.annotation.SuppressLint;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.media.AudioManager;
|
||||
import android.media.MediaRoute2Info;
|
||||
import android.media.MediaRoute2ProviderInfo;
|
||||
import android.media.MediaRoute2ProviderService;
|
||||
@@ -61,6 +63,12 @@ import java.util.stream.Stream;
|
||||
private static final boolean FORCE_GLOBAL_ROUTING_SESSION = true;
|
||||
private static final String PACKAGE_NAME_FOR_GLOBAL_SESSION = "";
|
||||
|
||||
/**
|
||||
* The portion of {@link RoutingSessionInfo#getVolumeMax()} that changes as a result of a volume
|
||||
* key press.
|
||||
*/
|
||||
private static final float VOLUME_KEY_PRESS_STEP = 0.05f;
|
||||
|
||||
private final PackageManager mPackageManager;
|
||||
|
||||
@GuardedBy("mLock")
|
||||
@@ -463,6 +471,77 @@ import java.util.stream.Stream;
|
||||
mCallback.onSessionUpdated(this, sessionInfo, packageNamesWithRoutingSessionOverrides);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean maybeHandleVolumeKeyEventForSystemMediaSession(long requestId, int direction) {
|
||||
synchronized (mLock) {
|
||||
var sessionCount = mSessionOriginalIdToSessionRecord.size();
|
||||
if (mSessionOriginalIdToSessionRecord.size() != 1) {
|
||||
// There's either no system media sessions, or too many for us to decide for one.
|
||||
if (sessionCount > 1) {
|
||||
Log.i(
|
||||
TAG,
|
||||
"Ignoring volume adjustment request due to multiple simultaneous"
|
||||
+ " sessions.");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
var volumeAdjustmentTargetSessionRecord =
|
||||
mSessionOriginalIdToSessionRecord.values().stream().findFirst().get();
|
||||
var proxyRecord = volumeAdjustmentTargetSessionRecord.getProxyRecord();
|
||||
if (proxyRecord == null) {
|
||||
Log.w(TAG, "Ignoring volume adjustment because proxy record is not present");
|
||||
return false;
|
||||
}
|
||||
Integer factor =
|
||||
switch (direction) {
|
||||
case AudioManager.ADJUST_RAISE -> 1;
|
||||
case AudioManager.ADJUST_LOWER -> -1;
|
||||
case AudioManager.ADJUST_SAME -> 0;
|
||||
default -> null;
|
||||
};
|
||||
if (factor == null) {
|
||||
Log.w(
|
||||
TAG,
|
||||
"Ignoring volume adjustment event due to unexpected direction: "
|
||||
+ direction);
|
||||
return false;
|
||||
}
|
||||
var currentSessionInfo = volumeAdjustmentTargetSessionRecord.mSourceSessionInfo;
|
||||
if (currentSessionInfo.getVolumeHandling() == PLAYBACK_VOLUME_FIXED) {
|
||||
Log.w(TAG, "Ignoring volume adjustment event due to fixed session volume");
|
||||
return false;
|
||||
}
|
||||
int volumeStep = Math.round(VOLUME_KEY_PRESS_STEP * currentSessionInfo.getVolumeMax());
|
||||
volumeStep = Math.max(1, volumeStep) * factor;
|
||||
int oldVolume = currentSessionInfo.getVolume();
|
||||
int newVolume = oldVolume + volumeStep;
|
||||
newVolume = Math.clamp(newVolume, /* min= */ 0, currentSessionInfo.getVolumeMax());
|
||||
if (oldVolume != newVolume) {
|
||||
String logMessage =
|
||||
TextUtils.formatSimple(
|
||||
"Setting volume to %d/%d on system media session managed by '%s'",
|
||||
newVolume,
|
||||
currentSessionInfo.getVolumeMax(),
|
||||
currentSessionInfo.getOwnerPackageName());
|
||||
Log.i(TAG, logMessage);
|
||||
proxyRecord.mProxy.setSessionVolume(
|
||||
requestId,
|
||||
volumeAdjustmentTargetSessionRecord.getServiceSessionId(),
|
||||
newVolume);
|
||||
} else {
|
||||
String logMessage =
|
||||
TextUtils.formatSimple(
|
||||
"Ignoring request to set volume to %d/%d on system media session"
|
||||
+ " managed by '%s'",
|
||||
newVolume,
|
||||
currentSessionInfo.getVolumeMax(),
|
||||
currentSessionInfo.getOwnerPackageName());
|
||||
Log.i(TAG, logMessage);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
private void onSessionOverrideUpdated(RoutingSessionInfo sessionInfo) {
|
||||
// TODO: b/362507305 - Consider adding routes from other provider services. This is not a
|
||||
// trivial change because a provider1-route to provider2-route transfer has seemingly two
|
||||
@@ -542,8 +621,7 @@ import java.util.stream.Stream;
|
||||
* @param packageName The name of the package whose media to route.
|
||||
* @param originalRouteId The {@link MediaRoute2Info#getOriginalId() original route id} of
|
||||
* the route that should be initially selected.
|
||||
* @param callback A {@link MediaRoute2ProviderServiceProxy.SystemMediaSessionCallback} for
|
||||
* events.
|
||||
* @param callback A {@link SystemMediaSessionCallback} for events.
|
||||
* @see MediaRoute2ProviderService#onCreateSystemRoutingSession
|
||||
*/
|
||||
public void requestCreateSystemMediaSession(
|
||||
|
||||
Reference in New Issue
Block a user