Merge "RowsFragment: use default alignment if setAlignment() not called" into nyc-support-25.1-dev
diff --git a/design/src/android/support/design/internal/BottomNavigationMenuView.java b/design/src/android/support/design/internal/BottomNavigationMenuView.java
index bc73970..82d983e 100644
--- a/design/src/android/support/design/internal/BottomNavigationMenuView.java
+++ b/design/src/android/support/design/internal/BottomNavigationMenuView.java
@@ -255,6 +255,7 @@
         }
         removeAllViews();
         if (mMenu.size() == 0) {
+            mButtons = null;
             return;
         }
         mButtons = new BottomNavigationItemView[mMenu.size()];
diff --git a/design/tests/src/android/support/design/widget/BottomNavigationViewTest.java b/design/tests/src/android/support/design/widget/BottomNavigationViewTest.java
index 37a58a6..f06a85a 100644
--- a/design/tests/src/android/support/design/widget/BottomNavigationViewTest.java
+++ b/design/tests/src/android/support/design/widget/BottomNavigationViewTest.java
@@ -223,6 +223,16 @@
         checkAndVerifyExclusiveItem(menu, R.id.destination_people);
     }
 
+    @UiThreadTest
+    @Test
+    @SmallTest
+    public void testClearingMenu() throws Throwable {
+        mBottomNavigation.getMenu().clear();
+        assertEquals(0, mBottomNavigation.getMenu().size());
+        mBottomNavigation.inflateMenu(R.menu.bottom_navigation_view_content);
+        assertEquals(3, mBottomNavigation.getMenu().size());
+    }
+
     private void checkAndVerifyExclusiveItem(final Menu menu, final int id) throws Throwable {
         menu.findItem(id).setChecked(true);
         for (int i = 0; i < menu.size(); i++) {
diff --git a/media-compat/tests/AndroidManifest.xml b/media-compat/tests/AndroidManifest.xml
index 00de2db..1216194 100644
--- a/media-compat/tests/AndroidManifest.xml
+++ b/media-compat/tests/AndroidManifest.xml
@@ -27,6 +27,11 @@
     <application android:supportsRtl="true">
         <uses-library android:name="android.test.runner"/>
         <activity android:name="android.support.v4.media.session.TestActivity" />
+        <receiver android:name="android.support.v4.media.session.MediaButtonReceiver" >
+            <intent-filter>
+                <action android:name="android.intent.action.MEDIA_BUTTON" />
+            </intent-filter>
+        </receiver>
     </application>
 
     <instrumentation
diff --git a/v7/recyclerview/src/android/support/v7/widget/RecyclerView.java b/v7/recyclerview/src/android/support/v7/widget/RecyclerView.java
index 066054c..7027acb 100644
--- a/v7/recyclerview/src/android/support/v7/widget/RecyclerView.java
+++ b/v7/recyclerview/src/android/support/v7/widget/RecyclerView.java
@@ -189,6 +189,16 @@
      */
     private static final boolean FORCE_ABS_FOCUS_SEARCH_DIRECTION = Build.VERSION.SDK_INT <= 15;
 
+    /**
+     * on API 15-, a focused child can still be considered a focused child of RV even after
+     * it's being removed or its focusable flag is set to false. This is because when this focused
+     * child is detached, the reference to this child is not removed in clearFocus. API 16 and above
+     * properly handle this case by calling ensureInputFocusOnFirstFocusable or rootViewRequestFocus
+     * to request focus on a new child, which will clear the focus on the old (detached) child as a
+     * side-effect.
+     */
+    private static final boolean IGNORE_DETACHED_FOCUSED_CHILD = Build.VERSION.SDK_INT <= 15;
+
     static final boolean DISPATCH_TEMP_DETACH = false;
     public static final int HORIZONTAL = 0;
     public static final int VERTICAL = 1;
@@ -3341,9 +3351,28 @@
         // only recover focus if RV itself has the focus or the focused view is hidden
         if (!isFocused()) {
             final View focusedChild = getFocusedChild();
-            if (!mChildHelper.isHidden(focusedChild)
-                    // on API 15, this happens :/.
-                    && focusedChild.getParent() == this && focusedChild.hasFocus()) {
+            if (IGNORE_DETACHED_FOCUSED_CHILD
+                    && (focusedChild.getParent() == null || !focusedChild.hasFocus())) {
+                // Special handling of API 15-. A focused child can be invalid because mFocus is not
+                // cleared when the child is detached (mParent = null),
+                // This happens because clearFocus on API 15- does not invalidate mFocus of its
+                // parent when this child is detached.
+                // For API 16+, this is not an issue because requestFocus takes care of clearing the
+                // prior detached focused child. For API 15- the problem happens in 2 cases because
+                // clearChild does not call clearChildFocus on RV: 1. setFocusable(false) is called
+                // for the current focused item which calls clearChild or 2. when the prior focused
+                // child is removed, removeDetachedView called in layout step 3 which calls
+                // clearChild. We should ignore this invalid focused child in all our calculations
+                // for the next view to receive focus, and apply the focus recovery logic instead.
+                if (mChildHelper.getChildCount() == 0) {
+                    // No children left. Request focus on the RV itself since one of its children
+                    // was holding focus previously.
+                    requestFocus();
+                    return;
+                }
+            } else if (!mChildHelper.isHidden(focusedChild)) {
+                // If the currently focused child is hidden, apply the focus recovery logic.
+                // Otherwise return, i.e. the currently (unhidden) focused child is good enough :/.
                 return;
             }
         }
diff --git a/v7/recyclerview/tests/src/android/support/v7/widget/RecyclerViewFocusRecoveryTest.java b/v7/recyclerview/tests/src/android/support/v7/widget/RecyclerViewFocusRecoveryTest.java
index 0c324a9..0354d53 100644
--- a/v7/recyclerview/tests/src/android/support/v7/widget/RecyclerViewFocusRecoveryTest.java
+++ b/v7/recyclerview/tests/src/android/support/v7/widget/RecyclerViewFocusRecoveryTest.java
@@ -421,6 +421,11 @@
      */
     private void assertFocusAfterLayout(int focusedChildIndexWhenRecoveryEnabled,
                                         int focusedChildIndexWhenRecoveryDisabled) {
+        if (mDisableAnimation && mDisableRecovery) {
+            // This case is not quite handled properly at the moment. For now, RV may become focused
+            // without re-delivering the focus down to the children. Skip the checks for now.
+            return;
+        }
         if (mRecyclerView.getChildCount() == 0) {
             assertThat("RV should have focus when it has no children",
                     mRecyclerView.hasFocus(), is(true));
@@ -428,11 +433,6 @@
                     mRecyclerView.isFocused(), is(true));
             return;
         }
-        if (mDisableAnimation && mDisableRecovery) {
-            // This case is not quite handled properly at the moment. For now, RV may become focused
-            // without re-delivering the focus down to the children. Skip the checks for now.
-            return;
-        }
 
         assertThat("RV should still have focus after layout", mRecyclerView.hasFocus(), is(true));
         if ((mDisableRecovery && focusedChildIndexWhenRecoveryDisabled == -1)