From 6904e77152df9365c627287013ce79e0b491da1f Mon Sep 17 00:00:00 2001 From: Sam Mortimer Date: Wed, 11 Oct 2017 09:39:12 -0700 Subject: [PATCH] lineage-sdk internal: Add StopMotionVectorDrawable from uicommon *) Move it here so that external/uicommon can be deprecated. Origial uicommon StopMotionVectorDrawable.java history: Author: Adrian DC Date: Fri Oct 28 10:55:25 2016 +0200 uicommon: Fix getAnimatorSetViaReflection UI usage * The receiver can be an instance of VectorDrawableAnimatorRT instead of VectorDrawableAnimatorUI since AOSP 7.x, therefore call the forceAnimationOnUI method to ensure mAnimatorSet is an instance of VectorDrawableAnimatorUI * Change introduced in "Support running AVD on UI thread", frameworks_base: I372ecd3dc52e3fa0bdce3a1e9c19443f9b199027 Change-Id: Ie00c86474f38600b4f10889e292b20a8f7e6531e Signed-off-by: Adrian DC Author: Steve Kondik Date: Wed Oct 12 17:02:17 2016 -0700 uicommon: Fix StopMotionVectorDrawable for N * The guts of AVD have changed in N, adjust hacks accordingly. Change-Id: Ia28c3099389252a3fd57373e02800fee9d467f64 Author: d34d Date: Wed May 4 08:59:27 2016 -0700 Add StopMotionVectorDrawable to uicommon The StopMotionVectorDrawable provides control over the position within an animation set of the warpped AnimatedVectorDrawable. Change-Id: I586c54c7dbd5c9f6f373a87e544bc0669f4ba8f1 Change-Id: I868a852e6c5f6f1ab7926deeb109fc68ad4449da --- .../drawable/StopMotionVectorDrawable.java | 102 ++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 sdk/src/java/org/lineageos/internal/graphics/drawable/StopMotionVectorDrawable.java diff --git a/sdk/src/java/org/lineageos/internal/graphics/drawable/StopMotionVectorDrawable.java b/sdk/src/java/org/lineageos/internal/graphics/drawable/StopMotionVectorDrawable.java new file mode 100644 index 00000000..952c86ad --- /dev/null +++ b/sdk/src/java/org/lineageos/internal/graphics/drawable/StopMotionVectorDrawable.java @@ -0,0 +1,102 @@ +/* + * Copyright (C) 2016 The CyanogenMod 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 org.lineageos.internal.graphics.drawable; + +import android.animation.Animator; +import android.animation.AnimatorSet; +import android.animation.ValueAnimator; +import android.graphics.drawable.AnimatedVectorDrawable; +import android.graphics.drawable.Drawable; +import android.graphics.drawable.DrawableWrapper; +import android.util.Log; + +import java.lang.reflect.Field; +import java.util.ArrayList; + +/** + * Wraps an {@link AnimatedVectorDrawable} and provides methods for setting the temporal position + * within the backing {@link AnimatorSet} of the wrapped {@link AnimatedVectorDrawable}. + */ +public class StopMotionVectorDrawable extends DrawableWrapper { + private static final String TAG = StopMotionVectorDrawable.class.getSimpleName(); + + private AnimatedVectorDrawable mDrawable; + private AnimatorSet mAnimatorSet; + + /** + * Creates a new wrapper around the specified drawable. + * + * @param dr The drawable to wrap. Must be an {@link AnimatedVectorDrawable} + */ + public StopMotionVectorDrawable(Drawable dr) { + super(dr); + setDrawable(dr); + } + + /** + * {@see DrawableWrapper$setDrawable} + * @param dr the wrapped drawable + * @throws IllegalArgumentException IF drawable is not an {@link AnimatedVectorDrawable} + */ + @Override + public void setDrawable(Drawable dr) { + if (dr != null && !(dr instanceof AnimatedVectorDrawable)) { + throw new IllegalArgumentException("Drawable must be an AnimatedVectorDrawable"); + } + + super.setDrawable(dr); + mDrawable = (AnimatedVectorDrawable) dr; + if (mDrawable != null) { + mDrawable.reset(); + getAnimatorSetViaReflection(); + } + } + + /** + * {@see android.animation.ValueAnimator#setCurrentFraction} + * @param fraction The fraction to which the animation is advanced or rewound. Values outside + * the range of 0 to the maximum fraction for the animator will be clamped to + * the correct range. + */ + public void setCurrentFraction(float fraction) { + if (mDrawable == null || mAnimatorSet == null) return; + + ArrayList animators = mAnimatorSet.getChildAnimations(); + for (Animator animator : animators) { + if (animator instanceof ValueAnimator) { + ((ValueAnimator) animator).setCurrentFraction(fraction); + } + } + + mDrawable.invalidateSelf(); + } + + private void getAnimatorSetViaReflection() { + try { + Field _mAnimatorSet = AnimatedVectorDrawable.class.getDeclaredField("mAnimatorSet"); + _mAnimatorSet.setAccessible(true); + Class innerClazz = Class.forName("android.graphics.drawable.AnimatedVectorDrawable$VectorDrawableAnimatorUI"); + mDrawable.forceAnimationOnUI(); + Object _inner = _mAnimatorSet.get(mDrawable); + Field _mSet = innerClazz.getDeclaredField("mSet"); + _mSet.setAccessible(true); + mAnimatorSet = (AnimatorSet) _mSet.get(_inner); + } catch (NoSuchFieldException | IllegalAccessException | ClassNotFoundException e) { + Log.e(TAG, "Could not get mAnimatorSet via reflection", e); + } + } +}