Issue before this CL:
RemoteVolumePreferenceController only return available when
it is casting, otherwise return CONDITIONALLY_UNAVAILABLE.
However slice database only index available controllers and keep
this cache. So remote slider won't be indexed if it is not casting
at that time.
As a tmp fix, this CL make controller always return available
unsearchable to make it indexed by database. However only add
that slice if it is casting.
Bug:
130124950
Test: RunSettingsRoboTests
Change-Id: I191144844d6ba7ccbe3dc1c9d19801adb978abc6
@VisibleForTesting
static final int REMOTE_VOLUME = 100;
- private MediaSessionManager mMediaSessionManager;
private MediaSessions mMediaSessions;
@VisibleForTesting
MediaSession.Token mActiveToken;
public RemoteVolumePreferenceController(Context context) {
super(context, KEY_REMOTE_VOLUME);
- mMediaSessionManager = context.getSystemService(MediaSessionManager.class);
mMediaSessions = new MediaSessions(context, Looper.getMainLooper(), mCallbacks);
+ updateToken(getActiveRemoteToken(mContext));
}
@Override
public int getAvailabilityStatus() {
- final List<MediaController> controllers = mMediaSessionManager.getActiveSessions(null);
+ // Always return true to make it indexed in database
+ return AVAILABLE_UNSEARCHABLE;
+ }
+
+ /**
+ * Return {@link android.media.session.MediaSession.Token} for active remote token, or
+ * {@code null} if there is no active remote token.
+ */
+ public static MediaSession.Token getActiveRemoteToken(Context context) {
+ final MediaSessionManager sessionManager = context.getSystemService(
+ MediaSessionManager.class);
+ final List<MediaController> controllers = sessionManager.getActiveSessions(null);
for (MediaController mediaController : controllers) {
final MediaController.PlaybackInfo pi = mediaController.getPlaybackInfo();
if (isRemote(pi)) {
- updateToken(mediaController.getSessionToken());
- return AVAILABLE;
+ return mediaController.getSessionToken();
}
}
// No active remote media at this point
- return CONDITIONALLY_UNAVAILABLE;
+ return null;
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
+ mPreference.setVisible(mActiveToken != null);
if (mMediaController != null) {
updatePreference(mPreference, mActiveToken, mMediaController.getPlaybackInfo());
}
import android.provider.Settings;
import com.android.settings.R;
+import com.android.settings.notification.RemoteVolumePreferenceController;
import java.util.ArrayList;
import java.util.List;
@Override
public List<Uri> getSlices() {
final List<Uri> uris = new ArrayList<>();
- uris.add(VOLUME_REMOTE_MEDIA_URI);
+ if (RemoteVolumePreferenceController.getActiveRemoteToken(mContext) != null) {
+ uris.add(VOLUME_REMOTE_MEDIA_URI);
+ }
uris.add(VOLUME_MEDIA_URI);
uris.add(MEDIA_OUTPUT_INDICATOR_SLICE_URI);
uris.add(VOLUME_CALL_URI);
import android.media.session.MediaSessionManager;
import com.android.settings.R;
+import com.android.settings.core.BasePreferenceController;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
+import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
private MediaSessionManager mMediaSessionManager;
@Mock
private MediaController mMediaController;
- @Mock
+ @Mock(answer = Answers.RETURNS_DEEP_STUBS)
private ISessionController mStub;
- @Mock
+ @Mock(answer = Answers.RETURNS_DEEP_STUBS)
private ISessionController mStub2;
private MediaSession.Token mToken;
private MediaSession.Token mToken2;
mPlaybackInfo = new MediaController.PlaybackInfo(
MediaController.PlaybackInfo.PLAYBACK_TYPE_REMOTE, 0, MAX_POS, CURRENT_POS, null);
when(mMediaController.getPlaybackInfo()).thenReturn(mPlaybackInfo);
+ when(mMediaController.getSessionToken()).thenReturn(mToken);
}
@Test
- public void isAvailable_containRemoteMedia_returnTrue() {
+ public void getActiveRemoteToken_containRemoteMedia_returnToken() {
when(mMediaController.getPlaybackInfo()).thenReturn(
new MediaController.PlaybackInfo(MediaController.PlaybackInfo.PLAYBACK_TYPE_REMOTE,
0, 0, 0, null));
- assertThat(mController.isAvailable()).isTrue();
+ assertThat(mController.getActiveRemoteToken(mContext)).isEqualTo(mToken);
}
@Test
- public void isAvailable_noRemoteMedia_returnFalse() {
+ public void getActiveRemoteToken_noRemoteMedia_returnNull() {
when(mMediaController.getPlaybackInfo()).thenReturn(
new MediaController.PlaybackInfo(MediaController.PlaybackInfo.PLAYBACK_TYPE_LOCAL,
0, 0, 0, null));
- assertThat(mController.isAvailable()).isFalse();
+ assertThat(mController.getActiveRemoteToken(mContext)).isNull();
+ }
+
+ @Test
+ public void isAvailable_returnAvailableUnsearchable() {
+ assertThat(mController.isAvailable()).isTrue();
+ assertThat(mController.getAvailabilityStatus()).isEqualTo(
+ BasePreferenceController.AVAILABLE_UNSEARCHABLE);
}
@Test