1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package androidx.vectordrawable.graphics.drawable;
18 
19 import static android.os.Build.VERSION_CODES.M;
20 
21 import android.graphics.drawable.Animatable;
22 import android.graphics.drawable.Animatable2;
23 import android.graphics.drawable.Drawable;
24 
25 import androidx.annotation.NonNull;
26 import androidx.annotation.RequiresApi;
27 
28 /**
29  * Interface that drawables supporting animations and callbacks should extend in support lib.
30  */
31 public interface Animatable2Compat extends Animatable {
32 
33     /**
34      * Adds a callback to listen to the animation events.
35      *
36      * @param callback Callback to add.
37      */
registerAnimationCallback(@onNull AnimationCallback callback)38     void registerAnimationCallback(@NonNull AnimationCallback callback);
39 
40     /**
41      * Removes the specified animation callback.
42      *
43      * @param callback Callback to remove.
44      * @return {@code false} if callback didn't exist in the call back list, or {@code true} if
45      *         callback has been removed successfully.
46      */
unregisterAnimationCallback(@onNull AnimationCallback callback)47     boolean unregisterAnimationCallback(@NonNull AnimationCallback callback);
48 
49     /**
50      * Removes all existing animation callbacks.
51      */
clearAnimationCallbacks()52     void clearAnimationCallbacks();
53 
54     /**
55      * Abstract class for animation callback. Used to notify animation events.
56      */
57     abstract class AnimationCallback {
58         /**
59          * Called when the animation starts.
60          *
61          * @param drawable The drawable started the animation.
62          */
onAnimationStart(Drawable drawable)63         public void onAnimationStart(Drawable drawable) {};
64         /**
65          * Called when the animation ends.
66          *
67          * @param drawable The drawable finished the animation.
68          */
onAnimationEnd(Drawable drawable)69         public void onAnimationEnd(Drawable drawable) {};
70 
71         // Only when passing this Animatable2Compat.AnimationCallback to a frameworks' AVD, we need
72         // to bridge this compat version callback with the frameworks' callback.
73         Animatable2.AnimationCallback mPlatformCallback;
74 
75         @RequiresApi(M)
getPlatformCallback()76         Animatable2.AnimationCallback getPlatformCallback() {
77             if (mPlatformCallback == null) {
78                 mPlatformCallback = new Animatable2.AnimationCallback() {
79                     @Override
80                     public void onAnimationStart(Drawable drawable) {
81                         AnimationCallback.this.onAnimationStart(drawable);
82                     }
83 
84                     @Override
85                     public void onAnimationEnd(Drawable drawable) {
86                         AnimationCallback.this.onAnimationEnd(drawable);
87                     }
88                 };
89             }
90             return mPlatformCallback;
91         }
92     }
93 }
94