1/* 2 * Copyright (C) 2014 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.v7.widget; 18 19import static android.support.annotation.RestrictTo.Scope.LIBRARY_GROUP; 20 21import android.graphics.Rect; 22import android.os.Build; 23import android.support.annotation.RestrictTo; 24import android.support.v4.view.ViewCompat; 25import android.util.Log; 26import android.view.View; 27 28import java.lang.reflect.InvocationTargetException; 29import java.lang.reflect.Method; 30 31/** 32 * @hide 33 */ 34@RestrictTo(LIBRARY_GROUP) 35public class ViewUtils { 36 private static final String TAG = "ViewUtils"; 37 38 private static Method sComputeFitSystemWindowsMethod; 39 40 static { 41 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { 42 try { 43 sComputeFitSystemWindowsMethod = View.class.getDeclaredMethod( 44 "computeFitSystemWindows", Rect.class, Rect.class); 45 if (!sComputeFitSystemWindowsMethod.isAccessible()) { 46 sComputeFitSystemWindowsMethod.setAccessible(true); 47 } 48 } catch (NoSuchMethodException e) { 49 Log.d(TAG, "Could not find method computeFitSystemWindows. Oh well."); 50 } 51 } 52 } 53 54 private ViewUtils() {} 55 56 public static boolean isLayoutRtl(View view) { 57 return ViewCompat.getLayoutDirection(view) == ViewCompat.LAYOUT_DIRECTION_RTL; 58 } 59 60 /** 61 * Merge two states as returned by {@link View#getMeasuredState()} ()}. 62 * @param curState The current state as returned from a view or the result 63 * of combining multiple views. 64 * @param newState The new view state to combine. 65 * @return Returns a new integer reflecting the combination of the two 66 * states. 67 * 68 * @deprecated Use {@link View#combineMeasuredStates(int, int)} directly. 69 */ 70 @Deprecated 71 public static int combineMeasuredStates(int curState, int newState) { 72 return View.combineMeasuredStates(curState, newState); 73 } 74 75 /** 76 * Allow calling the hidden method {@code computeFitSystemWindows(Rect, Rect)} through 77 * reflection on {@code view}. 78 */ 79 public static void computeFitSystemWindows(View view, Rect inoutInsets, Rect outLocalInsets) { 80 if (sComputeFitSystemWindowsMethod != null) { 81 try { 82 sComputeFitSystemWindowsMethod.invoke(view, inoutInsets, outLocalInsets); 83 } catch (Exception e) { 84 Log.d(TAG, "Could not invoke computeFitSystemWindows", e); 85 } 86 } 87 } 88 89 /** 90 * Allow calling the hidden method {@code makeOptionalFitsSystem()} through reflection on 91 * {@code view}. 92 */ 93 public static void makeOptionalFitsSystemWindows(View view) { 94 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { 95 try { 96 // We need to use getMethod() for makeOptionalFitsSystemWindows since both View 97 // and ViewGroup implement the method 98 Method method = view.getClass().getMethod("makeOptionalFitsSystemWindows"); 99 if (!method.isAccessible()) { 100 method.setAccessible(true); 101 } 102 method.invoke(view); 103 } catch (NoSuchMethodException e) { 104 Log.d(TAG, "Could not find method makeOptionalFitsSystemWindows. Oh well..."); 105 } catch (InvocationTargetException e) { 106 Log.d(TAG, "Could not invoke makeOptionalFitsSystemWindows", e); 107 } catch (IllegalAccessException e) { 108 Log.d(TAG, "Could not invoke makeOptionalFitsSystemWindows", e); 109 } 110 } 111 } 112} 113