Josh Gargus | 7edad9d | 2012-04-19 18:22:11 -0700 | [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.widget; |
| 18 | |
Josh Gargus | 7edad9d | 2012-04-19 18:22:11 -0700 | [diff] [blame] | 19 | import android.content.Context; |
| 20 | import android.view.View; |
Josh Gargus | fc81892 | 2012-04-25 15:08:43 -0700 | [diff] [blame] | 21 | import android.widget.FrameLayout; |
Josh Gargus | 7edad9d | 2012-04-19 18:22:11 -0700 | [diff] [blame] | 22 | |
Paul Soulos | 333091a | 2014-07-22 13:54:41 -0700 | [diff] [blame] | 23 | import com.android.contacts.detail.ContactDisplayUtils; |
Chiao Cheng | e0b2f1e | 2012-06-12 13:07:56 -0700 | [diff] [blame] | 24 | import com.android.contacts.util.ThemeUtils; |
| 25 | |
Josh Gargus | 7edad9d | 2012-04-19 18:22:11 -0700 | [diff] [blame] | 26 | /** |
| 27 | * A View that other Views can use to create a touch-interceptor layer above |
| 28 | * their other sub-views. This layer can be enabled and disabled; when enabled, |
| 29 | * clicks are intercepted and passed to a listener. |
| 30 | * |
| 31 | * Also supports an alpha layer to dim the content underneath. By default, the |
| 32 | * alpha layer is the same View as the touch-interceptor layer. However, for |
| 33 | * some use-cases, you want a few Views to not be dimmed, but still have touches |
| 34 | * intercepted (for example, {@link CarouselTab}'s label appears above the alpha |
| 35 | * layer). In this case, you can specify the View to use as the alpha layer via |
| 36 | * setAlphaLayer(); in this case you are responsible for managing the z-order of |
| 37 | * the alpha-layer with respect to your other sub-views. |
| 38 | * |
| 39 | * Typically, you would not use this class directly, but rather use another class |
| 40 | * that uses it, for example {@link FrameLayoutWithOverlay}. |
| 41 | */ |
Josh Gargus | fc81892 | 2012-04-25 15:08:43 -0700 | [diff] [blame] | 42 | public class AlphaTouchInterceptorOverlay extends FrameLayout { |
Josh Gargus | 7edad9d | 2012-04-19 18:22:11 -0700 | [diff] [blame] | 43 | |
Josh Gargus | fc81892 | 2012-04-25 15:08:43 -0700 | [diff] [blame] | 44 | private View mInterceptorLayer; |
| 45 | private View mAlphaLayer; |
| 46 | private float mAlpha = 0.0f; |
Josh Gargus | 7edad9d | 2012-04-19 18:22:11 -0700 | [diff] [blame] | 47 | |
| 48 | public AlphaTouchInterceptorOverlay(Context context) { |
| 49 | super(context); |
Josh Gargus | fc81892 | 2012-04-25 15:08:43 -0700 | [diff] [blame] | 50 | |
| 51 | mInterceptorLayer = new View(context); |
| 52 | final int resId = ThemeUtils.getSelectableItemBackground(context.getTheme()); |
| 53 | mInterceptorLayer.setBackgroundResource(resId); |
| 54 | addView(mInterceptorLayer); |
| 55 | |
| 56 | mAlphaLayer = this; |
Josh Gargus | 7edad9d | 2012-04-19 18:22:11 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Set the View that the overlay will use as its alpha-layer. If |
| 61 | * none is set it will use itself. Only necessary to set this if |
| 62 | * some child views need to appear above the alpha-layer but below |
| 63 | * the touch-interceptor. |
| 64 | */ |
| 65 | public void setAlphaLayer(View alphaLayer) { |
| 66 | if (mAlphaLayer == alphaLayer) return; |
| 67 | |
| 68 | // We're no longer the alpha-layer, so make ourself invisible. |
Paul Soulos | 333091a | 2014-07-22 13:54:41 -0700 | [diff] [blame] | 69 | if (mAlphaLayer == this) ContactDisplayUtils.setAlphaOnViewBackground(this, 0.0f); |
Josh Gargus | fc81892 | 2012-04-25 15:08:43 -0700 | [diff] [blame] | 70 | |
Josh Gargus | 7edad9d | 2012-04-19 18:22:11 -0700 | [diff] [blame] | 71 | mAlphaLayer = (alphaLayer == null) ? this : alphaLayer; |
| 72 | setAlphaLayerValue(mAlpha); |
| 73 | } |
| 74 | |
| 75 | /** Sets the alpha value on the alpha layer. */ |
| 76 | public void setAlphaLayerValue(float alpha) { |
| 77 | mAlpha = alpha; |
Josh Gargus | fc81892 | 2012-04-25 15:08:43 -0700 | [diff] [blame] | 78 | if (mAlphaLayer != null) { |
Paul Soulos | 333091a | 2014-07-22 13:54:41 -0700 | [diff] [blame] | 79 | ContactDisplayUtils.setAlphaOnViewBackground(mAlphaLayer, mAlpha); |
Josh Gargus | fc81892 | 2012-04-25 15:08:43 -0700 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | |
| 83 | /** Delegate to interceptor-layer. */ |
| 84 | public void setOverlayOnClickListener(OnClickListener listener) { |
| 85 | mInterceptorLayer.setOnClickListener(listener); |
| 86 | } |
| 87 | |
| 88 | /** Delegate to interceptor-layer. */ |
| 89 | public void setOverlayClickable(boolean clickable) { |
| 90 | mInterceptorLayer.setClickable(clickable); |
Josh Gargus | 7edad9d | 2012-04-19 18:22:11 -0700 | [diff] [blame] | 91 | } |
| 92 | } |