Merge "Revert "Fixed keyboard autoclosing and wrong position for EditTexts" ag/2385121" into oc-support-26.0-dev
diff --git a/v7/recyclerview/src/android/support/v7/widget/LinearLayoutManager.java b/v7/recyclerview/src/android/support/v7/widget/LinearLayoutManager.java
index a229846..a72975d 100644
--- a/v7/recyclerview/src/android/support/v7/widget/LinearLayoutManager.java
+++ b/v7/recyclerview/src/android/support/v7/widget/LinearLayoutManager.java
@@ -491,7 +491,6 @@
         // resolve layout direction
         resolveShouldLayoutReverse();
 
-        final View focused = getFocusedChild();
         if (!mAnchorInfo.mValid || mPendingScrollPosition != NO_POSITION
                 || mPendingSavedState != null) {
             mAnchorInfo.reset();
@@ -499,22 +498,6 @@
             // calculate anchor position and coordinate
             updateAnchorInfoForLayout(recycler, state, mAnchorInfo);
             mAnchorInfo.mValid = true;
-        } else if (focused != null && (mOrientationHelper.getDecoratedStart(focused)
-                        >= mOrientationHelper.getEndAfterPadding()
-                || mOrientationHelper.getDecoratedEnd(focused)
-                <= mOrientationHelper.getStartAfterPadding())) {
-            // This case relates to when the anchor child is the focused view and due to layout
-            // shrinking the focused view fell outside the viewport, e.g. when soft keyboard shows
-            // up after tapping an EditText which shrinks RV causing the focused view (The tapped
-            // EditText which is the anchor child) to get kicked out of the screen. Will update the
-            // anchor coordinate in order to make sure that the focused view is laid out. Otherwise,
-            // the available space in layoutState will be calculated as negative preventing the
-            // focused view from being laid out in fill.
-            // Note that we won't update the anchor position between layout passes (refer to
-            // TestResizingRelayoutWithAutoMeasure), which happens if we were to call
-            // updateAnchorInfoForLayout for an anchor that's not the focused view (e.g. a reference
-            // child which can change between layout passes).
-            mAnchorInfo.assignFromViewAndKeepVisibleRect(focused);
         }
         if (DEBUG) {
             Log.d(TAG, "Anchor info:" + mAnchorInfo);
diff --git a/v7/recyclerview/tests/src/android/support/v7/widget/BaseGridLayoutManagerTest.java b/v7/recyclerview/tests/src/android/support/v7/widget/BaseGridLayoutManagerTest.java
index 13dd1e4..cc6bbe8 100644
--- a/v7/recyclerview/tests/src/android/support/v7/widget/BaseGridLayoutManagerTest.java
+++ b/v7/recyclerview/tests/src/android/support/v7/widget/BaseGridLayoutManagerTest.java
@@ -325,36 +325,6 @@
         }
     }
 
-    class GridEditTextAdapter extends EditTextAdapter {
-
-        Set<Integer> mFullSpanItems = new HashSet<Integer>();
-        int mSpanPerItem = 1;
-
-        GridEditTextAdapter(int count) {
-            this(count, 1);
-        }
-
-        GridEditTextAdapter(int count, int spanPerItem) {
-            super(count);
-            mSpanPerItem = spanPerItem;
-        }
-
-        void setFullSpan(int... items) {
-            for (int i : items) {
-                mFullSpanItems.add(i);
-            }
-        }
-
-        void assignSpanSizeLookup(final GridLayoutManager glm) {
-            glm.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
-                @Override
-                public int getSpanSize(int position) {
-                    return mFullSpanItems.contains(position) ? glm.getSpanCount() : mSpanPerItem;
-                }
-            });
-        }
-    }
-
     class GridTestAdapter extends TestAdapter {
 
         Set<Integer> mFullSpanItems = new HashSet<Integer>();
diff --git a/v7/recyclerview/tests/src/android/support/v7/widget/BaseRecyclerViewInstrumentationTest.java b/v7/recyclerview/tests/src/android/support/v7/widget/BaseRecyclerViewInstrumentationTest.java
index 90e0536..8ca5f4e 100644
--- a/v7/recyclerview/tests/src/android/support/v7/widget/BaseRecyclerViewInstrumentationTest.java
+++ b/v7/recyclerview/tests/src/android/support/v7/widget/BaseRecyclerViewInstrumentationTest.java
@@ -38,12 +38,10 @@
 import android.support.v4.view.ViewCompat;
 import android.support.v7.recyclerview.test.R;
 import android.support.v7.recyclerview.test.SameActivityTestRule;
-import android.text.Editable;
 import android.util.Log;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
-import android.widget.EditText;
 import android.widget.FrameLayout;
 import android.widget.TextView;
 
@@ -795,37 +793,6 @@
         }
     }
 
-    public class EditTextAdapter extends RecyclerView.Adapter<TestViewHolder> {
-
-        final ArrayList<Editable> mEditables;
-        public EditTextAdapter(int count) {
-            mEditables = new ArrayList<>();
-            for (int i = 0; i < count; ++i) {
-                mEditables.add(Editable.Factory.getInstance().newEditable("Sample Text " + i));
-            }
-        }
-
-        @Override
-        public TestViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
-            final EditText editText = new EditText(parent.getContext());
-            editText.setLayoutParams(new ViewGroup.LayoutParams(
-                    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
-            final TestViewHolder viewHolder = new TestViewHolder(editText);
-            return viewHolder;
-        }
-
-        @Override
-        public void onBindViewHolder(TestViewHolder holder, int position) {
-            ((EditText) holder.itemView).setText(Editable.Factory.getInstance().newEditable(
-                    mEditables.get(position)));
-        }
-
-        @Override
-        public int getItemCount() {
-            return mEditables.size();
-        }
-    }
-
     public class TestAdapter extends RecyclerView.Adapter<TestViewHolder>
             implements AttachDetachCountingAdapter {
 
@@ -1230,27 +1197,6 @@
         }
     }
 
-
-    public static View findFirstFullyVisibleChild(RecyclerView parent) {
-        for (int i = 0; i < parent.getChildCount(); i++) {
-            View child = parent.getChildAt(i);
-            if (isViewFullyInBound(parent, child)) {
-                return child;
-            }
-        }
-        return null;
-    }
-
-    public static View findLastFullyVisibleChild(RecyclerView parent) {
-        for (int i = parent.getChildCount() - 1; i >= 0; i--) {
-            View child = parent.getChildAt(i);
-            if (isViewFullyInBound(parent, child)) {
-                return child;
-            }
-        }
-        return null;
-    }
-
     /**
      * Returns whether a child of RecyclerView is partially in bound. A child is
      * partially in-bounds if it's either fully or partially visible on the screen.
@@ -1286,7 +1232,7 @@
      * @param child The child view to be checked whether is fully within RV's bounds.
      * @return True if the child view is fully visible; false otherwise.
      */
-    public static boolean isViewFullyInBound(RecyclerView parent, View child) {
+    public boolean isViewFullyInBound(RecyclerView parent, View child) {
         if (child == null) {
             return false;
         }
diff --git a/v7/recyclerview/tests/src/android/support/v7/widget/GridLayoutManagerTest.java b/v7/recyclerview/tests/src/android/support/v7/widget/GridLayoutManagerTest.java
index 222e79d..5ebebfe 100644
--- a/v7/recyclerview/tests/src/android/support/v7/widget/GridLayoutManagerTest.java
+++ b/v7/recyclerview/tests/src/android/support/v7/widget/GridLayoutManagerTest.java
@@ -18,9 +18,6 @@
 
 import static android.support.v7.widget.LinearLayoutManager.HORIZONTAL;
 import static android.support.v7.widget.LinearLayoutManager.VERTICAL;
-import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
-import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
-import static android.view.WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE;
 
 import static org.hamcrest.CoreMatchers.is;
 import static org.junit.Assert.assertEquals;
@@ -39,11 +36,9 @@
 import android.support.test.runner.AndroidJUnit4;
 import android.support.v4.view.AccessibilityDelegateCompat;
 import android.support.v4.view.accessibility.AccessibilityNodeInfoCompat;
-import android.support.v7.util.TouchUtils;
 import android.test.UiThreadTest;
 import android.util.SparseIntArray;
 import android.util.StateSet;
-import android.view.KeyEvent;
 import android.view.View;
 import android.view.ViewGroup;
 
@@ -173,84 +168,6 @@
         }
     }
 
-    @Test
-    public void editTextVisibility() throws Throwable {
-        final int spanCount = 3;
-        final int itemCount = 100;
-
-        RecyclerView recyclerView = new WrappedRecyclerView(getActivity());
-        GridEditTextAdapter editTextAdapter = new GridEditTextAdapter(itemCount) {
-            @Override
-            public TestViewHolder onCreateViewHolder(ViewGroup parent,
-                    int viewType) {
-                TestViewHolder testViewHolder = super.onCreateViewHolder(parent, viewType);
-                // Good to have colors for debugging
-                StateListDrawable stl = new StateListDrawable();
-                stl.addState(new int[]{android.R.attr.state_focused},
-                        new ColorDrawable(Color.RED));
-                stl.addState(StateSet.WILD_CARD, new ColorDrawable(Color.BLUE));
-                //noinspection deprecation using this for kitkat tests
-                testViewHolder.itemView.setBackgroundDrawable(stl);
-                return testViewHolder;
-            }
-        };
-        mActivityRule.runOnUiThread(new Runnable() {
-            @Override
-            public void run() {
-                mActivityRule.getActivity().getWindow().setSoftInputMode(SOFT_INPUT_ADJUST_RESIZE);
-            }
-        });
-
-        recyclerView.setLayoutParams(
-                new ViewGroup.LayoutParams(MATCH_PARENT, WRAP_CONTENT));
-
-        Config config = new Config(spanCount, itemCount);
-        mGlm = new WrappedGridLayoutManager(getActivity(), config.mSpanCount, config.mOrientation,
-                config.mReverseLayout);
-        editTextAdapter.assignSpanSizeLookup(mGlm);
-        recyclerView.setAdapter(editTextAdapter);
-        recyclerView.setLayoutManager(mGlm);
-        waitForFirstLayout(recyclerView);
-
-        // First focus on the last fully visible EditText located at span index #1.
-        View toFocus = findLastFullyVisibleChild(mRecyclerView);
-        int focusIndex = mRecyclerView.getChildAdapterPosition(toFocus);
-        focusIndex = (focusIndex / spanCount) * spanCount + 1;
-        toFocus = mRecyclerView.findViewHolderForAdapterPosition(focusIndex).itemView;
-        assertTrue(focusIndex >= 1 && focusIndex < itemCount);
-
-        mGlm.expectLayout(1);
-        TouchUtils.tapView(getInstrumentation(), mRecyclerView, toFocus);
-        mGlm.waitForLayout(5);
-        getInstrumentation().waitForIdleSync();
-        waitForIdleScroll(mRecyclerView);
-        assertThat("Child at position " + focusIndex + " should be focused",
-                toFocus.hasFocus(), is(true));
-        assertTrue("Child view at adapter pos " + focusIndex + " should be fully visible.",
-                isViewPartiallyInBound(mRecyclerView, toFocus));
-
-        // Close soft input
-        mGlm.expectLayout(1);
-        getInstrumentation().sendKeyDownUpSync(KeyEvent.KEYCODE_BACK);
-        mGlm.waitForLayout(5);
-        getInstrumentation().waitForIdleSync();
-        waitForIdleScroll(mRecyclerView);
-        assertTrue("Child view at adapter pos " + focusIndex + " should be fully visible.",
-                isViewPartiallyInBound(mRecyclerView, toFocus));
-
-        // Now focus on the first fully visible EditText located at the last span index.
-        toFocus = findFirstFullyVisibleChild(mRecyclerView);
-        focusIndex = mRecyclerView.getChildAdapterPosition(toFocus);
-        focusIndex = (focusIndex / spanCount) * spanCount + (spanCount - 1);
-        toFocus = mRecyclerView.findViewHolderForAdapterPosition(focusIndex).itemView;
-        mGlm.expectLayout(1);
-        TouchUtils.tapView(getInstrumentation(), mRecyclerView, toFocus);
-        mGlm.waitForLayout(5);
-        getInstrumentation().waitForIdleSync();
-        waitForIdleScroll(mRecyclerView);
-        assertTrue("Child view at adapter pos " + focusIndex + " should be fully visible.",
-                isViewPartiallyInBound(mRecyclerView, toFocus));
-    }
 
     @Test
     public void topUnfocusableViewsVisibility() throws Throwable {
diff --git a/v7/recyclerview/tests/src/android/support/v7/widget/LinearLayoutManagerTest.java b/v7/recyclerview/tests/src/android/support/v7/widget/LinearLayoutManagerTest.java
index 9d44338..a2db640 100644
--- a/v7/recyclerview/tests/src/android/support/v7/widget/LinearLayoutManagerTest.java
+++ b/v7/recyclerview/tests/src/android/support/v7/widget/LinearLayoutManagerTest.java
@@ -18,9 +18,6 @@
 
 import static android.support.v7.widget.LinearLayoutManager.HORIZONTAL;
 import static android.support.v7.widget.LinearLayoutManager.VERTICAL;
-import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
-import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
-import static android.view.WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE;
 
 import static org.hamcrest.CoreMatchers.is;
 import static org.junit.Assert.assertEquals;
@@ -38,14 +35,11 @@
 import android.support.test.filters.LargeTest;
 import android.support.test.filters.SdkSuppress;
 import android.support.v4.view.AccessibilityDelegateCompat;
-import android.support.v7.util.TouchUtils;
 import android.util.Log;
 import android.util.StateSet;
-import android.view.KeyEvent;
 import android.view.View;
 import android.view.ViewGroup;
 import android.view.accessibility.AccessibilityEvent;
-import android.widget.LinearLayout;
 
 import org.junit.Test;
 
@@ -67,89 +61,6 @@
 public class LinearLayoutManagerTest extends BaseLinearLayoutManagerTest {
 
     @Test
-    public void editTextVisibility() throws Throwable {
-
-        // Simulating a scenario where an EditText is tapped (which will receive focus).
-        // The soft keyboard that's opened overlaps the focused EditText which will shrink RV's
-        // padded bounded area. LLM should still lay out the focused EditText so that it becomes
-        // visible above the soft keyboard.
-        // The condition for this test is setting RV's height to a non-exact height, so that measure
-        // is called twice (once with the larger height and another time with smaller height when
-        // the keyboard shows up). To ensure this resizing of RV, SOFT_INPUT_ADJUST_RESIZE is set.
-        final LinearLayout container = new LinearLayout(getActivity());
-        container.setOrientation(LinearLayout.VERTICAL);
-        container.setLayoutParams(
-                new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup
-                        .LayoutParams.MATCH_PARENT));
-
-        final EditTextAdapter editTextAdapter = new EditTextAdapter(50);
-
-        mRecyclerView = inflateWrappedRV();
-        ViewGroup.LayoutParams lp = mRecyclerView.getLayoutParams();
-        lp.height = WRAP_CONTENT;
-        lp.width = MATCH_PARENT;
-
-        mRecyclerView.setHasFixedSize(true);
-        mRecyclerView.setAdapter(editTextAdapter);
-        mLayoutManager = new WrappedLinearLayoutManager(getActivity(), VERTICAL, false);
-        mRecyclerView.setLayoutManager(mLayoutManager);
-
-        container.addView(mRecyclerView);
-
-        mLayoutManager.expectLayouts(1);
-        mActivityRule.runOnUiThread(new Runnable() {
-            @Override
-            public void run() {
-                getActivity().getContainer().addView(container);
-            }
-        });
-        mActivityRule.runOnUiThread(new Runnable() {
-            @Override
-            public void run() {
-                mActivityRule.getActivity().getWindow().setSoftInputMode(SOFT_INPUT_ADJUST_RESIZE);
-            }
-        });
-
-        // First focus on the last fully visible EditText.
-        View toFocus = findLastFullyVisibleChild(mRecyclerView);
-        int focusIndex = mRecyclerView.getChildAdapterPosition(toFocus);
-
-        mLayoutManager.expectLayouts(1);
-        TouchUtils.tapView(getInstrumentation(), mRecyclerView, toFocus);
-        mLayoutManager.waitForLayout(5);
-        getInstrumentation().waitForIdleSync();
-        waitForIdleScroll(mRecyclerView);
-        assertThat("Child at position " + focusIndex + " should be focused",
-                toFocus.hasFocus(), is(true));
-        // Testing for partial visibility instead of full visibility since TextView calls
-        // requestRectangleOnScreen (inside bringPointIntoView) for the focused view with a rect
-        // containing the content area. This rect is guaranteed to be fully visible whereas a
-        // portion of TextView could be out of bounds.
-        assertTrue("Child view at adapter pos " + focusIndex + " should be fully visible.",
-                isViewPartiallyInBound(mRecyclerView, toFocus));
-
-        // Close soft input
-        mLayoutManager.expectLayouts(1);
-        getInstrumentation().sendKeyDownUpSync(KeyEvent.KEYCODE_BACK);
-        mLayoutManager.waitForLayout(5);
-        getInstrumentation().waitForIdleSync();
-        waitForIdleScroll(mRecyclerView);
-        assertTrue("Child view at adapter pos " + focusIndex + " should be fully visible.",
-                isViewPartiallyInBound(mRecyclerView, toFocus));
-
-        // Now focus on the first fully visible EditText.
-        toFocus = findFirstFullyVisibleChild(mRecyclerView);
-        focusIndex = mRecyclerView.getChildAdapterPosition(toFocus);
-        mLayoutManager.expectLayouts(1);
-        TouchUtils.tapView(getInstrumentation(), mRecyclerView, toFocus);
-        mLayoutManager.waitForLayout(5);
-        getInstrumentation().waitForIdleSync();
-        waitForIdleScroll(mRecyclerView);
-        assertTrue("Child view at adapter pos " + focusIndex + " should be fully visible.",
-                isViewPartiallyInBound(mRecyclerView, toFocus));
-    }
-
-    @Test
     public void topUnfocusableViewsVisibility() throws Throwable {
         // The maximum number of child views that can be visible at any time.
         final int visibleChildCount = 5;