Update "Play media to" in Sound Settings

-Change string to "Play <APP Label> on"
-Hide it when there is no local playback
-Disable search index
-Add test cases

Bug: 155720628
Test: make -j50 RunSettingsRoboTests
Change-Id: Id104d5b49c069a761e4cf82385bf1225d494c95e
This commit is contained in:
Tim Peng
2020-05-06 13:52:24 +08:00
parent 032f77052b
commit f392eae213
3 changed files with 116 additions and 1 deletions

View File

@@ -20,8 +20,12 @@ import android.bluetooth.BluetoothDevice;
import android.content.Context;
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;
@@ -43,15 +47,18 @@ import java.util.List;
*/
public class MediaOutputPreferenceController extends AudioSwitchPreferenceController {
private MediaController mMediaController;
public MediaOutputPreferenceController(Context context, String key) {
super(context, key);
mMediaController = getActiveLocalMediaController();
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
if (!Utils.isAudioModeOngoingCall(mContext)) {
if (!Utils.isAudioModeOngoingCall(mContext) && mMediaController != null) {
mPreference.setVisible(true);
}
}
@@ -63,6 +70,11 @@ public class MediaOutputPreferenceController extends AudioSwitchPreferenceContro
return;
}
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);
@@ -81,6 +93,9 @@ public class MediaOutputPreferenceController extends AudioSwitchPreferenceContro
|| (connectedHADevices != null && !connectedHADevices.isEmpty()))) {
activeDevice = findActiveDevice();
}
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());
@@ -126,4 +141,26 @@ 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;
}
}