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.graphics.Matrix;
20import android.support.annotation.NonNull;
21import android.support.annotation.RequiresApi;
22import android.util.Log;
23import android.view.View;
24
25import java.lang.reflect.InvocationTargetException;
26import java.lang.reflect.Method;
27
28@RequiresApi(21)
29class ViewUtilsApi21 extends ViewUtilsApi19 {
30
31    private static final String TAG = "ViewUtilsApi21";
32
33    private static Method sTransformMatrixToGlobalMethod;
34    private static boolean sTransformMatrixToGlobalMethodFetched;
35    private static Method sTransformMatrixToLocalMethod;
36    private static boolean sTransformMatrixToLocalMethodFetched;
37    private static Method sSetAnimationMatrixMethod;
38    private static boolean sSetAnimationMatrixMethodFetched;
39
40    @Override
41    public void transformMatrixToGlobal(@NonNull View view, @NonNull Matrix matrix) {
42        fetchTransformMatrixToGlobalMethod();
43        if (sTransformMatrixToGlobalMethod != null) {
44            try {
45                sTransformMatrixToGlobalMethod.invoke(view, matrix);
46            } catch (IllegalAccessException e) {
47                // Do nothing
48            } catch (InvocationTargetException e) {
49                throw new RuntimeException(e.getCause());
50            }
51        }
52    }
53
54    @Override
55    public void transformMatrixToLocal(@NonNull View view, @NonNull Matrix matrix) {
56        fetchTransformMatrixToLocalMethod();
57        if (sTransformMatrixToLocalMethod != null) {
58            try {
59                sTransformMatrixToLocalMethod.invoke(view, matrix);
60            } catch (IllegalAccessException e) {
61                // Do nothing
62            } catch (InvocationTargetException e) {
63                throw new RuntimeException(e.getCause());
64            }
65        }
66    }
67
68    @Override
69    public void setAnimationMatrix(@NonNull View view, Matrix matrix) {
70        fetchSetAnimationMatrix();
71        if (sSetAnimationMatrixMethod != null) {
72            try {
73                sSetAnimationMatrixMethod.invoke(view, matrix);
74            } catch (InvocationTargetException e) {
75                // Do nothing
76            } catch (IllegalAccessException e) {
77                throw new RuntimeException(e.getCause());
78            }
79        }
80    }
81
82    private void fetchTransformMatrixToGlobalMethod() {
83        if (!sTransformMatrixToGlobalMethodFetched) {
84            try {
85                sTransformMatrixToGlobalMethod = View.class.getDeclaredMethod(
86                        "transformMatrixToGlobal", Matrix.class);
87                sTransformMatrixToGlobalMethod.setAccessible(true);
88            } catch (NoSuchMethodException e) {
89                Log.i(TAG, "Failed to retrieve transformMatrixToGlobal method", e);
90            }
91            sTransformMatrixToGlobalMethodFetched = true;
92        }
93    }
94
95    private void fetchTransformMatrixToLocalMethod() {
96        if (!sTransformMatrixToLocalMethodFetched) {
97            try {
98                sTransformMatrixToLocalMethod = View.class.getDeclaredMethod(
99                        "transformMatrixToLocal", Matrix.class);
100                sTransformMatrixToLocalMethod.setAccessible(true);
101            } catch (NoSuchMethodException e) {
102                Log.i(TAG, "Failed to retrieve transformMatrixToLocal method", e);
103            }
104            sTransformMatrixToLocalMethodFetched = true;
105        }
106    }
107
108    private void fetchSetAnimationMatrix() {
109        if (!sSetAnimationMatrixMethodFetched) {
110            try {
111                sSetAnimationMatrixMethod = View.class.getDeclaredMethod(
112                        "setAnimationMatrix", Matrix.class);
113                sSetAnimationMatrixMethod.setAccessible(true);
114            } catch (NoSuchMethodException e) {
115                Log.i(TAG, "Failed to retrieve setAnimationMatrix method", e);
116            }
117            sSetAnimationMatrixMethodFetched = true;
118        }
119    }
120
121}
122