blob: 1e5dcf7d1addba592ddbfc6430367ac3a370da70 [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
Jorim Jaggibe565df2014-04-28 17:51:23 +020036
37 private OnHeightChangedListener mOnHeightChangedListener;
Selim Cinek379ff8f2015-02-20 17:03:16 +010038 protected int mMaxViewHeight;
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;
Jorim Jaggi4e857f42014-11-17 19:14:04 +010042 private boolean mDark;
Selim Cinek24d7cfa2014-05-20 13:50:57 +020043 private ArrayList<View> mMatchParentViews = new ArrayList<View>();
Selim Cineka272dfe2015-02-20 18:12:28 +010044 private int mClipTopOptimization;
45 private static Rect mClipRect = new Rect();
Jorim Jaggibe565df2014-04-28 17:51:23 +020046
47 public ExpandableView(Context context, AttributeSet attrs) {
48 super(context, attrs);
Selim Cinek379ff8f2015-02-20 17:03:16 +010049 mMaxViewHeight = getResources().getDimensionPixelSize(
Selim Cinek24d7cfa2014-05-20 13:50:57 +020050 R.dimen.notification_max_height);
51 }
52
53 @Override
54 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Selim Cinek379ff8f2015-02-20 17:03:16 +010055 int ownMaxHeight = mMaxViewHeight;
Selim Cinek24d7cfa2014-05-20 13:50:57 +020056 int heightMode = MeasureSpec.getMode(heightMeasureSpec);
57 boolean hasFixedHeight = heightMode == MeasureSpec.EXACTLY;
58 boolean isHeightLimited = heightMode == MeasureSpec.AT_MOST;
59 if (hasFixedHeight || isHeightLimited) {
60 int size = MeasureSpec.getSize(heightMeasureSpec);
61 ownMaxHeight = Math.min(ownMaxHeight, size);
62 }
63 int newHeightSpec = MeasureSpec.makeMeasureSpec(ownMaxHeight, MeasureSpec.AT_MOST);
64 int maxChildHeight = 0;
65 int childCount = getChildCount();
66 for (int i = 0; i < childCount; i++) {
67 View child = getChildAt(i);
68 int childHeightSpec = newHeightSpec;
69 ViewGroup.LayoutParams layoutParams = child.getLayoutParams();
70 if (layoutParams.height != ViewGroup.LayoutParams.MATCH_PARENT) {
71 if (layoutParams.height >= 0) {
72 // An actual height is set
73 childHeightSpec = layoutParams.height > ownMaxHeight
74 ? MeasureSpec.makeMeasureSpec(ownMaxHeight, MeasureSpec.EXACTLY)
75 : MeasureSpec.makeMeasureSpec(layoutParams.height, MeasureSpec.EXACTLY);
76 }
Jorim Jaggib741f052014-06-03 19:57:26 +020077 child.measure(
78 getChildMeasureSpec(widthMeasureSpec, 0 /* padding */, layoutParams.width),
79 childHeightSpec);
Selim Cinek24d7cfa2014-05-20 13:50:57 +020080 int childHeight = child.getMeasuredHeight();
81 maxChildHeight = Math.max(maxChildHeight, childHeight);
82 } else {
83 mMatchParentViews.add(child);
84 }
85 }
Selim Cinek379ff8f2015-02-20 17:03:16 +010086 int ownHeight = hasFixedHeight ? ownMaxHeight :
87 isHeightLimited ? Math.min(ownMaxHeight, maxChildHeight) : maxChildHeight;
Selim Cinek24d7cfa2014-05-20 13:50:57 +020088 newHeightSpec = MeasureSpec.makeMeasureSpec(ownHeight, MeasureSpec.EXACTLY);
89 for (View child : mMatchParentViews) {
Jorim Jaggib741f052014-06-03 19:57:26 +020090 child.measure(getChildMeasureSpec(
91 widthMeasureSpec, 0 /* padding */, child.getLayoutParams().width),
92 newHeightSpec);
Selim Cinek24d7cfa2014-05-20 13:50:57 +020093 }
94 mMatchParentViews.clear();
95 int width = MeasureSpec.getSize(widthMeasureSpec);
96 setMeasuredDimension(width, ownHeight);
Jorim Jaggibe565df2014-04-28 17:51:23 +020097 }
98
99 @Override
Jorim Jaggibe565df2014-04-28 17:51:23 +0200100 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Selim Cinek24d7cfa2014-05-20 13:50:57 +0200101 super.onLayout(changed, left, top, right, bottom);
Jorim Jaggibe565df2014-04-28 17:51:23 +0200102 if (!mActualHeightInitialized && mActualHeight == 0) {
Selim Cinekd2319fb2014-09-01 19:41:54 +0200103 int initialHeight = getInitialHeight();
104 if (initialHeight != 0) {
105 setActualHeight(initialHeight);
106 }
Jorim Jaggibe565df2014-04-28 17:51:23 +0200107 }
Jorim Jaggibe565df2014-04-28 17:51:23 +0200108 }
109
Selim Cinek1a521f32014-11-03 17:39:29 +0100110 /**
111 * Resets the height of the view on the next layout pass
112 */
113 protected void resetActualHeight() {
Chris Wren310df312014-10-31 14:29:46 -0400114 mActualHeight = 0;
115 mActualHeightInitialized = false;
Selim Cinek1a521f32014-11-03 17:39:29 +0100116 requestLayout();
Chris Wren310df312014-10-31 14:29:46 -0400117 }
118
Selim Cinekc27437b2014-05-14 10:23:33 +0200119 protected int getInitialHeight() {
120 return getHeight();
121 }
122
Jorim Jaggi00ebdfe2014-05-02 17:29:56 +0200123 @Override
124 public boolean dispatchTouchEvent(MotionEvent ev) {
125 if (filterMotionEvent(ev)) {
126 return super.dispatchTouchEvent(ev);
127 }
128 return false;
129 }
130
Selim Cinek1a521f32014-11-03 17:39:29 +0100131 protected boolean filterMotionEvent(MotionEvent event) {
Jorim Jaggi00ebdfe2014-05-02 17:29:56 +0200132 return event.getActionMasked() != MotionEvent.ACTION_DOWN
133 || event.getY() > mClipTopAmount && event.getY() < mActualHeight;
134 }
135
Jorim Jaggibe565df2014-04-28 17:51:23 +0200136 /**
137 * Sets the actual height of this notification. This is different than the laid out
138 * {@link View#getHeight()}, as we want to avoid layouting during scrolling and expanding.
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200139 *
140 * @param actualHeight The height of this notification.
141 * @param notifyListeners Whether the listener should be informed about the change.
Jorim Jaggibe565df2014-04-28 17:51:23 +0200142 */
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200143 public void setActualHeight(int actualHeight, boolean notifyListeners) {
Jorim Jaggi2580a9762014-06-25 03:08:25 +0200144 mActualHeightInitialized = true;
Jorim Jaggibe565df2014-04-28 17:51:23 +0200145 mActualHeight = actualHeight;
Selim Cineka272dfe2015-02-20 18:12:28 +0100146 updateClipping();
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200147 if (notifyListeners) {
148 notifyHeightChanged();
149 }
150 }
151
152 public void setActualHeight(int actualHeight) {
153 setActualHeight(actualHeight, true);
Jorim Jaggibe565df2014-04-28 17:51:23 +0200154 }
155
156 /**
157 * See {@link #setActualHeight}.
158 *
Jorim Jaggi9cbadd32014-05-01 20:18:31 +0200159 * @return The current actual height of this notification.
Jorim Jaggibe565df2014-04-28 17:51:23 +0200160 */
161 public int getActualHeight() {
162 return mActualHeight;
163 }
164
165 /**
166 * @return The maximum height of this notification.
167 */
Jorim Jaggi4222d9a2014-04-23 16:13:15 +0200168 public int getMaxHeight() {
169 return getHeight();
170 }
171
172 /**
173 * @return The minimum height of this notification.
174 */
175 public int getMinHeight() {
176 return getHeight();
177 }
Jorim Jaggibe565df2014-04-28 17:51:23 +0200178
179 /**
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200180 * Sets the notification as dimmed. The default implementation does nothing.
181 *
182 * @param dimmed Whether the notification should be dimmed.
183 * @param fade Whether an animation should be played to change the state.
184 */
185 public void setDimmed(boolean dimmed, boolean fade) {
186 }
187
188 /**
John Spurlockbf370992014-06-17 13:58:31 -0400189 * Sets the notification as dark. The default implementation does nothing.
190 *
191 * @param dark Whether the notification should be dark.
192 * @param fade Whether an animation should be played to change the state.
Jorim Jaggi4e857f42014-11-17 19:14:04 +0100193 * @param delay If fading, the delay of the animation.
John Spurlockbf370992014-06-17 13:58:31 -0400194 */
Jorim Jaggi4e857f42014-11-17 19:14:04 +0100195 public void setDark(boolean dark, boolean fade, long delay) {
196 mDark = dark;
197 }
198
199 public boolean isDark() {
200 return mDark;
John Spurlockbf370992014-06-17 13:58:31 -0400201 }
202
203 /**
Jorim Jaggiae441282014-08-01 02:45:18 +0200204 * See {@link #setHideSensitive}. This is a variant which notifies this view in advance about
205 * the upcoming state of hiding sensitive notifications. It gets called at the very beginning
206 * of a stack scroller update such that the updated intrinsic height (which is dependent on
207 * whether private or public layout is showing) gets taken into account into all layout
208 * calculations.
209 */
210 public void setHideSensitiveForIntrinsicHeight(boolean hideSensitive) {
211 }
212
213 /**
214 * Sets whether the notification should hide its private contents if it is sensitive.
215 */
216 public void setHideSensitive(boolean hideSensitive, boolean animated, long delay,
217 long duration) {
218 }
219
220 /**
Jorim Jaggi9cbadd32014-05-01 20:18:31 +0200221 * @return The desired notification height.
222 */
223 public int getIntrinsicHeight() {
Selim Cineka5eaa602014-05-12 21:27:47 +0200224 return getHeight();
Jorim Jaggi9cbadd32014-05-01 20:18:31 +0200225 }
226
227 /**
Jorim Jaggibe565df2014-04-28 17:51:23 +0200228 * Sets the amount this view should be clipped from the top. This is used when an expanded
229 * notification is scrolling in the top or bottom stack.
230 *
231 * @param clipTopAmount The amount of pixels this view should be clipped from top.
232 */
233 public void setClipTopAmount(int clipTopAmount) {
234 mClipTopAmount = clipTopAmount;
Jorim Jaggibe565df2014-04-28 17:51:23 +0200235 }
236
Selim Cinekeb973562014-05-02 17:07:49 +0200237 public int getClipTopAmount() {
238 return mClipTopAmount;
239 }
240
Jorim Jaggibe565df2014-04-28 17:51:23 +0200241 public void setOnHeightChangedListener(OnHeightChangedListener listener) {
242 mOnHeightChangedListener = listener;
243 }
244
245 /**
Jorim Jaggi4222d9a2014-04-23 16:13:15 +0200246 * @return Whether we can expand this views content.
Jorim Jaggibe565df2014-04-28 17:51:23 +0200247 */
Jorim Jaggi4222d9a2014-04-23 16:13:15 +0200248 public boolean isContentExpandable() {
249 return false;
Jorim Jaggibe565df2014-04-28 17:51:23 +0200250 }
251
Jorim Jaggi9cbadd32014-05-01 20:18:31 +0200252 public void notifyHeightChanged() {
253 if (mOnHeightChangedListener != null) {
254 mOnHeightChangedListener.onHeightChanged(this);
255 }
256 }
257
Selim Cinekc27437b2014-05-14 10:23:33 +0200258 public boolean isTransparent() {
259 return false;
260 }
261
Jorim Jaggibe565df2014-04-28 17:51:23 +0200262 /**
Selim Cinek8efa6dd2014-05-19 16:27:37 +0200263 * Perform a remove animation on this view.
264 *
Jorim Jaggi60d07c52014-07-31 15:38:21 +0200265 * @param duration The duration of the remove animation.
Selim Cinek8efa6dd2014-05-19 16:27:37 +0200266 * @param translationDirection The direction value from [-1 ... 1] indicating in which the
267 * animation should be performed. A value of -1 means that The
268 * remove animation should be performed upwards,
269 * such that the child appears to be going away to the top. 1
270 * Should mean the opposite.
271 * @param onFinishedRunnable A runnable which should be run when the animation is finished.
272 */
Jorim Jaggi60d07c52014-07-31 15:38:21 +0200273 public abstract void performRemoveAnimation(long duration, float translationDirection,
Selim Cinek8efa6dd2014-05-19 16:27:37 +0200274 Runnable onFinishedRunnable);
275
Jorim Jaggi60d07c52014-07-31 15:38:21 +0200276 public abstract void performAddAnimation(long delay, long duration);
Selim Cinek8efa6dd2014-05-19 16:27:37 +0200277
Selim Cinek3d2b94bf2014-07-02 22:12:47 +0200278 public void setBelowSpeedBump(boolean below) {
279 }
280
Selim Cinek31094df2014-08-14 19:28:15 +0200281 public void onHeightReset() {
Selim Cineke34c6512014-08-14 11:19:41 +0200282 if (mOnHeightChangedListener != null) {
283 mOnHeightChangedListener.onReset(this);
284 }
Selim Cineka5e211b2014-08-11 17:35:48 +0200285 }
286
Selim Cinek8efa6dd2014-05-19 16:27:37 +0200287 /**
Selim Cineke32010a2014-08-20 23:50:41 +0200288 * This method returns the drawing rect for the view which is different from the regular
289 * drawing rect, since we layout all children in the {@link NotificationStackScrollLayout} at
290 * position 0 and usually the translation is neglected. Since we are manually clipping this
291 * view,we also need to subtract the clipTopAmount from the top. This is needed in order to
292 * ensure that accessibility and focusing work correctly.
293 *
294 * @param outRect The (scrolled) drawing bounds of the view.
295 */
296 @Override
297 public void getDrawingRect(Rect outRect) {
298 super.getDrawingRect(outRect);
299 outRect.left += getTranslationX();
300 outRect.right += getTranslationX();
301 outRect.bottom = (int) (outRect.top + getTranslationY() + getActualHeight());
302 outRect.top += getTranslationY() + getClipTopAmount();
303 }
304
Selim Cineka272dfe2015-02-20 18:12:28 +0100305 private void updateClipping() {
306 mClipRect.set(0, mClipTopOptimization, getWidth(), getActualHeight());
307 setClipBounds(mClipRect);
308 }
309
310 public int getClipTopOptimization() {
311 return mClipTopOptimization;
312 }
313
314 /**
315 * Set that the view will be clipped by a given amount from the top. Contrary to
316 * {@link #setClipTopAmount} this amount doesn't effect shadows and the background.
317 *
318 * @param clipTopOptimization the amount to clip from the top
319 */
320 public void setClipTopOptimization(int clipTopOptimization) {
321 mClipTopOptimization = clipTopOptimization;
322 updateClipping();
323 }
324
Selim Cineke32010a2014-08-20 23:50:41 +0200325 /**
Jorim Jaggibe565df2014-04-28 17:51:23 +0200326 * A listener notifying when {@link #getActualHeight} changes.
327 */
328 public interface OnHeightChangedListener {
Jorim Jaggi30c305c2014-07-01 23:34:41 +0200329
330 /**
331 * @param view the view for which the height changed, or {@code null} if just the top
332 * padding or the padding between the elements changed
333 */
Jorim Jaggibe565df2014-04-28 17:51:23 +0200334 void onHeightChanged(ExpandableView view);
Selim Cineka5e211b2014-08-11 17:35:48 +0200335
336 /**
337 * Called when the view is reset and therefore the height will change abruptly
338 *
339 * @param view The view which was reset.
340 */
341 void onReset(ExpandableView view);
Jorim Jaggibe565df2014-04-28 17:51:23 +0200342 }
343}