1 /*
2  * Copyright (C) 2019 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 com.android.settings.widget;
18 
19 import android.content.Context;
20 import android.graphics.drawable.Drawable;
21 import android.view.TextureView;
22 import android.view.View;
23 
24 import androidx.vectordrawable.graphics.drawable.Animatable2Compat;
25 import androidx.vectordrawable.graphics.drawable.AnimatedVectorDrawableCompat;
26 
27 /**
28  * A {@link VideoPreference.AnimationController} containing a {@link
29  * AnimatedVectorDrawableCompat}. The controller is used by {@link VideoPreference}
30  * to display AnimatedVectorDrawable content.
31  */
32 class VectorAnimationController implements VideoPreference.AnimationController {
33     private AnimatedVectorDrawableCompat mAnimatedVectorDrawableCompat;
34     private Drawable mPreviewDrawable;
35     private Animatable2Compat.AnimationCallback mAnimationCallback;
36 
37     /**
38      * Called by a preference panel fragment to finish itself.
39      *
40      * @param context Application Context
41      * @param animationId An {@link android.graphics.drawable.AnimationDrawable} resource id
42      */
VectorAnimationController(Context context, int animationId)43     VectorAnimationController(Context context, int animationId) {
44         mAnimatedVectorDrawableCompat = AnimatedVectorDrawableCompat.create(context, animationId);
45         mAnimationCallback = new Animatable2Compat.AnimationCallback() {
46             @Override
47             public void onAnimationEnd(Drawable drawable) {
48                 mAnimatedVectorDrawableCompat.start();
49             }
50         };
51     }
52 
53     @Override
getVideoWidth()54     public int getVideoWidth() {
55         return mAnimatedVectorDrawableCompat.getIntrinsicWidth();
56     }
57 
58     @Override
getVideoHeight()59     public int getVideoHeight() {
60         return mAnimatedVectorDrawableCompat.getIntrinsicHeight();
61     }
62 
63     @Override
pause()64     public void pause() {
65         mAnimatedVectorDrawableCompat.stop();
66     }
67 
68     @Override
start()69     public void start() {
70         mAnimatedVectorDrawableCompat.start();
71     }
72 
73     @Override
isPlaying()74     public boolean isPlaying() {
75         return mAnimatedVectorDrawableCompat.isRunning();
76     }
77 
78     @Override
getDuration()79     public int getDuration() {
80         // We can't get duration from AnimatedVectorDrawable, just return a non zero value.
81         return 5000;
82     }
83 
84     @Override
attachView(TextureView video, View preview, View playButton)85     public void attachView(TextureView video, View preview, View playButton) {
86         mPreviewDrawable = preview.getForeground();
87         video.setVisibility(View.GONE);
88         updateViewStates(preview, playButton);
89         preview.setOnClickListener(v -> updateViewStates(preview, playButton));
90     }
91 
92     @Override
release()93     public void release() {
94         mAnimatedVectorDrawableCompat.stop();
95         mAnimatedVectorDrawableCompat.clearAnimationCallbacks();
96     }
97 
updateViewStates(View imageView, View playButton)98     private void updateViewStates(View imageView, View playButton) {
99         if (mAnimatedVectorDrawableCompat.isRunning()) {
100             mAnimatedVectorDrawableCompat.stop();
101             mAnimatedVectorDrawableCompat.clearAnimationCallbacks();
102             playButton.setVisibility(View.VISIBLE);
103             imageView.setForeground(mPreviewDrawable);
104         } else {
105             playButton.setVisibility(View.GONE);
106             imageView.setForeground(mAnimatedVectorDrawableCompat);
107             mAnimatedVectorDrawableCompat.start();
108             mAnimatedVectorDrawableCompat.registerAnimationCallback(mAnimationCallback);
109         }
110     }
111 }
112