Removing old scroll indicator and adding page indicator dots.

Change-Id: I44c5b4b532e8464010d45dd0a3dfeccb2d475216
diff --git a/src/com/android/launcher3/PagedView.java b/src/com/android/launcher3/PagedView.java
index 275195b..81a842e 100644
--- a/src/com/android/launcher3/PagedView.java
+++ b/src/com/android/launcher3/PagedView.java
@@ -192,18 +192,9 @@
 
     protected boolean mAllowLongPress = true;
 
-    // Scrolling indicator
-    private ValueAnimator mScrollIndicatorAnimator;
-    private View mScrollIndicator;
-    private int mScrollIndicatorPaddingLeft;
-    private int mScrollIndicatorPaddingRight;
-    private boolean mHasScrollIndicator = true;
-    private boolean mShouldShowScrollIndicator = false;
-    private boolean mShouldShowScrollIndicatorImmediately = false;
-    protected static final int sScrollIndicatorFadeInDuration = 150;
-    protected static final int sScrollIndicatorFadeOutDuration = 650;
-    protected static final int sScrollIndicatorFlashDuration = 650;
-    private boolean mScrollingPaused = false;
+    // Page Indicator
+    private int mPageIndicatorViewId;
+    private PageIndicator mPageIndicator;
 
     // The viewport whether the pages are to be contained (the actual view may be larger than the
     // viewport)
@@ -296,10 +287,7 @@
                 R.styleable.PagedView_pageLayoutWidthGap, 0);
         mPageLayoutHeightGap = a.getDimensionPixelSize(
                 R.styleable.PagedView_pageLayoutHeightGap, 0);
-        mScrollIndicatorPaddingLeft =
-            a.getDimensionPixelSize(R.styleable.PagedView_scrollIndicatorPaddingLeft, 0);
-        mScrollIndicatorPaddingRight =
-            a.getDimensionPixelSize(R.styleable.PagedView_scrollIndicatorPaddingRight, 0);
+        mPageIndicatorViewId = a.getResourceId(R.styleable.PagedView_pageIndicator, -1);
         a.recycle();
 
         setHapticFeedbackEnabled(false);
@@ -332,6 +320,21 @@
         setOnHierarchyChangeListener(this);
     }
 
+    protected void onAttachedToWindow() {
+        // Hook up the page indicator
+        ViewGroup parent = (ViewGroup) getParent();
+        if (mPageIndicator == null && mPageIndicatorViewId > -1) {
+            mPageIndicator = (PageIndicator) parent.findViewById(mPageIndicatorViewId);
+            mPageIndicator.removeAllMarkers();
+            mPageIndicator.addMarkers(getChildCount());
+        }
+    }
+
+    protected void onDetachedFromWindow() {
+        // Unhook the page indicator
+        mPageIndicator = null;
+    }
+
     void setDeleteDropTarget(View v) {
         mDeleteDropTarget = v;
     }
@@ -473,8 +476,6 @@
      */
     void pauseScrolling() {
         mScroller.forceFinished(true);
-        cancelScrollingIndicatorAnimations();
-        mScrollingPaused = true;
     }
 
     /**
@@ -482,7 +483,6 @@
      * @see #pauseScrolling()
      */
     void resumeScrolling() {
-        mScrollingPaused = false;
     }
     /**
      * Sets the current page.
@@ -499,8 +499,6 @@
 
         mForceScreenScrolled = true;
         mCurrentPage = Math.max(0, Math.min(currentPage, getPageCount() - 1));
-        updateCurrentPageScroll();
-        updateScrollingIndicator();
         notifyPageSwitchListener();
         invalidate();
     }
@@ -509,6 +507,11 @@
         if (mPageSwitchListener != null) {
             mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
         }
+
+        // Update the page indicator (when we aren't reordering)
+        if (mPageIndicator != null && !isReordering(false)) {
+            mPageIndicator.setActiveMarker(getNextPage());
+        }
     }
     protected void pageBeginMoving() {
         if (!mIsPageMoving) {
@@ -750,8 +753,6 @@
             }
         }
 
-        updateScrollingIndicatorPosition();
-
         if (childCount > 0) {
             final int index = isLayoutRtl() ? 0 : childCount - 1;
             mMaxScrollX = getChildOffset(index) - getRelativeChildOffset(index);
@@ -814,9 +815,6 @@
     }
 
     protected void screenScrolled(int screenCenter) {
-        if (isScrollingIndicatorEnabled()) {
-            updateScrollingIndicator();
-        }
         boolean isInOverscroll = mOverScrollX < 0 || mOverScrollX > mMaxScrollX;
 
         if (mFadeInAdjacentScreens && !isInOverscroll) {
@@ -834,6 +832,12 @@
 
     @Override
     public void onChildViewAdded(View parent, View child) {
+        // Update the page indicator, we don't update the page indicator as we
+        // add/remove pages
+        if (mPageIndicator != null && !isReordering(false)) {
+            mPageIndicator.addMarker(indexOfChild(child));
+        }
+
         // This ensures that when children are added, they get the correct transforms / alphas
         // in accordance with any scroll effects.
         mForceScreenScrolled = true;
@@ -849,6 +853,46 @@
         invalidateCachedOffsets();
     }
 
+    private void removeMarkerForView(int index) {
+        // Update the page indicator, we don't update the page indicator as we
+        // add/remove pages
+        if (mPageIndicator != null && !isReordering(false)) {
+            mPageIndicator.removeMarker(index);
+        }
+    }
+
+    @Override
+    public void removeView(View v) {
+        // XXX: We should find a better way to hook into this before the view
+        // gets removed form its parent...
+        removeMarkerForView(indexOfChild(v));
+        super.removeView(v);
+    }
+    @Override
+    public void removeViewInLayout(View v) {
+        // XXX: We should find a better way to hook into this before the view
+        // gets removed form its parent...
+        removeMarkerForView(indexOfChild(v));
+        super.removeViewInLayout(v);
+    }
+    @Override
+    public void removeViewAt(int index) {
+        // XXX: We should find a better way to hook into this before the view
+        // gets removed form its parent...
+        removeViewAt(index);
+        super.removeViewAt(index);
+    }
+    @Override
+    public void removeAllViewsInLayout() {
+        // Update the page indicator, we don't update the page indicator as we
+        // add/remove pages
+        if (mPageIndicator != null) {
+            mPageIndicator.removeAllMarkers();
+        }
+
+        super.removeAllViewsInLayout();
+    }
+
     protected void invalidateCachedOffsets() {
         int count = getChildCount();
         if (count == 0) {
@@ -1605,6 +1649,7 @@
                                 addView(mDragView, pageUnderPointIndex);
                                 onAddView(mDragView, pageUnderPointIndex);
                                 mSidePageHoverIndex = -1;
+                                mPageIndicator.setActiveMarker(getNextPage());
                             }
                         };
                         postDelayed(mSidePageHoverRunnable, REORDERING_SIDE_PAGE_HOVER_TIMEOUT);
@@ -2172,153 +2217,6 @@
         }
     }
 
-    protected View getScrollingIndicator() {
-        // We use mHasScrollIndicator to prevent future lookups if there is no sibling indicator
-        // found
-        if (mHasScrollIndicator && mScrollIndicator == null) {
-            ViewGroup parent = (ViewGroup) getParent();
-            if (parent != null) {
-                mScrollIndicator = (View) (parent.findViewById(R.id.paged_view_indicator));
-                mHasScrollIndicator = mScrollIndicator != null;
-                if (mHasScrollIndicator) {
-                    mScrollIndicator.setVisibility(View.VISIBLE);
-                }
-            }
-        }
-        return mScrollIndicator;
-    }
-
-    protected boolean isScrollingIndicatorEnabled() {
-        return true;
-    }
-
-    Runnable hideScrollingIndicatorRunnable = new Runnable() {
-        @Override
-        public void run() {
-            hideScrollingIndicator(false);
-        }
-    };
-
-    protected void flashScrollingIndicator(boolean animated) {
-        removeCallbacks(hideScrollingIndicatorRunnable);
-        showScrollingIndicator(!animated);
-        postDelayed(hideScrollingIndicatorRunnable, sScrollIndicatorFlashDuration);
-    }
-
-    protected void showScrollingIndicator(boolean immediately) {
-        mShouldShowScrollIndicator = true;
-        mShouldShowScrollIndicatorImmediately = true;
-        if (getChildCount() <= 1) return;
-        if (!isScrollingIndicatorEnabled()) return;
-
-        mShouldShowScrollIndicator = false;
-        getScrollingIndicator();
-        if (mScrollIndicator != null) {
-            // Fade the indicator in
-            updateScrollingIndicatorPosition();
-            mScrollIndicator.setVisibility(View.VISIBLE);
-            cancelScrollingIndicatorAnimations();
-            if (immediately) {
-                mScrollIndicator.setAlpha(1f);
-            } else {
-                mScrollIndicatorAnimator = ObjectAnimator.ofFloat(mScrollIndicator, "alpha", 1f);
-                mScrollIndicatorAnimator.setDuration(sScrollIndicatorFadeInDuration);
-                mScrollIndicatorAnimator.start();
-            }
-        }
-    }
-
-    protected void cancelScrollingIndicatorAnimations() {
-        if (mScrollIndicatorAnimator != null) {
-            mScrollIndicatorAnimator.cancel();
-        }
-    }
-
-    protected void hideScrollingIndicator(boolean immediately) {
-        if (getChildCount() <= 1) return;
-        if (!isScrollingIndicatorEnabled()) return;
-
-        getScrollingIndicator();
-        if (mScrollIndicator != null) {
-            // Fade the indicator out
-            updateScrollingIndicatorPosition();
-            cancelScrollingIndicatorAnimations();
-            if (immediately) {
-                mScrollIndicator.setVisibility(View.INVISIBLE);
-                mScrollIndicator.setAlpha(0f);
-            } else {
-                mScrollIndicatorAnimator = ObjectAnimator.ofFloat(mScrollIndicator, "alpha", 0f);
-                mScrollIndicatorAnimator.setDuration(sScrollIndicatorFadeOutDuration);
-                mScrollIndicatorAnimator.addListener(new AnimatorListenerAdapter() {
-                    private boolean cancelled = false;
-                    @Override
-                    public void onAnimationCancel(android.animation.Animator animation) {
-                        cancelled = true;
-                    }
-                    @Override
-                    public void onAnimationEnd(Animator animation) {
-                        if (!cancelled) {
-                            mScrollIndicator.setVisibility(View.INVISIBLE);
-                        }
-                    }
-                });
-                mScrollIndicatorAnimator.start();
-            }
-        }
-    }
-
-    /**
-     * To be overridden by subclasses to determine whether the scroll indicator should stretch to
-     * fill its space on the track or not.
-     */
-    protected boolean hasElasticScrollIndicator() {
-        return true;
-    }
-
-    private void updateScrollingIndicator() {
-        if (getChildCount() <= 1) return;
-        if (!isScrollingIndicatorEnabled()) return;
-
-        getScrollingIndicator();
-        if (mScrollIndicator != null) {
-            updateScrollingIndicatorPosition();
-        }
-        if (mShouldShowScrollIndicator) {
-            showScrollingIndicator(mShouldShowScrollIndicatorImmediately);
-        }
-    }
-
-    private void updateScrollingIndicatorPosition() {
-        final boolean isRtl = isLayoutRtl();
-        if (!isScrollingIndicatorEnabled()) return;
-        if (mScrollIndicator == null) return;
-        int numPages = getChildCount();
-        int pageWidth = getViewportWidth();
-        int lastChildIndex = Math.max(0, getChildCount() - 1);
-        int maxScrollX = getChildOffset(lastChildIndex) - getRelativeChildOffset(lastChildIndex);
-        int trackWidth = pageWidth - mScrollIndicatorPaddingLeft - mScrollIndicatorPaddingRight;
-        int indicatorWidth = mScrollIndicator.getMeasuredWidth() -
-                mScrollIndicator.getPaddingLeft() - mScrollIndicator.getPaddingRight();
-
-        float scrollPos = isRtl ? mMaxScrollX - getScrollX() : getScrollX();
-        float offset = Math.max(0f, Math.min(1f, (float) scrollPos / mMaxScrollX));
-        if (isRtl) {
-            offset = 1f - offset;
-        }
-        int indicatorSpace = trackWidth / numPages;
-        int indicatorPos = (int) (offset * (trackWidth - indicatorSpace)) + mScrollIndicatorPaddingLeft;
-        if (hasElasticScrollIndicator()) {
-            if (mScrollIndicator.getMeasuredWidth() != indicatorSpace) {
-                mScrollIndicator.getLayoutParams().width = indicatorSpace;
-                mScrollIndicator.requestLayout();
-            }
-        } else {
-            int indicatorCenterOffset = indicatorSpace / 2 - indicatorWidth / 2;
-            indicatorPos += indicatorCenterOffset;
-        }
-        mScrollIndicator.setTranslationX(indicatorPos);
-    }
-
     // Animate the drag view back to the original position
     void animateDragViewToOriginalPosition() {
         if (mDragView != null) {