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; |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 20 | import android.util.AttributeSet; |
Jorim Jaggi | 00ebdfe | 2014-05-02 17:29:56 +0200 | [diff] [blame] | 21 | import android.view.MotionEvent; |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 22 | import android.view.View; |
Selim Cinek | c9c00ae | 2014-05-20 03:33:40 +0200 | [diff] [blame^] | 23 | import android.view.ViewGroup; |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 24 | |
| 25 | /** |
| 26 | * An abstract view for expandable views. |
| 27 | */ |
Selim Cinek | c9c00ae | 2014-05-20 03:33:40 +0200 | [diff] [blame^] | 28 | public abstract class ExpandableView extends ViewGroup { |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 29 | |
| 30 | private OnHeightChangedListener mOnHeightChangedListener; |
| 31 | protected int mActualHeight; |
| 32 | protected int mClipTopAmount; |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 33 | private boolean mActualHeightInitialized; |
| 34 | |
| 35 | public ExpandableView(Context context, AttributeSet attrs) { |
| 36 | super(context, attrs); |
| 37 | } |
| 38 | |
| 39 | @Override |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 40 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) { |
Selim Cinek | c9c00ae | 2014-05-20 03:33:40 +0200 | [diff] [blame^] | 41 | for (int i = 0; i < getChildCount(); i++) { |
| 42 | View child = getChildAt(i); |
| 43 | int height = child.getMeasuredHeight(); |
| 44 | int width = child.getMeasuredWidth(); |
| 45 | int center = getWidth() / 2; |
| 46 | int childLeft = center - width / 2; |
| 47 | child.layout(childLeft, |
| 48 | 0, |
| 49 | childLeft + width, |
| 50 | height); |
| 51 | } |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 52 | if (!mActualHeightInitialized && mActualHeight == 0) { |
Selim Cinek | c27437b | 2014-05-14 10:23:33 +0200 | [diff] [blame] | 53 | mActualHeight = getInitialHeight(); |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 54 | } |
| 55 | mActualHeightInitialized = true; |
| 56 | } |
| 57 | |
Selim Cinek | c27437b | 2014-05-14 10:23:33 +0200 | [diff] [blame] | 58 | protected int getInitialHeight() { |
| 59 | return getHeight(); |
| 60 | } |
| 61 | |
Jorim Jaggi | 00ebdfe | 2014-05-02 17:29:56 +0200 | [diff] [blame] | 62 | @Override |
| 63 | public boolean dispatchTouchEvent(MotionEvent ev) { |
| 64 | if (filterMotionEvent(ev)) { |
| 65 | return super.dispatchTouchEvent(ev); |
| 66 | } |
| 67 | return false; |
| 68 | } |
| 69 | |
| 70 | private boolean filterMotionEvent(MotionEvent event) { |
| 71 | return event.getActionMasked() != MotionEvent.ACTION_DOWN |
| 72 | || event.getY() > mClipTopAmount && event.getY() < mActualHeight; |
| 73 | } |
| 74 | |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 75 | /** |
| 76 | * Sets the actual height of this notification. This is different than the laid out |
| 77 | * {@link View#getHeight()}, as we want to avoid layouting during scrolling and expanding. |
Jorim Jaggi | d552d9d | 2014-05-07 19:41:13 +0200 | [diff] [blame] | 78 | * |
| 79 | * @param actualHeight The height of this notification. |
| 80 | * @param notifyListeners Whether the listener should be informed about the change. |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 81 | */ |
Jorim Jaggi | d552d9d | 2014-05-07 19:41:13 +0200 | [diff] [blame] | 82 | public void setActualHeight(int actualHeight, boolean notifyListeners) { |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 83 | mActualHeight = actualHeight; |
Jorim Jaggi | d552d9d | 2014-05-07 19:41:13 +0200 | [diff] [blame] | 84 | if (notifyListeners) { |
| 85 | notifyHeightChanged(); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | public void setActualHeight(int actualHeight) { |
| 90 | setActualHeight(actualHeight, true); |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | /** |
| 94 | * See {@link #setActualHeight}. |
| 95 | * |
Jorim Jaggi | 9cbadd3 | 2014-05-01 20:18:31 +0200 | [diff] [blame] | 96 | * @return The current actual height of this notification. |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 97 | */ |
| 98 | public int getActualHeight() { |
| 99 | return mActualHeight; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * @return The maximum height of this notification. |
| 104 | */ |
Jorim Jaggi | 4222d9a | 2014-04-23 16:13:15 +0200 | [diff] [blame] | 105 | public int getMaxHeight() { |
| 106 | return getHeight(); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * @return The minimum height of this notification. |
| 111 | */ |
| 112 | public int getMinHeight() { |
| 113 | return getHeight(); |
| 114 | } |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 115 | |
| 116 | /** |
Jorim Jaggi | d552d9d | 2014-05-07 19:41:13 +0200 | [diff] [blame] | 117 | * Sets the notification as dimmed. The default implementation does nothing. |
| 118 | * |
| 119 | * @param dimmed Whether the notification should be dimmed. |
| 120 | * @param fade Whether an animation should be played to change the state. |
| 121 | */ |
| 122 | public void setDimmed(boolean dimmed, boolean fade) { |
| 123 | } |
| 124 | |
| 125 | /** |
Jorim Jaggi | 9cbadd3 | 2014-05-01 20:18:31 +0200 | [diff] [blame] | 126 | * @return The desired notification height. |
| 127 | */ |
| 128 | public int getIntrinsicHeight() { |
Selim Cinek | a5eaa60 | 2014-05-12 21:27:47 +0200 | [diff] [blame] | 129 | return getHeight(); |
Jorim Jaggi | 9cbadd3 | 2014-05-01 20:18:31 +0200 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | /** |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 133 | * Sets the amount this view should be clipped from the top. This is used when an expanded |
| 134 | * notification is scrolling in the top or bottom stack. |
| 135 | * |
| 136 | * @param clipTopAmount The amount of pixels this view should be clipped from top. |
| 137 | */ |
| 138 | public void setClipTopAmount(int clipTopAmount) { |
| 139 | mClipTopAmount = clipTopAmount; |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 140 | } |
| 141 | |
Selim Cinek | eb97356 | 2014-05-02 17:07:49 +0200 | [diff] [blame] | 142 | public int getClipTopAmount() { |
| 143 | return mClipTopAmount; |
| 144 | } |
| 145 | |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 146 | public void setOnHeightChangedListener(OnHeightChangedListener listener) { |
| 147 | mOnHeightChangedListener = listener; |
| 148 | } |
| 149 | |
| 150 | /** |
Jorim Jaggi | 4222d9a | 2014-04-23 16:13:15 +0200 | [diff] [blame] | 151 | * @return Whether we can expand this views content. |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 152 | */ |
Jorim Jaggi | 4222d9a | 2014-04-23 16:13:15 +0200 | [diff] [blame] | 153 | public boolean isContentExpandable() { |
| 154 | return false; |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 155 | } |
| 156 | |
Jorim Jaggi | 9cbadd3 | 2014-05-01 20:18:31 +0200 | [diff] [blame] | 157 | public void notifyHeightChanged() { |
| 158 | if (mOnHeightChangedListener != null) { |
| 159 | mOnHeightChangedListener.onHeightChanged(this); |
| 160 | } |
| 161 | } |
| 162 | |
Selim Cinek | c27437b | 2014-05-14 10:23:33 +0200 | [diff] [blame] | 163 | public boolean isTransparent() { |
| 164 | return false; |
| 165 | } |
| 166 | |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 167 | /** |
| 168 | * A listener notifying when {@link #getActualHeight} changes. |
| 169 | */ |
| 170 | public interface OnHeightChangedListener { |
| 171 | void onHeightChanged(ExpandableView view); |
| 172 | } |
| 173 | } |