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:
Jasmine Cha
2023-10-03 16:56:43 +08:00
parent 1340427763
commit 9e45469833
7 changed files with 287 additions and 31 deletions

View File

@@ -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;