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 // TODO: Use {@link android.support.v4.view.ViewCompat} instead of this utility class.
     24 // Currently {@link #getPaddingEnd(View)} and {@link #setPaddingRelative(View,int,int,int,int)}
     25 // are missing from android-support-v4 static library in KitKat SDK.
     26 public final class ViewCompatUtils {
     27     // Note that View.getPaddingEnd(), View.setPaddingRelative(int,int,int,int) have been
     28     // introduced in API level 17 (Build.VERSION_CODE.JELLY_BEAN_MR1).
     29     private static final Method METHOD_getPaddingEnd = CompatUtils.getMethod(
     30             View.class, "getPaddingEnd");
     31     private static final Method METHOD_setPaddingRelative = CompatUtils.getMethod(
     32             View.class, "setPaddingRelative",
     33             int.class, int.class, int.class, int.class);
     34     // Note that View.setTextAlignment(int) has been introduced in API level 17.
     35     private static final Method METHOD_setTextAlignment = CompatUtils.getMethod(
     36             View.class, "setTextAlignment", int.class);
     37 
     38     private ViewCompatUtils() {
     39         // This utility class is not publicly instantiable.
     40     }
     41 
     42     public static int getPaddingEnd(final View view) {
     43         if (METHOD_getPaddingEnd == null) {
     44             return view.getPaddingRight();
     45         }
     46         return (Integer)CompatUtils.invoke(view, 0, METHOD_getPaddingEnd);
     47     }
     48 
     49     public static void setPaddingRelative(final View view, final int start, final int top,
     50             final int end, final int bottom) {
     51         if (METHOD_setPaddingRelative == null) {
     52             view.setPadding(start, top, end, bottom);
     53             return;
     54         }
     55         CompatUtils.invoke(view, null, METHOD_setPaddingRelative, start, top, end, bottom);
     56     }
     57 
     58     // These TEXT_ALIGNMENT_* constants have been introduced in API 17.
     59     public static final int TEXT_ALIGNMENT_INHERIT = 0;
     60     public static final int TEXT_ALIGNMENT_GRAVITY = 1;
     61     public static final int TEXT_ALIGNMENT_TEXT_START = 2;
     62     public static final int TEXT_ALIGNMENT_TEXT_END = 3;
     63     public static final int TEXT_ALIGNMENT_CENTER = 4;
     64     public static final int TEXT_ALIGNMENT_VIEW_START = 5;
     65     public static final int TEXT_ALIGNMENT_VIEW_END = 6;
     66 
     67     public static void setTextAlignment(final View view, final int textAlignment) {
     68         CompatUtils.invoke(view, null, METHOD_setTextAlignment, textAlignment);
     69     }
     70 }
     71