Merge "Add PathInterpolator to interpolate along a Path."
diff --git a/api/current.txt b/api/current.txt
index 6a869ab..54352b0 100644
--- a/api/current.txt
+++ b/api/current.txt
@@ -400,6 +400,10 @@
     field public static final int content = 16843355; // 0x101025b
     field public static final int contentAuthority = 16843408; // 0x1010290
     field public static final int contentDescription = 16843379; // 0x1010273
+    field public static final int controlX1 = 16843770; // 0x10103fa
+    field public static final int controlX2 = 16843772; // 0x10103fc
+    field public static final int controlY1 = 16843771; // 0x10103fb
+    field public static final int controlY2 = 16843773; // 0x10103fd
     field public static final int cropToPadding = 16843043; // 0x1010123
     field public static final int cursorVisible = 16843090; // 0x1010152
     field public static final int customNavigationLayout = 16843474; // 0x10102d2
@@ -30469,6 +30473,14 @@
     method public float getInterpolation(float);
   }
 
+  public class PathInterpolator implements android.view.animation.Interpolator {
+    ctor public PathInterpolator(android.graphics.Path);
+    ctor public PathInterpolator(float, float);
+    ctor public PathInterpolator(float, float, float, float);
+    ctor public PathInterpolator(android.content.Context, android.util.AttributeSet);
+    method public float getInterpolation(float);
+  }
+
   public class RotateAnimation extends android.view.animation.Animation {
     ctor public RotateAnimation(android.content.Context, android.util.AttributeSet);
     ctor public RotateAnimation(float, float);
@@ -45662,12 +45674,12 @@
   public final class Matcher implements java.util.regex.MatchResult {
     method public java.util.regex.Matcher appendReplacement(java.lang.StringBuffer, java.lang.String);
     method public java.lang.StringBuffer appendTail(java.lang.StringBuffer);
-    method public int end(int);
     method public int end();
+    method public int end(int);
     method public boolean find(int);
     method public boolean find();
-    method public java.lang.String group(int);
     method public java.lang.String group();
+    method public java.lang.String group(int);
     method public int groupCount();
     method public boolean hasAnchoringBounds();
     method public boolean hasTransparentBounds();
@@ -45684,8 +45696,8 @@
     method public boolean requireEnd();
     method public java.util.regex.Matcher reset();
     method public java.util.regex.Matcher reset(java.lang.CharSequence);
-    method public int start(int) throws java.lang.IllegalStateException;
     method public int start();
+    method public int start(int) throws java.lang.IllegalStateException;
     method public java.util.regex.MatchResult toMatchResult();
     method public java.util.regex.Matcher useAnchoringBounds(boolean);
     method public java.util.regex.Matcher usePattern(java.util.regex.Pattern);
diff --git a/core/java/android/view/animation/AnimationUtils.java b/core/java/android/view/animation/AnimationUtils.java
index 38043b2..1d1fa1e 100644
--- a/core/java/android/view/animation/AnimationUtils.java
+++ b/core/java/android/view/animation/AnimationUtils.java
@@ -324,6 +324,8 @@
                 interpolator = new AnticipateOvershootInterpolator(c, attrs);
             } else if (name.equals("bounceInterpolator")) {
                 interpolator = new BounceInterpolator(c, attrs);
+            } else if (name.equals("pathInterpolator")) {
+                interpolator = new PathInterpolator(c, attrs);
             } else {
                 throw new RuntimeException("Unknown interpolator name: " + parser.getName());
             }
diff --git a/core/java/android/view/animation/PathInterpolator.java b/core/java/android/view/animation/PathInterpolator.java
new file mode 100644
index 0000000..a369509
--- /dev/null
+++ b/core/java/android/view/animation/PathInterpolator.java
@@ -0,0 +1,203 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package android.view.animation;
+
+import android.content.Context;
+import android.content.res.TypedArray;
+import android.graphics.Path;
+import android.util.AttributeSet;
+import android.view.InflateException;
+
+/**
+ * An interpolator that can traverse a Path that extends from <code>Point</code>
+ * <code>(0, 0)</code> to <code>(1, 1)</code>. The x coordinate along the <code>Path</code>
+ * is the input value and the output is the y coordinate of the line at that point.
+ * This means that the Path must conform to a function <code>y = f(x)</code>.
+ *
+ * <p>The <code>Path</code> must not have gaps in the x direction and must not
+ * loop back on itself such that there can be two points sharing the same x coordinate.
+ * It is alright to have a disjoint line in the vertical direction:</p>
+ * <p><blockquote><pre>
+ *     Path path = new Path();
+ *     path.lineTo(0.25f, 0.25f);
+ *     path.moveTo(0.25f, 0.5f);
+ *     path.lineTo(1f, 1f);
+ * </pre></blockquote></p>
+ */
+public class PathInterpolator implements Interpolator {
+
+    // This governs how accurate the approximation of the Path is.
+    private static final float PRECISION = 0.002f;
+
+    private float[] mX; // x coordinates in the line
+
+    private float[] mY; // y coordinates in the line
+
+    /**
+     * Create an interpolator for an arbitrary <code>Path</code>. The <code>Path</code>
+     * must begin at <code>(0, 0)</code> and end at <code>(1, 1)</code>.
+     *
+     * @param path The <code>Path</code> to use to make the line representing the interpolator.
+     */
+    public PathInterpolator(Path path) {
+        initPath(path);
+    }
+
+    /**
+     * Create an interpolator for a quadratic Bezier curve. The end points
+     * <code>(0, 0)</code> and <code>(1, 1)</code> are assumed.
+     *
+     * @param controlX The x coordinate of the quadratic Bezier control point.
+     * @param controlY The y coordinate of the quadratic Bezier control point.
+     */
+    public PathInterpolator(float controlX, float controlY) {
+        initQuad(controlX, controlY);
+    }
+
+    /**
+     * Create an interpolator for a cubic Bezier curve.  The end points
+     * <code>(0, 0)</code> and <code>(1, 1)</code> are assumed.
+     *
+     * @param controlX1 The x coordinate of the first control point of the cubic Bezier.
+     * @param controlY1 The y coordinate of the first control point of the cubic Bezier.
+     * @param controlX2 The x coordinate of the second control point of the cubic Bezier.
+     * @param controlY2 The y coordinate of the second control point of the cubic Bezier.
+     */
+    public PathInterpolator(float controlX1, float controlY1, float controlX2, float controlY2) {
+        initCubic(controlX1, controlY1, controlX2, controlY2);
+    }
+
+    public PathInterpolator(Context context, AttributeSet attrs) {
+        TypedArray a = context.obtainStyledAttributes(attrs,
+                com.android.internal.R.styleable.PathInterpolator);
+        if (!a.hasValue(com.android.internal.R.styleable.PathInterpolator_controlX1)) {
+            throw new InflateException("pathInterpolator requires the controlX1 attribute");
+        } else if (!a.hasValue(com.android.internal.R.styleable.PathInterpolator_controlY1)) {
+            throw new InflateException("pathInterpolator requires the controlY1 attribute");
+        }
+        float x1 = a.getFloat(com.android.internal.R.styleable.PathInterpolator_controlX1, 0);
+        float y1 = a.getFloat(com.android.internal.R.styleable.PathInterpolator_controlY1, 0);
+
+        boolean hasX2 = a.hasValue(com.android.internal.R.styleable.PathInterpolator_controlX2);
+        boolean hasY2 = a.hasValue(com.android.internal.R.styleable.PathInterpolator_controlY2);
+
+        if (hasX2 != hasY2) {
+            throw new InflateException(
+                    "pathInterpolator requires both controlX2 and controlY2 for cubic Beziers.");
+        }
+
+        if (!hasX2) {
+            initQuad(x1, y1);
+        } else {
+            float x2 = a.getFloat(com.android.internal.R.styleable.PathInterpolator_controlX2, 0);
+            float y2 = a.getFloat(com.android.internal.R.styleable.PathInterpolator_controlY2, 0);
+            initCubic(x1, y1, x2, y2);
+        }
+
+        a.recycle();
+    }
+
+    private void initQuad(float controlX, float controlY) {
+        Path path = new Path();
+        path.moveTo(0, 0);
+        path.quadTo(controlX, controlY, 1f, 1f);
+        initPath(path);
+    }
+
+    private void initCubic(float x1, float y1, float x2, float y2) {
+        Path path = new Path();
+        path.moveTo(0, 0);
+        path.cubicTo(x1, y1, x2, y2, 1f, 1f);
+        initPath(path);
+    }
+
+    private void initPath(Path path) {
+        float[] pointComponents = path.approximate(PRECISION);
+
+        int numPoints = pointComponents.length / 3;
+        if (pointComponents[1] != 0 || pointComponents[2] != 0
+                || pointComponents[pointComponents.length - 2] != 1
+                || pointComponents[pointComponents.length - 1] != 1) {
+            throw new IllegalArgumentException("The Path must start at (0,0) and end at (1,1)");
+        }
+
+        mX = new float[numPoints];
+        mY = new float[numPoints];
+        float prevX = 0;
+        float prevFraction = 0;
+        int componentIndex = 0;
+        for (int i = 0; i < numPoints; i++) {
+            float fraction = pointComponents[componentIndex++];
+            float x = pointComponents[componentIndex++];
+            float y = pointComponents[componentIndex++];
+            if (fraction == prevFraction && x != prevX) {
+                throw new IllegalArgumentException(
+                        "The Path cannot have discontinuity in the X axis.");
+            }
+            if (x < prevX) {
+                throw new IllegalArgumentException("The Path cannot loop back on itself.");
+            }
+            mX[i] = x;
+            mY[i] = y;
+            prevX = x;
+            prevFraction = fraction;
+        }
+    }
+
+    /**
+     * Using the line in the Path in this interpolator that can be described as
+     * <code>y = f(x)</code>, finds the y coordinate of the line given <code>t</code>
+     * as the x coordinate. Values less than 0 will always return 0 and values greater
+     * than 1 will always return 1.
+     *
+     * @param t Treated as the x coordinate along the line.
+     * @return The y coordinate of the Path along the line where x = <code>t</code>.
+     * @see Interpolator#getInterpolation(float)
+     */
+    @Override
+    public float getInterpolation(float t) {
+        if (t <= 0) {
+            return 0;
+        } else if (t >= 1) {
+            return 1;
+        }
+        // Do a binary search for the correct x to interpolate between.
+        int startIndex = 0;
+        int endIndex = mX.length - 1;
+
+        while (endIndex - startIndex > 1) {
+            int midIndex = (startIndex + endIndex) / 2;
+            if (t < mX[midIndex]) {
+                endIndex = midIndex;
+            } else {
+                startIndex = midIndex;
+            }
+        }
+
+        float xRange = mX[endIndex] - mX[startIndex];
+        if (xRange == 0) {
+            return mY[startIndex];
+        }
+
+        float tInRange = t - mX[startIndex];
+        float fraction = tInRange / xRange;
+
+        float startY = mY[startIndex];
+        float endY = mY[endIndex];
+        return startY + (fraction * (endY - startY));
+    }
+
+}
diff --git a/core/res/res/values/attrs.xml b/core/res/res/values/attrs.xml
index 6af708d..ca9c8bd3 100644
--- a/core/res/res/values/attrs.xml
+++ b/core/res/res/values/attrs.xml
@@ -4651,6 +4651,13 @@
         <attr name="extraTension" format="float" />
     </declare-styleable>
 
+    <declare-styleable name="PathInterpolator">
+        <attr name="controlX1" format="float" />
+        <attr name="controlY1" format="float" />
+        <attr name="controlX2" format="float" />
+        <attr name="controlY2" format="float" />
+    </declare-styleable>
+
     <!-- ========================== -->
     <!-- Transition attributes -->
     <!-- ========================== -->
diff --git a/core/res/res/values/public.xml b/core/res/res/values/public.xml
index 94298aa..e99b5ba 100644
--- a/core/res/res/values/public.xml
+++ b/core/res/res/values/public.xml
@@ -2100,6 +2100,10 @@
   <public type="attr" name="colorFilterColor" />
   <public type="attr" name="colorFilterMode" />
   <public type="attr" name="isolatedZVolume" />
+  <public type="attr" name="controlX1" />
+  <public type="attr" name="controlY1" />
+  <public type="attr" name="controlX2" />
+  <public type="attr" name="controlY2" />
 
   <public type="style" name="Widget.Holo.FragmentBreadCrumbs" />
   <public type="style" name="Widget.Holo.Light.FragmentBreadCrumbs" />