• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2013, Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.mail.utils;
18 
19 import android.annotation.SuppressLint;
20 import android.app.Activity;
21 import android.content.Context;
22 import android.support.annotation.ColorRes;
23 import android.support.v4.view.ViewCompat;
24 import android.view.View;
25 import android.view.ViewParent;
26 import android.view.Window;
27 import android.view.accessibility.AccessibilityEvent;
28 import android.view.accessibility.AccessibilityManager;
29 
30 /**
31  * Utility class to perform some common operations on views.
32  */
33 public class ViewUtils {
34 
35     /**
36      * Determines whether the given view has RTL layout. NOTE: do not call this
37      * on a view until it has been measured. This value is not guaranteed to be
38      * accurate until then.
39      */
isViewRtl(View view)40     public static boolean isViewRtl(View view) {
41         return ViewCompat.getLayoutDirection(view) == ViewCompat.LAYOUT_DIRECTION_RTL;
42     }
43 
44     /**
45      * @return the start padding of the view. Prior to API 17, will return the left padding.
46      */
47     @SuppressLint("NewApi")
getPaddingStart(View view)48     public static int getPaddingStart(View view) {
49         return Utils.isRunningJBMR1OrLater() ? view.getPaddingStart() : view.getPaddingLeft();
50     }
51 
52     /**
53      * @return the end padding of the view. Prior to API 17, will return the right padding.
54      */
55     @SuppressLint("NewApi")
getPaddingEnd(View view)56     public static int getPaddingEnd(View view) {
57         return Utils.isRunningJBMR1OrLater() ? view.getPaddingEnd() : view.getPaddingRight();
58     }
59 
60     /**
61      * Sets the text alignment of the view. Prior to API 17, will no-op.
62      */
63     @SuppressLint("NewApi")
setTextAlignment(View view, int textAlignment)64     public static void setTextAlignment(View view, int textAlignment) {
65         if (Utils.isRunningJBMR1OrLater()) {
66             view.setTextAlignment(textAlignment);
67         }
68     }
69 
70     /**
71      * Convenience method for sending a {@link android.view.accessibility.AccessibilityEvent#TYPE_ANNOUNCEMENT}
72      * {@link android.view.accessibility.AccessibilityEvent} to make an announcement which is related to some
73      * sort of a context change for which none of the events representing UI transitions
74      * is a good fit. For example, announcing a new page in a book. If accessibility
75      * is not enabled this method does nothing.
76      *
77      * @param view view to perform the accessibility announcement
78      * @param text The announcement text.
79      */
announceForAccessibility(View view, CharSequence text)80     public static void announceForAccessibility(View view, CharSequence text) {
81         final AccessibilityManager accessibilityManager = (AccessibilityManager)
82                 view.getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
83         final ViewParent parent = view.getParent();
84         if (accessibilityManager.isEnabled() && parent != null) {
85             AccessibilityEvent event = AccessibilityEvent.obtain(
86                     AccessibilityEvent.TYPE_ANNOUNCEMENT);
87             view.onInitializeAccessibilityEvent(event);
88             event.getText().add(text);
89             event.setContentDescription(null);
90             parent.requestSendAccessibilityEvent(view, event);
91         }
92     }
93 
94     /**
95      * Sets the status bar color of the provided activity.
96      */
97     @SuppressLint("NewApi")
setStatusBarColor(Activity activity, @ColorRes int colorId)98     public static void setStatusBarColor(Activity activity, @ColorRes int colorId) {
99         if (Utils.isRunningLOrLater() && activity != null) {
100             final Window window = activity.getWindow();
101             if (window != null) {
102                 window.setStatusBarColor(activity.getResources().getColor(colorId));
103             }
104         }
105     }
106 }
107