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.
5 package org.chromium.base;
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;
19 * Utility class to use new APIs that were added after ICS (API level 14).
21 public class ApiCompatibilityUtils {
23 private static final String TAG = "ApiCompatibilityUtils";
25 private ApiCompatibilityUtils() {
29 * Returns true if view's layout direction is right-to-left.
31 * @param view the View whose layout is being considered
33 public static boolean isLayoutRtl(View view) {
34 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
35 return view.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL;
37 // All layouts are LTR before JB MR1.
43 * @see Configuration#getLayoutDirection()
45 public static int getLayoutDirection(Configuration configuration) {
46 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
47 return configuration.getLayoutDirection();
49 // All layouts are LTR before JB MR1.
50 return View.LAYOUT_DIRECTION_LTR;
55 * @return True if the running version of the Android supports printing.
57 public static boolean isPrintingSupported() {
58 return Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
62 * @return True if the running version of the Android supports HTML clipboard.
64 public static boolean isHTMLClipboardSupported() {
65 return Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN;
69 * @see android.view.View#setLayoutDirection(int)
71 public static void setLayoutDirection(View view, int layoutDirection) {
72 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
73 view.setLayoutDirection(layoutDirection);
75 // Do nothing. RTL layouts aren't supported before JB MR1.
80 * @see android.view.View#setTextDirection(int)
82 public static void setTextAlignment(View view, int textAlignment) {
83 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
84 view.setTextAlignment(textAlignment);
86 // Do nothing. RTL text isn't supported before JB MR1.
91 * @see android.view.ViewGroup.MarginLayoutParams#setMarginEnd(int)
93 public static void setMarginEnd(MarginLayoutParams layoutParams, int end) {
94 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
95 layoutParams.setMarginEnd(end);
97 layoutParams.rightMargin = end;
102 * @see android.view.ViewGroup.MarginLayoutParams#getMarginEnd()
104 public static int getMarginEnd(MarginLayoutParams layoutParams) {
105 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
106 return layoutParams.getMarginEnd();
108 return layoutParams.rightMargin;
113 * @see android.view.ViewGroup.MarginLayoutParams#setMarginStart(int)
115 public static void setMarginStart(MarginLayoutParams layoutParams, int start) {
116 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
117 layoutParams.setMarginStart(start);
119 layoutParams.leftMargin = start;
124 * @see android.view.ViewGroup.MarginLayoutParams#getMarginStart()
126 public static int getMarginStart(MarginLayoutParams layoutParams) {
127 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
128 return layoutParams.getMarginStart();
130 return layoutParams.leftMargin;
135 * @see android.view.View#setPaddingRelative(int, int, int, int)
137 public static void setPaddingRelative(View view, int start, int top, int end, int bottom) {
138 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
139 view.setPaddingRelative(start, top, end, bottom);
141 // Before JB MR1, all layouts are left-to-right, so start == left, etc.
142 view.setPadding(start, top, end, bottom);
147 * @see android.view.View#getPaddingStart()
149 public static int getPaddingStart(View view) {
150 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
151 return view.getPaddingStart();
153 // Before JB MR1, all layouts are left-to-right, so start == left.
154 return view.getPaddingLeft();
159 * @see android.view.View#getPaddingEnd()
161 public static int getPaddingEnd(View view) {
162 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
163 return view.getPaddingEnd();
165 // Before JB MR1, all layouts are left-to-right, so end == right.
166 return view.getPaddingRight();
171 * @see android.widget.TextView#setCompoundDrawablesRelative(Drawable, Drawable, Drawable,
174 public static void setCompoundDrawablesRelative(TextView textView, Drawable start, Drawable top,
175 Drawable end, Drawable bottom) {
176 if (Build.VERSION.SDK_INT == Build.VERSION_CODES.JELLY_BEAN_MR1) {
177 // On JB MR1, due to a platform bug, setCompoundDrawablesRelative() is a no-op if the
178 // view has ever been measured. As a workaround, use setCompoundDrawables() directly.
179 // See: http://crbug.com/368196 and http://crbug.com/361709
180 boolean isRtl = isLayoutRtl(textView);
181 textView.setCompoundDrawables(isRtl ? end : start, top, isRtl ? start : end, bottom);
182 } else if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR1) {
183 textView.setCompoundDrawablesRelative(start, top, end, bottom);
185 textView.setCompoundDrawables(start, top, end, bottom);
190 * @see android.widget.TextView#setCompoundDrawablesRelativeWithIntrinsicBounds(Drawable,
191 * Drawable, Drawable, Drawable)
193 public static void setCompoundDrawablesRelativeWithIntrinsicBounds(TextView textView,
194 Drawable start, Drawable top, Drawable end, Drawable bottom) {
195 if (Build.VERSION.SDK_INT == Build.VERSION_CODES.JELLY_BEAN_MR1) {
196 // Work around the platform bug described in setCompoundDrawablesRelative() above.
197 boolean isRtl = isLayoutRtl(textView);
198 textView.setCompoundDrawablesWithIntrinsicBounds(isRtl ? end : start, top,
199 isRtl ? start : end, bottom);
200 } else if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR1) {
201 textView.setCompoundDrawablesRelativeWithIntrinsicBounds(start, top, end, bottom);
203 textView.setCompoundDrawablesWithIntrinsicBounds(start, top, end, bottom);
208 * @see android.widget.TextView#setCompoundDrawablesRelativeWithIntrinsicBounds(int, int, int,
211 public static void setCompoundDrawablesRelativeWithIntrinsicBounds(TextView textView,
212 int start, int top, int end, int bottom) {
213 if (Build.VERSION.SDK_INT == Build.VERSION_CODES.JELLY_BEAN_MR1) {
214 // Work around the platform bug described in setCompoundDrawablesRelative() above.
215 boolean isRtl = isLayoutRtl(textView);
216 textView.setCompoundDrawablesWithIntrinsicBounds(isRtl ? end : start, top,
217 isRtl ? start : end, bottom);
218 } else if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR1) {
219 textView.setCompoundDrawablesRelativeWithIntrinsicBounds(start, top, end, bottom);
221 textView.setCompoundDrawablesWithIntrinsicBounds(start, top, end, bottom);
226 * @see android.view.View#postInvalidateOnAnimation()
228 public static void postInvalidateOnAnimation(View view) {
229 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
230 view.postInvalidateOnAnimation();
232 view.postInvalidate();
237 * @see android.widget.RemoteViews#setContentDescription(int, CharSequence)
239 public static void setContentDescriptionForRemoteView(RemoteViews remoteViews, int viewId,
240 CharSequence contentDescription) {
241 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
242 remoteViews.setContentDescription(viewId, contentDescription);
244 // setContentDescription() is unavailable in earlier versions.
248 // These methods have a new name, and the old name is deprecated.
251 * @see android.view.View#setBackground(Drawable)
253 @SuppressWarnings("deprecation")
254 public static void setBackgroundForView(View view, Drawable drawable) {
255 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
256 view.setBackground(drawable);
258 view.setBackgroundDrawable(drawable);
263 * @see android.view.ViewTreeObserver#removeOnGlobalLayoutListener()
265 @SuppressWarnings("deprecation")
266 public static void removeOnGlobalLayoutListener(
267 View view, ViewTreeObserver.OnGlobalLayoutListener listener) {
268 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
269 view.getViewTreeObserver().removeOnGlobalLayoutListener(listener);
271 view.getViewTreeObserver().removeGlobalOnLayoutListener(listener);
276 * @see android.widget.ImageView#setImageAlpha(int)
278 @SuppressWarnings("deprecation")
279 public static void setImageAlpha(ImageView iv, int alpha) {
280 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
281 iv.setImageAlpha(alpha);
288 * @see android.app.PendingIntent#getCreatorPackage()
290 @SuppressWarnings("deprecation")
291 public static String getCreatorPackage(PendingIntent intent) {
292 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
293 return intent.getCreatorPackage();
295 return intent.getTargetPackage();