Chiao Cheng | 0a73660 | 2012-12-05 12:06:58 -0800 | [diff] [blame] | 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 | |
Andrew Lee | f2032d2 | 2014-04-24 14:37:37 -0700 | [diff] [blame] | 19 | import android.content.res.Resources; |
| 20 | import android.graphics.Outline; |
Chiao Cheng | 0a73660 | 2012-12-05 12:06:58 -0800 | [diff] [blame] | 21 | import android.view.View; |
| 22 | import android.view.ViewGroup; |
Chris Craik | a718cfd | 2014-07-11 15:57:17 -0700 | [diff] [blame] | 23 | import android.view.ViewOutlineProvider; |
Yorke Lee | b372e48 | 2014-05-22 15:51:04 -0700 | [diff] [blame] | 24 | import android.widget.ListView; |
Chiao Cheng | 0a73660 | 2012-12-05 12:06:58 -0800 | [diff] [blame] | 25 | |
Andrew Lee | f2032d2 | 2014-04-24 14:37:37 -0700 | [diff] [blame] | 26 | import com.android.contacts.common.R; |
Wenyi Wang | 54ea4b1 | 2015-12-16 14:18:59 -0800 | [diff] [blame] | 27 | import com.android.contacts.common.compat.CompatUtils; |
Andrew Lee | f2032d2 | 2014-04-24 14:37:37 -0700 | [diff] [blame] | 28 | |
Chiao Cheng | 0a73660 | 2012-12-05 12:06:58 -0800 | [diff] [blame] | 29 | /** |
| 30 | * Provides static functions to work with views |
| 31 | */ |
| 32 | public 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 Lee | 50a89a5 | 2013-11-04 14:44:30 -0800 | [diff] [blame] | 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 | */ |
| 56 | public static boolean isViewLayoutRtl(View view) { |
| 57 | return view.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL; |
| 58 | } |
Andrew Lee | f2032d2 | 2014-04-24 14:37:37 -0700 | [diff] [blame] | 59 | |
Wenyi Wang | 54ea4b1 | 2015-12-16 14:18:59 -0800 | [diff] [blame] | 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; |
Chris Craik | a718cfd | 2014-07-11 15:57:17 -0700 | [diff] [blame] | 71 | } |
Wenyi Wang | 54ea4b1 | 2015-12-16 14:18:59 -0800 | [diff] [blame] | 72 | } |
Chris Craik | a718cfd | 2014-07-11 15:57:17 -0700 | [diff] [blame] | 73 | |
Wenyi Wang | 54ea4b1 | 2015-12-16 14:18:59 -0800 | [diff] [blame] | 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; |
Brian Attwell | f27dd32 | 2014-08-07 11:10:44 -0700 | [diff] [blame] | 85 | } |
Wenyi Wang | 54ea4b1 | 2015-12-16 14:18:59 -0800 | [diff] [blame] | 86 | } |
Brian Attwell | f27dd32 | 2014-08-07 11:10:44 -0700 | [diff] [blame] | 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 | */ |
| 94 | public static void addRectangularOutlineProvider(View view, Resources res) { |
Wenyi Wang | 54ea4b1 | 2015-12-16 14:18:59 -0800 | [diff] [blame] | 95 | if (CompatUtils.isLollipopCompatible()) { |
| 96 | view.setOutlineProvider(RECT_OUTLINE_PROVIDER); |
| 97 | } |
Brian Attwell | f27dd32 | 2014-08-07 11:10:44 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Andrew Lee | f2032d2 | 2014-04-24 14:37:37 -0700 | [diff] [blame] | 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 | */ |
| 105 | public static void setupFloatingActionButton(View view, Resources res) { |
Wenyi Wang | 54ea4b1 | 2015-12-16 14:18:59 -0800 | [diff] [blame] | 106 | if (CompatUtils.isLollipopCompatible()) { |
| 107 | view.setOutlineProvider(OVAL_OUTLINE_PROVIDER); |
| 108 | view.setTranslationZ( |
| 109 | res.getDimensionPixelSize(R.dimen.floating_action_button_translation_z)); |
| 110 | } |
Andrew Lee | f2032d2 | 2014-04-24 14:37:37 -0700 | [diff] [blame] | 111 | } |
Yorke Lee | b372e48 | 2014-05-22 15:51:04 -0700 | [diff] [blame] | 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 | */ |
| 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 Cheng | 0a73660 | 2012-12-05 12:06:58 -0800 | [diff] [blame] | 127 | } |