Home | History | Annotate | Download | only in widget
      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 
     19 import android.content.Context;
     20 import android.view.View;
     21 import android.widget.FrameLayout;
     22 
     23 import com.android.contacts.detail.ContactDisplayUtils;
     24 import com.android.contacts.util.ThemeUtils;
     25 
     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  */
     42 public class AlphaTouchInterceptorOverlay extends FrameLayout {
     43 
     44     private View mInterceptorLayer;
     45     private View mAlphaLayer;
     46     private float mAlpha = 0.0f;
     47 
     48     public AlphaTouchInterceptorOverlay(Context context) {
     49         super(context);
     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;
     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.
     69         if (mAlphaLayer == this) ContactDisplayUtils.setAlphaOnViewBackground(this, 0.0f);
     70 
     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;
     78         if (mAlphaLayer != null) {
     79             ContactDisplayUtils.setAlphaOnViewBackground(mAlphaLayer, mAlpha);
     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);
     91     }
     92 }
     93