Merge Android 12 QPR 3

Bug: 236045730
Merged-In: I6c4077c0b40dcd8f975e59dca4f3693820ec4fac
Change-Id: I38858b525ddf81f3a3e38578660dd37d28c383ab
diff --git a/src/com/android/car/media/MediaActivity.java b/src/com/android/car/media/MediaActivity.java
index b437a1a..591285a 100644
--- a/src/com/android/car/media/MediaActivity.java
+++ b/src/com/android/car/media/MediaActivity.java
@@ -426,10 +426,6 @@
                 break;
             case BROWSING:
                 if (oldMode == Mode.PLAYBACK) {
-                    // When switching from PLAYBACK mode to BROWSING mode, if a CarUiRecyclerView
-                    // shows up and it's in rotary mode, restore focus in the CarUiRecyclerView.
-                    mMediaActivityController.restoreFocusInCurrentNode();
-
                     ViewUtils.hideViewAnimated(mErrorContainer, 0);
                     ViewUtils.showViewAnimated(mBrowseContainer, 0);
                     animateOutPlaybackContainer(fadeOutDuration);
diff --git a/src/com/android/car/media/MediaActivityController.java b/src/com/android/car/media/MediaActivityController.java
index a29b40d..9882e70 100644
--- a/src/com/android/car/media/MediaActivityController.java
+++ b/src/com/android/car/media/MediaActivityController.java
@@ -406,6 +406,7 @@
         updateAppBar();
     }
 
+    @NonNull
     private BrowseViewController getControllerForItem(@NonNull MediaItemMetadata item) {
         BrowseViewController controller = mBrowseViewControllersByNode.get(item);
         if (controller == null) {
@@ -438,15 +439,15 @@
     }
 
     // If the current node has a CarUiRecyclerView and it's in rotary mode, restore focus in it.
-    void restoreFocusInCurrentNode() {
+    // Should remain private and definitely NOT be called from MediaActivity#changeModeInternal
+    // as the controller isn't ready to show the browse data of the new media source (it hasn't
+    // connected to it (b/217159531).
+    private void restoreFocusInCurrentNode() {
         MediaItemMetadata currentNode = getCurrentMediaItem();
         if (currentNode == null) {
             return;
         }
         BrowseViewController controller = getControllerForItem(currentNode);
-        if (controller == null) {
-            return;
-        }
         CarUiRecyclerView carUiRecyclerView =
                 controller.getContent().findViewById(R.id.browse_list);
         if (carUiRecyclerView != null && carUiRecyclerView instanceof LazyLayoutView