Home | History | Annotate | Download | only in statusbar
      1 /*
      2  * Copyright (C) 2014 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.systemui.statusbar;
     18 
     19 import android.content.Context;
     20 import android.graphics.Outline;
     21 import android.graphics.Rect;
     22 import android.graphics.RectF;
     23 import android.util.AttributeSet;
     24 import android.view.View;
     25 import android.view.ViewOutlineProvider;
     26 
     27 /**
     28  * Like {@link ExpandableView}, but setting an outline for the height and clipping.
     29  */
     30 public abstract class ExpandableOutlineView extends ExpandableView {
     31 
     32     private final Rect mOutlineRect = new Rect();
     33     private boolean mCustomOutline;
     34     private float mOutlineAlpha = -1f;
     35 
     36     ViewOutlineProvider mProvider = new ViewOutlineProvider() {
     37         @Override
     38         public void getOutline(View view, Outline outline) {
     39             int translation = (int) getTranslation();
     40             if (!mCustomOutline) {
     41                 outline.setRect(translation,
     42                         mClipTopAmount,
     43                         getWidth() + translation,
     44                         Math.max(getActualHeight(), mClipTopAmount));
     45             } else {
     46                 outline.setRect(mOutlineRect);
     47             }
     48             outline.setAlpha(mOutlineAlpha);
     49         }
     50     };
     51 
     52     public ExpandableOutlineView(Context context, AttributeSet attrs) {
     53         super(context, attrs);
     54         setOutlineProvider(mProvider);
     55     }
     56 
     57     @Override
     58     public void setActualHeight(int actualHeight, boolean notifyListeners) {
     59         super.setActualHeight(actualHeight, notifyListeners);
     60         invalidateOutline();
     61     }
     62 
     63     @Override
     64     public void setClipTopAmount(int clipTopAmount) {
     65         super.setClipTopAmount(clipTopAmount);
     66         invalidateOutline();
     67     }
     68 
     69     protected void setOutlineAlpha(float alpha) {
     70         if (alpha != mOutlineAlpha) {
     71             mOutlineAlpha = alpha;
     72             invalidateOutline();
     73         }
     74     }
     75 
     76     @Override
     77     public float getOutlineAlpha() {
     78         return mOutlineAlpha;
     79     }
     80 
     81     protected void setOutlineRect(RectF rect) {
     82         if (rect != null) {
     83             setOutlineRect(rect.left, rect.top, rect.right, rect.bottom);
     84         } else {
     85             mCustomOutline = false;
     86             setClipToOutline(false);
     87             invalidateOutline();
     88         }
     89     }
     90 
     91     @Override
     92     public int getOutlineTranslation() {
     93         return mCustomOutline ? mOutlineRect.left : (int) getTranslation();
     94     }
     95 
     96     public void updateOutline() {
     97         if (mCustomOutline) {
     98             return;
     99         }
    100         boolean hasOutline = true;
    101         if (isChildInGroup()) {
    102             hasOutline = isGroupExpanded() && !isGroupExpansionChanging();
    103         } else if (isSummaryWithChildren()) {
    104             hasOutline = !isGroupExpanded() || isGroupExpansionChanging();
    105         }
    106         setOutlineProvider(hasOutline ? mProvider : null);
    107     }
    108 
    109     public boolean isOutlineShowing() {
    110         ViewOutlineProvider op = getOutlineProvider();
    111         return op != null;
    112     }
    113 
    114     protected void setOutlineRect(float left, float top, float right, float bottom) {
    115         mCustomOutline = true;
    116         setClipToOutline(true);
    117 
    118         mOutlineRect.set((int) left, (int) top, (int) right, (int) bottom);
    119 
    120         // Outlines need to be at least 1 dp
    121         mOutlineRect.bottom = (int) Math.max(top, mOutlineRect.bottom);
    122         mOutlineRect.right = (int) Math.max(left, mOutlineRect.right);
    123 
    124         invalidateOutline();
    125     }
    126 
    127 }
    128