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
17package com.android.systemui.statusbar;
18
19import android.content.Context;
20import android.graphics.Outline;
21import android.graphics.Rect;
22import android.graphics.RectF;
23import android.util.AttributeSet;
24import android.view.View;
25import android.view.ViewOutlineProvider;
26
27import com.android.systemui.R;
28
29/**
30 * Like {@link ExpandableView}, but setting an outline for the height and clipping.
31 */
32public abstract class ExpandableOutlineView extends ExpandableView {
33
34    private final Rect mOutlineRect = new Rect();
35    protected final int mRoundedRectCornerRadius;
36    private boolean mCustomOutline;
37    private float mOutlineAlpha = 1f;
38
39    public ExpandableOutlineView(Context context, AttributeSet attrs) {
40        super(context, attrs);
41        mRoundedRectCornerRadius = getResources().getDimensionPixelSize(
42                R.dimen.notification_material_rounded_rect_radius);
43        setOutlineProvider(new ViewOutlineProvider() {
44            @Override
45            public void getOutline(View view, Outline outline) {
46                if (!mCustomOutline) {
47                    outline.setRect(0,
48                            mClipTopAmount,
49                            getWidth(),
50                            Math.max(getActualHeight(), mClipTopAmount));
51                } else {
52                    outline.setRoundRect(mOutlineRect, mRoundedRectCornerRadius);
53                }
54                outline.setAlpha(mOutlineAlpha);
55            }
56        });
57    }
58
59    @Override
60    public void setActualHeight(int actualHeight, boolean notifyListeners) {
61        super.setActualHeight(actualHeight, notifyListeners);
62        invalidateOutline();
63    }
64
65    @Override
66    public void setClipTopAmount(int clipTopAmount) {
67        super.setClipTopAmount(clipTopAmount);
68        invalidateOutline();
69    }
70
71    protected void setOutlineAlpha(float alpha) {
72        mOutlineAlpha = alpha;
73        invalidateOutline();
74    }
75
76    protected void setOutlineRect(RectF rect) {
77        if (rect != null) {
78            setOutlineRect(rect.left, rect.top, rect.right, rect.bottom);
79        } else {
80            mCustomOutline = false;
81            setClipToOutline(false);
82            invalidateOutline();
83        }
84    }
85
86    protected void setOutlineRect(float left, float top, float right, float bottom) {
87        mCustomOutline = true;
88        setClipToOutline(true);
89
90        mOutlineRect.set((int) left, (int) top, (int) right, (int) bottom);
91
92        // Outlines need to be at least 1 dp
93        mOutlineRect.bottom = (int) Math.max(top, mOutlineRect.bottom);
94        mOutlineRect.right = (int) Math.max(left, mOutlineRect.right);
95
96        invalidateOutline();
97    }
98
99}
100