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