Fix fragment carousel going to wrong scroll position after rotation

- On devices with no physical buttons, there is a status
bar on screen, which triggers an onSizeChanged() method call
which causes the ContactDetailFragmentCarousel to scroll to
the focused view (which was wrongly set as the ListView in the
details page in phone landscape).

- Make sure the right view is focused on and remove the manual
scroll code in onLayout() in ContactDetailFragmentCarousel
because it doesn't do anything because the width of the child
view is not correct (when the page is first launched and doesn't
cause any scroll changes). Instead, when the data is loaded
and we enable/disable swipe, we select the right view to be focused
on, which takes care of scrolling to the right page.

Bug: 5265640
Change-Id: Id3b568b2d1c3af5d4d6cae285038131bb4cb1a45
diff --git a/res/layout/contact_detail_fragment_carousel.xml b/res/layout/contact_detail_fragment_carousel.xml
index 4f65549..2572cc4 100644
--- a/res/layout/contact_detail_fragment_carousel.xml
+++ b/res/layout/contact_detail_fragment_carousel.xml
@@ -30,7 +30,9 @@
         android:id="@+id/about_fragment_container"
         android:layout_width="0dip"
         android:layout_height="match_parent"
-        android:layout_weight="1" />
+        android:layout_weight="1"
+        android:focusable="true"
+        android:focusableInTouchMode="true" />
 
     <!--
       Container for the "Updates" page fragment on the contact card for a contact
@@ -42,6 +44,8 @@
         android:layout_width="0dip"
         android:layout_height="match_parent"
         android:layout_weight="1"
-        android:visibility="gone" />
+        android:visibility="gone"
+        android:focusable="true"
+        android:focusableInTouchMode="true" />
 
 </LinearLayout>
\ No newline at end of file
diff --git a/src/com/android/contacts/detail/ContactDetailFragmentCarousel.java b/src/com/android/contacts/detail/ContactDetailFragmentCarousel.java
index b01316b..756b1c7 100644
--- a/src/com/android/contacts/detail/ContactDetailFragmentCarousel.java
+++ b/src/com/android/contacts/detail/ContactDetailFragmentCarousel.java
@@ -19,7 +19,6 @@
 import com.android.contacts.R;
 
 import android.content.Context;
-import android.os.Handler;
 import android.util.AttributeSet;
 import android.view.LayoutInflater;
 import android.view.MotionEvent;
@@ -88,8 +87,6 @@
     private View mDetailFragmentView;
     private View mUpdatesFragmentView;
 
-    private boolean mScrollToCurrentPage = false;
-
     public ContactDetailFragmentCarousel(Context context) {
         this(context, null);
     }
@@ -144,31 +141,9 @@
                 resolveSize(screenHeight, heightMeasureSpec));
     }
 
-    @Override
-    protected void onLayout(boolean changed, int l, int t, int r, int b) {
-        super.onLayout(changed, l, t, r, b);
-        if (mScrollToCurrentPage) {
-            mScrollToCurrentPage = false;
-            // Use scrollTo() instead of smoothScrollTo() to prevent a visible flicker to the user
-            scrollTo(mCurrentPage == ABOUT_PAGE ? 0 : mAllowedHorizontalScrollLength, 0);
-            updateTouchInterceptors();
-        }
-    }
-
     /**
-     * Set the current page that should be restored when the view is first laid out.
-     */
-    public void restoreCurrentPage(int pageIndex) {
-        setCurrentPage(pageIndex);
-        // It is only possible to scroll the view after onMeasure() has been called (where the
-        // allowed horizontal scroll length is determined). Hence, set a flag that will be read
-        // in onLayout() after the children and this view have finished being laid out.
-        mScrollToCurrentPage = true;
-    }
-
-    /**
-     * Set the current page. This auto-scrolls the carousel to the current page and dims out
-     * the non-selected page.
+     * Set the current page. This dims out the non-selected page but doesn't do any scrolling of
+     * the carousel.
      */
     public void setCurrentPage(int pageIndex) {
         mCurrentPage = pageIndex;
@@ -176,7 +151,6 @@
         if (mAboutFragment != null && mUpdatesFragment != null) {
             mAboutFragment.setAlphaLayerValue(mCurrentPage == ABOUT_PAGE ? 0 : MAX_ALPHA);
             mUpdatesFragment.setAlphaLayerValue(mCurrentPage == UPDATES_PAGE ? 0 : MAX_ALPHA);
-            snapToEdge();
         }
     }
 
@@ -205,9 +179,12 @@
             mEnableSwipe = enable;
             if (mUpdatesFragmentView != null) {
                 mUpdatesFragmentView.setVisibility(enable ? View.VISIBLE : View.GONE);
-                mScrollToCurrentPage = true;
-                requestLayout();
-                invalidate();
+                if (mCurrentPage == ABOUT_PAGE) {
+                    mDetailFragmentView.requestFocus();
+                } else {
+                    mUpdatesFragmentView.requestFocus();
+                }
+                updateTouchInterceptors();
             }
         }
     }
diff --git a/src/com/android/contacts/detail/ContactDetailLayoutController.java b/src/com/android/contacts/detail/ContactDetailLayoutController.java
index 7a7f400..4b31a6e 100644
--- a/src/com/android/contacts/detail/ContactDetailLayoutController.java
+++ b/src/com/android/contacts/detail/ContactDetailLayoutController.java
@@ -215,7 +215,7 @@
 
                 mFragmentCarousel.setFragmentViews(mDetailFragmentView, mUpdatesFragmentView);
                 mFragmentCarousel.setFragments(mDetailFragment, mUpdatesFragment);
-                mFragmentCarousel.restoreCurrentPage(currentPageIndex);
+                mFragmentCarousel.setCurrentPage(currentPageIndex);
                 break;
             }
         }