Merge "[Sound Panel] Fix bugs for sound panel slice" into tm-qpr-dev
diff --git a/src/com/android/settings/media/MediaOutputUtils.java b/src/com/android/settings/media/MediaOutputUtils.java
index 977c517..fcb2fb2 100644
--- a/src/com/android/settings/media/MediaOutputUtils.java
+++ b/src/com/android/settings/media/MediaOutputUtils.java
@@ -64,7 +64,9 @@
                         + ", play back type : " + pi.getPlaybackType() + ", play back state : "
                         + playbackState.getState());
             }
-            if (playbackState.getState() != PlaybackState.STATE_PLAYING) {
+            if (playbackState.getState() == PlaybackState.STATE_STOPPED
+                    || playbackState.getState() == PlaybackState.STATE_NONE
+                    || playbackState.getState() == PlaybackState.STATE_ERROR) {
                 // do nothing
                 continue;
             }
diff --git a/src/com/android/settings/panel/PanelFragment.java b/src/com/android/settings/panel/PanelFragment.java
index 04ae311..1ce4492 100644
--- a/src/com/android/settings/panel/PanelFragment.java
+++ b/src/com/android/settings/panel/PanelFragment.java
@@ -60,6 +60,7 @@
 
 import com.google.android.setupdesign.DividerItemDecoration;
 
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.LinkedHashMap;
 import java.util.List;
@@ -339,8 +340,13 @@
             mSliceLiveData.put(uri, sliceLiveData);
 
             sliceLiveData.observe(getViewLifecycleOwner(), slice -> {
-                // If the Slice has already loaded, do nothing.
+
+                // If the Slice has already loaded, refresh list with slice data.
                 if (mPanelSlicesLoaderCountdownLatch.isSliceLoaded(uri)) {
+                    if (mAdapter != null) {
+                        int itemIndex = (new ArrayList<>(mSliceLiveData.keySet())).indexOf(uri);
+                        mAdapter.notifyItemChanged(itemIndex);
+                    }
                     return;
                 }
 
diff --git a/src/com/android/settings/panel/PanelSlicesAdapter.java b/src/com/android/settings/panel/PanelSlicesAdapter.java
index afccc78..d728366 100644
--- a/src/com/android/settings/panel/PanelSlicesAdapter.java
+++ b/src/com/android/settings/panel/PanelSlicesAdapter.java
@@ -84,7 +84,7 @@
 
     @Override
     public void onBindViewHolder(@NonNull SliceRowViewHolder sliceRowViewHolder, int position) {
-        sliceRowViewHolder.onBind(mSliceLiveData.get(position), position);
+        sliceRowViewHolder.onBind(mSliceLiveData.get(position).getValue());
     }
 
     /**
@@ -132,15 +132,14 @@
         /**
          * Called when the view is displayed.
          */
-        public void onBind(LiveData<Slice> sliceLiveData, int position) {
-            sliceLiveData.observe(mPanelFragment.getViewLifecycleOwner(), sliceView);
-
-            // Do not show the divider above media devices switcher slice per request
-            final Slice slice = sliceLiveData.getValue();
-
+        public void onBind(Slice slice) {
             // Hides slice which reports with error hint or not contain any slice sub-item.
             if (slice == null || !isValidSlice(slice)) {
                 sliceView.setVisibility(View.GONE);
+                return;
+            } else {
+                sliceView.setSlice(slice);
+                sliceView.setVisibility(View.VISIBLE);
             }
 
             // Add divider for the end icon
@@ -154,7 +153,7 @@
                                 .action(0 /* attribution */,
                                         SettingsEnums.ACTION_PANEL_INTERACTION,
                                         mMetricsCategory,
-                                        sliceLiveData.getValue().getUri().getLastPathSegment()
+                                        slice.getUri().getLastPathSegment()
                                         /* log key */,
                                         eventInfo.actionType /* value */);
                     })
diff --git a/tests/robotests/src/com/android/settings/media/MediaOutputUtilsTest.java b/tests/robotests/src/com/android/settings/media/MediaOutputUtilsTest.java
index dea6a2c..5fa7fdd 100644
--- a/tests/robotests/src/com/android/settings/media/MediaOutputUtilsTest.java
+++ b/tests/robotests/src/com/android/settings/media/MediaOutputUtilsTest.java
@@ -66,7 +66,18 @@
 
     @Test
     public void getActiveLocalMediaController_localMediaPlaying_returnController() {
-        initPlayback();
+        initPlayback(PlaybackState.STATE_PLAYING);
+
+        when(mMediaController.getPlaybackInfo()).thenReturn(mPlaybackInfo);
+        when(mMediaController.getPlaybackState()).thenReturn(mPlaybackState);
+
+        assertThat(MediaOutputUtils.getActiveLocalMediaController(mMediaSessionManager)).isEqualTo(
+                mMediaController);
+    }
+
+    @Test
+    public void getActiveLocalMediaController_localMediaPause_returnController() {
+        initPlayback(PlaybackState.STATE_PAUSED);
 
         when(mMediaController.getPlaybackInfo()).thenReturn(mPlaybackInfo);
         when(mMediaController.getPlaybackState()).thenReturn(mPlaybackState);
@@ -114,6 +125,44 @@
     }
 
     @Test
+    public void getActiveLocalMediaController_localMediaNone_returnNull() {
+        mPlaybackInfo = new MediaController.PlaybackInfo(
+                MediaController.PlaybackInfo.PLAYBACK_TYPE_LOCAL,
+                VolumeProvider.VOLUME_CONTROL_ABSOLUTE,
+                100,
+                10,
+                new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_MEDIA).build(),
+                null);
+        mPlaybackState = new PlaybackState.Builder()
+                .setState(PlaybackState.STATE_NONE, 0, 1)
+                .build();
+
+        when(mMediaController.getPlaybackInfo()).thenReturn(mPlaybackInfo);
+        when(mMediaController.getPlaybackState()).thenReturn(mPlaybackState);
+
+        assertThat(MediaOutputUtils.getActiveLocalMediaController(mMediaSessionManager)).isNull();
+    }
+
+    @Test
+    public void getActiveLocalMediaController_localMediaError_returnNull() {
+        mPlaybackInfo = new MediaController.PlaybackInfo(
+                MediaController.PlaybackInfo.PLAYBACK_TYPE_LOCAL,
+                VolumeProvider.VOLUME_CONTROL_ABSOLUTE,
+                100,
+                10,
+                new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_MEDIA).build(),
+                null);
+        mPlaybackState = new PlaybackState.Builder()
+                .setState(PlaybackState.STATE_ERROR, 0, 1)
+                .build();
+
+        when(mMediaController.getPlaybackInfo()).thenReturn(mPlaybackInfo);
+        when(mMediaController.getPlaybackState()).thenReturn(mPlaybackState);
+
+        assertThat(MediaOutputUtils.getActiveLocalMediaController(mMediaSessionManager)).isNull();
+    }
+
+    @Test
     public void getActiveLocalMediaController_bothHaveRemoteMediaAndLocalMedia_returnNull() {
         mMediaControllers.clear();
         final MediaController.PlaybackInfo playbackInfo = new MediaController.PlaybackInfo(
@@ -130,7 +179,7 @@
 
         mMediaControllers.add(remoteMediaController);
         mMediaControllers.add(mMediaController);
-        initPlayback();
+        initPlayback(PlaybackState.STATE_PLAYING);
 
         when(mMediaController.getPlaybackInfo()).thenReturn(mPlaybackInfo);
         when(mMediaController.getPlaybackState()).thenReturn(mPlaybackState);
@@ -155,7 +204,7 @@
         final MediaController remoteMediaController = mock(MediaController.class);
 
         mMediaControllers.add(remoteMediaController);
-        initPlayback();
+        initPlayback(PlaybackState.STATE_PLAYING);
 
         when(mMediaController.getPlaybackInfo()).thenReturn(mPlaybackInfo);
         when(mMediaController.getPlaybackState()).thenReturn(mPlaybackState);
@@ -165,7 +214,7 @@
         assertThat(MediaOutputUtils.getActiveLocalMediaController(mMediaSessionManager)).isNull();
     }
 
-    private void initPlayback() {
+    private void initPlayback(int playbackState) {
         mPlaybackInfo = new MediaController.PlaybackInfo(
                 MediaController.PlaybackInfo.PLAYBACK_TYPE_LOCAL,
                 VolumeProvider.VOLUME_CONTROL_ABSOLUTE,
@@ -174,7 +223,7 @@
                 new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_MEDIA).build(),
                 null);
         mPlaybackState = new PlaybackState.Builder()
-                .setState(PlaybackState.STATE_PLAYING, 0, 1)
+                .setState(playbackState, 0, 1)
                 .build();
     }
 }