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