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.util.Log; 20 import android.view.View; 21 22 import androidx.annotation.NonNull; 23 import androidx.annotation.RequiresApi; 24 25 import java.lang.reflect.InvocationTargetException; 26 import java.lang.reflect.Method; 27 28 @RequiresApi(19) 29 class ViewUtilsApi19 extends ViewUtilsBase { 30 31 private static final String TAG = "ViewUtilsApi19"; 32 33 private static Method sSetTransitionAlphaMethod; 34 private static boolean sSetTransitionAlphaMethodFetched; 35 private static Method sGetTransitionAlphaMethod; 36 private static boolean sGetTransitionAlphaMethodFetched; 37 38 @Override 39 public void setTransitionAlpha(@NonNull View view, float alpha) { 40 fetchSetTransitionAlphaMethod(); 41 if (sSetTransitionAlphaMethod != null) { 42 try { 43 sSetTransitionAlphaMethod.invoke(view, alpha); 44 } catch (IllegalAccessException e) { 45 // Do nothing 46 } catch (InvocationTargetException e) { 47 throw new RuntimeException(e.getCause()); 48 } 49 } else { 50 view.setAlpha(alpha); 51 } 52 } 53 54 @Override 55 public float getTransitionAlpha(@NonNull View view) { 56 fetchGetTransitionAlphaMethod(); 57 if (sGetTransitionAlphaMethod != null) { 58 try { 59 return (Float) sGetTransitionAlphaMethod.invoke(view); 60 } catch (IllegalAccessException e) { 61 // Do nothing 62 } catch (InvocationTargetException e) { 63 throw new RuntimeException(e.getCause()); 64 } 65 } 66 return super.getTransitionAlpha(view); 67 } 68 69 @Override 70 public void saveNonTransitionAlpha(@NonNull View view) { 71 // Do nothing 72 } 73 74 @Override 75 public void clearNonTransitionAlpha(@NonNull View view) { 76 // Do nothing 77 } 78 79 private void fetchSetTransitionAlphaMethod() { 80 if (!sSetTransitionAlphaMethodFetched) { 81 try { 82 sSetTransitionAlphaMethod = View.class.getDeclaredMethod("setTransitionAlpha", 83 float.class); 84 sSetTransitionAlphaMethod.setAccessible(true); 85 } catch (NoSuchMethodException e) { 86 Log.i(TAG, "Failed to retrieve setTransitionAlpha method", e); 87 } 88 sSetTransitionAlphaMethodFetched = true; 89 } 90 } 91 92 private void fetchGetTransitionAlphaMethod() { 93 if (!sGetTransitionAlphaMethodFetched) { 94 try { 95 sGetTransitionAlphaMethod = View.class.getDeclaredMethod("getTransitionAlpha"); 96 sGetTransitionAlphaMethod.setAccessible(true); 97 } catch (NoSuchMethodException e) { 98 Log.i(TAG, "Failed to retrieve getTransitionAlpha method", e); 99 } 100 sGetTransitionAlphaMethodFetched = true; 101 } 102 } 103 104 } 105