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; |
Selim Cinek | e32010a | 2014-08-20 23:50:41 +0200 | [diff] [blame^] | 20 | import android.graphics.Rect; |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 21 | import android.util.AttributeSet; |
Jorim Jaggi | 00ebdfe | 2014-05-02 17:29:56 +0200 | [diff] [blame] | 22 | import android.view.MotionEvent; |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 23 | import android.view.View; |
Selim Cinek | c9c00ae | 2014-05-20 03:33:40 +0200 | [diff] [blame] | 24 | import android.view.ViewGroup; |
Selim Cinek | 24d7cfa | 2014-05-20 13:50:57 +0200 | [diff] [blame] | 25 | import android.widget.FrameLayout; |
| 26 | import com.android.systemui.R; |
Selim Cinek | e32010a | 2014-08-20 23:50:41 +0200 | [diff] [blame^] | 27 | import com.android.systemui.statusbar.stack.NotificationStackScrollLayout; |
Selim Cinek | 24d7cfa | 2014-05-20 13:50:57 +0200 | [diff] [blame] | 28 | |
| 29 | import java.util.ArrayList; |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 30 | |
| 31 | /** |
| 32 | * An abstract view for expandable views. |
| 33 | */ |
Selim Cinek | 24d7cfa | 2014-05-20 13:50:57 +0200 | [diff] [blame] | 34 | public abstract class ExpandableView extends FrameLayout { |
| 35 | |
| 36 | private final int mMaxNotificationHeight; |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 37 | |
| 38 | private OnHeightChangedListener mOnHeightChangedListener; |
| 39 | protected int mActualHeight; |
| 40 | protected int mClipTopAmount; |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 41 | private boolean mActualHeightInitialized; |
Selim Cinek | 24d7cfa | 2014-05-20 13:50:57 +0200 | [diff] [blame] | 42 | private ArrayList<View> mMatchParentViews = new ArrayList<View>(); |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 43 | |
| 44 | public ExpandableView(Context context, AttributeSet attrs) { |
| 45 | super(context, attrs); |
Selim Cinek | 24d7cfa | 2014-05-20 13:50:57 +0200 | [diff] [blame] | 46 | mMaxNotificationHeight = getResources().getDimensionPixelSize( |
| 47 | R.dimen.notification_max_height); |
| 48 | } |
| 49 | |
| 50 | @Override |
| 51 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 52 | int ownMaxHeight = mMaxNotificationHeight; |
| 53 | int heightMode = MeasureSpec.getMode(heightMeasureSpec); |
| 54 | boolean hasFixedHeight = heightMode == MeasureSpec.EXACTLY; |
| 55 | boolean isHeightLimited = heightMode == MeasureSpec.AT_MOST; |
| 56 | if (hasFixedHeight || isHeightLimited) { |
| 57 | int size = MeasureSpec.getSize(heightMeasureSpec); |
| 58 | ownMaxHeight = Math.min(ownMaxHeight, size); |
| 59 | } |
| 60 | int newHeightSpec = MeasureSpec.makeMeasureSpec(ownMaxHeight, MeasureSpec.AT_MOST); |
| 61 | int maxChildHeight = 0; |
| 62 | int childCount = getChildCount(); |
| 63 | for (int i = 0; i < childCount; i++) { |
| 64 | View child = getChildAt(i); |
| 65 | int childHeightSpec = newHeightSpec; |
| 66 | ViewGroup.LayoutParams layoutParams = child.getLayoutParams(); |
| 67 | if (layoutParams.height != ViewGroup.LayoutParams.MATCH_PARENT) { |
| 68 | if (layoutParams.height >= 0) { |
| 69 | // An actual height is set |
| 70 | childHeightSpec = layoutParams.height > ownMaxHeight |
| 71 | ? MeasureSpec.makeMeasureSpec(ownMaxHeight, MeasureSpec.EXACTLY) |
| 72 | : MeasureSpec.makeMeasureSpec(layoutParams.height, MeasureSpec.EXACTLY); |
| 73 | } |
Jorim Jaggi | b741f05 | 2014-06-03 19:57:26 +0200 | [diff] [blame] | 74 | child.measure( |
| 75 | getChildMeasureSpec(widthMeasureSpec, 0 /* padding */, layoutParams.width), |
| 76 | childHeightSpec); |
Selim Cinek | 24d7cfa | 2014-05-20 13:50:57 +0200 | [diff] [blame] | 77 | int childHeight = child.getMeasuredHeight(); |
| 78 | maxChildHeight = Math.max(maxChildHeight, childHeight); |
| 79 | } else { |
| 80 | mMatchParentViews.add(child); |
| 81 | } |
| 82 | } |
| 83 | int ownHeight = hasFixedHeight ? ownMaxHeight : maxChildHeight; |
| 84 | newHeightSpec = MeasureSpec.makeMeasureSpec(ownHeight, MeasureSpec.EXACTLY); |
| 85 | for (View child : mMatchParentViews) { |
Jorim Jaggi | b741f05 | 2014-06-03 19:57:26 +0200 | [diff] [blame] | 86 | child.measure(getChildMeasureSpec( |
| 87 | widthMeasureSpec, 0 /* padding */, child.getLayoutParams().width), |
| 88 | newHeightSpec); |
Selim Cinek | 24d7cfa | 2014-05-20 13:50:57 +0200 | [diff] [blame] | 89 | } |
| 90 | mMatchParentViews.clear(); |
| 91 | int width = MeasureSpec.getSize(widthMeasureSpec); |
| 92 | setMeasuredDimension(width, ownHeight); |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | @Override |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 96 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) { |
Selim Cinek | 24d7cfa | 2014-05-20 13:50:57 +0200 | [diff] [blame] | 97 | super.onLayout(changed, left, top, right, bottom); |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 98 | if (!mActualHeightInitialized && mActualHeight == 0) { |
Jorim Jaggi | 3c3c3fc | 2014-05-20 23:16:42 +0200 | [diff] [blame] | 99 | setActualHeight(getInitialHeight()); |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 100 | } |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 101 | } |
| 102 | |
Selim Cinek | c27437b | 2014-05-14 10:23:33 +0200 | [diff] [blame] | 103 | protected int getInitialHeight() { |
| 104 | return getHeight(); |
| 105 | } |
| 106 | |
Jorim Jaggi | 00ebdfe | 2014-05-02 17:29:56 +0200 | [diff] [blame] | 107 | @Override |
| 108 | public boolean dispatchTouchEvent(MotionEvent ev) { |
| 109 | if (filterMotionEvent(ev)) { |
| 110 | return super.dispatchTouchEvent(ev); |
| 111 | } |
| 112 | return false; |
| 113 | } |
| 114 | |
| 115 | private boolean filterMotionEvent(MotionEvent event) { |
| 116 | return event.getActionMasked() != MotionEvent.ACTION_DOWN |
| 117 | || event.getY() > mClipTopAmount && event.getY() < mActualHeight; |
| 118 | } |
| 119 | |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 120 | /** |
| 121 | * Sets the actual height of this notification. This is different than the laid out |
| 122 | * {@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] | 123 | * |
| 124 | * @param actualHeight The height of this notification. |
| 125 | * @param notifyListeners Whether the listener should be informed about the change. |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 126 | */ |
Jorim Jaggi | d552d9d | 2014-05-07 19:41:13 +0200 | [diff] [blame] | 127 | public void setActualHeight(int actualHeight, boolean notifyListeners) { |
Jorim Jaggi | 2580a976 | 2014-06-25 03:08:25 +0200 | [diff] [blame] | 128 | mActualHeightInitialized = true; |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 129 | mActualHeight = actualHeight; |
Jorim Jaggi | d552d9d | 2014-05-07 19:41:13 +0200 | [diff] [blame] | 130 | if (notifyListeners) { |
| 131 | notifyHeightChanged(); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | public void setActualHeight(int actualHeight) { |
| 136 | setActualHeight(actualHeight, true); |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | /** |
| 140 | * See {@link #setActualHeight}. |
| 141 | * |
Jorim Jaggi | 9cbadd3 | 2014-05-01 20:18:31 +0200 | [diff] [blame] | 142 | * @return The current actual height of this notification. |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 143 | */ |
| 144 | public int getActualHeight() { |
| 145 | return mActualHeight; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * @return The maximum height of this notification. |
| 150 | */ |
Jorim Jaggi | 4222d9a | 2014-04-23 16:13:15 +0200 | [diff] [blame] | 151 | public int getMaxHeight() { |
| 152 | return getHeight(); |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * @return The minimum height of this notification. |
| 157 | */ |
| 158 | public int getMinHeight() { |
| 159 | return getHeight(); |
| 160 | } |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 161 | |
| 162 | /** |
Jorim Jaggi | d552d9d | 2014-05-07 19:41:13 +0200 | [diff] [blame] | 163 | * Sets the notification as dimmed. The default implementation does nothing. |
| 164 | * |
| 165 | * @param dimmed Whether the notification should be dimmed. |
| 166 | * @param fade Whether an animation should be played to change the state. |
| 167 | */ |
| 168 | public void setDimmed(boolean dimmed, boolean fade) { |
| 169 | } |
| 170 | |
| 171 | /** |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 172 | * Sets the notification as dark. The default implementation does nothing. |
| 173 | * |
| 174 | * @param dark Whether the notification should be dark. |
| 175 | * @param fade Whether an animation should be played to change the state. |
| 176 | */ |
| 177 | public void setDark(boolean dark, boolean fade) { |
| 178 | } |
| 179 | |
| 180 | /** |
Jorim Jaggi | ae44128 | 2014-08-01 02:45:18 +0200 | [diff] [blame] | 181 | * See {@link #setHideSensitive}. This is a variant which notifies this view in advance about |
| 182 | * the upcoming state of hiding sensitive notifications. It gets called at the very beginning |
| 183 | * of a stack scroller update such that the updated intrinsic height (which is dependent on |
| 184 | * whether private or public layout is showing) gets taken into account into all layout |
| 185 | * calculations. |
| 186 | */ |
| 187 | public void setHideSensitiveForIntrinsicHeight(boolean hideSensitive) { |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Sets whether the notification should hide its private contents if it is sensitive. |
| 192 | */ |
| 193 | public void setHideSensitive(boolean hideSensitive, boolean animated, long delay, |
| 194 | long duration) { |
| 195 | } |
| 196 | |
| 197 | /** |
Jorim Jaggi | 9cbadd3 | 2014-05-01 20:18:31 +0200 | [diff] [blame] | 198 | * @return The desired notification height. |
| 199 | */ |
| 200 | public int getIntrinsicHeight() { |
Selim Cinek | a5eaa60 | 2014-05-12 21:27:47 +0200 | [diff] [blame] | 201 | return getHeight(); |
Jorim Jaggi | 9cbadd3 | 2014-05-01 20:18:31 +0200 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | /** |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 205 | * Sets the amount this view should be clipped from the top. This is used when an expanded |
| 206 | * notification is scrolling in the top or bottom stack. |
| 207 | * |
| 208 | * @param clipTopAmount The amount of pixels this view should be clipped from top. |
| 209 | */ |
| 210 | public void setClipTopAmount(int clipTopAmount) { |
| 211 | mClipTopAmount = clipTopAmount; |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 212 | } |
| 213 | |
Selim Cinek | eb97356 | 2014-05-02 17:07:49 +0200 | [diff] [blame] | 214 | public int getClipTopAmount() { |
| 215 | return mClipTopAmount; |
| 216 | } |
| 217 | |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 218 | public void setOnHeightChangedListener(OnHeightChangedListener listener) { |
| 219 | mOnHeightChangedListener = listener; |
| 220 | } |
| 221 | |
| 222 | /** |
Jorim Jaggi | 4222d9a | 2014-04-23 16:13:15 +0200 | [diff] [blame] | 223 | * @return Whether we can expand this views content. |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 224 | */ |
Jorim Jaggi | 4222d9a | 2014-04-23 16:13:15 +0200 | [diff] [blame] | 225 | public boolean isContentExpandable() { |
| 226 | return false; |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 227 | } |
| 228 | |
Jorim Jaggi | 9cbadd3 | 2014-05-01 20:18:31 +0200 | [diff] [blame] | 229 | public void notifyHeightChanged() { |
| 230 | if (mOnHeightChangedListener != null) { |
| 231 | mOnHeightChangedListener.onHeightChanged(this); |
| 232 | } |
| 233 | } |
| 234 | |
Selim Cinek | c27437b | 2014-05-14 10:23:33 +0200 | [diff] [blame] | 235 | public boolean isTransparent() { |
| 236 | return false; |
| 237 | } |
| 238 | |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 239 | /** |
Selim Cinek | 8efa6dd | 2014-05-19 16:27:37 +0200 | [diff] [blame] | 240 | * Perform a remove animation on this view. |
| 241 | * |
Jorim Jaggi | 60d07c5 | 2014-07-31 15:38:21 +0200 | [diff] [blame] | 242 | * @param duration The duration of the remove animation. |
Selim Cinek | 8efa6dd | 2014-05-19 16:27:37 +0200 | [diff] [blame] | 243 | * @param translationDirection The direction value from [-1 ... 1] indicating in which the |
| 244 | * animation should be performed. A value of -1 means that The |
| 245 | * remove animation should be performed upwards, |
| 246 | * such that the child appears to be going away to the top. 1 |
| 247 | * Should mean the opposite. |
| 248 | * @param onFinishedRunnable A runnable which should be run when the animation is finished. |
| 249 | */ |
Jorim Jaggi | 60d07c5 | 2014-07-31 15:38:21 +0200 | [diff] [blame] | 250 | public abstract void performRemoveAnimation(long duration, float translationDirection, |
Selim Cinek | 8efa6dd | 2014-05-19 16:27:37 +0200 | [diff] [blame] | 251 | Runnable onFinishedRunnable); |
| 252 | |
Jorim Jaggi | 60d07c5 | 2014-07-31 15:38:21 +0200 | [diff] [blame] | 253 | public abstract void performAddAnimation(long delay, long duration); |
Selim Cinek | 8efa6dd | 2014-05-19 16:27:37 +0200 | [diff] [blame] | 254 | |
Selim Cinek | f54090e | 2014-06-17 17:24:51 -0700 | [diff] [blame] | 255 | public abstract void setScrimAmount(float scrimAmount); |
| 256 | |
Selim Cinek | 3d2b94bf | 2014-07-02 22:12:47 +0200 | [diff] [blame] | 257 | public void setBelowSpeedBump(boolean below) { |
| 258 | } |
| 259 | |
Selim Cinek | 31094df | 2014-08-14 19:28:15 +0200 | [diff] [blame] | 260 | public void onHeightReset() { |
Selim Cinek | e34c651 | 2014-08-14 11:19:41 +0200 | [diff] [blame] | 261 | if (mOnHeightChangedListener != null) { |
| 262 | mOnHeightChangedListener.onReset(this); |
| 263 | } |
Selim Cinek | a5e211b | 2014-08-11 17:35:48 +0200 | [diff] [blame] | 264 | } |
| 265 | |
Selim Cinek | 8efa6dd | 2014-05-19 16:27:37 +0200 | [diff] [blame] | 266 | /** |
Selim Cinek | e32010a | 2014-08-20 23:50:41 +0200 | [diff] [blame^] | 267 | * This method returns the drawing rect for the view which is different from the regular |
| 268 | * drawing rect, since we layout all children in the {@link NotificationStackScrollLayout} at |
| 269 | * position 0 and usually the translation is neglected. Since we are manually clipping this |
| 270 | * view,we also need to subtract the clipTopAmount from the top. This is needed in order to |
| 271 | * ensure that accessibility and focusing work correctly. |
| 272 | * |
| 273 | * @param outRect The (scrolled) drawing bounds of the view. |
| 274 | */ |
| 275 | @Override |
| 276 | public void getDrawingRect(Rect outRect) { |
| 277 | super.getDrawingRect(outRect); |
| 278 | outRect.left += getTranslationX(); |
| 279 | outRect.right += getTranslationX(); |
| 280 | outRect.bottom = (int) (outRect.top + getTranslationY() + getActualHeight()); |
| 281 | outRect.top += getTranslationY() + getClipTopAmount(); |
| 282 | } |
| 283 | |
| 284 | /** |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 285 | * A listener notifying when {@link #getActualHeight} changes. |
| 286 | */ |
| 287 | public interface OnHeightChangedListener { |
Jorim Jaggi | 30c305c | 2014-07-01 23:34:41 +0200 | [diff] [blame] | 288 | |
| 289 | /** |
| 290 | * @param view the view for which the height changed, or {@code null} if just the top |
| 291 | * padding or the padding between the elements changed |
| 292 | */ |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 293 | void onHeightChanged(ExpandableView view); |
Selim Cinek | a5e211b | 2014-08-11 17:35:48 +0200 | [diff] [blame] | 294 | |
| 295 | /** |
| 296 | * Called when the view is reset and therefore the height will change abruptly |
| 297 | * |
| 298 | * @param view The view which was reset. |
| 299 | */ |
| 300 | void onReset(ExpandableView view); |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 301 | } |
| 302 | } |