aboutsummaryrefslogtreecommitdiff
path: root/shadows/framework/src/main/java/org/robolectric/shadows/ShadowNativeAnimatedVectorDrawable.java
blob: 1564642687eaebcb04b9dad3af749a7f38be853b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package org.robolectric.shadows;

import static android.os.Build.VERSION_CODES.N;
import static android.os.Build.VERSION_CODES.N_MR1;
import static android.os.Build.VERSION_CODES.O;
import static org.robolectric.util.reflector.Reflector.reflector;

import android.graphics.drawable.AnimatedVectorDrawable;
import android.graphics.drawable.AnimatedVectorDrawable.VectorDrawableAnimatorRT;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import org.robolectric.annotation.RealObject;
import org.robolectric.nativeruntime.AnimatedVectorDrawableNatives;
import org.robolectric.nativeruntime.DefaultNativeRuntimeLoader;
import org.robolectric.shadows.ShadowNativeAnimatedVectorDrawable.Picker;
import org.robolectric.util.reflector.Direct;
import org.robolectric.util.reflector.ForType;
import org.robolectric.versioning.AndroidVersions.U;

/** Shadow for {@link AnimatedVectorDrawable} that is backed by native code */
@Implements(
    value = AnimatedVectorDrawable.class,
    minSdk = O,
    callNativeMethodsByDefault = true,
    shadowPicker = Picker.class)
public class ShadowNativeAnimatedVectorDrawable extends ShadowDrawable {

  @RealObject protected AnimatedVectorDrawable realAnimatedVectorDrawable;

  private boolean startInitiated;

  @Implementation
  protected void start() {
    reflector(AnimatedVectorDrawableReflector.class, realAnimatedVectorDrawable).start();
    startInitiated = true;
  }

  @Implementation
  protected void stop() {
    reflector(AnimatedVectorDrawableReflector.class, realAnimatedVectorDrawable).stop();
    startInitiated = false;
  }

  /**
   * Returns true if {@link #start()} was called and false if {@link #start()} was not called or
   * {@link #stop()} was called.
   */
  public final boolean isStartInitiated() {
    return startInitiated;
  }

  @Implementation(minSdk = N, maxSdk = U.SDK_INT)
  protected static long nCreateAnimatorSet() {
    DefaultNativeRuntimeLoader.injectAndLoad();
    return AnimatedVectorDrawableNatives.nCreateAnimatorSet();
  }

  @Implementation(minSdk = N_MR1, maxSdk = U.SDK_INT)
  protected static void nSetVectorDrawableTarget(long animatorPtr, long vectorDrawablePtr) {
    AnimatedVectorDrawableNatives.nSetVectorDrawableTarget(animatorPtr, vectorDrawablePtr);
  }

  @Implementation(minSdk = N_MR1, maxSdk = U.SDK_INT)
  protected static void nAddAnimator(
      long setPtr,
      long propertyValuesHolder,
      long nativeInterpolator,
      long startDelay,
      long duration,
      int repeatCount,
      int repeatMode) {
    AnimatedVectorDrawableNatives.nAddAnimator(
        setPtr,
        propertyValuesHolder,
        nativeInterpolator,
        startDelay,
        duration,
        repeatCount,
        repeatMode);
  }

  @Implementation(minSdk = N, maxSdk = U.SDK_INT)
  protected static void nSetPropertyHolderData(long nativePtr, float[] data, int length) {
    AnimatedVectorDrawableNatives.nSetPropertyHolderData(nativePtr, data, length);
  }

  @Implementation(minSdk = N_MR1, maxSdk = U.SDK_INT)
  protected static void nSetPropertyHolderData(long nativePtr, int[] data, int length) {
    AnimatedVectorDrawableNatives.nSetPropertyHolderData(nativePtr, data, length);
  }

  @Implementation(minSdk = N, maxSdk = U.SDK_INT)
  protected static void nStart(long animatorSetPtr, VectorDrawableAnimatorRT set, int id) {
    AnimatedVectorDrawableNatives.nStart(animatorSetPtr, set, id);
  }

  @Implementation(minSdk = N, maxSdk = U.SDK_INT)
  protected static void nReverse(long animatorSetPtr, VectorDrawableAnimatorRT set, int id) {
    AnimatedVectorDrawableNatives.nReverse(animatorSetPtr, set, id);
  }

  @Implementation(minSdk = N, maxSdk = U.SDK_INT)
  protected static long nCreateGroupPropertyHolder(
      long nativePtr, int propertyId, float startValue, float endValue) {
    return AnimatedVectorDrawableNatives.nCreateGroupPropertyHolder(
        nativePtr, propertyId, startValue, endValue);
  }

  @Implementation(minSdk = N, maxSdk = U.SDK_INT)
  protected static long nCreatePathDataPropertyHolder(
      long nativePtr, long startValuePtr, long endValuePtr) {
    return AnimatedVectorDrawableNatives.nCreatePathDataPropertyHolder(
        nativePtr, startValuePtr, endValuePtr);
  }

  @Implementation(minSdk = N, maxSdk = U.SDK_INT)
  protected static long nCreatePathColorPropertyHolder(
      long nativePtr, int propertyId, int startValue, int endValue) {
    return AnimatedVectorDrawableNatives.nCreatePathColorPropertyHolder(
        nativePtr, propertyId, startValue, endValue);
  }

  @Implementation(minSdk = N, maxSdk = U.SDK_INT)
  protected static long nCreatePathPropertyHolder(
      long nativePtr, int propertyId, float startValue, float endValue) {
    return AnimatedVectorDrawableNatives.nCreatePathPropertyHolder(
        nativePtr, propertyId, startValue, endValue);
  }

  @Implementation(minSdk = N, maxSdk = U.SDK_INT)
  protected static long nCreateRootAlphaPropertyHolder(
      long nativePtr, float startValue, float endValue) {
    return AnimatedVectorDrawableNatives.nCreateRootAlphaPropertyHolder(
        nativePtr, startValue, endValue);
  }

  @Implementation(minSdk = N, maxSdk = U.SDK_INT)
  protected static void nEnd(long animatorSetPtr) {
    AnimatedVectorDrawableNatives.nEnd(animatorSetPtr);
  }

  @Implementation(minSdk = N, maxSdk = U.SDK_INT)
  protected static void nReset(long animatorSetPtr) {
    AnimatedVectorDrawableNatives.nReset(animatorSetPtr);
  }

  /** Shadow picker for {@link AnimatedVectorDrawable}. */
  public static final class Picker extends GraphicsShadowPicker<Object> {
    public Picker() {
      super(null, ShadowNativeAnimatedVectorDrawable.class);
    }
  }

  /** Accessor interface for {@link AnimatedVectorDrawable} internals. */
  @ForType(AnimatedVectorDrawable.class)
  private interface AnimatedVectorDrawableReflector {
    @Direct
    void start();

    @Direct
    void stop();
  }
}