Pull in recent seek/progress bar changes.

Change-Id: I4fb8dba0dfa9aabe17542ed518d6f677b442080d
diff --git a/src/com/android/musicfx/seekbar/AbsSeekBar.java b/src/com/android/musicfx/seekbar/AbsSeekBar.java
index 026bcba..35e0e80 100644
--- a/src/com/android/musicfx/seekbar/AbsSeekBar.java
+++ b/src/com/android/musicfx/seekbar/AbsSeekBar.java
@@ -24,6 +24,7 @@
 import android.util.AttributeSet;
 import android.view.KeyEvent;
 import android.view.MotionEvent;
+import android.view.ViewConfiguration;
 
 public abstract class AbsSeekBar extends ProgressBar {
     private Drawable mThumb;
@@ -50,6 +51,10 @@
     private static final int NO_ALPHA = 0xFF;
     private float mDisabledAlpha;
     
+    private int mScaledTouchSlop;
+    private float mTouchDownX;
+    private boolean mIsDragging;
+
     public AbsSeekBar(Context context) {
         super(context);
     }
@@ -75,6 +80,8 @@
                 com.android.internal.R.styleable.Theme, 0, 0);
         mDisabledAlpha = a.getFloat(com.android.internal.R.styleable.Theme_disabledAlpha, 0.5f);
         a.recycle();
+
+        mScaledTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
     }
 
     /**
@@ -394,20 +401,48 @@
         
         switch (event.getAction()) {
             case MotionEvent.ACTION_DOWN:
-                setPressed(true);
-                onStartTrackingTouch();
-                trackTouchEvent(event);
+                if (isInScrollingContainer()) {
+                    mTouchDownX = event.getX();
+                } else {
+                    setPressed(true);
+                    if (mThumb != null) {
+                        invalidate(mThumb.getBounds()); // This may be within the padding region
+                    }
+                    onStartTrackingTouch();
+                    trackTouchEvent(event);
+                    attemptClaimDrag();
+                }
                 break;
                 
             case MotionEvent.ACTION_MOVE:
-                trackTouchEvent(event);
-                attemptClaimDrag();
+                if (mIsDragging) {
+                    trackTouchEvent(event);
+                } else {
+                    final float x = event.getX();
+                    if (Math.abs(x - mTouchDownX) > mScaledTouchSlop) {
+                        setPressed(true);
+                        if (mThumb != null) {
+                            invalidate(mThumb.getBounds()); // This may be within the padding region
+                        }
+                        onStartTrackingTouch();
+                        trackTouchEvent(event);
+                        attemptClaimDrag();
+                    }
+                }
                 break;
                 
             case MotionEvent.ACTION_UP:
-                trackTouchEvent(event);
-                onStopTrackingTouch();
-                setPressed(false);
+                if (mIsDragging) {
+                    trackTouchEvent(event);
+                    onStopTrackingTouch();
+                    setPressed(false);
+                } else {
+                    // Touch up when we never crossed the touch slop threshold should
+                    // be interpreted as a tap-seek to that location.
+                    onStartTrackingTouch();
+                    trackTouchEvent(event);
+                    onStopTrackingTouch();
+                }
                 // ProgressBar doesn't know to repaint the thumb drawable
                 // in its inactive state when the touch stops (because the
                 // value has not apparently changed)
@@ -415,8 +450,10 @@
                 break;
                 
             case MotionEvent.ACTION_CANCEL:
-                onStopTrackingTouch();
-                setPressed(false);
+                if (mIsDragging) {
+                    onStopTrackingTouch();
+                    setPressed(false);
+                }
                 invalidate(); // see above explanation
                 break;
         }
@@ -476,6 +513,7 @@
      * This is called when the user has started touching this widget.
      */
     void onStartTrackingTouch() {
+        mIsDragging = true;
     }
 
     /**
@@ -483,6 +521,7 @@
      * canceled.
      */
     void onStopTrackingTouch() {
+        mIsDragging = false;
     }
 
     /**
diff --git a/src/com/android/musicfx/seekbar/ProgressBar.java b/src/com/android/musicfx/seekbar/ProgressBar.java
index b31f65d..57d8da2 100644
--- a/src/com/android/musicfx/seekbar/ProgressBar.java
+++ b/src/com/android/musicfx/seekbar/ProgressBar.java
@@ -933,9 +933,33 @@
         // onDraw will translate the canvas so we draw starting at 0,0
         int right = w - mPaddingRight - mPaddingLeft;
         int bottom = h - mPaddingBottom - mPaddingTop;
+        int top = 0;
+        int left = 0;
 
         if (mIndeterminateDrawable != null) {
-            mIndeterminateDrawable.setBounds(0, 0, right, bottom);
+            // Aspect ratio logic does not apply to AnimationDrawables
+            if (mOnlyIndeterminate && !(mIndeterminateDrawable instanceof AnimationDrawable)) {
+                // Maintain aspect ratio. Certain kinds of animated drawables
+                // get very confused otherwise.
+                final int intrinsicWidth = mIndeterminateDrawable.getIntrinsicWidth();
+                final int intrinsicHeight = mIndeterminateDrawable.getIntrinsicHeight();
+                final float intrinsicAspect = (float) intrinsicWidth / intrinsicHeight;
+                final float boundAspect = (float) w / h;
+                if (intrinsicAspect != boundAspect) {
+                    if (boundAspect > intrinsicAspect) {
+                        // New width is larger. Make it smaller to match height.
+                        final int width = (int) (h * intrinsicAspect);
+                        left = (w - width) / 2;
+                        right = left + width;
+                    } else {
+                        // New height is larger. Make it smaller to match width.
+                        final int height = (int) (w * (1 / intrinsicAspect));
+                        top = (h - height) / 2;
+                        bottom = top + height;
+                    }
+                }
+            }
+            mIndeterminateDrawable.setBounds(left, top, right, bottom);
         }
         
         if (mProgressDrawable != null) {
diff --git a/src/com/android/musicfx/seekbar/SeekBar.java b/src/com/android/musicfx/seekbar/SeekBar.java
index 24c4b61..2be99ce 100644
--- a/src/com/android/musicfx/seekbar/SeekBar.java
+++ b/src/com/android/musicfx/seekbar/SeekBar.java
@@ -104,6 +104,7 @@
     
     @Override
     void onStartTrackingTouch() {
+        super.onStartTrackingTouch();
         if (mOnSeekBarChangeListener != null) {
             mOnSeekBarChangeListener.onStartTrackingTouch(this);
         }
@@ -111,6 +112,7 @@
     
     @Override
     void onStopTrackingTouch() {
+        super.onStopTrackingTouch();
         if (mOnSeekBarChangeListener != null) {
             mOnSeekBarChangeListener.onStopTrackingTouch(this);
         }