blob: 895b757f7a1d12ae5165b134752ae5483f682561 [file] [log] [blame]
Chiao Cheng0a736602012-12-05 12:06:58 -08001/*
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
17package com.android.contacts.common.util;
18
Andrew Leef2032d22014-04-24 14:37:37 -070019import android.content.res.Resources;
20import android.graphics.Outline;
Chiao Cheng0a736602012-12-05 12:06:58 -080021import android.view.View;
22import android.view.ViewGroup;
Chris Craika718cfd2014-07-11 15:57:17 -070023import android.view.ViewOutlineProvider;
Yorke Leeb372e482014-05-22 15:51:04 -070024import android.widget.ListView;
Chiao Cheng0a736602012-12-05 12:06:58 -080025
Andrew Leef2032d22014-04-24 14:37:37 -070026import com.android.contacts.common.R;
Wenyi Wang54ea4b12015-12-16 14:18:59 -080027import com.android.contacts.common.compat.CompatUtils;
Andrew Leef2032d22014-04-24 14:37:37 -070028
Chiao Cheng0a736602012-12-05 12:06:58 -080029/**
30 * Provides static functions to work with views
31 */
32public class 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 */
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 }
Yorke Lee50a89a52013-11-04 14:44:30 -080049
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 */
56 public static boolean isViewLayoutRtl(View view) {
57 return view.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL;
58 }
Andrew Leef2032d22014-04-24 14:37:37 -070059
Wenyi Wang54ea4b12015-12-16 14:18:59 -080060 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;
Chris Craika718cfd2014-07-11 15:57:17 -070071 }
Wenyi Wang54ea4b12015-12-16 14:18:59 -080072 }
Chris Craika718cfd2014-07-11 15:57:17 -070073
Wenyi Wang54ea4b12015-12-16 14:18:59 -080074 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;
Brian Attwellf27dd322014-08-07 11:10:44 -070085 }
Wenyi Wang54ea4b12015-12-16 14:18:59 -080086 }
Brian Attwellf27dd322014-08-07 11:10:44 -070087
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 */
94 public static void addRectangularOutlineProvider(View view, Resources res) {
Wenyi Wang54ea4b12015-12-16 14:18:59 -080095 if (CompatUtils.isLollipopCompatible()) {
96 view.setOutlineProvider(RECT_OUTLINE_PROVIDER);
97 }
Brian Attwellf27dd322014-08-07 11:10:44 -070098 }
99
Andrew Leef2032d22014-04-24 14:37:37 -0700100 /**
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 */
105 public static void setupFloatingActionButton(View view, Resources res) {
Wenyi Wang54ea4b12015-12-16 14:18:59 -0800106 if (CompatUtils.isLollipopCompatible()) {
107 view.setOutlineProvider(OVAL_OUTLINE_PROVIDER);
108 view.setTranslationZ(
109 res.getDimensionPixelSize(R.dimen.floating_action_button_translation_z));
110 }
Andrew Leef2032d22014-04-24 14:37:37 -0700111 }
Yorke Leeb372e482014-05-22 15:51:04 -0700112
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 */
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 }
Chiao Cheng0a736602012-12-05 12:06:58 -0800127}