Merge "Fix output switcher will show 2 media session when remote playing" into rvc-qpr-dev am: d7cf95ef74

Original change: https://googleplex-android-review.googlesource.com/c/platform/packages/apps/Settings/+/13109544

Change-Id: I5ca519b1a3475afb9697bc76b20f550997afda2b
This commit is contained in:
TreeHugger Robot
2020-12-07 03:29:14 +00:00
committed by Automerger Merge Worker
6 changed files with 274 additions and 44 deletions

View File

@@ -30,6 +30,7 @@ import android.net.Uri;
import android.os.UserHandle;
import android.os.UserManager;
import android.text.TextUtils;
import android.util.Log;
import androidx.annotation.VisibleForTesting;
@@ -51,6 +52,9 @@ import java.util.concurrent.CopyOnWriteArrayList;
public class MediaDeviceUpdateWorker extends SliceBackgroundWorker
implements LocalMediaManager.DeviceCallback {
private static final String TAG = "MediaDeviceUpdateWorker";
private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
protected final Context mContext;
protected final Collection<MediaDevice> mMediaDevices = new CopyOnWriteArrayList<>();
private final DevicesChangedBroadcastReceiver mReceiver;
@@ -213,6 +217,10 @@ public class MediaDeviceUpdateWorker extends SliceBackgroundWorker
final List<RoutingSessionInfo> sessionInfos = new ArrayList<>();
for (RoutingSessionInfo info : mLocalMediaManager.getActiveMediaSession()) {
if (!info.isSystemSession()) {
if (DEBUG) {
Log.d(TAG, "getActiveRemoteMediaDevice() info : " + info.toString()
+ ", package name : " + info.getClientPackageName());
}
sessionInfos.add(info);
}
}

View File

@@ -25,7 +25,6 @@ import android.content.IntentFilter;
import android.media.AudioManager;
import android.media.session.MediaController;
import android.media.session.MediaSessionManager;
import android.media.session.PlaybackState;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;
@@ -53,6 +52,7 @@ public class MediaOutputIndicatorWorker extends SliceBackgroundWorker implements
LocalMediaManager.DeviceCallback {
private static final String TAG = "MediaOutputIndWorker";
private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
private final DevicesChangedBroadcastReceiver mReceiver;
private final Context mContext;
@@ -127,24 +127,8 @@ public class MediaOutputIndicatorWorker extends SliceBackgroundWorker implements
@Nullable
MediaController getActiveLocalMediaController() {
final MediaSessionManager mMediaSessionManager = mContext.getSystemService(
MediaSessionManager.class);
for (MediaController controller : mMediaSessionManager.getActiveSessions(null)) {
final MediaController.PlaybackInfo pi = controller.getPlaybackInfo();
if (pi == null) {
return null;
}
final PlaybackState playbackState = controller.getPlaybackState();
if (playbackState == null) {
return null;
}
if (pi.getPlaybackType() == MediaController.PlaybackInfo.PLAYBACK_TYPE_LOCAL
&& playbackState.getState() == PlaybackState.STATE_PLAYING) {
return controller;
}
}
return null;
return MediaOutputUtils.getActiveLocalMediaController(mContext.getSystemService(
MediaSessionManager.class));
}
@Override

View File

@@ -0,0 +1,82 @@
/*
* 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.media;
import android.media.session.MediaController;
import android.media.session.MediaSessionManager;
import android.media.session.PlaybackState;
import android.text.TextUtils;
import android.util.Log;
import androidx.annotation.Nullable;
import com.android.settings.sound.MediaOutputPreferenceController;
/**
* Utilities that can be shared between {@link MediaOutputIndicatorWorker} and
* {@link MediaOutputPreferenceController}.
*/
public class MediaOutputUtils {
private static final String TAG = "MediaOutputUtils";
private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
/**
* Returns a {@link MediaController} that state is playing and type is local playback,
* and also have active sessions.
*/
@Nullable
public static MediaController getActiveLocalMediaController(
MediaSessionManager mediaSessionManager) {
MediaController localController = null;
for (MediaController controller : mediaSessionManager.getActiveSessions(null)) {
final MediaController.PlaybackInfo pi = controller.getPlaybackInfo();
if (pi == null) {
// do nothing
continue;
}
final PlaybackState playbackState = controller.getPlaybackState();
if (playbackState == null) {
// do nothing
continue;
}
if (DEBUG) {
Log.d(TAG, "getActiveLocalMediaController() package name : "
+ controller.getPackageName()
+ ", play back type : " + pi.getPlaybackType() + ", play back state : "
+ playbackState.getState());
}
if (playbackState.getState() != PlaybackState.STATE_PLAYING) {
// do nothing
continue;
}
if (pi.getPlaybackType() == MediaController.PlaybackInfo.PLAYBACK_TYPE_REMOTE) {
if (localController != null && TextUtils.equals(localController.getPackageName(),
controller.getPackageName())) {
localController = null;
}
continue;
}
if (pi.getPlaybackType() == MediaController.PlaybackInfo.PLAYBACK_TYPE_LOCAL) {
if (localController == null) {
localController = controller;
}
}
}
return localController;
}
}

View File

@@ -22,14 +22,13 @@ import android.content.Intent;
import android.media.AudioManager;
import android.media.session.MediaController;
import android.media.session.MediaSessionManager;
import android.media.session.PlaybackState;
import android.text.TextUtils;
import androidx.annotation.Nullable;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settings.media.MediaOutputUtils;
import com.android.settingslib.Utils;
import com.android.settingslib.bluetooth.A2dpProfile;
import com.android.settingslib.bluetooth.HearingAidProfile;
@@ -51,7 +50,8 @@ public class MediaOutputPreferenceController extends AudioSwitchPreferenceContro
public MediaOutputPreferenceController(Context context, String key) {
super(context, key);
mMediaController = getActiveLocalMediaController();
mMediaController = MediaOutputUtils.getActiveLocalMediaController(context.getSystemService(
MediaSessionManager.class));
}
@Override
@@ -141,26 +141,4 @@ public class MediaOutputPreferenceController extends AudioSwitchPreferenceContro
}
return false;
}
@Nullable
MediaController getActiveLocalMediaController() {
final MediaSessionManager mMediaSessionManager = mContext.getSystemService(
MediaSessionManager.class);
for (MediaController controller : mMediaSessionManager.getActiveSessions(null)) {
final MediaController.PlaybackInfo pi = controller.getPlaybackInfo();
if (pi == null) {
return null;
}
final PlaybackState playbackState = controller.getPlaybackState();
if (playbackState == null) {
return null;
}
if (pi.getPlaybackType() == MediaController.PlaybackInfo.PLAYBACK_TYPE_LOCAL
&& playbackState.getState() == PlaybackState.STATE_PLAYING) {
return controller;
}
}
return null;
}
}