@Override
 public void onAnimationEnd(Animation animation) {
   if (mRefreshing) {
     // Make sure the progress view is fully visible
     mProgress.setAlpha(MAX_ALPHA);
     mProgress.start();
     if (mNotify) {
       if (mListener != null) {
         mListener.onRefresh();
       }
     }
   } else {
     mProgress.stop();
     mCircleView.setVisibility(View.GONE);
     setColorViewAlpha(MAX_ALPHA);
     // Return the circle to its start position
     if (mScale) {
       setAnimationProgress(0 /* animation complete and view is hidden */);
     } else {
       setTargetOffsetTopAndBottom(
           mOriginalOffsetTop - mCurrentTargetOffsetTop, true /* requires update */);
     }
   }
   mCurrentTargetOffsetTop = mCircleView.getTop();
 }
  @Override
  public boolean onInterceptTouchEvent(MotionEvent ev) {
    ensureTarget();

    final int action = MotionEventCompat.getActionMasked(ev);

    if (mReturningToStart && action == MotionEvent.ACTION_DOWN) {
      mReturningToStart = false;
    }

    if (!isEnabled() || mReturningToStart || canChildScrollUp() || mRefreshing) {
      // Fail fast if we're not in a state where a swipe is possible
      return false;
    }

    switch (action) {
      case MotionEvent.ACTION_DOWN:
        setTargetOffsetTopAndBottom(mOriginalOffsetTop - mCircleView.getTop(), true);
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        mIsBeingDragged = false;
        final float initialDownY = getMotionEventY(ev, mActivePointerId);
        if (initialDownY == -1) {
          return false;
        }
        mInitialDownY = initialDownY;
        break;

      case MotionEvent.ACTION_MOVE:
        if (mActivePointerId == INVALID_POINTER) {
          Log.e(LOG_TAG, "Got ACTION_MOVE event but don't have an active pointer id.");
          return false;
        }

        final float y = getMotionEventY(ev, mActivePointerId);
        if (y == -1) {
          return false;
        }
        final float yDiff = y - mInitialDownY;
        if (yDiff > mTouchSlop && !mIsBeingDragged) {
          mInitialMotionY = mInitialDownY + mTouchSlop;
          mIsBeingDragged = true;
          mProgress.setAlpha(STARTING_PROGRESS_ALPHA);
        }
        break;

      case MotionEventCompat.ACTION_POINTER_UP:
        onSecondaryPointerUp(ev);
        break;

      case MotionEvent.ACTION_UP:
      case MotionEvent.ACTION_CANCEL:
        mIsBeingDragged = false;
        mActivePointerId = INVALID_POINTER;
        break;
    }

    return mIsBeingDragged;
  }
 private void startScaleUpAnimation(AnimationListener listener) {
   mCircleView.setVisibility(View.VISIBLE);
   if (android.os.Build.VERSION.SDK_INT >= 11) {
     // Pre API 11, alpha is used in place of scale up to show the
     // progress circle appearing.
     // Don't adjust the alpha during appearance otherwise.
     mProgress.setAlpha(MAX_ALPHA);
   }
   mScaleAnimation =
       new Animation() {
         @Override
         public void applyTransformation(float interpolatedTime, Transformation t) {
           setAnimationProgress(interpolatedTime);
         }
       };
   mScaleAnimation.setDuration(mMediumAnimationDuration);
   if (listener != null) {
     mCircleView.setAnimationListener(listener);
   }
   mCircleView.clearAnimation();
   mCircleView.startAnimation(mScaleAnimation);
 }
 private void setColorViewAlpha(int targetAlpha) {
   mCircleView.getBackground().setAlpha(targetAlpha);
   mProgress.setAlpha(targetAlpha);
 }