ViewCompatUtils.java revision f507d1febb6742b009ae6acf1c70b657eba9b3a6
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
17package com.android.inputmethod.compat;
18
19import android.view.View;
20
21import 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.
26public 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.setElevation(float) has been introduced in API level 21.
35    private static final Method METHOD_setElevation = CompatUtils.getMethod(
36            View.class, "setElevation", float.class);
37    // Note that View.setTextAlignment(int) has been introduced in API level 17.
38    private static final Method METHOD_setTextAlignment = CompatUtils.getMethod(
39            View.class, "setTextAlignment", int.class);
40
41    private ViewCompatUtils() {
42        // This utility class is not publicly instantiable.
43    }
44
45    public static int getPaddingEnd(final View view) {
46        if (METHOD_getPaddingEnd == null) {
47            return view.getPaddingRight();
48        }
49        return (Integer)CompatUtils.invoke(view, 0, METHOD_getPaddingEnd);
50    }
51
52    public static void setPaddingRelative(final View view, final int start, final int top,
53            final int end, final int bottom) {
54        if (METHOD_setPaddingRelative == null) {
55            view.setPadding(start, top, end, bottom);
56            return;
57        }
58        CompatUtils.invoke(view, null, METHOD_setPaddingRelative, start, top, end, bottom);
59    }
60
61    public static void setElevation(final View view, final float elevation) {
62        CompatUtils.invoke(view, null, METHOD_setElevation, elevation);
63    }
64
65    // These TEXT_ALIGNMENT_* constants have been introduced in API 17.
66    public static final int TEXT_ALIGNMENT_INHERIT = 0;
67    public static final int TEXT_ALIGNMENT_GRAVITY = 1;
68    public static final int TEXT_ALIGNMENT_TEXT_START = 2;
69    public static final int TEXT_ALIGNMENT_TEXT_END = 3;
70    public static final int TEXT_ALIGNMENT_CENTER = 4;
71    public static final int TEXT_ALIGNMENT_VIEW_START = 5;
72    public static final int TEXT_ALIGNMENT_VIEW_END = 6;
73
74    public static void setTextAlignment(final View view, final int textAlignment) {
75        CompatUtils.invoke(view, null, METHOD_setTextAlignment, textAlignment);
76    }
77}
78