Show the output switcher when no media is playing
- Support output switcher for system routing. - Add an new string to indicate the device that audio will output to. Bug: 284227163 Test: device/host atest atest MediaOutputPreferenceControllerTest atest AudioOutputSwitchPreferenceControllerTest atest MediaOutputIndicatorSliceTest Change-Id: I94bcf84e7e93b3e4f5db1d95d5380a54a3e0c460 Signed-off-by: Jasmine Cha <chajasmine@google.com>
This commit is contained in:
@@ -18,6 +18,9 @@ package com.android.settings.sound;
|
||||
|
||||
import static android.media.AudioManager.STREAM_DEVICES_CHANGED_ACTION;
|
||||
|
||||
import static com.android.settingslib.media.flags.Flags.enableOutputSwitcherForSystemRouting;
|
||||
|
||||
import android.annotation.Nullable;
|
||||
import android.bluetooth.BluetoothDevice;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
@@ -28,6 +31,8 @@ import android.media.AudioDeviceCallback;
|
||||
import android.media.AudioDeviceInfo;
|
||||
import android.media.AudioManager;
|
||||
import android.media.MediaRouter;
|
||||
import android.media.session.MediaController;
|
||||
import android.media.session.MediaSessionManager;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.util.FeatureFlagUtils;
|
||||
@@ -79,6 +84,8 @@ public abstract class AudioSwitchPreferenceController extends BasePreferenceCont
|
||||
private final WiredHeadsetBroadcastReceiver mReceiver;
|
||||
private final Handler mHandler;
|
||||
private LocalBluetoothManager mLocalBluetoothManager;
|
||||
@Nullable private MediaSessionManager.OnActiveSessionsChangedListener mSessionListener;
|
||||
@Nullable private MediaSessionManager mMediaSessionManager;
|
||||
|
||||
public interface AudioSwitchCallback {
|
||||
void onPreferenceDataChanged(ListPreference preference);
|
||||
@@ -107,6 +114,14 @@ public abstract class AudioSwitchPreferenceController extends BasePreferenceCont
|
||||
return;
|
||||
}
|
||||
mProfileManager = mLocalBluetoothManager.getProfileManager();
|
||||
|
||||
if (enableOutputSwitcherForSystemRouting()) {
|
||||
mMediaSessionManager = context.getSystemService(MediaSessionManager.class);
|
||||
mSessionListener = new SessionChangeListener();
|
||||
} else {
|
||||
mMediaSessionManager = null;
|
||||
mSessionListener = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -329,13 +344,27 @@ public abstract class AudioSwitchPreferenceController extends BasePreferenceCont
|
||||
// Register for misc other intent broadcasts.
|
||||
IntentFilter intentFilter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
|
||||
intentFilter.addAction(STREAM_DEVICES_CHANGED_ACTION);
|
||||
mContext.registerReceiver(mReceiver, intentFilter);
|
||||
|
||||
if (enableOutputSwitcherForSystemRouting()) {
|
||||
mContext.registerReceiver(mReceiver, intentFilter, Context.RECEIVER_NOT_EXPORTED);
|
||||
if (mMediaSessionManager != null) {
|
||||
mMediaSessionManager.addOnActiveSessionsChangedListener(
|
||||
mSessionListener, null, mHandler);
|
||||
}
|
||||
} else {
|
||||
mContext.registerReceiver(mReceiver, intentFilter);
|
||||
}
|
||||
}
|
||||
|
||||
private void unregister() {
|
||||
mLocalBluetoothManager.getEventManager().unregisterCallback(this);
|
||||
mAudioManager.unregisterAudioDeviceCallback(mAudioManagerAudioDeviceCallback);
|
||||
mContext.unregisterReceiver(mReceiver);
|
||||
if (enableOutputSwitcherForSystemRouting()) {
|
||||
if (mMediaSessionManager != null) {
|
||||
mMediaSessionManager.removeOnActiveSessionsChangedListener(mSessionListener);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Notifications of audio device connection and disconnection events. */
|
||||
@@ -362,4 +391,12 @@ public abstract class AudioSwitchPreferenceController extends BasePreferenceCont
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private class SessionChangeListener
|
||||
implements MediaSessionManager.OnActiveSessionsChangedListener {
|
||||
@Override
|
||||
public void onActiveSessionsChanged(List<MediaController> controllers) {
|
||||
updateState(mPreference);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,6 +16,9 @@
|
||||
|
||||
package com.android.settings.sound;
|
||||
|
||||
import static com.android.settingslib.media.flags.Flags.enableOutputSwitcherForSystemRouting;
|
||||
|
||||
import android.annotation.Nullable;
|
||||
import android.bluetooth.BluetoothDevice;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
@@ -46,21 +49,22 @@ import java.util.List;
|
||||
*/
|
||||
public class MediaOutputPreferenceController extends AudioSwitchPreferenceController {
|
||||
|
||||
private MediaController mMediaController;
|
||||
private static final String TAG = "MediaOutputPreferenceController";
|
||||
@Nullable private MediaController mMediaController;
|
||||
private MediaSessionManager mMediaSessionManager;
|
||||
|
||||
public MediaOutputPreferenceController(Context context, String key) {
|
||||
super(context, key);
|
||||
mMediaController = MediaOutputUtils.getActiveLocalMediaController(context.getSystemService(
|
||||
MediaSessionManager.class));
|
||||
mMediaSessionManager = context.getSystemService(MediaSessionManager.class);
|
||||
mMediaController = MediaOutputUtils.getActiveLocalMediaController(mMediaSessionManager);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void displayPreference(PreferenceScreen screen) {
|
||||
super.displayPreference(screen);
|
||||
|
||||
if (!Utils.isAudioModeOngoingCall(mContext) && mMediaController != null) {
|
||||
mPreference.setVisible(true);
|
||||
}
|
||||
mPreference.setVisible(!Utils.isAudioModeOngoingCall(mContext)
|
||||
&& (enableOutputSwitcherForSystemRouting() ? true : mMediaController != null));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -70,11 +74,16 @@ public class MediaOutputPreferenceController extends AudioSwitchPreferenceContro
|
||||
return;
|
||||
}
|
||||
|
||||
if (mMediaController == null) {
|
||||
// No active local playback
|
||||
return;
|
||||
if (enableOutputSwitcherForSystemRouting()) {
|
||||
mMediaController = MediaOutputUtils.getActiveLocalMediaController(mMediaSessionManager);
|
||||
} else {
|
||||
if (mMediaController == null) {
|
||||
// No active local playback
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (Utils.isAudioModeOngoingCall(mContext)) {
|
||||
// Ongoing call status, switch entry for media will be disabled.
|
||||
mPreference.setVisible(false);
|
||||
@@ -95,9 +104,14 @@ public class MediaOutputPreferenceController extends AudioSwitchPreferenceContro
|
||||
|| (connectedLeAudioDevices != null && !connectedLeAudioDevices.isEmpty()))) {
|
||||
activeDevice = findActiveDevice();
|
||||
}
|
||||
mPreference.setTitle(mContext.getString(R.string.media_output_label_title,
|
||||
com.android.settings.Utils.getApplicationLabel(mContext,
|
||||
mMediaController.getPackageName())));
|
||||
|
||||
if (mMediaController == null) {
|
||||
mPreference.setTitle(mContext.getString(R.string.media_output_title_without_playing));
|
||||
} else {
|
||||
mPreference.setTitle(mContext.getString(R.string.media_output_label_title,
|
||||
com.android.settings.Utils.getApplicationLabel(mContext,
|
||||
mMediaController.getPackageName())));
|
||||
}
|
||||
mPreference.setSummary((activeDevice == null) ?
|
||||
mContext.getText(R.string.media_output_default_summary) :
|
||||
activeDevice.getAlias());
|
||||
@@ -145,13 +159,19 @@ public class MediaOutputPreferenceController extends AudioSwitchPreferenceContro
|
||||
@Override
|
||||
public boolean handlePreferenceTreeClick(Preference preference) {
|
||||
if (TextUtils.equals(preference.getKey(), getPreferenceKey())) {
|
||||
mContext.sendBroadcast(new Intent()
|
||||
.setAction(MediaOutputConstants.ACTION_LAUNCH_MEDIA_OUTPUT_DIALOG)
|
||||
.setPackage(MediaOutputConstants.SYSTEMUI_PACKAGE_NAME)
|
||||
.putExtra(MediaOutputConstants.EXTRA_PACKAGE_NAME,
|
||||
mMediaController.getPackageName())
|
||||
.putExtra(MediaOutputConstants.KEY_MEDIA_SESSION_TOKEN,
|
||||
mMediaController.getSessionToken()));
|
||||
if (enableOutputSwitcherForSystemRouting() && mMediaController == null) {
|
||||
mContext.sendBroadcast(new Intent()
|
||||
.setAction(MediaOutputConstants.ACTION_LAUNCH_SYSTEM_MEDIA_OUTPUT_DIALOG)
|
||||
.setPackage(MediaOutputConstants.SYSTEMUI_PACKAGE_NAME));
|
||||
} else if (mMediaController != null) {
|
||||
mContext.sendBroadcast(new Intent()
|
||||
.setAction(MediaOutputConstants.ACTION_LAUNCH_MEDIA_OUTPUT_DIALOG)
|
||||
.setPackage(MediaOutputConstants.SYSTEMUI_PACKAGE_NAME)
|
||||
.putExtra(MediaOutputConstants.EXTRA_PACKAGE_NAME,
|
||||
mMediaController.getPackageName())
|
||||
.putExtra(MediaOutputConstants.KEY_MEDIA_SESSION_TOKEN,
|
||||
mMediaController.getSessionToken()));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user