->Added attribute to PagedView to specify spacing between
adjacent pages
->Fixing crash on phones
Change-Id: I8ea97ce6d569c59b6d51b544dc10b0310d294b43
diff --git a/src/com/android/launcher2/AllAppsPagedView.java b/src/com/android/launcher2/AllAppsPagedView.java
index 3fb1679..b392959 100644
--- a/src/com/android/launcher2/AllAppsPagedView.java
+++ b/src/com/android/launcher2/AllAppsPagedView.java
@@ -66,13 +66,6 @@
     static final int ALL_APPS_FLAG = -1;
     private int mAppFilter = ALL_APPS_FLAG;
 
-    private int mCellCountX;
-    private int mCellCountY;
-    private int mPageLayoutPaddingTop;
-    private int mPageLayoutPaddingBottom;
-    private int mPageLayoutPaddingLeft;
-    private int mPageLayoutPaddingRight;
-
     private final LayoutInflater mInflater;
 
     private ViewGroup mOrigInfoButtonParent;
@@ -94,14 +87,6 @@
         TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PagedView, defStyle, 0);
         mCellCountX = a.getInt(R.styleable.PagedView_cellCountX, 6);
         mCellCountY = a.getInt(R.styleable.PagedView_cellCountY, 4);
-        mPageLayoutPaddingTop = a.getDimensionPixelSize(
-                R.styleable.PagedView_pageLayoutPaddingTop, 10);
-        mPageLayoutPaddingBottom = a.getDimensionPixelSize(
-                R.styleable.PagedView_pageLayoutPaddingBottom, 10);
-        mPageLayoutPaddingLeft = a.getDimensionPixelSize(
-                R.styleable.PagedView_pageLayoutPaddingLeft, 10);
-        mPageLayoutPaddingRight = a.getDimensionPixelSize(
-                R.styleable.PagedView_pageLayoutPaddingRight, 10);
         mInflater = LayoutInflater.from(context);
         a.recycle();
         setSoundEffectsEnabled(false);
diff --git a/src/com/android/launcher2/CellLayout.java b/src/com/android/launcher2/CellLayout.java
index 0f1d469..7da6612 100644
--- a/src/com/android/launcher2/CellLayout.java
+++ b/src/com/android/launcher2/CellLayout.java
@@ -226,8 +226,10 @@
             } else {
                 bg = mBackground;
             }
-            bg.setAlpha((int) (mBackgroundAlpha * 255));
-            bg.draw(canvas);
+            if (bg != null) {
+                bg.setAlpha((int) (mBackgroundAlpha * 255));
+                bg.draw(canvas);
+            }
         }
         super.dispatchDraw(canvas);
     }
diff --git a/src/com/android/launcher2/CustomizePagedView.java b/src/com/android/launcher2/CustomizePagedView.java
index 62e5496..b7e5dcd 100644
--- a/src/com/android/launcher2/CustomizePagedView.java
+++ b/src/com/android/launcher2/CustomizePagedView.java
@@ -115,12 +115,6 @@
     private List<ResolveInfo> mWallpaperList;
     private List<ApplicationInfo> mApps;
 
-    private int mCellCountX;
-    private int mCellCountY;
-    private int mPageLayoutPaddingTop;
-    private int mPageLayoutPaddingBottom;
-    private int mPageLayoutPaddingLeft;
-    private int mPageLayoutPaddingRight;
     private static final int sMinWidgetCellHSpan = 2;
     private static final int sMaxWidgetCellHSpan = 4;
 
@@ -151,14 +145,7 @@
         a = context.obtainStyledAttributes(attrs, R.styleable.PagedView, defStyle, 0);
         mCellCountX = a.getInt(R.styleable.PagedView_cellCountX, 7);
         mCellCountY = a.getInt(R.styleable.PagedView_cellCountY, 4);
-        mPageLayoutPaddingTop = a.getDimensionPixelSize(
-                R.styleable.PagedView_pageLayoutPaddingTop, 10);
-        mPageLayoutPaddingBottom = a.getDimensionPixelSize(
-                R.styleable.PagedView_pageLayoutPaddingBottom, 10);
-        mPageLayoutPaddingLeft = a.getDimensionPixelSize(
-                R.styleable.PagedView_pageLayoutPaddingLeft, 10);
-        mPageLayoutPaddingRight = a.getDimensionPixelSize(
-                R.styleable.PagedView_pageLayoutPaddingRight, 10);
+
         a.recycle();
         mCustomizationType = CustomizationType.WidgetCustomization;
         mWidgetPages = new ArrayList<ArrayList<AppWidgetProviderInfo>>();
diff --git a/src/com/android/launcher2/PagedView.java b/src/com/android/launcher2/PagedView.java
index a1b1e08..732bfbd 100644
--- a/src/com/android/launcher2/PagedView.java
+++ b/src/com/android/launcher2/PagedView.java
@@ -20,6 +20,7 @@
 import java.util.HashMap;
 
 import android.content.Context;
+import android.content.res.TypedArray;
 import android.graphics.Bitmap;
 import android.graphics.Canvas;
 import android.graphics.Rect;
@@ -90,6 +91,13 @@
     private int mTouchSlop;
     private int mPagingTouchSlop;
     private int mMaximumVelocity;
+    protected int mPageSpacing;
+    protected int mPageLayoutPaddingTop;
+    protected int mPageLayoutPaddingBottom;
+    protected int mPageLayoutPaddingLeft;
+    protected int mPageLayoutPaddingRight;
+    protected int mCellCountX;
+    protected int mCellCountY;
 
     protected static final int INVALID_POINTER = -1;
 
@@ -168,6 +176,19 @@
         super(context, attrs, defStyle);
         mChoiceMode = CHOICE_MODE_NONE;
 
+        TypedArray a = context.obtainStyledAttributes(attrs,
+                R.styleable.PagedView, defStyle, 0);
+        mPageSpacing = a.getDimensionPixelSize(R.styleable.PagedView_pageSpacing, 0);
+        mPageLayoutPaddingTop = a.getDimensionPixelSize(
+                R.styleable.PagedView_pageLayoutPaddingTop, 10);
+        mPageLayoutPaddingBottom = a.getDimensionPixelSize(
+                R.styleable.PagedView_pageLayoutPaddingBottom, 10);
+        mPageLayoutPaddingLeft = a.getDimensionPixelSize(
+                R.styleable.PagedView_pageLayoutPaddingLeft, 10);
+        mPageLayoutPaddingRight = a.getDimensionPixelSize(
+                R.styleable.PagedView_pageLayoutPaddingRight, 10);
+        a.recycle();
+
         setHapticFeedbackEnabled(false);
         init();
     }
@@ -359,7 +380,7 @@
                 final int childHeight = (getMeasuredHeight() - child.getMeasuredHeight()) / 2;
                 child.layout(childLeft, childHeight,
                         childLeft + childWidth, childHeight + child.getMeasuredHeight());
-                childLeft += childWidth;
+                childLeft += childWidth + mPageSpacing;
             }
         }
     }
@@ -387,6 +408,7 @@
                             d += getChildAt(i + 1).getMeasuredWidth() / 2;
                         }
                     }
+                    d += mPageSpacing;
 
                     float dimAlpha = (float) (Math.abs(distanceFromScreenCenter)) / d;
                     dimAlpha = Math.max(0.0f, Math.min(1.0f, (dimAlpha * dimAlpha)));
@@ -433,7 +455,7 @@
             int rightScreen = 0;
             while (x <= mScrollX) {
                 leftScreen++;
-                x += pageWidth;
+                x += pageWidth + mPageSpacing;
                 // replace above line with this if you don't assume all pages have same width as 0th
                 // page:
                 // x += getChildAt(leftScreen).getMeasuredWidth();
@@ -441,7 +463,7 @@
             rightScreen = leftScreen;
             while (x < mScrollX + screenWidth) {
                 rightScreen++;
-                x += pageWidth;
+                x += pageWidth + mPageSpacing;
                 // replace above line with this if you don't assume all pages have same width as 0th
                 // page:
                 //if (rightScreen < pageCount) {
@@ -874,13 +896,14 @@
 
     protected int getChildIndexForRelativeOffset(int relativeOffset) {
         final int childCount = getChildCount();
-        int left = getRelativeChildOffset(0);
+        int left;
+        int right;
         for (int i = 0; i < childCount; ++i) {
-            final int right = (left + getChildAt(i).getMeasuredWidth());
+            left = getRelativeChildOffset(i);
+            right = (left + getChildAt(i).getMeasuredWidth());
             if (left <= relativeOffset && relativeOffset <= right) {
                 return i;
             }
-            left = right;
         }
         return -1;
     }
@@ -895,7 +918,7 @@
 
         int offset = getRelativeChildOffset(0);
         for (int i = 0; i < index; ++i) {
-            offset += getChildAt(i).getMeasuredWidth();
+            offset += getChildAt(i).getMeasuredWidth() + mPageSpacing;
         }
         return offset;
     }
diff --git a/src/com/android/launcher2/Workspace.java b/src/com/android/launcher2/Workspace.java
index eeb496d..47e172f 100644
--- a/src/com/android/launcher2/Workspace.java
+++ b/src/com/android/launcher2/Workspace.java
@@ -475,9 +475,9 @@
 
     @Override
     protected void screenScrolled(int screenCenter) {
-        View cur = getChildAt(mCurrentPage);
-        View toRight = getChildAt(mCurrentPage + 1);
-        View toLeft = getChildAt(mCurrentPage - 1);
+        CellLayout cur = (CellLayout) getChildAt(mCurrentPage);
+        CellLayout toRight = (CellLayout) getChildAt(mCurrentPage + 1);
+        CellLayout toLeft = (CellLayout) getChildAt(mCurrentPage - 1);
 
         for (int i = 0; i < mCurrentPage - 1; i++) {
             View v = getChildAt(i);
@@ -494,11 +494,12 @@
             }
         }
 
+        int halfScreenSize = getMeasuredWidth() / 2;
         int pageWidth = cur.getMeasuredWidth();
-        int delta = screenCenter - (mCurrentPage * pageWidth + pageWidth / 2 +
-                getRelativeChildOffset(0));
+        int delta = screenCenter - (getChildOffset(mCurrentPage) -
+                getRelativeChildOffset(mCurrentPage) + halfScreenSize);
 
-        float scrollProgress = Math.abs(delta/(pageWidth*1.0f));
+        float scrollProgress = Math.abs(delta/(pageWidth*1.0f + mPageSpacing));
         int scrollDirection = delta > 0 ? SCROLL_LEFT : SCROLL_RIGHT;
 
         float rotation;
@@ -507,6 +508,7 @@
             rotation = -scrollProgress * WORKSPACE_ROTATION;
             cur.setRotationY(rotation);
             cur.setScaleX(getScaleXForRotation(rotation));
+
             if (toLeft != null) {
                 rotation = WORKSPACE_ROTATION * (1 - scrollProgress);
                 toLeft.setRotationY(rotation);