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 ApiCompatibilityUtils() {
27 * Returns true if view's layout direction is right-to-left.
29 * @param view the View whose layout is being considered
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;
35 // All layouts are LTR before JB MR1.
41 * @see Configuration#getLayoutDirection()
43 public static int getLayoutDirection(Configuration configuration) {
44 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
45 return configuration.getLayoutDirection();
47 // All layouts are LTR before JB MR1.
48 return View.LAYOUT_DIRECTION_LTR;
53 * @return True if the running version of the Android supports printing.
55 public static boolean isPrintingSupported() {
56 return Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
60 * @return True if the running version of the Android supports HTML clipboard.
62 public static boolean isHTMLClipboardSupported() {
63 return Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN;
67 * @see android.view.View#setLayoutDirection(int)
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);
73 // Do nothing. RTL layouts aren't supported before JB MR1.
78 * @see android.view.View#setTextDirection(int)
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);
84 // Do nothing. RTL text isn't supported before JB MR1.
89 * @see android.view.ViewGroup.MarginLayoutParams#setMarginEnd(int)
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);
95 layoutParams.rightMargin = end;
100 * @see android.view.ViewGroup.MarginLayoutParams#getMarginEnd()
102 public static int getMarginEnd(MarginLayoutParams layoutParams) {
103 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
104 return layoutParams.getMarginEnd();
106 return layoutParams.rightMargin;
111 * @see android.view.ViewGroup.MarginLayoutParams#setMarginStart(int)
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);
117 layoutParams.leftMargin = start;
122 * @see android.view.ViewGroup.MarginLayoutParams#getMarginStart()
124 public static int getMarginStart(MarginLayoutParams layoutParams) {
125 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
126 return layoutParams.getMarginStart();
128 return layoutParams.leftMargin;
133 * @see android.view.View#setPaddingRelative(int, int, int, int)
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);
139 // Before JB MR1, all layouts are left-to-right, so start == left, etc.
140 view.setPadding(start, top, end, bottom);
145 * @see android.view.View#getPaddingStart()
147 public static int getPaddingStart(View view) {
148 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
149 return view.getPaddingStart();
151 // Before JB MR1, all layouts are left-to-right, so start == left.
152 return view.getPaddingLeft();
157 * @see android.view.View#getPaddingEnd()
159 public static int getPaddingEnd(View view) {
160 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
161 return view.getPaddingEnd();
163 // Before JB MR1, all layouts are left-to-right, so end == right.
164 return view.getPaddingRight();
169 * @see android.widget.TextView#setCompoundDrawablesRelative(Drawable, Drawable, Drawable,
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);
177 textView.setCompoundDrawables(start, top, bottom, end);
182 * @see android.widget.TextView#setCompoundDrawablesRelativeWithIntrinsicBounds(Drawable,
183 * Drawable, Drawable, Drawable)
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);
190 textView.setCompoundDrawablesWithIntrinsicBounds(start, top, bottom, end);
195 * @see android.widget.TextView#setCompoundDrawablesRelativeWithIntrinsicBounds(int, int, int,
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);
203 textView.setCompoundDrawablesWithIntrinsicBounds(start, top, bottom, end);
208 * @see android.view.View#postInvalidateOnAnimation()
210 public static void postInvalidateOnAnimation(View view) {
211 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
212 view.postInvalidateOnAnimation();
214 view.postInvalidate();
219 * @see android.widget.RemoteViews#setContentDescription(int, CharSequence)
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);
226 // setContentDescription() is unavailable in earlier versions.
230 // These methods have a new name, and the old name is deprecated.
233 * @see android.view.View#setBackground(Drawable)
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);
240 view.setBackgroundDrawable(drawable);
245 * @see android.view.ViewTreeObserver#removeOnGlobalLayoutListener()
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);
253 view.getViewTreeObserver().removeGlobalOnLayoutListener(listener);
258 * @see android.widget.ImageView#setImageAlpha(int)
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);
270 * @see android.app.PendingIntent#getCreatorPackage()
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();
277 return intent.getTargetPackage();