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 android.support.transition;
18
19import android.animation.Animator;
20import android.graphics.Matrix;
21import android.support.annotation.RequiresApi;
22import android.util.Log;
23import android.widget.ImageView;
24
25import java.lang.reflect.InvocationTargetException;
26import java.lang.reflect.Method;
27
28@RequiresApi(21)
29class ImageViewUtilsApi21 implements ImageViewUtilsImpl {
30
31    private static final String TAG = "ImageViewUtilsApi21";
32
33    private static Method sAnimateTransformMethod;
34    private static boolean sAnimateTransformMethodFetched;
35
36    @Override
37    public void startAnimateTransform(ImageView view) {
38        // Do nothing
39    }
40
41    @Override
42    public void animateTransform(ImageView view, Matrix matrix) {
43        fetchAnimateTransformMethod();
44        if (sAnimateTransformMethod != null) {
45            try {
46                sAnimateTransformMethod.invoke(view, matrix);
47            } catch (IllegalAccessException e) {
48                // Do nothing
49            } catch (InvocationTargetException e) {
50                throw new RuntimeException(e.getCause());
51            }
52        }
53    }
54
55    @Override
56    public void reserveEndAnimateTransform(ImageView view, Animator animator) {
57        // Do nothing
58    }
59
60    private void fetchAnimateTransformMethod() {
61        if (!sAnimateTransformMethodFetched) {
62            try {
63                sAnimateTransformMethod = ImageView.class.getDeclaredMethod("animateTransform",
64                        Matrix.class);
65                sAnimateTransformMethod.setAccessible(true);
66            } catch (NoSuchMethodException e) {
67                Log.i(TAG, "Failed to retrieve animateTransform method", e);
68            }
69            sAnimateTransformMethodFetched = true;
70        }
71    }
72
73}
74