1868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
2868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// found in the LICENSE file.
4868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
5868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)package org.chromium.base;
6868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
71320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucciimport android.animation.ValueAnimator;
81320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucciimport android.app.ActivityOptions;
91320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucciimport android.app.Notification;
10a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)import android.app.PendingIntent;
111320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucciimport android.content.Context;
121320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucciimport android.content.Intent;
13a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)import android.content.res.Configuration;
14868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)import android.graphics.drawable.Drawable;
15868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)import android.os.Build;
161320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucciimport android.os.Bundle;
17868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)import android.view.View;
18868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)import android.view.ViewGroup.MarginLayoutParams;
19868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)import android.view.ViewTreeObserver;
20a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)import android.widget.ImageView;
21f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)import android.widget.RemoteViews;
22f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)import android.widget.TextView;
23868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
24868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)/**
25868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) * Utility class to use new APIs that were added after ICS (API level 14).
26868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) */
27868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)public class ApiCompatibilityUtils {
28868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
29a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    private static final String TAG = "ApiCompatibilityUtils";
30a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch
31868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    private ApiCompatibilityUtils() {
32868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    }
33868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
34868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    /**
35868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)     * Returns true if view's layout direction is right-to-left.
36868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)     *
37868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)     * @param view the View whose layout is being considered
38868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)     */
39868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    public static boolean isLayoutRtl(View view) {
40868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
41868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)            return view.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL;
42868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        } else {
43868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)            // All layouts are LTR before JB MR1.
44868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)            return false;
45868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        }
46868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    }
47868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
48868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    /**
49a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)     * @see Configuration#getLayoutDirection()
50a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)     */
51a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    public static int getLayoutDirection(Configuration configuration) {
52a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
53a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)            return configuration.getLayoutDirection();
54a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        } else {
55a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)            // All layouts are LTR before JB MR1.
56a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)            return View.LAYOUT_DIRECTION_LTR;
57a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        }
58a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    }
59a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
60a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    /**
61f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)     * @return True if the running version of the Android supports printing.
62f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)     */
63f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    public static boolean isPrintingSupported() {
64f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
65f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    }
66f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
67f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    /**
68a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)     * @return True if the running version of the Android supports HTML clipboard.
69a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)     */
70a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    public static boolean isHTMLClipboardSupported() {
71a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN;
72a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    }
73a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
74a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    /**
75868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)     * @see android.view.View#setLayoutDirection(int)
76868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)     */
77868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    public static void setLayoutDirection(View view, int layoutDirection) {
78868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
79868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)            view.setLayoutDirection(layoutDirection);
80868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        } else {
81868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)            // Do nothing. RTL layouts aren't supported before JB MR1.
82868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        }
83868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    }
84868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
85868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    /**
86ab8f6f0bd665d3c1ff476eb06c58c42630e462d4Ben Murdoch     * @see android.view.View#setTextAlignment(int)
87f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)     */
88f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    public static void setTextAlignment(View view, int textAlignment) {
89f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
90f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)            view.setTextAlignment(textAlignment);
91f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        } else {
92f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)            // Do nothing. RTL text isn't supported before JB MR1.
93f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        }
94ab8f6f0bd665d3c1ff476eb06c58c42630e462d4Ben Murdoch    }
95ab8f6f0bd665d3c1ff476eb06c58c42630e462d4Ben Murdoch
96ab8f6f0bd665d3c1ff476eb06c58c42630e462d4Ben Murdoch    /**
97ab8f6f0bd665d3c1ff476eb06c58c42630e462d4Ben Murdoch     * @see android.view.View#setTextDirection(int)
98ab8f6f0bd665d3c1ff476eb06c58c42630e462d4Ben Murdoch     */
99ab8f6f0bd665d3c1ff476eb06c58c42630e462d4Ben Murdoch    public static void setTextDirection(View view, int textDirection) {
100ab8f6f0bd665d3c1ff476eb06c58c42630e462d4Ben Murdoch        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
101ab8f6f0bd665d3c1ff476eb06c58c42630e462d4Ben Murdoch            view.setTextDirection(textDirection);
102ab8f6f0bd665d3c1ff476eb06c58c42630e462d4Ben Murdoch        } else {
103ab8f6f0bd665d3c1ff476eb06c58c42630e462d4Ben Murdoch            // Do nothing. RTL text isn't supported before JB MR1.
104ab8f6f0bd665d3c1ff476eb06c58c42630e462d4Ben Murdoch        }
105f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    }
106f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
107f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    /**
108868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)     * @see android.view.ViewGroup.MarginLayoutParams#setMarginEnd(int)
109868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)     */
110868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    public static void setMarginEnd(MarginLayoutParams layoutParams, int end) {
111868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
112868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)            layoutParams.setMarginEnd(end);
113868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        } else {
114868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)            layoutParams.rightMargin = end;
115868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        }
116868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    }
117868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
118868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    /**
1193551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)     * @see android.view.ViewGroup.MarginLayoutParams#getMarginEnd()
1203551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)     */
1213551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    public static int getMarginEnd(MarginLayoutParams layoutParams) {
1223551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
1233551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)            return layoutParams.getMarginEnd();
1243551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)        } else {
1253551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)            return layoutParams.rightMargin;
1263551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)        }
1273551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    }
1283551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
1293551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    /**
1307dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch     * @see android.view.ViewGroup.MarginLayoutParams#setMarginStart(int)
1317dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch     */
1327dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    public static void setMarginStart(MarginLayoutParams layoutParams, int start) {
1337dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
1347dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch            layoutParams.setMarginStart(start);
1357dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        } else {
1367dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch            layoutParams.leftMargin = start;
1377dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        }
1387dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    }
1397dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
1407dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    /**
141868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)     * @see android.view.ViewGroup.MarginLayoutParams#getMarginStart()
142868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)     */
143868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    public static int getMarginStart(MarginLayoutParams layoutParams) {
144868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
145868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)            return layoutParams.getMarginStart();
146868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        } else {
147868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)            return layoutParams.leftMargin;
148868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        }
149868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    }
150868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
151868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    /**
152f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)     * @see android.view.View#setPaddingRelative(int, int, int, int)
153f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)     */
154f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    public static void setPaddingRelative(View view, int start, int top, int end, int bottom) {
155f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
156f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)            view.setPaddingRelative(start, top, end, bottom);
157f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        } else {
158f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)            // Before JB MR1, all layouts are left-to-right, so start == left, etc.
159f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)            view.setPadding(start, top, end, bottom);
160f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        }
161f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    }
162f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
163f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    /**
164f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)     * @see android.view.View#getPaddingStart()
165f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)     */
1665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    public static int getPaddingStart(View view) {
167f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
1685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)            return view.getPaddingStart();
169f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        } else {
170f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)            // Before JB MR1, all layouts are left-to-right, so start == left.
1715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)            return view.getPaddingLeft();
172f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        }
173f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    }
174f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
175f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    /**
176f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)     * @see android.view.View#getPaddingEnd()
177f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)     */
1785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    public static int getPaddingEnd(View view) {
179f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
1805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)            return view.getPaddingEnd();
181f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        } else {
182f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)            // Before JB MR1, all layouts are left-to-right, so end == right.
1835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)            return view.getPaddingRight();
184f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        }
185f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    }
186f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
187f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    /**
188f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)     * @see android.widget.TextView#setCompoundDrawablesRelative(Drawable, Drawable, Drawable,
189f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)     *      Drawable)
190f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)     */
191f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    public static void setCompoundDrawablesRelative(TextView textView, Drawable start, Drawable top,
192f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)            Drawable end, Drawable bottom) {
1935c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu        if (Build.VERSION.SDK_INT == Build.VERSION_CODES.JELLY_BEAN_MR1) {
1945c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu            // On JB MR1, due to a platform bug, setCompoundDrawablesRelative() is a no-op if the
1955c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu            // view has ever been measured. As a workaround, use setCompoundDrawables() directly.
1965c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu            // See: http://crbug.com/368196 and http://crbug.com/361709
1975c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu            boolean isRtl = isLayoutRtl(textView);
1985c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu            textView.setCompoundDrawables(isRtl ? end : start, top, isRtl ? start : end, bottom);
1995c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu        } else if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR1) {
2005c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu            textView.setCompoundDrawablesRelative(start, top, end, bottom);
201f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        } else {
2025c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu            textView.setCompoundDrawables(start, top, end, bottom);
203f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        }
204f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    }
205f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
206f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    /**
207f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)     * @see android.widget.TextView#setCompoundDrawablesRelativeWithIntrinsicBounds(Drawable,
208f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)     *      Drawable, Drawable, Drawable)
209f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)     */
210f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    public static void setCompoundDrawablesRelativeWithIntrinsicBounds(TextView textView,
211f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)            Drawable start, Drawable top, Drawable end, Drawable bottom) {
2125c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu        if (Build.VERSION.SDK_INT == Build.VERSION_CODES.JELLY_BEAN_MR1) {
2135c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu            // Work around the platform bug described in setCompoundDrawablesRelative() above.
2145c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu            boolean isRtl = isLayoutRtl(textView);
2155c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu            textView.setCompoundDrawablesWithIntrinsicBounds(isRtl ? end : start, top,
2165c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu                    isRtl ? start : end, bottom);
2175c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu        } else if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR1) {
2185c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu            textView.setCompoundDrawablesRelativeWithIntrinsicBounds(start, top, end, bottom);
219f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        } else {
2205c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu            textView.setCompoundDrawablesWithIntrinsicBounds(start, top, end, bottom);
221f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        }
222f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    }
223f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
224f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    /**
225f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)     * @see android.widget.TextView#setCompoundDrawablesRelativeWithIntrinsicBounds(int, int, int,
226f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)     *      int)
227f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)     */
228f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    public static void setCompoundDrawablesRelativeWithIntrinsicBounds(TextView textView,
229f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)            int start, int top, int end, int bottom) {
2305c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu        if (Build.VERSION.SDK_INT == Build.VERSION_CODES.JELLY_BEAN_MR1) {
2315c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu            // Work around the platform bug described in setCompoundDrawablesRelative() above.
2325c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu            boolean isRtl = isLayoutRtl(textView);
2335c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu            textView.setCompoundDrawablesWithIntrinsicBounds(isRtl ? end : start, top,
2345c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu                    isRtl ? start : end, bottom);
2355c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu        } else if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR1) {
2365c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu            textView.setCompoundDrawablesRelativeWithIntrinsicBounds(start, top, end, bottom);
237f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        } else {
2385c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu            textView.setCompoundDrawablesWithIntrinsicBounds(start, top, end, bottom);
239f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        }
240f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    }
241f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
242f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    /**
243868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)     * @see android.view.View#postInvalidateOnAnimation()
244868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)     */
245868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    public static void postInvalidateOnAnimation(View view) {
246868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
247868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)            view.postInvalidateOnAnimation();
248868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        } else {
2497d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)            view.postInvalidate();
250868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        }
251868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    }
252868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
253f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    /**
2541320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci     * @see android.view.View#postOnAnimation()
2551320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci     */
2561320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    public static void postOnAnimation(View view, Runnable action) {
2571320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
2581320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci            view.postOnAnimation(action);
2591320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        } else {
2601320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci            view.postDelayed(action, getFrameTime());
2611320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        }
2621320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    }
2631320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
2641320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    /**
2651320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci     * @see android.view.View#postOnAnimationDelayed()
2661320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci     */
2671320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    public static void postOnAnimationDelayed(View view, Runnable action, long delayMillis) {
2681320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
2691320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci            view.postOnAnimationDelayed(action, delayMillis);
2701320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        } else {
2711320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci            view.postDelayed(action, getFrameTime() + delayMillis);
2721320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        }
2731320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    }
2741320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
2751320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    private static long getFrameTime() {
2761320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
2771320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci            return ValueAnimator.getFrameDelay();
2781320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        } else {
2791320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci            // Any reasonable fake frame delay will have to do.
2801320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci            return 10;
2811320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        }
2821320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    }
2831320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
2841320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    /**
285f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)     * @see android.widget.RemoteViews#setContentDescription(int, CharSequence)
286f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)     */
287f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    public static void setContentDescriptionForRemoteView(RemoteViews remoteViews, int viewId,
288f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)            CharSequence contentDescription) {
289f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
290f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)            remoteViews.setContentDescription(viewId, contentDescription);
291f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        } else {
292f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)            // setContentDescription() is unavailable in earlier versions.
293f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        }
294f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    }
295f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
2961320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    /**
2971320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci     * @see android.app.Activity#startActivity(Intent, Bundle)
2981320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci     */
2991320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    public static void startActivity(Context context, Intent intent, Bundle options) {
3001320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
3011320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci            context.startActivity(intent, options);
3021320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        } else {
3031320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci            context.startActivity(intent);
3041320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        }
3051320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    }
3061320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
3071320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    /**
3081320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci     * @see android.app.ActivityOptions#toBundle()
3091320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci     */
3101320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    public static Bundle toBundle(ActivityOptions options) {
3111320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
3121320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci            return options.toBundle();
3131320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        } else {
3141320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci            return null;
3151320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        }
3161320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    }
3171320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
318868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    // These methods have a new name, and the old name is deprecated.
319868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
320868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    /**
321868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)     * @see android.view.View#setBackground(Drawable)
322868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)     */
323868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    @SuppressWarnings("deprecation")
324868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    public static void setBackgroundForView(View view, Drawable drawable) {
325868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
326868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)            view.setBackground(drawable);
327868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        } else {
328868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)            view.setBackgroundDrawable(drawable);
329868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        }
330868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    }
331868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
332868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    /**
333868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)     * @see android.view.ViewTreeObserver#removeOnGlobalLayoutListener()
334868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)     */
335868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    @SuppressWarnings("deprecation")
336868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    public static void removeOnGlobalLayoutListener(
337868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)            View view, ViewTreeObserver.OnGlobalLayoutListener listener) {
338868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
339868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)            view.getViewTreeObserver().removeOnGlobalLayoutListener(listener);
340868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        } else {
341868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)            view.getViewTreeObserver().removeGlobalOnLayoutListener(listener);
342868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        }
343868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    }
344a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
345a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    /**
346a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)     * @see android.widget.ImageView#setImageAlpha(int)
347a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)     */
348a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    @SuppressWarnings("deprecation")
349a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    public static void setImageAlpha(ImageView iv, int alpha) {
350a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
351a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)            iv.setImageAlpha(alpha);
352a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        } else {
353a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)            iv.setAlpha(alpha);
354a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        }
355a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    }
356a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
357a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    /**
358a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)     * @see android.app.PendingIntent#getCreatorPackage()
359a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)     */
360a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    @SuppressWarnings("deprecation")
361a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    public static String getCreatorPackage(PendingIntent intent) {
362a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
363a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)            return intent.getCreatorPackage();
364a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        } else {
365a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)            return intent.getTargetPackage();
366a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        }
367a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    }
3685f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
3691320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    /**
3701320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci     * @see android.app.Notification.Builder#setLocalOnly(boolean)
3711320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci     */
3721320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    @SuppressWarnings("deprecation")
3731320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    public static Notification build(Notification.Builder builder) {
3741320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
3751320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci            return builder.build();
3761320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        } else {
3771320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci            return builder.getNotification();
3781320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        }
3795f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    }
380868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
381