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 import android.view.ViewGroup;
     23 
     24 import androidx.annotation.NonNull;
     25 import androidx.annotation.RequiresApi;
     26 
     27 import java.lang.reflect.InvocationTargetException;
     28 import java.lang.reflect.Method;
     29 
     30 @RequiresApi(21)
     31 class GhostViewApi21 implements GhostViewImpl {
     32 
     33     private static final String TAG = "GhostViewApi21";
     34 
     35     private static Class<?> sGhostViewClass;
     36     private static boolean sGhostViewClassFetched;
     37     private static Method sAddGhostMethod;
     38     private static boolean sAddGhostMethodFetched;
     39     private static Method sRemoveGhostMethod;
     40     private static boolean sRemoveGhostMethodFetched;
     41 
     42     static GhostViewImpl addGhost(View view, ViewGroup viewGroup, Matrix matrix) {
     43         fetchAddGhostMethod();
     44         if (sAddGhostMethod != null) {
     45             try {
     46                 return new GhostViewApi21(
     47                         (View) sAddGhostMethod.invoke(null, view, viewGroup, matrix));
     48             } catch (IllegalAccessException e) {
     49                 // Do nothing
     50             } catch (InvocationTargetException e) {
     51                 throw new RuntimeException(e.getCause());
     52             }
     53         }
     54         return null;
     55     }
     56 
     57     static void removeGhost(View view) {
     58         fetchRemoveGhostMethod();
     59         if (sRemoveGhostMethod != null) {
     60             try {
     61                 sRemoveGhostMethod.invoke(null, view);
     62             } catch (IllegalAccessException e) {
     63                 // Do nothing
     64             } catch (InvocationTargetException e) {
     65                 throw new RuntimeException(e.getCause());
     66             }
     67         }
     68     }
     69 
     70     /** A handle to the platform android.view.GhostView. */
     71     private final View mGhostView;
     72 
     73     private GhostViewApi21(@NonNull View ghostView) {
     74         mGhostView = ghostView;
     75     }
     76 
     77     @Override
     78     public void setVisibility(int visibility) {
     79         mGhostView.setVisibility(visibility);
     80     }
     81 
     82     @Override
     83     public void reserveEndViewTransition(ViewGroup viewGroup, View view) {
     84         // No need
     85     }
     86 
     87     private static void fetchGhostViewClass() {
     88         if (!sGhostViewClassFetched) {
     89             try {
     90                 sGhostViewClass = Class.forName("android.view.GhostView");
     91             } catch (ClassNotFoundException e) {
     92                 Log.i(TAG, "Failed to retrieve GhostView class", e);
     93             }
     94             sGhostViewClassFetched = true;
     95         }
     96     }
     97 
     98     private static void fetchAddGhostMethod() {
     99         if (!sAddGhostMethodFetched) {
    100             try {
    101                 fetchGhostViewClass();
    102                 sAddGhostMethod = sGhostViewClass.getDeclaredMethod("addGhost", View.class,
    103                         ViewGroup.class, Matrix.class);
    104                 sAddGhostMethod.setAccessible(true);
    105             } catch (NoSuchMethodException e) {
    106                 Log.i(TAG, "Failed to retrieve addGhost method", e);
    107             }
    108             sAddGhostMethodFetched = true;
    109         }
    110     }
    111 
    112     private static void fetchRemoveGhostMethod() {
    113         if (!sRemoveGhostMethodFetched) {
    114             try {
    115                 fetchGhostViewClass();
    116                 sRemoveGhostMethod = sGhostViewClass.getDeclaredMethod("removeGhost", View.class);
    117                 sRemoveGhostMethod.setAccessible(true);
    118             } catch (NoSuchMethodException e) {
    119                 Log.i(TAG, "Failed to retrieve removeGhost method", e);
    120             }
    121             sRemoveGhostMethodFetched = true;
    122         }
    123     }
    124 
    125 }
    126