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
17package androidx.transition;
18
19import android.animation.Animator;
20import android.animation.AnimatorListenerAdapter;
21import android.graphics.Matrix;
22import android.os.Build;
23import android.util.Log;
24import android.widget.ImageView;
25
26import java.lang.reflect.InvocationTargetException;
27import java.lang.reflect.Method;
28
29class ImageViewUtils {
30    private static final String TAG = "ImageViewUtils";
31
32    private static Method sAnimateTransformMethod;
33    private static boolean sAnimateTransformMethodFetched;
34
35    /**
36     * Starts animating the transformation of the image view. This has to be called before calling
37     * {@link #animateTransform(ImageView, Matrix)}.
38     */
39    static void startAnimateTransform(ImageView view) {
40        if (Build.VERSION.SDK_INT < 21) {
41            final ImageView.ScaleType scaleType = view.getScaleType();
42            view.setTag(R.id.save_scale_type, scaleType);
43            if (scaleType == ImageView.ScaleType.MATRIX) {
44                view.setTag(R.id.save_image_matrix, view.getImageMatrix());
45            } else {
46                view.setScaleType(ImageView.ScaleType.MATRIX);
47            }
48            view.setImageMatrix(MatrixUtils.IDENTITY_MATRIX);
49        }
50    }
51
52    /**
53     * Sets the matrix to animate the content of the image view.
54     */
55    static void animateTransform(ImageView view, Matrix matrix) {
56        if (Build.VERSION.SDK_INT < 21) {
57            view.setImageMatrix(matrix);
58        } else {
59            fetchAnimateTransformMethod();
60            if (sAnimateTransformMethod != null) {
61                try {
62                    sAnimateTransformMethod.invoke(view, matrix);
63                } catch (IllegalAccessException e) {
64                    // Do nothing
65                } catch (InvocationTargetException e) {
66                    throw new RuntimeException(e.getCause());
67                }
68            }
69        }
70    }
71
72    private static void fetchAnimateTransformMethod() {
73        if (!sAnimateTransformMethodFetched) {
74            try {
75                sAnimateTransformMethod = ImageView.class.getDeclaredMethod("animateTransform",
76                        Matrix.class);
77                sAnimateTransformMethod.setAccessible(true);
78            } catch (NoSuchMethodException e) {
79                Log.i(TAG, "Failed to retrieve animateTransform method", e);
80            }
81            sAnimateTransformMethodFetched = true;
82        }
83    }
84
85    /**
86     * Reserves that the caller will stop calling {@link #animateTransform(ImageView, Matrix)} when
87     * the specified animator ends.
88     */
89    static void reserveEndAnimateTransform(final ImageView view, Animator animator) {
90        if (Build.VERSION.SDK_INT < 21) {
91            animator.addListener(new AnimatorListenerAdapter() {
92                @Override
93                public void onAnimationEnd(Animator animation) {
94                    final ImageView.ScaleType scaleType = (ImageView.ScaleType)
95                            view.getTag(R.id.save_scale_type);
96                    view.setScaleType(scaleType);
97                    view.setTag(R.id.save_scale_type, null);
98                    if (scaleType == ImageView.ScaleType.MATRIX) {
99                        view.setImageMatrix((Matrix) view.getTag(R.id.save_image_matrix));
100                        view.setTag(R.id.save_image_matrix, null);
101                    }
102                    animation.removeListener(this);
103                }
104            });
105        }
106    }
107
108    private ImageViewUtils() {
109    }
110}
111