Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame^] | 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.Canvas; |
| 21 | import android.graphics.Outline; |
| 22 | import android.graphics.Rect; |
| 23 | import android.graphics.drawable.Drawable; |
| 24 | import android.graphics.drawable.InsetDrawable; |
| 25 | import android.util.AttributeSet; |
| 26 | import android.view.View; |
| 27 | import android.widget.FrameLayout; |
| 28 | |
| 29 | /** |
| 30 | * An abstract view for expandable views. |
| 31 | */ |
| 32 | public abstract class ExpandableView extends FrameLayout { |
| 33 | |
| 34 | private OnHeightChangedListener mOnHeightChangedListener; |
| 35 | protected int mActualHeight; |
| 36 | protected int mClipTopAmount; |
| 37 | protected Drawable mCustomBackground; |
| 38 | private boolean mActualHeightInitialized; |
| 39 | |
| 40 | public ExpandableView(Context context, AttributeSet attrs) { |
| 41 | super(context, attrs); |
| 42 | } |
| 43 | |
| 44 | @Override |
| 45 | protected void onDraw(Canvas canvas) { |
| 46 | if (mCustomBackground != null) { |
| 47 | mCustomBackground.setBounds(0, mClipTopAmount, getWidth(), mActualHeight); |
| 48 | mCustomBackground.draw(canvas); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | @Override |
| 53 | protected boolean verifyDrawable(Drawable who) { |
| 54 | return super.verifyDrawable(who) || who == mCustomBackground; |
| 55 | } |
| 56 | |
| 57 | @Override |
| 58 | protected void drawableStateChanged() { |
| 59 | final Drawable d = mCustomBackground; |
| 60 | if (d != null && d.isStateful()) { |
| 61 | d.setState(getDrawableState()); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | @Override |
| 66 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) { |
| 67 | super.onLayout(changed, left, top, right, bottom); |
| 68 | if (!mActualHeightInitialized && mActualHeight == 0) { |
| 69 | mActualHeight = getHeight(); |
| 70 | } |
| 71 | mActualHeightInitialized = true; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Sets the actual height of this notification. This is different than the laid out |
| 76 | * {@link View#getHeight()}, as we want to avoid layouting during scrolling and expanding. |
| 77 | */ |
| 78 | public void setActualHeight(int actualHeight) { |
| 79 | mActualHeight = actualHeight; |
| 80 | invalidate(); |
| 81 | if (mOnHeightChangedListener != null) { |
| 82 | mOnHeightChangedListener.onHeightChanged(this); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * See {@link #setActualHeight}. |
| 88 | * |
| 89 | * @return The actual height of this notification. |
| 90 | */ |
| 91 | public int getActualHeight() { |
| 92 | return mActualHeight; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * @return The maximum height of this notification. |
| 97 | */ |
| 98 | public abstract int getMaxHeight(); |
| 99 | |
| 100 | /** |
| 101 | * Sets the amount this view should be clipped from the top. This is used when an expanded |
| 102 | * notification is scrolling in the top or bottom stack. |
| 103 | * |
| 104 | * @param clipTopAmount The amount of pixels this view should be clipped from top. |
| 105 | */ |
| 106 | public void setClipTopAmount(int clipTopAmount) { |
| 107 | mClipTopAmount = clipTopAmount; |
| 108 | invalidate(); |
| 109 | } |
| 110 | |
| 111 | public void setOnHeightChangedListener(OnHeightChangedListener listener) { |
| 112 | mOnHeightChangedListener = listener; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Sets a custom background drawable. As we need to change our bounds independently of layout, |
| 117 | * we need the notition of a custom background. |
| 118 | */ |
| 119 | public void setCustomBackground(Drawable customBackground) { |
| 120 | if (mCustomBackground != null) { |
| 121 | mCustomBackground.setCallback(null); |
| 122 | unscheduleDrawable(mCustomBackground); |
| 123 | } |
| 124 | mCustomBackground = customBackground; |
| 125 | mCustomBackground.setCallback(this); |
| 126 | setWillNotDraw(customBackground == null); |
| 127 | invalidate(); |
| 128 | } |
| 129 | |
| 130 | public void setCustomBackgroundResource(int drawableResId) { |
| 131 | setCustomBackground(getResources().getDrawable(drawableResId)); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * A listener notifying when {@link #getActualHeight} changes. |
| 136 | */ |
| 137 | public interface OnHeightChangedListener { |
| 138 | void onHeightChanged(ExpandableView view); |
| 139 | } |
| 140 | } |