1 /*
2  * Copyright (C) 2012 The Android Open Source Project
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.contacts.util;
18 
19 import android.content.res.Resources;
20 import android.graphics.Outline;
21 import android.view.View;
22 import android.view.ViewGroup;
23 import android.view.ViewOutlineProvider;
24 import android.widget.ListView;
25 
26 import com.android.contacts.R;
27 import com.android.contacts.compat.CompatUtils;
28 
29 /**
30  * Provides static functions to work with views
31  */
32 public class ViewUtil {
ViewUtil()33     private ViewUtil() {}
34 
35     /**
36      * Returns the width as specified in the LayoutParams
37      * @throws IllegalStateException Thrown if the view's width is unknown before a layout pass
38      * s
39      */
getConstantPreLayoutWidth(View view)40     public static int getConstantPreLayoutWidth(View view) {
41         // We haven't been layed out yet, so get the size from the LayoutParams
42         final ViewGroup.LayoutParams p = view.getLayoutParams();
43         if (p.width < 0) {
44             throw new IllegalStateException("Expecting view's width to be a constant rather " +
45                     "than a result of the layout pass");
46         }
47         return p.width;
48     }
49 
50     /**
51      * Returns a boolean indicating whether or not the view's layout direction is RTL
52      *
53      * @param view - A valid view
54      * @return True if the view's layout direction is RTL
55      */
isViewLayoutRtl(View view)56     public static boolean isViewLayoutRtl(View view) {
57         return view.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL;
58     }
59 
60     private static final ViewOutlineProvider OVAL_OUTLINE_PROVIDER;
61     static {
62         if (CompatUtils.isLollipopCompatible()) {
63             OVAL_OUTLINE_PROVIDER = new ViewOutlineProvider() {
64                 @Override
65                 public void getOutline(View view, Outline outline) {
66                     outline.setOval(0, 0, view.getWidth(), view.getHeight());
67                 }
68             };
69         } else {
70             OVAL_OUTLINE_PROVIDER = null;
71         }
72     }
73 
74     private static final ViewOutlineProvider RECT_OUTLINE_PROVIDER;
75     static {
76         if (CompatUtils.isLollipopCompatible()) {
77             RECT_OUTLINE_PROVIDER = new ViewOutlineProvider() {
78                 @Override
79                 public void getOutline(View view, Outline outline) {
80                     outline.setRect(0, 0, view.getWidth(), view.getHeight());
81                 }
82             };
83         } else {
84             RECT_OUTLINE_PROVIDER = null;
85         }
86     }
87 
88     /**
89      * Adds a rectangular outline to a view. This can be useful when you want to add a shadow
90      * to a transparent view. See b/16856049.
91      * @param view view that the outline is added to
92      * @param res The resources file.
93      */
addRectangularOutlineProvider(View view, Resources res)94     public static void addRectangularOutlineProvider(View view, Resources res) {
95         if (CompatUtils.isLollipopCompatible()) {
96             view.setOutlineProvider(RECT_OUTLINE_PROVIDER);
97         }
98     }
99 
100     /**
101      * Configures the floating action button, clipping it to a circle and setting its translation z.
102      * @param view The float action button's view.
103      * @param res The resources file.
104      */
setupFloatingActionButton(View view, Resources res)105     public static void setupFloatingActionButton(View view, Resources res) {
106         if (CompatUtils.isLollipopCompatible()) {
107             view.setOutlineProvider(OVAL_OUTLINE_PROVIDER);
108             view.setTranslationZ(
109                     res.getDimensionPixelSize(R.dimen.floating_action_button_translation_z));
110         }
111     }
112 
113     /**
114      * Adds padding to the bottom of the given {@link ListView} so that the floating action button
115      * does not obscure any content.
116      *
117      * @param listView to add the padding to
118      * @param res valid resources object
119      */
addBottomPaddingToListViewForFab(ListView listView, Resources res)120     public static void addBottomPaddingToListViewForFab(ListView listView, Resources res) {
121         final int fabPadding = res.getDimensionPixelSize(
122                 R.dimen.floating_action_button_list_bottom_padding);
123         listView.setPaddingRelative(listView.getPaddingStart(), listView.getPaddingTop(),
124                 listView.getPaddingEnd(), listView.getPaddingBottom() + fabPadding);
125         listView.setClipToPadding(false);
126     }
127 }
128