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