Integrate AVDC into AppCompat

BUG: 26638431

Change-Id: I94f5ca63099c1118575ac0b3bf9faabb2a795757
diff --git a/graphics/drawable/animated/src/android/support/graphics/drawable/AnimatedVectorDrawableCompat.java b/graphics/drawable/animated/src/android/support/graphics/drawable/AnimatedVectorDrawableCompat.java
index ddb16e7..b054b33 100644
--- a/graphics/drawable/animated/src/android/support/graphics/drawable/AnimatedVectorDrawableCompat.java
+++ b/graphics/drawable/animated/src/android/support/graphics/drawable/AnimatedVectorDrawableCompat.java
@@ -201,11 +201,8 @@
             if (type != XmlPullParser.START_TAG) {
                 throw new XmlPullParserException("No start tag found");
             }
-
-            final AnimatedVectorDrawableCompat drawable = new AnimatedVectorDrawableCompat(context);
-            drawable.inflate(resources, parser, attrs, context.getTheme());
-
-            return drawable;
+            return createFromXmlInner(context, context.getResources(), parser, attrs,
+                    context.getTheme());
         } catch (XmlPullParserException e) {
             Log.e(LOGTAG, "parser error", e);
         } catch (IOException e) {
@@ -215,6 +212,20 @@
     }
 
     /**
+     * Create a AnimatedVectorDrawableCompat from inside an XML document using an optional
+     * {@link Theme}. Called on a parser positioned at a tag in an XML
+     * document, tries to create a Drawable from that tag. Returns {@code null}
+     * if the tag is not a valid drawable.
+     */
+    public static AnimatedVectorDrawableCompat createFromXmlInner(Context context, Resources r,
+            XmlPullParser parser, AttributeSet attrs, Theme theme)
+            throws XmlPullParserException, IOException {
+        final AnimatedVectorDrawableCompat drawable = new AnimatedVectorDrawableCompat(context);
+        drawable.inflate(r, parser, attrs, theme);
+        return drawable;
+    }
+
+    /**
      * {@inheritDoc}
      * <strong>Note</strong> that we don't support constant state when SDK < 21.
      * Make sure you check the return value before using it.
diff --git a/v7/appcompat/Android.mk b/v7/appcompat/Android.mk
index 0eb6a7b..49f3ba2 100644
--- a/v7/appcompat/Android.mk
+++ b/v7/appcompat/Android.mk
@@ -23,7 +23,8 @@
 LOCAL_SDK_VERSION := current
 LOCAL_SRC_FILES := $(call all-java-files-under,src)
 LOCAL_RESOURCE_DIR := $(LOCAL_PATH)/res
-LOCAL_STATIC_JAVA_LIBRARIES := android-support-vectordrawable
+LOCAL_STATIC_JAVA_LIBRARIES := android-support-vectordrawable \
+        android-support-animatedvectordrawable
 LOCAL_JAVA_LIBRARIES := android-support-v4
 LOCAL_AAPT_FLAGS += --auto-add-overlay \
         --no-version-vectors
diff --git a/v7/appcompat/build.gradle b/v7/appcompat/build.gradle
index 7687733..98d5f82 100644
--- a/v7/appcompat/build.gradle
+++ b/v7/appcompat/build.gradle
@@ -5,6 +5,8 @@
 dependencies {
     compile project(':support-v4')
     compile project(':support-vector-drawable')
+    compile project(':support-animated-vector-drawable')
+
     androidTestCompile ('com.android.support.test:runner:0.4.1') {
         exclude module: 'support-annotations'
     }
diff --git a/v7/appcompat/src/android/support/v7/widget/AppCompatDrawableManager.java b/v7/appcompat/src/android/support/v7/widget/AppCompatDrawableManager.java
index 6b38482..1c7dd75 100644
--- a/v7/appcompat/src/android/support/v7/widget/AppCompatDrawableManager.java
+++ b/v7/appcompat/src/android/support/v7/widget/AppCompatDrawableManager.java
@@ -36,6 +36,7 @@
 import android.support.annotation.DrawableRes;
 import android.support.annotation.NonNull;
 import android.support.annotation.Nullable;
+import android.support.graphics.drawable.AnimatedVectorDrawableCompat;
 import android.support.graphics.drawable.VectorDrawableCompat;
 import android.support.v4.content.ContextCompat;
 import android.support.v4.graphics.ColorUtils;
@@ -63,7 +64,7 @@
 public final class AppCompatDrawableManager {
 
     private interface InflateDelegate {
-        Drawable createFromXmlInner(@NonNull Resources r, @NonNull XmlPullParser parser,
+        Drawable createFromXmlInner(@NonNull Context context, @NonNull XmlPullParser parser,
                 @NonNull AttributeSet attrs, @Nullable Resources.Theme theme);
     }
 
@@ -77,16 +78,25 @@
     public static AppCompatDrawableManager get() {
         if (INSTANCE == null) {
             INSTANCE = new AppCompatDrawableManager();
-
-            if (Build.VERSION.SDK_INT < 21) {
-                // We only want to use the automatic VectorDrawableCompat handling where it's
-                // needed: on devices running before Lollipop
-                INSTANCE.addDelegate("vector", new VdcInflateDelegate());
-            }
+            installDefaultInflateDelegates(INSTANCE);
         }
         return INSTANCE;
     }
 
+    private static void installDefaultInflateDelegates(@NonNull AppCompatDrawableManager manager) {
+        final int sdk = Build.VERSION.SDK_INT;
+        if (sdk < 21) {
+            // We only want to use the automatic VectorDrawableCompat handling where it's
+            // needed: on devices running before Lollipop
+            manager.addDelegate("vector", new VdcInflateDelegate());
+
+            if (sdk >= 11) {
+                // AnimatedVectorDrawableCompat only works on API v11+
+                manager.addDelegate("animated-vector", new AvdcInflateDelegate());
+            }
+        }
+    }
+
     private static final ColorFilterLruCache COLOR_FILTER_CACHE = new ColorFilterLruCache(6);
 
     /**
@@ -290,7 +300,8 @@
                     // Now try and find a delegate for the tag name and inflate if found
                     final InflateDelegate delegate = mDelegates.get(tagName);
                     if (delegate != null) {
-                        dr = delegate.createFromXmlInner(res, parser, attrs, context.getTheme());
+                        dr = delegate.createFromXmlInner(context, parser, attrs,
+                                context.getTheme());
                     }
                     if (dr != null) {
                         // Add it to the drawable cache
@@ -812,14 +823,29 @@
 
     private static class VdcInflateDelegate implements InflateDelegate {
         @Override
-        public Drawable createFromXmlInner(@NonNull Resources r, @NonNull XmlPullParser parser,
+        public Drawable createFromXmlInner(@NonNull Context context, @NonNull XmlPullParser parser,
                 @NonNull AttributeSet attrs, @Nullable Resources.Theme theme) {
             try {
-                return VectorDrawableCompat.createFromXmlInner(r, parser, attrs, theme);
+                return VectorDrawableCompat
+                        .createFromXmlInner(context.getResources(), parser, attrs, theme);
             } catch (Exception e) {
                 Log.e("VdcInflateDelegate", "Exception while inflating <vector>", e);
                 return null;
             }
         }
     }
+
+    private static class AvdcInflateDelegate implements InflateDelegate {
+        @Override
+        public Drawable createFromXmlInner(@NonNull Context context, @NonNull XmlPullParser parser,
+                @NonNull AttributeSet attrs, @Nullable Resources.Theme theme) {
+            try {
+                return AnimatedVectorDrawableCompat
+                        .createFromXmlInner(context, context.getResources(), parser, attrs, theme);
+            } catch (Exception e) {
+                Log.e("AvdcInflateDelegate", "Exception while inflating <animated-vector>", e);
+                return null;
+            }
+        }
+    }
 }