blob: f2031932306e51a7bd087852037d335585afc194 [file] [log] [blame]
Josh Gargus7edad9d2012-04-19 18:22:11 -07001/*
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.widget;
18
Josh Gargus7edad9d2012-04-19 18:22:11 -070019import android.content.Context;
20import android.view.View;
Josh Gargusfc818922012-04-25 15:08:43 -070021import android.widget.FrameLayout;
Josh Gargus7edad9d2012-04-19 18:22:11 -070022
Paul Soulos333091a2014-07-22 13:54:41 -070023import com.android.contacts.detail.ContactDisplayUtils;
Chiao Chenge0b2f1e2012-06-12 13:07:56 -070024import com.android.contacts.util.ThemeUtils;
25
Josh Gargus7edad9d2012-04-19 18:22:11 -070026/**
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 Gargusfc818922012-04-25 15:08:43 -070042public class AlphaTouchInterceptorOverlay extends FrameLayout {
Josh Gargus7edad9d2012-04-19 18:22:11 -070043
Josh Gargusfc818922012-04-25 15:08:43 -070044 private View mInterceptorLayer;
45 private View mAlphaLayer;
46 private float mAlpha = 0.0f;
Josh Gargus7edad9d2012-04-19 18:22:11 -070047
48 public AlphaTouchInterceptorOverlay(Context context) {
49 super(context);
Josh Gargusfc818922012-04-25 15:08:43 -070050
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 Gargus7edad9d2012-04-19 18:22:11 -070057 }
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 Soulos333091a2014-07-22 13:54:41 -070069 if (mAlphaLayer == this) ContactDisplayUtils.setAlphaOnViewBackground(this, 0.0f);
Josh Gargusfc818922012-04-25 15:08:43 -070070
Josh Gargus7edad9d2012-04-19 18:22:11 -070071 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 Gargusfc818922012-04-25 15:08:43 -070078 if (mAlphaLayer != null) {
Paul Soulos333091a2014-07-22 13:54:41 -070079 ContactDisplayUtils.setAlphaOnViewBackground(mAlphaLayer, mAlpha);
Josh Gargusfc818922012-04-25 15:08:43 -070080 }
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 Gargus7edad9d2012-04-19 18:22:11 -070091 }
92}