Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 6de55b88 authored by Leon Scroggins III's avatar Leon Scroggins III
Browse files

AID: Change loop to repeat and set from xml

Bug: 73529437
Test: I60fbee6d3610070413e8c46f363ec3c19c2e7c52

Allow setting AnimatedImageDrawable's loop count in xml. Use
"repeatCount" to match the existing attr used by other objects, and
change the Java api from "loop" to "repeat".

Cache the repeat count in the State object for the situation where we
inflate but are waiting on a theme, so that when we get a src we can
apply the existing repeat count.

Change-Id: Ie723fe9a40c64dd0e1e8fd7b5d5cf81e5c5ccd0d
parent c9aa8565
Loading
Loading
Loading
Loading
+3 −3
Original line number Original line Diff line number Diff line
@@ -14510,18 +14510,18 @@ package android.graphics.drawable {
    ctor public AnimatedImageDrawable();
    ctor public AnimatedImageDrawable();
    method public void clearAnimationCallbacks();
    method public void clearAnimationCallbacks();
    method public void draw(android.graphics.Canvas);
    method public void draw(android.graphics.Canvas);
    method public int getLoopCount();
    method public int getOpacity();
    method public int getOpacity();
    method public int getRepeatCount();
    method public final boolean isAutoMirrored();
    method public final boolean isAutoMirrored();
    method public boolean isRunning();
    method public boolean isRunning();
    method public void registerAnimationCallback(android.graphics.drawable.Animatable2.AnimationCallback);
    method public void registerAnimationCallback(android.graphics.drawable.Animatable2.AnimationCallback);
    method public void setAlpha(int);
    method public void setAlpha(int);
    method public void setColorFilter(android.graphics.ColorFilter);
    method public void setColorFilter(android.graphics.ColorFilter);
    method public void setLoopCount(int);
    method public void setRepeatCount(int);
    method public void start();
    method public void start();
    method public void stop();
    method public void stop();
    method public boolean unregisterAnimationCallback(android.graphics.drawable.Animatable2.AnimationCallback);
    method public boolean unregisterAnimationCallback(android.graphics.drawable.Animatable2.AnimationCallback);
    field public static final int LOOP_INFINITE = -1; // 0xffffffff
    field public static final int REPEAT_INFINITE = -1; // 0xffffffff
  }
  }
  public class AnimatedStateListDrawable extends android.graphics.drawable.StateListDrawable {
  public class AnimatedStateListDrawable extends android.graphics.drawable.StateListDrawable {
+10 −0
Original line number Original line Diff line number Diff line
@@ -173,6 +173,16 @@ package android.graphics {


}
}


package android.graphics.drawable {

  public class AnimatedImageDrawable extends android.graphics.drawable.Drawable implements android.graphics.drawable.Animatable2 {
    method public deprecated int getLoopCount(int);
    method public deprecated void setLoopCount(int);
    field public static final deprecated int LOOP_INFINITE = -1; // 0xffffffff
  }

}

package android.hardware {
package android.hardware {


  public final class HardwareBuffer implements java.lang.AutoCloseable android.os.Parcelable {
  public final class HardwareBuffer implements java.lang.AutoCloseable android.os.Parcelable {
+5 −5
Original line number Original line Diff line number Diff line
@@ -132,12 +132,12 @@ static jboolean AnimatedImageDrawable_nStop(JNIEnv* env, jobject /*clazz*/, jlon
// Java's LOOP_INFINITE relies on this being the same.
// Java's LOOP_INFINITE relies on this being the same.
static_assert(SkCodec::kRepetitionCountInfinite == -1);
static_assert(SkCodec::kRepetitionCountInfinite == -1);


static jint AnimatedImageDrawable_nGetLoopCount(JNIEnv* env, jobject /*clazz*/, jlong nativePtr) {
static jint AnimatedImageDrawable_nGetRepeatCount(JNIEnv* env, jobject /*clazz*/, jlong nativePtr) {
    auto* drawable = reinterpret_cast<AnimatedImageDrawable*>(nativePtr);
    auto* drawable = reinterpret_cast<AnimatedImageDrawable*>(nativePtr);
    return drawable->getRepetitionCount();
    return drawable->getRepetitionCount();
}
}


static void AnimatedImageDrawable_nSetLoopCount(JNIEnv* env, jobject /*clazz*/, jlong nativePtr,
static void AnimatedImageDrawable_nSetRepeatCount(JNIEnv* env, jobject /*clazz*/, jlong nativePtr,
                                                  jint loopCount) {
                                                  jint loopCount) {
    auto* drawable = reinterpret_cast<AnimatedImageDrawable*>(nativePtr);
    auto* drawable = reinterpret_cast<AnimatedImageDrawable*>(nativePtr);
    drawable->setRepetitionCount(loopCount);
    drawable->setRepetitionCount(loopCount);
@@ -223,8 +223,8 @@ static const JNINativeMethod gAnimatedImageDrawableMethods[] = {
    { "nIsRunning",          "(J)Z",                                                         (void*) AnimatedImageDrawable_nIsRunning },
    { "nIsRunning",          "(J)Z",                                                         (void*) AnimatedImageDrawable_nIsRunning },
    { "nStart",              "(J)Z",                                                         (void*) AnimatedImageDrawable_nStart },
    { "nStart",              "(J)Z",                                                         (void*) AnimatedImageDrawable_nStart },
    { "nStop",               "(J)Z",                                                         (void*) AnimatedImageDrawable_nStop },
    { "nStop",               "(J)Z",                                                         (void*) AnimatedImageDrawable_nStop },
    { "nGetLoopCount",       "(J)I",                                                         (void*) AnimatedImageDrawable_nGetLoopCount },
    { "nGetRepeatCount",     "(J)I",                                                         (void*) AnimatedImageDrawable_nGetRepeatCount },
    { "nSetLoopCount",       "(JI)V",                                                        (void*) AnimatedImageDrawable_nSetLoopCount },
    { "nSetRepeatCount",     "(JI)V",                                                        (void*) AnimatedImageDrawable_nSetRepeatCount },
    { "nSetOnAnimationEndListener", "(JLandroid/graphics/drawable/AnimatedImageDrawable;)V", (void*) AnimatedImageDrawable_nSetOnAnimationEndListener },
    { "nSetOnAnimationEndListener", "(JLandroid/graphics/drawable/AnimatedImageDrawable;)V", (void*) AnimatedImageDrawable_nSetOnAnimationEndListener },
    { "nNativeByteSize",     "(J)J",                                                         (void*) AnimatedImageDrawable_nNativeByteSize },
    { "nNativeByteSize",     "(J)J",                                                         (void*) AnimatedImageDrawable_nNativeByteSize },
    { "nMarkInvisible",      "(J)V",                                                         (void*) AnimatedImageDrawable_nMarkInvisible },
    { "nMarkInvisible",      "(J)V",                                                         (void*) AnimatedImageDrawable_nMarkInvisible },
+5 −0
Original line number Original line Diff line number Diff line
@@ -5954,6 +5954,11 @@
        <!-- Indicates if the drawable needs to be mirrored when its layout direction is
        <!-- Indicates if the drawable needs to be mirrored when its layout direction is
             RTL (right-to-left). -->
             RTL (right-to-left). -->
        <attr name="autoMirrored" />
        <attr name="autoMirrored" />
        <!-- Replace the loop count in the encoded data. A repeat count of 0 means that
             the animation will play once, regardless of the number of times specified
             in the encoded data. Setting this to infinite (-1) will result in the
             animation repeating as long as it is displayed (once start() is called). -->
        <attr name="repeatCount"/>
    </declare-styleable>
    </declare-styleable>


    <!-- Drawable used to draw bitmaps. -->
    <!-- Drawable used to draw bitmaps. -->
+82 −15
Original line number Original line Diff line number Diff line
@@ -77,6 +77,7 @@ public class AnimatedImageDrawable extends Drawable implements Animatable2 {


        int[] mThemeAttrs = null;
        int[] mThemeAttrs = null;
        boolean mAutoMirrored = false;
        boolean mAutoMirrored = false;
        int mRepeatCount = REPEAT_UNDEFINED;
    }
    }


    private State mState;
    private State mState;
@@ -86,33 +87,87 @@ public class AnimatedImageDrawable extends Drawable implements Animatable2 {
    private ColorFilter mColorFilter;
    private ColorFilter mColorFilter;


    /**
    /**
     *  Pass this to {@link #setLoopCount} to loop infinitely.
     *  Pass this to {@link #setRepeatCount} to repeat infinitely.
     *
     *
     *  <p>{@link Animatable2.AnimationCallback#onAnimationEnd} will never be
     *  <p>{@link Animatable2.AnimationCallback#onAnimationEnd} will never be
     *  called unless there is an error.</p>
     *  called unless there is an error.</p>
     */
     */
    public static final int LOOP_INFINITE = -1;
    public static final int REPEAT_INFINITE = -1;

    /** @removed
     * @deprecated Replaced with REPEAT_INFINITE to match other APIs.
     */
    @java.lang.Deprecated
    public static final int LOOP_INFINITE = REPEAT_INFINITE;

    private static final int REPEAT_UNDEFINED = -2;


    /**
    /**
     *  Specify the number of times to loop the animation.
     *  Specify the number of times to repeat the animation.
     *
     *
     *  <p>By default, the loop count in the encoded data is respected.</p>
     *  <p>By default, the repeat count in the encoded data is respected. If set
     *  to {@link #REPEAT_INFINITE}, the animation will repeat as long as it is
     *  displayed. If the value is {@code 0}, the animation will play once.</p>
     *
     *  <p>This call replaces the current repeat count. If the encoded data
     *  specified a repeat count of {@code 2} (meaning that
     *  {@link #getRepeatCount()} returns {@code 2}, the animation will play
     *  three times. Calling {@code setRepeatCount(1)} will result in playing only
     *  twice and {@link #getRepeatCount()} returning {@code 1}.</p>
     *
     *  <p>If the animation is already playing, the iterations that have already
     *  occurred count towards the new count. If the animation has already
     *  repeated the appropriate number of times (or more), it will finish its
     *  current iteration and then stop.</p>
     */
     */
    public void setLoopCount(int loopCount) {
    public void setRepeatCount(@IntRange(from = REPEAT_INFINITE) int repeatCount) {
        if (mState.mNativePtr == 0) {
        if (repeatCount < REPEAT_INFINITE) {
            throw new IllegalStateException("called setLoopCount on empty AnimatedImageDrawable");
            throw new IllegalArgumentException("invalid value passed to setRepeatCount"
                    + repeatCount);
        }
        if (mState.mRepeatCount != repeatCount) {
            mState.mRepeatCount = repeatCount;
            if (mState.mNativePtr != 0) {
                nSetRepeatCount(mState.mNativePtr, repeatCount);
            }
        }
        }
        nSetLoopCount(mState.mNativePtr, loopCount);
    }

    /** @removed
     * @deprecated Replaced with setRepeatCount to match other APIs.
     */
    @java.lang.Deprecated
    public void setLoopCount(int loopCount) {
        setRepeatCount(loopCount);
    }
    }


    /**
    /**
     * Retrieve the number of times the animation will loop.
     *  Retrieve the number of times the animation will repeat.
     *
     *  <p>By default, the repeat count in the encoded data is respected. If the
     *  value is {@link #REPEAT_INFINITE}, the animation will repeat as long as
     *  it is displayed. If the value is {@code 0}, it will play once.</p>
     *
     *  <p>Calling {@link #setRepeatCount} will make future calls to this method
     *  return the value passed to {@link #setRepeatCount}.</p>
     */
     */
    public int getLoopCount() {
    public int getRepeatCount() {
        if (mState.mNativePtr == 0) {
        if (mState.mNativePtr == 0) {
            throw new IllegalStateException("called getLoopCount on empty AnimatedImageDrawable");
            throw new IllegalStateException("called getRepeatCount on empty AnimatedImageDrawable");
        }
        }
        return nGetLoopCount(mState.mNativePtr);
        if (mState.mRepeatCount == REPEAT_UNDEFINED) {
            mState.mRepeatCount = nGetRepeatCount(mState.mNativePtr);

        }
        return mState.mRepeatCount;
    }

    /** @removed
     * @deprecated Replaced with getRepeatCount to match other APIs.
     */
    @java.lang.Deprecated
    public int getLoopCount(int loopCount) {
        return getRepeatCount();
    }
    }


    /**
    /**
@@ -176,12 +231,18 @@ public class AnimatedImageDrawable extends Drawable implements Animatable2 {
                        ": <animated-image> did not decode animated");
                        ": <animated-image> did not decode animated");
            }
            }


            // This may have previously been set without a src if we were waiting for a
            // theme.
            final int repeatCount = mState.mRepeatCount;
            // Transfer the state of other to this one. other will be discarded.
            // Transfer the state of other to this one. other will be discarded.
            AnimatedImageDrawable other = (AnimatedImageDrawable) drawable;
            AnimatedImageDrawable other = (AnimatedImageDrawable) drawable;
            mState = other.mState;
            mState = other.mState;
            other.mState = null;
            other.mState = null;
            mIntrinsicWidth =  other.mIntrinsicWidth;
            mIntrinsicWidth =  other.mIntrinsicWidth;
            mIntrinsicHeight = other.mIntrinsicHeight;
            mIntrinsicHeight = other.mIntrinsicHeight;
            if (repeatCount != REPEAT_UNDEFINED) {
                this.setRepeatCount(repeatCount);
            }
        }
        }


        mState.mThemeAttrs = a.extractThemeAttrs();
        mState.mThemeAttrs = a.extractThemeAttrs();
@@ -193,6 +254,12 @@ public class AnimatedImageDrawable extends Drawable implements Animatable2 {


        mState.mAutoMirrored = a.getBoolean(
        mState.mAutoMirrored = a.getBoolean(
                R.styleable.AnimatedImageDrawable_autoMirrored, oldState.mAutoMirrored);
                R.styleable.AnimatedImageDrawable_autoMirrored, oldState.mAutoMirrored);

        int repeatCount = a.getInt(
                R.styleable.AnimatedImageDrawable_repeatCount, REPEAT_UNDEFINED);
        if (repeatCount != REPEAT_UNDEFINED) {
            this.setRepeatCount(repeatCount);
        }
    }
    }


    /**
    /**
@@ -508,9 +575,9 @@ public class AnimatedImageDrawable extends Drawable implements Animatable2 {
    @FastNative
    @FastNative
    private static native boolean nStop(long nativePtr);
    private static native boolean nStop(long nativePtr);
    @FastNative
    @FastNative
    private static native int nGetLoopCount(long nativePtr);
    private static native int nGetRepeatCount(long nativePtr);
    @FastNative
    @FastNative
    private static native void nSetLoopCount(long nativePtr, int loopCount);
    private static native void nSetRepeatCount(long nativePtr, int repeatCount);
    // Pass the drawable down to native so it can call onAnimationEnd.
    // Pass the drawable down to native so it can call onAnimationEnd.
    private static native void nSetOnAnimationEndListener(long nativePtr,
    private static native void nSetOnAnimationEndListener(long nativePtr,
            @Nullable AnimatedImageDrawable drawable);
            @Nullable AnimatedImageDrawable drawable);