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() - mClipBottomAmount, mClipTopAmount));
45             } else {
46                 outline.setRect(mOutlineRect);
47             }
48             outline.setAlpha(mOutlineAlpha);
49         }
50     };
51 
ExpandableOutlineView(Context context, AttributeSet attrs)52     public ExpandableOutlineView(Context context, AttributeSet attrs) {
53         super(context, attrs);
54         setOutlineProvider(mProvider);
55     }
56 
57     @Override
setActualHeight(int actualHeight, boolean notifyListeners)58     public void setActualHeight(int actualHeight, boolean notifyListeners) {
59         super.setActualHeight(actualHeight, notifyListeners);
60         invalidateOutline();
61     }
62 
63     @Override
setClipTopAmount(int clipTopAmount)64     public void setClipTopAmount(int clipTopAmount) {
65         super.setClipTopAmount(clipTopAmount);
66         invalidateOutline();
67     }
68 
69     @Override
setClipBottomAmount(int clipBottomAmount)70     public void setClipBottomAmount(int clipBottomAmount) {
71         super.setClipBottomAmount(clipBottomAmount);
72         invalidateOutline();
73     }
74 
setOutlineAlpha(float alpha)75     protected void setOutlineAlpha(float alpha) {
76         if (alpha != mOutlineAlpha) {
77             mOutlineAlpha = alpha;
78             invalidateOutline();
79         }
80     }
81 
82     @Override
getOutlineAlpha()83     public float getOutlineAlpha() {
84         return mOutlineAlpha;
85     }
86 
setOutlineRect(RectF rect)87     protected void setOutlineRect(RectF rect) {
88         if (rect != null) {
89             setOutlineRect(rect.left, rect.top, rect.right, rect.bottom);
90         } else {
91             mCustomOutline = false;
92             setClipToOutline(false);
93             invalidateOutline();
94         }
95     }
96 
97     @Override
getOutlineTranslation()98     public int getOutlineTranslation() {
99         return mCustomOutline ? mOutlineRect.left : (int) getTranslation();
100     }
101 
updateOutline()102     public void updateOutline() {
103         if (mCustomOutline) {
104             return;
105         }
106         boolean hasOutline = needsOutline();
107         setOutlineProvider(hasOutline ? mProvider : null);
108     }
109 
110     /**
111      * @return whether the view currently needs an outline. This is usually false in case it doesn't
112      * have a background.
113      */
needsOutline()114     protected boolean needsOutline() {
115         if (isChildInGroup()) {
116             return isGroupExpanded() && !isGroupExpansionChanging();
117         } else if (isSummaryWithChildren()) {
118             return !isGroupExpanded() || isGroupExpansionChanging();
119         }
120         return true;
121     }
122 
isOutlineShowing()123     public boolean isOutlineShowing() {
124         ViewOutlineProvider op = getOutlineProvider();
125         return op != null;
126     }
127 
setOutlineRect(float left, float top, float right, float bottom)128     protected void setOutlineRect(float left, float top, float right, float bottom) {
129         mCustomOutline = true;
130         setClipToOutline(true);
131 
132         mOutlineRect.set((int) left, (int) top, (int) right, (int) bottom);
133 
134         // Outlines need to be at least 1 dp
135         mOutlineRect.bottom = (int) Math.max(top, mOutlineRect.bottom);
136         mOutlineRect.right = (int) Math.max(left, mOutlineRect.right);
137 
138         invalidateOutline();
139     }
140 
141 }
142