Home | History | Annotate | Download | only in compat
      1 /*
      2  * Copyright (C) 2013 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 com.android.inputmethod.compat;
     18 
     19 import android.view.View;
     20 
     21 import java.lang.reflect.Method;
     22 
     23 public final class ViewCompatUtils {
     24     // Note that View.LAYOUT_DIRECTION_LTR and View.LAYOUT_DIRECTION_RTL have been introduced in
     25     // API level 17 (Build.VERSION_CODE.JELLY_BEAN_MR1).
     26     public static final int LAYOUT_DIRECTION_LTR = (Integer)CompatUtils.getFieldValue(null, 0x0,
     27             CompatUtils.getField(View.class, "LAYOUT_DIRECTION_LTR"));
     28     public static final int LAYOUT_DIRECTION_RTL = (Integer)CompatUtils.getFieldValue(null, 0x1,
     29             CompatUtils.getField(View.class, "LAYOUT_DIRECTION_RTL"));
     30 
     31     // Note that View.getPaddingEnd(), View.setPaddingRelative(int,int,int,int), and
     32     // View.getLayoutDirection() have been introduced in API level 17
     33     // (Build.VERSION_CODE.JELLY_BEAN_MR1).
     34     private static final Method METHOD_getPaddingEnd = CompatUtils.getMethod(
     35             View.class, "getPaddingEnd");
     36     private static final Method METHOD_setPaddingRelative = CompatUtils.getMethod(
     37             View.class, "setPaddingRelative",
     38             Integer.TYPE, Integer.TYPE, Integer.TYPE, Integer.TYPE);
     39     private static final Method METHOD_getLayoutDirection = CompatUtils.getMethod(
     40             View.class, "getLayoutDirection");
     41 
     42     private ViewCompatUtils() {
     43         // This utility class is not publicly instantiable.
     44     }
     45 
     46     public static int getPaddingEnd(final View view) {
     47         if (METHOD_getPaddingEnd == null) {
     48             return view.getPaddingRight();
     49         }
     50         return (Integer)CompatUtils.invoke(view, 0, METHOD_getPaddingEnd);
     51     }
     52 
     53     public static void setPaddingRelative(final View view, final int start, final int top,
     54             final int end, final int bottom) {
     55         if (METHOD_setPaddingRelative == null) {
     56             view.setPadding(start, top, end, bottom);
     57             return;
     58         }
     59         CompatUtils.invoke(view, null, METHOD_setPaddingRelative, start, top, end, bottom);
     60     }
     61 
     62     public static int getLayoutDirection(final View view) {
     63         if (METHOD_getLayoutDirection == null) {
     64             return LAYOUT_DIRECTION_LTR;
     65         }
     66         return (Integer)CompatUtils.invoke(view, 0, METHOD_getLayoutDirection);
     67     }
     68 }
     69