ViewUtils.java revision 1f4989216d62f70088cea29f9ae200a9bb3ff955
149c78900da0d43140fb602431fb93212bd7f6c70Chris Banes/* 249c78900da0d43140fb602431fb93212bd7f6c70Chris Banes * Copyright (C) 2014 The Android Open Source Project 349c78900da0d43140fb602431fb93212bd7f6c70Chris Banes * 449c78900da0d43140fb602431fb93212bd7f6c70Chris Banes * Licensed under the Apache License, Version 2.0 (the "License"); 549c78900da0d43140fb602431fb93212bd7f6c70Chris Banes * you may not use this file except in compliance with the License. 649c78900da0d43140fb602431fb93212bd7f6c70Chris Banes * You may obtain a copy of the License at 749c78900da0d43140fb602431fb93212bd7f6c70Chris Banes * 849c78900da0d43140fb602431fb93212bd7f6c70Chris Banes * http://www.apache.org/licenses/LICENSE-2.0 949c78900da0d43140fb602431fb93212bd7f6c70Chris Banes * 1049c78900da0d43140fb602431fb93212bd7f6c70Chris Banes * Unless required by applicable law or agreed to in writing, software 1149c78900da0d43140fb602431fb93212bd7f6c70Chris Banes * distributed under the License is distributed on an "AS IS" BASIS, 1249c78900da0d43140fb602431fb93212bd7f6c70Chris Banes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1349c78900da0d43140fb602431fb93212bd7f6c70Chris Banes * See the License for the specific language governing permissions and 1449c78900da0d43140fb602431fb93212bd7f6c70Chris Banes * limitations under the License. 1549c78900da0d43140fb602431fb93212bd7f6c70Chris Banes */ 1649c78900da0d43140fb602431fb93212bd7f6c70Chris Banes 1749c78900da0d43140fb602431fb93212bd7f6c70Chris Banespackage android.support.v7.internal.widget; 1849c78900da0d43140fb602431fb93212bd7f6c70Chris Banes 191f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banesimport android.graphics.Rect; 201f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banesimport android.os.Build; 2149c78900da0d43140fb602431fb93212bd7f6c70Chris Banesimport android.support.v4.view.ViewCompat; 221f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banesimport android.util.Log; 2349c78900da0d43140fb602431fb93212bd7f6c70Chris Banesimport android.view.View; 2449c78900da0d43140fb602431fb93212bd7f6c70Chris Banes 251f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banesimport java.lang.reflect.Method; 261f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes 2749c78900da0d43140fb602431fb93212bd7f6c70Chris Banes/** 2849c78900da0d43140fb602431fb93212bd7f6c70Chris Banes * @hide 2949c78900da0d43140fb602431fb93212bd7f6c70Chris Banes */ 3049c78900da0d43140fb602431fb93212bd7f6c70Chris Banespublic class ViewUtils { 311f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes private static final String TAG = "ViewUtils"; 321f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes 331f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes private static Method sComputeFitSystemWindowsMethod; 341f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes private static Method sMakeOptionalFitsSystemWindowsMethod; 351f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes 361f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes static { 371f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { 381f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes try { 391f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes sComputeFitSystemWindowsMethod = View.class.getDeclaredMethod( 401f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes "computeFitSystemWindows", Rect.class, Rect.class); 411f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes } catch (NoSuchMethodException e) { 421f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes Log.i(TAG, "Could not find method computeFitSystemWindows. Oh well."); 431f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes } 441f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes } 451f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes 461f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { 471f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes try { 481f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes sMakeOptionalFitsSystemWindowsMethod = View.class.getDeclaredMethod( 491f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes "makeOptionalFitsSystemWindows"); 501f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes } catch (NoSuchMethodException e) { 511f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes Log.i(TAG, "Could not find method makeOptionalFitsSystemWindows. Oh well."); 521f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes } 531f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes } 541f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes } 5549c78900da0d43140fb602431fb93212bd7f6c70Chris Banes 5649c78900da0d43140fb602431fb93212bd7f6c70Chris Banes private ViewUtils() {} 5749c78900da0d43140fb602431fb93212bd7f6c70Chris Banes 5849c78900da0d43140fb602431fb93212bd7f6c70Chris Banes public static boolean isLayoutRtl(View view) { 5949c78900da0d43140fb602431fb93212bd7f6c70Chris Banes return ViewCompat.getLayoutDirection(view) == ViewCompat.LAYOUT_DIRECTION_RTL; 6049c78900da0d43140fb602431fb93212bd7f6c70Chris Banes } 6149c78900da0d43140fb602431fb93212bd7f6c70Chris Banes 6249c78900da0d43140fb602431fb93212bd7f6c70Chris Banes /** 6349c78900da0d43140fb602431fb93212bd7f6c70Chris Banes * Merge two states as returned by {@link ViewCompat#getMeasuredState(android.view.View)} ()}. 6449c78900da0d43140fb602431fb93212bd7f6c70Chris Banes * @param curState The current state as returned from a view or the result 6549c78900da0d43140fb602431fb93212bd7f6c70Chris Banes * of combining multiple views. 6649c78900da0d43140fb602431fb93212bd7f6c70Chris Banes * @param newState The new view state to combine. 6749c78900da0d43140fb602431fb93212bd7f6c70Chris Banes * @return Returns a new integer reflecting the combination of the two 6849c78900da0d43140fb602431fb93212bd7f6c70Chris Banes * states. 6949c78900da0d43140fb602431fb93212bd7f6c70Chris Banes */ 7049c78900da0d43140fb602431fb93212bd7f6c70Chris Banes public static int combineMeasuredStates(int curState, int newState) { 7149c78900da0d43140fb602431fb93212bd7f6c70Chris Banes return curState | newState; 7249c78900da0d43140fb602431fb93212bd7f6c70Chris Banes } 731f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes 741f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes /** 751f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes * Allow calling the hidden method {@code computeFitSystemWindows(Rect, Rect)} through 761f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes * reflection on {@code view}. 771f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes */ 781f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes public static void computeFitSystemWindows(View view, Rect inoutInsets, Rect outLocalInsets) { 791f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes if (sComputeFitSystemWindowsMethod != null) { 801f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes try { 811f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes sComputeFitSystemWindowsMethod.invoke(view, inoutInsets, outLocalInsets); 821f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes } catch (Exception e) { 831f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes Log.i(TAG, "Could not invoke computeFitSystemWindows", e); 841f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes } 851f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes } 861f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes } 871f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes 881f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes /** 891f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes * Allow calling the hidden method {@code makeOptionalFitsSystem()} through reflection on 901f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes * {@code view}. 911f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes */ 921f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes public static void makeOptionalFitsSystemWindows(View view) { 931f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes if (sMakeOptionalFitsSystemWindowsMethod != null) { 941f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes try { 951f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes sMakeOptionalFitsSystemWindowsMethod.invoke(view); 961f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes } catch (Exception e) { 971f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes Log.i(TAG, "Could not invoke makeOptionalFitsSystemWindows", e); 981f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes } 991f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes } 1001f4989216d62f70088cea29f9ae200a9bb3ff955Chris Banes } 10149c78900da0d43140fb602431fb93212bd7f6c70Chris Banes} 102