- add sources.
[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.Notification;
8 import android.graphics.drawable.Drawable;
9 import android.os.Build;
10 import android.view.View;
11 import android.view.ViewGroup.MarginLayoutParams;
12 import android.view.ViewTreeObserver;
13
14 /**
15  * Utility class to use new APIs that were added after ICS (API level 14).
16  */
17 public class ApiCompatibilityUtils {
18
19     private ApiCompatibilityUtils() {
20     }
21
22     /**
23      * Returns true if view's layout direction is right-to-left.
24      *
25      * @param view the View whose layout is being considered
26      */
27     public static boolean isLayoutRtl(View view) {
28         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
29             return view.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL;
30         } else {
31             // All layouts are LTR before JB MR1.
32             return false;
33         }
34     }
35
36     /**
37      * @see android.view.View#setLayoutDirection(int)
38      */
39     public static void setLayoutDirection(View view, int layoutDirection) {
40         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
41             view.setLayoutDirection(layoutDirection);
42         } else {
43             // Do nothing. RTL layouts aren't supported before JB MR1.
44         }
45     }
46
47     /**
48      * @see android.view.ViewGroup.MarginLayoutParams#setMarginEnd(int)
49      */
50     public static void setMarginEnd(MarginLayoutParams layoutParams, int end) {
51         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
52             layoutParams.setMarginEnd(end);
53         } else {
54             layoutParams.rightMargin = end;
55         }
56     }
57
58     /**
59      * @see android.view.ViewGroup.MarginLayoutParams#getMarginEnd()
60      */
61     public static int getMarginEnd(MarginLayoutParams layoutParams) {
62         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
63             return layoutParams.getMarginEnd();
64         } else {
65             return layoutParams.rightMargin;
66         }
67     }
68
69     /**
70      * @see android.view.ViewGroup.MarginLayoutParams#setMarginStart(int)
71      */
72     public static void setMarginStart(MarginLayoutParams layoutParams, int start) {
73         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
74             layoutParams.setMarginStart(start);
75         } else {
76             layoutParams.leftMargin = start;
77         }
78     }
79
80     /**
81      * @see android.view.ViewGroup.MarginLayoutParams#getMarginStart()
82      */
83     public static int getMarginStart(MarginLayoutParams layoutParams) {
84         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
85             return layoutParams.getMarginStart();
86         } else {
87             return layoutParams.leftMargin;
88         }
89     }
90
91     /**
92      * @see android.view.View#postInvalidateOnAnimation()
93      */
94     public static void postInvalidateOnAnimation(View view) {
95         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
96             view.postInvalidateOnAnimation();
97         } else {
98             view.postInvalidate();
99         }
100     }
101
102     // These methods have a new name, and the old name is deprecated.
103
104     /**
105      * @see android.view.View#setBackground(Drawable)
106      */
107     @SuppressWarnings("deprecation")
108     public static void setBackgroundForView(View view, Drawable drawable) {
109         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
110             view.setBackground(drawable);
111         } else {
112             view.setBackgroundDrawable(drawable);
113         }
114     }
115
116     /**
117      * @see android.view.ViewTreeObserver#removeOnGlobalLayoutListener()
118      */
119     @SuppressWarnings("deprecation")
120     public static void removeOnGlobalLayoutListener(
121             View view, ViewTreeObserver.OnGlobalLayoutListener listener) {
122         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
123             view.getViewTreeObserver().removeOnGlobalLayoutListener(listener);
124         } else {
125             view.getViewTreeObserver().removeGlobalOnLayoutListener(listener);
126         }
127     }
128
129     /**
130      * @see android.app.Notification.Builder#build()
131      */
132     @SuppressWarnings("deprecation")
133     public static Notification buildNotification(Notification.Builder builder) {
134         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
135             return builder.build();
136         } else {
137             return builder.getNotification();
138         }
139     }
140 }