blob: bf1e78e8b660be12334b807cf85c07fa5ca8041c [file] [log] [blame]
Jorim Jaggibe565df2014-04-28 17:51:23 +02001/*
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;
Selim Cineke32010a2014-08-20 23:50:41 +020020import android.graphics.Rect;
Jorim Jaggibe565df2014-04-28 17:51:23 +020021import android.util.AttributeSet;
Jorim Jaggi00ebdfe2014-05-02 17:29:56 +020022import android.view.MotionEvent;
Jorim Jaggibe565df2014-04-28 17:51:23 +020023import android.view.View;
Selim Cinekc9c00ae2014-05-20 03:33:40 +020024import android.view.ViewGroup;
Selim Cinek24d7cfa2014-05-20 13:50:57 +020025import android.widget.FrameLayout;
26import com.android.systemui.R;
Selim Cineke32010a2014-08-20 23:50:41 +020027import com.android.systemui.statusbar.stack.NotificationStackScrollLayout;
Selim Cinek24d7cfa2014-05-20 13:50:57 +020028
29import java.util.ArrayList;
Jorim Jaggibe565df2014-04-28 17:51:23 +020030
31/**
32 * An abstract view for expandable views.
33 */
Selim Cinek24d7cfa2014-05-20 13:50:57 +020034public abstract class ExpandableView extends FrameLayout {
35
36 private final int mMaxNotificationHeight;
Jorim Jaggibe565df2014-04-28 17:51:23 +020037
38 private OnHeightChangedListener mOnHeightChangedListener;
Chris Wren310df312014-10-31 14:29:46 -040039 private int mActualHeight;
Jorim Jaggibe565df2014-04-28 17:51:23 +020040 protected int mClipTopAmount;
Jorim Jaggibe565df2014-04-28 17:51:23 +020041 private boolean mActualHeightInitialized;
Selim Cinek24d7cfa2014-05-20 13:50:57 +020042 private ArrayList<View> mMatchParentViews = new ArrayList<View>();
Jorim Jaggibe565df2014-04-28 17:51:23 +020043
44 public ExpandableView(Context context, AttributeSet attrs) {
45 super(context, attrs);
Selim Cinek24d7cfa2014-05-20 13:50:57 +020046 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 Jaggib741f052014-06-03 19:57:26 +020074 child.measure(
75 getChildMeasureSpec(widthMeasureSpec, 0 /* padding */, layoutParams.width),
76 childHeightSpec);
Selim Cinek24d7cfa2014-05-20 13:50:57 +020077 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 Jaggib741f052014-06-03 19:57:26 +020086 child.measure(getChildMeasureSpec(
87 widthMeasureSpec, 0 /* padding */, child.getLayoutParams().width),
88 newHeightSpec);
Selim Cinek24d7cfa2014-05-20 13:50:57 +020089 }
90 mMatchParentViews.clear();
91 int width = MeasureSpec.getSize(widthMeasureSpec);
92 setMeasuredDimension(width, ownHeight);
Jorim Jaggibe565df2014-04-28 17:51:23 +020093 }
94
95 @Override
Jorim Jaggibe565df2014-04-28 17:51:23 +020096 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Selim Cinek24d7cfa2014-05-20 13:50:57 +020097 super.onLayout(changed, left, top, right, bottom);
Jorim Jaggibe565df2014-04-28 17:51:23 +020098 if (!mActualHeightInitialized && mActualHeight == 0) {
Selim Cinekd2319fb2014-09-01 19:41:54 +020099 int initialHeight = getInitialHeight();
100 if (initialHeight != 0) {
101 setActualHeight(initialHeight);
102 }
Jorim Jaggibe565df2014-04-28 17:51:23 +0200103 }
Jorim Jaggibe565df2014-04-28 17:51:23 +0200104 }
105
Selim Cinek1a521f32014-11-03 17:39:29 +0100106 /**
107 * Resets the height of the view on the next layout pass
108 */
109 protected void resetActualHeight() {
Chris Wren310df312014-10-31 14:29:46 -0400110 mActualHeight = 0;
111 mActualHeightInitialized = false;
Selim Cinek1a521f32014-11-03 17:39:29 +0100112 requestLayout();
Chris Wren310df312014-10-31 14:29:46 -0400113 }
114
Selim Cinekc27437b2014-05-14 10:23:33 +0200115 protected int getInitialHeight() {
116 return getHeight();
117 }
118
Jorim Jaggi00ebdfe2014-05-02 17:29:56 +0200119 @Override
120 public boolean dispatchTouchEvent(MotionEvent ev) {
121 if (filterMotionEvent(ev)) {
122 return super.dispatchTouchEvent(ev);
123 }
124 return false;
125 }
126
Selim Cinek1a521f32014-11-03 17:39:29 +0100127 protected boolean filterMotionEvent(MotionEvent event) {
Jorim Jaggi00ebdfe2014-05-02 17:29:56 +0200128 return event.getActionMasked() != MotionEvent.ACTION_DOWN
129 || event.getY() > mClipTopAmount && event.getY() < mActualHeight;
130 }
131
Jorim Jaggibe565df2014-04-28 17:51:23 +0200132 /**
133 * Sets the actual height of this notification. This is different than the laid out
134 * {@link View#getHeight()}, as we want to avoid layouting during scrolling and expanding.
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200135 *
136 * @param actualHeight The height of this notification.
137 * @param notifyListeners Whether the listener should be informed about the change.
Jorim Jaggibe565df2014-04-28 17:51:23 +0200138 */
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200139 public void setActualHeight(int actualHeight, boolean notifyListeners) {
Jorim Jaggi2580a9762014-06-25 03:08:25 +0200140 mActualHeightInitialized = true;
Jorim Jaggibe565df2014-04-28 17:51:23 +0200141 mActualHeight = actualHeight;
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200142 if (notifyListeners) {
143 notifyHeightChanged();
144 }
145 }
146
147 public void setActualHeight(int actualHeight) {
148 setActualHeight(actualHeight, true);
Jorim Jaggibe565df2014-04-28 17:51:23 +0200149 }
150
151 /**
152 * See {@link #setActualHeight}.
153 *
Jorim Jaggi9cbadd32014-05-01 20:18:31 +0200154 * @return The current actual height of this notification.
Jorim Jaggibe565df2014-04-28 17:51:23 +0200155 */
156 public int getActualHeight() {
157 return mActualHeight;
158 }
159
160 /**
161 * @return The maximum height of this notification.
162 */
Jorim Jaggi4222d9a2014-04-23 16:13:15 +0200163 public int getMaxHeight() {
164 return getHeight();
165 }
166
167 /**
168 * @return The minimum height of this notification.
169 */
170 public int getMinHeight() {
171 return getHeight();
172 }
Jorim Jaggibe565df2014-04-28 17:51:23 +0200173
174 /**
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200175 * Sets the notification as dimmed. The default implementation does nothing.
176 *
177 * @param dimmed Whether the notification should be dimmed.
178 * @param fade Whether an animation should be played to change the state.
179 */
180 public void setDimmed(boolean dimmed, boolean fade) {
181 }
182
183 /**
John Spurlockbf370992014-06-17 13:58:31 -0400184 * Sets the notification as dark. The default implementation does nothing.
185 *
186 * @param dark Whether the notification should be dark.
187 * @param fade Whether an animation should be played to change the state.
188 */
189 public void setDark(boolean dark, boolean fade) {
190 }
191
192 /**
Jorim Jaggiae441282014-08-01 02:45:18 +0200193 * See {@link #setHideSensitive}. This is a variant which notifies this view in advance about
194 * the upcoming state of hiding sensitive notifications. It gets called at the very beginning
195 * of a stack scroller update such that the updated intrinsic height (which is dependent on
196 * whether private or public layout is showing) gets taken into account into all layout
197 * calculations.
198 */
199 public void setHideSensitiveForIntrinsicHeight(boolean hideSensitive) {
200 }
201
202 /**
203 * Sets whether the notification should hide its private contents if it is sensitive.
204 */
205 public void setHideSensitive(boolean hideSensitive, boolean animated, long delay,
206 long duration) {
207 }
208
209 /**
Jorim Jaggi9cbadd32014-05-01 20:18:31 +0200210 * @return The desired notification height.
211 */
212 public int getIntrinsicHeight() {
Selim Cineka5eaa602014-05-12 21:27:47 +0200213 return getHeight();
Jorim Jaggi9cbadd32014-05-01 20:18:31 +0200214 }
215
216 /**
Jorim Jaggibe565df2014-04-28 17:51:23 +0200217 * Sets the amount this view should be clipped from the top. This is used when an expanded
218 * notification is scrolling in the top or bottom stack.
219 *
220 * @param clipTopAmount The amount of pixels this view should be clipped from top.
221 */
222 public void setClipTopAmount(int clipTopAmount) {
223 mClipTopAmount = clipTopAmount;
Jorim Jaggibe565df2014-04-28 17:51:23 +0200224 }
225
Selim Cinekeb973562014-05-02 17:07:49 +0200226 public int getClipTopAmount() {
227 return mClipTopAmount;
228 }
229
Jorim Jaggibe565df2014-04-28 17:51:23 +0200230 public void setOnHeightChangedListener(OnHeightChangedListener listener) {
231 mOnHeightChangedListener = listener;
232 }
233
234 /**
Jorim Jaggi4222d9a2014-04-23 16:13:15 +0200235 * @return Whether we can expand this views content.
Jorim Jaggibe565df2014-04-28 17:51:23 +0200236 */
Jorim Jaggi4222d9a2014-04-23 16:13:15 +0200237 public boolean isContentExpandable() {
238 return false;
Jorim Jaggibe565df2014-04-28 17:51:23 +0200239 }
240
Jorim Jaggi9cbadd32014-05-01 20:18:31 +0200241 public void notifyHeightChanged() {
242 if (mOnHeightChangedListener != null) {
243 mOnHeightChangedListener.onHeightChanged(this);
244 }
245 }
246
Selim Cinekc27437b2014-05-14 10:23:33 +0200247 public boolean isTransparent() {
248 return false;
249 }
250
Jorim Jaggibe565df2014-04-28 17:51:23 +0200251 /**
Selim Cinek8efa6dd2014-05-19 16:27:37 +0200252 * Perform a remove animation on this view.
253 *
Jorim Jaggi60d07c52014-07-31 15:38:21 +0200254 * @param duration The duration of the remove animation.
Selim Cinek8efa6dd2014-05-19 16:27:37 +0200255 * @param translationDirection The direction value from [-1 ... 1] indicating in which the
256 * animation should be performed. A value of -1 means that The
257 * remove animation should be performed upwards,
258 * such that the child appears to be going away to the top. 1
259 * Should mean the opposite.
260 * @param onFinishedRunnable A runnable which should be run when the animation is finished.
261 */
Jorim Jaggi60d07c52014-07-31 15:38:21 +0200262 public abstract void performRemoveAnimation(long duration, float translationDirection,
Selim Cinek8efa6dd2014-05-19 16:27:37 +0200263 Runnable onFinishedRunnable);
264
Jorim Jaggi60d07c52014-07-31 15:38:21 +0200265 public abstract void performAddAnimation(long delay, long duration);
Selim Cinek8efa6dd2014-05-19 16:27:37 +0200266
Selim Cinek3d2b94bf2014-07-02 22:12:47 +0200267 public void setBelowSpeedBump(boolean below) {
268 }
269
Selim Cinek31094df2014-08-14 19:28:15 +0200270 public void onHeightReset() {
Selim Cineke34c6512014-08-14 11:19:41 +0200271 if (mOnHeightChangedListener != null) {
272 mOnHeightChangedListener.onReset(this);
273 }
Selim Cineka5e211b2014-08-11 17:35:48 +0200274 }
275
Selim Cinek8efa6dd2014-05-19 16:27:37 +0200276 /**
Selim Cineke32010a2014-08-20 23:50:41 +0200277 * This method returns the drawing rect for the view which is different from the regular
278 * drawing rect, since we layout all children in the {@link NotificationStackScrollLayout} at
279 * position 0 and usually the translation is neglected. Since we are manually clipping this
280 * view,we also need to subtract the clipTopAmount from the top. This is needed in order to
281 * ensure that accessibility and focusing work correctly.
282 *
283 * @param outRect The (scrolled) drawing bounds of the view.
284 */
285 @Override
286 public void getDrawingRect(Rect outRect) {
287 super.getDrawingRect(outRect);
288 outRect.left += getTranslationX();
289 outRect.right += getTranslationX();
290 outRect.bottom = (int) (outRect.top + getTranslationY() + getActualHeight());
291 outRect.top += getTranslationY() + getClipTopAmount();
292 }
293
294 /**
Jorim Jaggibe565df2014-04-28 17:51:23 +0200295 * A listener notifying when {@link #getActualHeight} changes.
296 */
297 public interface OnHeightChangedListener {
Jorim Jaggi30c305c2014-07-01 23:34:41 +0200298
299 /**
300 * @param view the view for which the height changed, or {@code null} if just the top
301 * padding or the padding between the elements changed
302 */
Jorim Jaggibe565df2014-04-28 17:51:23 +0200303 void onHeightChanged(ExpandableView view);
Selim Cineka5e211b2014-08-11 17:35:48 +0200304
305 /**
306 * Called when the view is reset and therefore the height will change abruptly
307 *
308 * @param view The view which was reset.
309 */
310 void onReset(ExpandableView view);
Jorim Jaggibe565df2014-04-28 17:51:23 +0200311 }
312}