1e8a143eb0a6e501cc9c188a28125f61103ad51f
[platform/framework/web/crosswalk.git] / src / base / android / java / src / org / chromium / base / ApiCompatibilityUtils.java
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 package org.chromium.base;
6
7 import android.app.PendingIntent;
8 import android.content.res.Configuration;
9 import android.graphics.drawable.Drawable;
10 import android.os.Build;
11 import android.view.View;
12 import android.view.ViewGroup.MarginLayoutParams;
13 import android.view.ViewTreeObserver;
14 import android.widget.ImageView;
15 import android.widget.RemoteViews;
16 import android.widget.TextView;
17
18 /**
19  * Utility class to use new APIs that were added after ICS (API level 14).
20  */
21 public class ApiCompatibilityUtils {
22
23     private ApiCompatibilityUtils() {
24     }
25
26     /**
27      * Returns true if view's layout direction is right-to-left.
28      *
29      * @param view the View whose layout is being considered
30      */
31     public static boolean isLayoutRtl(View view) {
32         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
33             return view.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL;
34         } else {
35             // All layouts are LTR before JB MR1.
36             return false;
37         }
38     }
39
40     /**
41      * @see Configuration#getLayoutDirection()
42      */
43     public static int getLayoutDirection(Configuration configuration) {
44         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
45             return configuration.getLayoutDirection();
46         } else {
47             // All layouts are LTR before JB MR1.
48             return View.LAYOUT_DIRECTION_LTR;
49         }
50     }
51
52     /**
53      * @return True if the running version of the Android supports printing.
54      */
55     public static boolean isPrintingSupported() {
56         return Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
57     }
58
59     /**
60      * @return True if the running version of the Android supports HTML clipboard.
61      */
62     public static boolean isHTMLClipboardSupported() {
63         return Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN;
64     }
65
66     /**
67      * @see android.view.View#setLayoutDirection(int)
68      */
69     public static void setLayoutDirection(View view, int layoutDirection) {
70         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
71             view.setLayoutDirection(layoutDirection);
72         } else {
73             // Do nothing. RTL layouts aren't supported before JB MR1.
74         }
75     }
76
77     /**
78      * @see android.view.View#setTextDirection(int)
79      */
80     public static void setTextAlignment(View view, int textAlignment) {
81         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
82             view.setTextAlignment(textAlignment);
83         } else {
84             // Do nothing. RTL text isn't supported before JB MR1.
85         }
86     }
87
88     /**
89      * @see android.view.ViewGroup.MarginLayoutParams#setMarginEnd(int)
90      */
91     public static void setMarginEnd(MarginLayoutParams layoutParams, int end) {
92         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
93             layoutParams.setMarginEnd(end);
94         } else {
95             layoutParams.rightMargin = end;
96         }
97     }
98
99     /**
100      * @see android.view.ViewGroup.MarginLayoutParams#getMarginEnd()
101      */
102     public static int getMarginEnd(MarginLayoutParams layoutParams) {
103         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
104             return layoutParams.getMarginEnd();
105         } else {
106             return layoutParams.rightMargin;
107         }
108     }
109
110     /**
111      * @see android.view.ViewGroup.MarginLayoutParams#setMarginStart(int)
112      */
113     public static void setMarginStart(MarginLayoutParams layoutParams, int start) {
114         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
115             layoutParams.setMarginStart(start);
116         } else {
117             layoutParams.leftMargin = start;
118         }
119     }
120
121     /**
122      * @see android.view.ViewGroup.MarginLayoutParams#getMarginStart()
123      */
124     public static int getMarginStart(MarginLayoutParams layoutParams) {
125         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
126             return layoutParams.getMarginStart();
127         } else {
128             return layoutParams.leftMargin;
129         }
130     }
131
132     /**
133      * @see android.view.View#setPaddingRelative(int, int, int, int)
134      */
135     public static void setPaddingRelative(View view, int start, int top, int end, int bottom) {
136         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
137             view.setPaddingRelative(start, top, end, bottom);
138         } else {
139             // Before JB MR1, all layouts are left-to-right, so start == left, etc.
140             view.setPadding(start, top, end, bottom);
141         }
142     }
143
144     /**
145      * @see android.view.View#getPaddingStart()
146      */
147     public static int getPaddingStart(View view) {
148         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
149             return view.getPaddingStart();
150         } else {
151             // Before JB MR1, all layouts are left-to-right, so start == left.
152             return view.getPaddingLeft();
153         }
154     }
155
156     /**
157      * @see android.view.View#getPaddingEnd()
158      */
159     public static int getPaddingEnd(View view) {
160         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
161             return view.getPaddingEnd();
162         } else {
163             // Before JB MR1, all layouts are left-to-right, so end == right.
164             return view.getPaddingRight();
165         }
166     }
167
168     /**
169      * @see android.widget.TextView#setCompoundDrawablesRelative(Drawable, Drawable, Drawable,
170      *      Drawable)
171      */
172     public static void setCompoundDrawablesRelative(TextView textView, Drawable start, Drawable top,
173             Drawable end, Drawable bottom) {
174         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
175             textView.setCompoundDrawablesRelative(start, top, bottom, end);
176         } else {
177             textView.setCompoundDrawables(start, top, bottom, end);
178         }
179     }
180
181     /**
182      * @see android.widget.TextView#setCompoundDrawablesRelativeWithIntrinsicBounds(Drawable,
183      *      Drawable, Drawable, Drawable)
184      */
185     public static void setCompoundDrawablesRelativeWithIntrinsicBounds(TextView textView,
186             Drawable start, Drawable top, Drawable end, Drawable bottom) {
187         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
188             textView.setCompoundDrawablesRelativeWithIntrinsicBounds(start, top, bottom, end);
189         } else {
190             textView.setCompoundDrawablesWithIntrinsicBounds(start, top, bottom, end);
191         }
192     }
193
194     /**
195      * @see android.widget.TextView#setCompoundDrawablesRelativeWithIntrinsicBounds(int, int, int,
196      *      int)
197      */
198     public static void setCompoundDrawablesRelativeWithIntrinsicBounds(TextView textView,
199             int start, int top, int end, int bottom) {
200         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
201             textView.setCompoundDrawablesRelativeWithIntrinsicBounds(start, top, bottom, end);
202         } else {
203             textView.setCompoundDrawablesWithIntrinsicBounds(start, top, bottom, end);
204         }
205     }
206
207     /**
208      * @see android.view.View#postInvalidateOnAnimation()
209      */
210     public static void postInvalidateOnAnimation(View view) {
211         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
212             view.postInvalidateOnAnimation();
213         } else {
214             view.postInvalidate();
215         }
216     }
217
218     /**
219      * @see android.widget.RemoteViews#setContentDescription(int, CharSequence)
220      */
221     public static void setContentDescriptionForRemoteView(RemoteViews remoteViews, int viewId,
222             CharSequence contentDescription) {
223         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
224             remoteViews.setContentDescription(viewId, contentDescription);
225         } else {
226             // setContentDescription() is unavailable in earlier versions.
227         }
228     }
229
230     // These methods have a new name, and the old name is deprecated.
231
232     /**
233      * @see android.view.View#setBackground(Drawable)
234      */
235     @SuppressWarnings("deprecation")
236     public static void setBackgroundForView(View view, Drawable drawable) {
237         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
238             view.setBackground(drawable);
239         } else {
240             view.setBackgroundDrawable(drawable);
241         }
242     }
243
244     /**
245      * @see android.view.ViewTreeObserver#removeOnGlobalLayoutListener()
246      */
247     @SuppressWarnings("deprecation")
248     public static void removeOnGlobalLayoutListener(
249             View view, ViewTreeObserver.OnGlobalLayoutListener listener) {
250         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
251             view.getViewTreeObserver().removeOnGlobalLayoutListener(listener);
252         } else {
253             view.getViewTreeObserver().removeGlobalOnLayoutListener(listener);
254         }
255     }
256
257     /**
258      * @see android.widget.ImageView#setImageAlpha(int)
259      */
260     @SuppressWarnings("deprecation")
261     public static void setImageAlpha(ImageView iv, int alpha) {
262         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
263             iv.setImageAlpha(alpha);
264         } else {
265             iv.setAlpha(alpha);
266         }
267     }
268
269     /**
270      * @see android.app.PendingIntent#getCreatorPackage()
271      */
272     @SuppressWarnings("deprecation")
273     public static String getCreatorPackage(PendingIntent intent) {
274         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
275             return intent.getCreatorPackage();
276         } else {
277             return intent.getTargetPackage();
278         }
279     }
280 }