Fixing issue where last workspace page was offset when rotated. (Bug 6413570)

- Also adding check to prevent NPE in updating scroll indicator

Change-Id: If445be7fa00497bd21a4b9a6f9ebce93ceb30f2c
diff --git a/src/com/android/launcher2/PagedView.java b/src/com/android/launcher2/PagedView.java
index 4bcb4c7..adfe0de 100644
--- a/src/com/android/launcher2/PagedView.java
+++ b/src/com/android/launcher2/PagedView.java
@@ -292,7 +292,9 @@
      * the previous tab page.
      */
     protected void updateCurrentPageScroll() {
-        int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
+        int offset = getChildOffset(mCurrentPage);
+        int relOffset = getRelativeChildOffset(mCurrentPage);
+        int newX = offset - relOffset;
         scrollTo(newX, 0);
         mScroller.setFinalX(newX);
         mScroller.forceFinished(true);
@@ -505,6 +507,23 @@
 
         setMeasuredDimension(widthSize, heightSize);
 
+        if (childCount > 0) {
+            if (DEBUG) Log.d(TAG, "getRelativeChildOffset(): " + getMeasuredWidth() + ", "
+                    + getChildWidth(0));
+
+            // Calculate the variable page spacing if necessary
+            if (mPageSpacing < 0) {
+                // The gap between pages in the PagedView should be equal to the gap from the page
+                // to the edge of the screen (so it is not visible in the current screen).  To
+                // account for unequal padding on each side of the paged view, we take the maximum
+                // of the left/right gap and use that as the gap between each page.
+                int offset = getRelativeChildOffset(0);
+                int spacing = Math.max(offset, widthSize - offset -
+                        getChildAt(0).getMeasuredWidth());
+                setPageSpacing(spacing);
+            }
+        }
+
         // We can't call getChildOffset/getRelativeChildOffset until we set the measured dimensions.
         // We also wait until we set the measured dimensions before flushing the cache as well, to
         // ensure that the cache is filled with good values.
@@ -577,24 +596,7 @@
         if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
         final int verticalPadding = getPaddingTop() + getPaddingBottom();
         final int childCount = getChildCount();
-        int childLeft = 0;
-        if (childCount > 0) {
-            if (DEBUG) Log.d(TAG, "getRelativeChildOffset(): " + getMeasuredWidth() + ", "
-                    + getChildWidth(0));
-            childLeft = getRelativeChildOffset(0);
-
-            // Calculate the variable page spacing if necessary
-            if (mPageSpacing < 0) {
-                // The gap between pages in the PagedView should be equal to the gap from the page
-                // to the edge of the screen (so it is not visible in the current screen).  To
-                // account for unequal padding on each side of the paged view, we take the maximum
-                // of the left/right gap and use that as the gap between each page.
-                int offset = getRelativeChildOffset(0);
-                int spacing = Math.max(offset, (right - left) - offset -
-                        getChildAt(0).getMeasuredWidth());
-                setPageSpacing(spacing);
-            }
-        }
+        int childLeft = getRelativeChildOffset(0);
 
         for (int i = 0; i < childCount; i++) {
             final View child = getPageAt(i);
@@ -619,10 +621,6 @@
             setHorizontalScrollBarEnabled(true);
             mFirstLayout = false;
         }
-
-        if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
-            mFirstLayout = false;
-        }
     }
 
     protected void screenScrolled(int screenCenter) {
@@ -709,13 +707,6 @@
         }
     }
 
-    protected int getScaledRelativeChildOffset(int index) {
-        final int padding = getPaddingLeft() + getPaddingRight();
-        final int offset = getPaddingLeft() + (getMeasuredWidth() - padding -
-                getScaledMeasuredWidth(getPageAt(index))) / 2;
-        return offset;
-    }
-
     protected int getScaledMeasuredWidth(View child) {
         // This functions are called enough times that it actually makes a difference in the
         // profiler -- so just inline the max() here
@@ -1702,10 +1693,12 @@
         // found
         if (mHasScrollIndicator && mScrollIndicator == null) {
             ViewGroup parent = (ViewGroup) getParent();
-            mScrollIndicator = (View) (parent.findViewById(R.id.paged_view_indicator));
-            mHasScrollIndicator = mScrollIndicator != null;
-            if (mHasScrollIndicator) {
-                mScrollIndicator.setVisibility(View.VISIBLE);
+            if (parent != null) {
+                mScrollIndicator = (View) (parent.findViewById(R.id.paged_view_indicator));
+                mHasScrollIndicator = mScrollIndicator != null;
+                if (mHasScrollIndicator) {
+                    mScrollIndicator.setVisibility(View.VISIBLE);
+                }
             }
         }
         return mScrollIndicator;