blob: b759684e84ecfc81059381020df4b488a02d60e0 [file] [log] [blame]
Chris Wren51c75102013-07-16 20:49:17 -04001/*
2 * Copyright (C) 2013 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
Selim Cinekb5605e52015-02-20 18:21:41 +010019import android.animation.Animator;
20import android.animation.AnimatorListenerAdapter;
21import android.animation.ValueAnimator;
Chris Wren51c75102013-07-16 20:49:17 -040022import android.content.Context;
Selim Cinekcab4a602014-09-03 14:47:57 +020023import android.graphics.drawable.AnimatedVectorDrawable;
24import android.graphics.drawable.AnimationDrawable;
Selim Cinekb5605e52015-02-20 18:21:41 +010025import android.graphics.drawable.ColorDrawable;
Selim Cinekcab4a602014-09-03 14:47:57 +020026import android.graphics.drawable.Drawable;
Jorim Jaggib1cd3c12014-09-08 19:55:17 +020027import android.service.notification.StatusBarNotification;
Chris Wren51c75102013-07-16 20:49:17 -040028import android.util.AttributeSet;
Selim Cinek1a521f32014-11-03 17:39:29 +010029import android.view.MotionEvent;
Dan Sandlera5e0f412014-01-23 15:11:54 -050030import android.view.View;
Jorim Jaggib1cd3c12014-09-08 19:55:17 +020031import android.view.ViewStub;
Jorim Jaggife40f7d2014-04-28 15:20:04 +020032import android.view.accessibility.AccessibilityEvent;
Selim Cinekb5605e52015-02-20 18:21:41 +010033import android.view.animation.LinearInterpolator;
Selim Cinekcab4a602014-09-03 14:47:57 +020034import android.widget.ImageView;
Selim Cinekb5605e52015-02-20 18:21:41 +010035
Dan Sandlera5e0f412014-01-23 15:11:54 -050036import com.android.systemui.R;
Selim Cinekb5605e52015-02-20 18:21:41 +010037import com.android.systemui.statusbar.phone.NotificationGroupManager;
38import com.android.systemui.statusbar.phone.PhoneStatusBar;
39import com.android.systemui.statusbar.stack.NotificationChildrenContainer;
40import com.android.systemui.statusbar.stack.StackScrollState;
41import com.android.systemui.statusbar.stack.StackStateAnimator;
42import com.android.systemui.statusbar.stack.StackViewState;
43
44import java.util.List;
Dan Sandlera5e0f412014-01-23 15:11:54 -050045
Jorim Jaggi4222d9a2014-04-23 16:13:15 +020046public class ExpandableNotificationRow extends ActivatableNotificationView {
Selim Cinekb5605e52015-02-20 18:21:41 +010047
48 private static final int DEFAULT_DIVIDER_ALPHA = 0x29;
49 private static final int COLORED_DIVIDER_ALPHA = 0x7B;
50 private final LinearInterpolator mLinearInterpolator = new LinearInterpolator();
Selim Cinek1685e632014-04-08 02:27:49 +020051 private int mRowMinHeight;
Chris Wren51c75102013-07-16 20:49:17 -040052
Selim Cinek1685e632014-04-08 02:27:49 +020053 /** Does this row contain layouts that can adapt to row expansion */
Chris Wren51c75102013-07-16 20:49:17 -040054 private boolean mExpandable;
Selim Cinek1685e632014-04-08 02:27:49 +020055 /** Has the user actively changed the expansion state of this row */
56 private boolean mHasUserChangedExpansion;
57 /** If {@link #mHasUserChangedExpansion}, has the user expanded this row */
Chris Wren51c75102013-07-16 20:49:17 -040058 private boolean mUserExpanded;
Selim Cinek1685e632014-04-08 02:27:49 +020059 /** Is the user touching this row */
Chris Wren51c75102013-07-16 20:49:17 -040060 private boolean mUserLocked;
Selim Cinek1685e632014-04-08 02:27:49 +020061 /** Are we showing the "public" version */
Dan Sandlera5e0f412014-01-23 15:11:54 -050062 private boolean mShowingPublic;
Jorim Jaggiae441282014-08-01 02:45:18 +020063 private boolean mSensitive;
64 private boolean mShowingPublicInitialized;
65 private boolean mShowingPublicForIntrinsicHeight;
Chris Wren51c75102013-07-16 20:49:17 -040066
Selim Cinek1685e632014-04-08 02:27:49 +020067 /**
68 * Is this notification expanded by the system. The expansion state can be overridden by the
69 * user expansion.
70 */
71 private boolean mIsSystemExpanded;
Jorim Jaggidce3c4c2014-04-29 23:12:24 +020072
73 /**
74 * Whether the notification expansion is disabled. This is the case on Keyguard.
75 */
76 private boolean mExpansionDisabled;
77
Jorim Jaggibe565df2014-04-28 17:51:23 +020078 private NotificationContentView mPublicLayout;
79 private NotificationContentView mPrivateLayout;
Selim Cinek1685e632014-04-08 02:27:49 +020080 private int mMaxExpandHeight;
Selim Cinek8d490d42015-04-10 00:05:50 -070081 private int mHeadsUpHeight;
Selim Cinek863834b2014-05-20 04:20:25 +020082 private View mVetoButton;
Dan Sandler0d3e62f2014-07-14 17:13:50 -040083 private boolean mClearable;
Chris Wren78403d72014-07-28 10:23:24 +010084 private ExpansionLogger mLogger;
85 private String mLoggingKey;
Selim Cineka5e211b2014-08-11 17:35:48 +020086 private boolean mWasReset;
Chris Wren78403d72014-07-28 10:23:24 +010087
Selim Cinek8d490d42015-04-10 00:05:50 -070088 private NotificationGuts mGuts;
Jorim Jaggib1cd3c12014-09-08 19:55:17 +020089 private StatusBarNotification mStatusBarNotification;
Selim Cinek1a521f32014-11-03 17:39:29 +010090 private boolean mIsHeadsUp;
Selim Cinekb5605e52015-02-20 18:21:41 +010091 private View mExpandButton;
92 private View mExpandButtonDivider;
93 private ViewStub mExpandButtonStub;
94 private ViewStub mChildrenContainerStub;
95 private NotificationGroupManager mGroupManager;
96 private View mExpandButtonContainer;
97 private boolean mChildrenExpanded;
98 private NotificationChildrenContainer mChildrenContainer;
99 private ValueAnimator mChildExpandAnimator;
100 private float mChildrenExpandProgress;
101 private float mExpandButtonStart;
Selim Cinekab29aeb2015-02-20 18:18:32 +0100102 private ViewStub mGutsStub;
Selim Cinekb5605e52015-02-20 18:21:41 +0100103 private boolean mHasExpandAction;
104 private boolean mIsSystemChildExpanded;
105 private OnClickListener mExpandClickListener = new OnClickListener() {
106 @Override
107 public void onClick(View v) {
108 mGroupManager.setGroupExpanded(mStatusBarNotification,
109 !mChildrenExpanded);
110 }
111 };
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700112 private boolean mInShade;
Jorim Jaggib1cd3c12014-09-08 19:55:17 +0200113
Selim Cinek8d490d42015-04-10 00:05:50 -0700114 public NotificationContentView getPrivateLayout() {
115 return mPrivateLayout;
116 }
117
118 public NotificationContentView getPublicLayout() {
119 return mPublicLayout;
120 }
121
Selim Cinekcab4a602014-09-03 14:47:57 +0200122 public void setIconAnimationRunning(boolean running) {
123 setIconAnimationRunning(running, mPublicLayout);
124 setIconAnimationRunning(running, mPrivateLayout);
125 }
126
127 private void setIconAnimationRunning(boolean running, NotificationContentView layout) {
128 if (layout != null) {
129 View contractedChild = layout.getContractedChild();
130 View expandedChild = layout.getExpandedChild();
Selim Cinek8d490d42015-04-10 00:05:50 -0700131 View headsUpChild = layout.getHeadsUpChild();
Selim Cinekcab4a602014-09-03 14:47:57 +0200132 setIconAnimationRunningForChild(running, contractedChild);
133 setIconAnimationRunningForChild(running, expandedChild);
Selim Cinek8d490d42015-04-10 00:05:50 -0700134 setIconAnimationRunningForChild(running, headsUpChild);
Selim Cinekcab4a602014-09-03 14:47:57 +0200135 }
136 }
137
138 private void setIconAnimationRunningForChild(boolean running, View child) {
139 if (child != null) {
140 ImageView icon = (ImageView) child.findViewById(com.android.internal.R.id.icon);
141 setIconRunning(icon, running);
142 ImageView rightIcon = (ImageView) child.findViewById(
143 com.android.internal.R.id.right_icon);
144 setIconRunning(rightIcon, running);
145 }
146 }
147
148 private void setIconRunning(ImageView imageView, boolean running) {
149 if (imageView != null) {
150 Drawable drawable = imageView.getDrawable();
151 if (drawable instanceof AnimationDrawable) {
152 AnimationDrawable animationDrawable = (AnimationDrawable) drawable;
153 if (running) {
154 animationDrawable.start();
155 } else {
156 animationDrawable.stop();
157 }
158 } else if (drawable instanceof AnimatedVectorDrawable) {
159 AnimatedVectorDrawable animationDrawable = (AnimatedVectorDrawable) drawable;
160 if (running) {
161 animationDrawable.start();
162 } else {
163 animationDrawable.stop();
164 }
165 }
166 }
167 }
168
Jorim Jaggib1cd3c12014-09-08 19:55:17 +0200169 public void setStatusBarNotification(StatusBarNotification statusBarNotification) {
170 mStatusBarNotification = statusBarNotification;
Selim Cineka37774f2014-11-11 19:16:18 +0100171 updateVetoButton();
Selim Cinekb5605e52015-02-20 18:21:41 +0100172 updateExpandButton();
Jorim Jaggib1cd3c12014-09-08 19:55:17 +0200173 }
174
175 public StatusBarNotification getStatusBarNotification() {
176 return mStatusBarNotification;
177 }
178
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700179 public boolean isHeadsUp() {
180 return mIsHeadsUp;
181 }
182
Selim Cinek1a521f32014-11-03 17:39:29 +0100183 public void setHeadsUp(boolean isHeadsUp) {
184 mIsHeadsUp = isHeadsUp;
Selim Cinek8d490d42015-04-10 00:05:50 -0700185 mPrivateLayout.setHeadsUp(isHeadsUp);
Selim Cinek1a521f32014-11-03 17:39:29 +0100186 }
187
Selim Cinekb5605e52015-02-20 18:21:41 +0100188 public void setGroupManager(NotificationGroupManager groupManager) {
189 mGroupManager = groupManager;
190 }
191
192 public void addChildNotification(ExpandableNotificationRow row) {
193 addChildNotification(row, -1);
194 }
195
196 /**
197 * Add a child notification to this view.
198 *
199 * @param row the row to add
200 * @param childIndex the index to add it at, if -1 it will be added at the end
201 */
202 public void addChildNotification(ExpandableNotificationRow row, int childIndex) {
203 if (mChildrenContainer == null) {
204 mChildrenContainerStub.inflate();
205 }
206 mChildrenContainer.addNotification(row, childIndex);
207 }
208
209 public void removeChildNotification(ExpandableNotificationRow row) {
210 if (mChildrenContainer != null) {
211 mChildrenContainer.removeNotification(row);
212 }
213 }
214
215 @Override
216 public boolean areChildrenExpanded() {
217 return mChildrenExpanded;
218 }
219
220 public List<ExpandableNotificationRow> getNotificationChildren() {
221 return mChildrenContainer == null ? null : mChildrenContainer.getNotificationChildren();
222 }
223
224 /**
225 * Apply the order given in the list to the children.
226 *
227 * @param childOrder the new list order
228 * @return whether the list order has changed
229 */
230 public boolean applyChildOrder(List<ExpandableNotificationRow> childOrder) {
231 return mChildrenContainer != null && mChildrenContainer.applyChildOrder(childOrder);
232 }
233
234 public void getChildrenStates(StackScrollState resultState) {
235 if (mChildrenExpanded) {
236 StackViewState parentState = resultState.getViewStateForView(this);
237 mChildrenContainer.getState(resultState, parentState);
238 }
239 }
240
241 public void applyChildrenState(StackScrollState state) {
242 if (mChildrenExpanded) {
243 mChildrenContainer.applyState(state);
244 }
245 }
246
247 public void prepareExpansionChanged(StackScrollState state) {
248 if (mChildrenExpanded) {
249 mChildrenContainer.prepareExpansionChanged(state);
250 }
251 }
252
253 public void startChildAnimation(StackScrollState finalState,
254 StackStateAnimator stateAnimator, boolean withDelays, long delay, long duration) {
255 if (mChildrenExpanded) {
256 mChildrenContainer.startAnimationToState(finalState, stateAnimator, withDelays, delay,
257 duration);
258 }
259 }
260
261 public ExpandableNotificationRow getViewAtPosition(float y) {
262 if (!mChildrenExpanded) {
263 return this;
264 } else {
265 ExpandableNotificationRow view = mChildrenContainer.getViewAtPosition(y);
266 return view == null ? this : view;
267 }
268 }
269
Selim Cinekab29aeb2015-02-20 18:18:32 +0100270 public NotificationGuts getGuts() {
271 return mGuts;
272 }
273
Selim Cinekb5605e52015-02-20 18:21:41 +0100274 protected int calculateContentHeightFromActualHeight(int actualHeight) {
275 int realActualHeight = actualHeight;
276 if (hasBottomDecor()) {
277 realActualHeight -= getBottomDecorHeight();
278 }
279 realActualHeight = Math.max(getMinHeight(), realActualHeight);
280 return realActualHeight;
281 }
282
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700283 public void setInShade(boolean inShade) {
284 mInShade = inShade;
285 }
286
287 public boolean isInShade() {
288 return mInShade;
289 }
290
291 public int getHeadsUpHeight() {
Selim Cinek8d490d42015-04-10 00:05:50 -0700292 return mHeadsUpHeight;
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700293 }
294
Chris Wren78403d72014-07-28 10:23:24 +0100295 public interface ExpansionLogger {
296 public void logNotificationExpansion(String key, boolean userAction, boolean expanded);
297 }
Selim Cinek1685e632014-04-08 02:27:49 +0200298
Chris Wren51c75102013-07-16 20:49:17 -0400299 public ExpandableNotificationRow(Context context, AttributeSet attrs) {
300 super(context, attrs);
301 }
302
Christoph Studera7fe6312014-06-27 19:32:44 +0200303 /**
304 * Resets this view so it can be re-used for an updated notification.
305 */
Christoph Studer22f2ee52014-07-29 22:57:21 +0200306 @Override
Christoph Studera7fe6312014-06-27 19:32:44 +0200307 public void reset() {
Christoph Studer22f2ee52014-07-29 22:57:21 +0200308 super.reset();
Christoph Studera7fe6312014-06-27 19:32:44 +0200309 mRowMinHeight = 0;
Chris Wren78403d72014-07-28 10:23:24 +0100310 final boolean wasExpanded = isExpanded();
Selim Cinek379ff8f2015-02-20 17:03:16 +0100311 mMaxViewHeight = 0;
Christoph Studera7fe6312014-06-27 19:32:44 +0200312 mExpandable = false;
313 mHasUserChangedExpansion = false;
314 mUserLocked = false;
315 mShowingPublic = false;
Jorim Jaggiae441282014-08-01 02:45:18 +0200316 mSensitive = false;
317 mShowingPublicInitialized = false;
Christoph Studera7fe6312014-06-27 19:32:44 +0200318 mIsSystemExpanded = false;
319 mExpansionDisabled = false;
Selim Cinek1a521f32014-11-03 17:39:29 +0100320 mPublicLayout.reset(mIsHeadsUp);
321 mPrivateLayout.reset(mIsHeadsUp);
Selim Cinek31094df2014-08-14 19:28:15 +0200322 resetHeight();
323 logExpansionEvent(false, wasExpanded);
324 }
325
326 public void resetHeight() {
Selim Cinek1a521f32014-11-03 17:39:29 +0100327 if (mIsHeadsUp) {
328 resetActualHeight();
329 }
Christoph Studera7fe6312014-06-27 19:32:44 +0200330 mMaxExpandHeight = 0;
Selim Cinek8d490d42015-04-10 00:05:50 -0700331 mHeadsUpHeight = 0;
Selim Cineka5e211b2014-08-11 17:35:48 +0200332 mWasReset = true;
Selim Cinek31094df2014-08-14 19:28:15 +0200333 onHeightReset();
Selim Cinek6e28a672014-09-05 14:43:28 +0200334 requestLayout();
Christoph Studera7fe6312014-06-27 19:32:44 +0200335 }
336
Jorim Jaggi251957d2014-04-09 04:24:09 +0200337 @Override
Selim Cinek1a521f32014-11-03 17:39:29 +0100338 protected boolean filterMotionEvent(MotionEvent event) {
339 return mIsHeadsUp || super.filterMotionEvent(event);
340 }
341
342 @Override
Jorim Jaggi251957d2014-04-09 04:24:09 +0200343 protected void onFinishInflate() {
344 super.onFinishInflate();
Jorim Jaggibe565df2014-04-28 17:51:23 +0200345 mPublicLayout = (NotificationContentView) findViewById(R.id.expandedPublic);
346 mPrivateLayout = (NotificationContentView) findViewById(R.id.expanded);
Selim Cinekab29aeb2015-02-20 18:18:32 +0100347 mGutsStub = (ViewStub) findViewById(R.id.notification_guts_stub);
348 mGutsStub.setOnInflateListener(new ViewStub.OnInflateListener() {
Jorim Jaggib1cd3c12014-09-08 19:55:17 +0200349 @Override
350 public void onInflate(ViewStub stub, View inflated) {
351 mGuts = (NotificationGuts) inflated;
352 mGuts.setClipTopAmount(getClipTopAmount());
353 mGuts.setActualHeight(getActualHeight());
Selim Cinekab29aeb2015-02-20 18:18:32 +0100354 mGutsStub = null;
Jorim Jaggib1cd3c12014-09-08 19:55:17 +0200355 }
356 });
Selim Cinekb5605e52015-02-20 18:21:41 +0100357 mExpandButtonStub = (ViewStub) findViewById(R.id.more_button_stub);
358 mExpandButtonStub.setOnInflateListener(new ViewStub.OnInflateListener() {
359
360 @Override
361 public void onInflate(ViewStub stub, View inflated) {
362 mExpandButtonContainer = inflated;
363 mExpandButton = inflated.findViewById(R.id.notification_expand_button);
364 mExpandButtonDivider = inflated.findViewById(R.id.notification_expand_divider);
365 mExpandButtonContainer.setOnClickListener(mExpandClickListener);
366 }
367 });
368 mChildrenContainerStub = (ViewStub) findViewById(R.id.child_container_stub);
369 mChildrenContainerStub.setOnInflateListener(new ViewStub.OnInflateListener() {
370
371 @Override
372 public void onInflate(ViewStub stub, View inflated) {
373 mChildrenContainer = (NotificationChildrenContainer) inflated;
374 mChildrenContainer.setCollapseClickListener(mExpandClickListener);
375 updateChildrenVisibility(false);
376 }
377 });
Selim Cinek863834b2014-05-20 04:20:25 +0200378 mVetoButton = findViewById(R.id.veto);
Jorim Jaggife40f7d2014-04-28 15:20:04 +0200379 }
380
Selim Cinekab29aeb2015-02-20 18:18:32 +0100381 public void inflateGuts() {
382 if (mGuts == null) {
383 mGutsStub.inflate();
384 }
385 }
386
Selim Cinekb5605e52015-02-20 18:21:41 +0100387 private void updateChildrenVisibility(boolean animated) {
388 if (mChildrenContainer == null) {
389 return;
390 }
391 if (mChildExpandAnimator != null) {
392 mChildExpandAnimator.cancel();
393 }
394 float targetProgress = mChildrenExpanded ? 1.0f : 0.0f;
395 if (animated) {
396 if (mChildrenExpanded) {
397 mChildrenContainer.setVisibility(VISIBLE);
398 }
399 mExpandButtonStart = mExpandButtonContainer.getTranslationY();
400 mChildExpandAnimator = ValueAnimator.ofFloat(mChildrenExpandProgress, targetProgress);
401 mChildExpandAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
402 @Override
403 public void onAnimationUpdate(ValueAnimator animation) {
404 setChildrenExpandProgress((float) animation.getAnimatedValue());
405 }
406 });
407 mChildExpandAnimator.addListener(new AnimatorListenerAdapter() {
408 @Override
409 public void onAnimationEnd(Animator animation) {
410 mChildExpandAnimator = null;
411 if (!mChildrenExpanded) {
412 mChildrenContainer.setVisibility(INVISIBLE);
413 }
414 }
415 });
416 mChildExpandAnimator.setInterpolator(mLinearInterpolator);
417 mChildExpandAnimator.setDuration(
418 StackStateAnimator.ANIMATION_DURATION_EXPAND_CLICKED);
419 mChildExpandAnimator.start();
420 } else {
421 setChildrenExpandProgress(targetProgress);
422 mChildrenContainer.setVisibility(mChildrenExpanded ? VISIBLE : INVISIBLE);
423 }
424 }
425
426 private void setChildrenExpandProgress(float progress) {
427 mChildrenExpandProgress = progress;
428 updateExpandButtonAppearance();
429 NotificationContentView showingLayout = getShowingLayout();
430 float alpha = 1.0f - mChildrenExpandProgress;
431 alpha = PhoneStatusBar.ALPHA_OUT.getInterpolation(alpha);
432 showingLayout.setAlpha(alpha);
433 }
434
Jorim Jaggife40f7d2014-04-28 15:20:04 +0200435 @Override
Alan Viverettea54956a2015-01-07 16:05:02 -0800436 public boolean onRequestSendAccessibilityEventInternal(View child, AccessibilityEvent event) {
437 if (super.onRequestSendAccessibilityEventInternal(child, event)) {
Jorim Jaggife40f7d2014-04-28 15:20:04 +0200438 // Add a record for the entire layout since its content is somehow small.
439 // The event comes from a leaf view that is interacted with.
440 AccessibilityEvent record = AccessibilityEvent.obtain();
441 onInitializeAccessibilityEvent(record);
442 dispatchPopulateAccessibilityEvent(record);
443 event.appendRecord(record);
444 return true;
445 }
446 return false;
Jorim Jaggic5dc0d02014-04-15 15:42:55 +0200447 }
Chris Wren51c75102013-07-16 20:49:17 -0400448
John Spurlocke15452b2014-08-21 09:44:39 -0400449 @Override
Jorim Jaggi4e857f42014-11-17 19:14:04 +0100450 public void setDark(boolean dark, boolean fade, long delay) {
451 super.setDark(dark, fade, delay);
John Spurlocke15452b2014-08-21 09:44:39 -0400452 final NotificationContentView showing = getShowingLayout();
453 if (showing != null) {
Jorim Jaggi4e857f42014-11-17 19:14:04 +0100454 showing.setDark(dark, fade, delay);
John Spurlocke15452b2014-08-21 09:44:39 -0400455 }
456 }
457
Selim Cinek1685e632014-04-08 02:27:49 +0200458 public void setHeightRange(int rowMinHeight, int rowMaxHeight) {
459 mRowMinHeight = rowMinHeight;
Selim Cinek379ff8f2015-02-20 17:03:16 +0100460 mMaxViewHeight = rowMaxHeight;
Chris Wren51c75102013-07-16 20:49:17 -0400461 }
462
463 public boolean isExpandable() {
464 return mExpandable;
465 }
466
467 public void setExpandable(boolean expandable) {
468 mExpandable = expandable;
469 }
470
Selim Cinek1685e632014-04-08 02:27:49 +0200471 /**
472 * @return whether the user has changed the expansion state
473 */
474 public boolean hasUserChangedExpansion() {
475 return mHasUserChangedExpansion;
476 }
477
Chris Wren51c75102013-07-16 20:49:17 -0400478 public boolean isUserExpanded() {
479 return mUserExpanded;
480 }
481
Selim Cinek1685e632014-04-08 02:27:49 +0200482 /**
483 * Set this notification to be expanded by the user
484 *
485 * @param userExpanded whether the user wants this notification to be expanded
486 */
Chris Wren51c75102013-07-16 20:49:17 -0400487 public void setUserExpanded(boolean userExpanded) {
Christoph Studera7fe6312014-06-27 19:32:44 +0200488 if (userExpanded && !mExpandable) return;
Chris Wren78403d72014-07-28 10:23:24 +0100489 final boolean wasExpanded = isExpanded();
Selim Cinek1685e632014-04-08 02:27:49 +0200490 mHasUserChangedExpansion = true;
Chris Wren51c75102013-07-16 20:49:17 -0400491 mUserExpanded = userExpanded;
Chris Wren78403d72014-07-28 10:23:24 +0100492 logExpansionEvent(true, wasExpanded);
Chris Wren51c75102013-07-16 20:49:17 -0400493 }
494
Selim Cinekccd14fb2014-08-12 18:53:24 +0200495 public void resetUserExpansion() {
496 mHasUserChangedExpansion = false;
497 mUserExpanded = false;
498 }
499
Chris Wren51c75102013-07-16 20:49:17 -0400500 public boolean isUserLocked() {
501 return mUserLocked;
502 }
503
504 public void setUserLocked(boolean userLocked) {
505 mUserLocked = userLocked;
506 }
507
Selim Cinek1685e632014-04-08 02:27:49 +0200508 /**
509 * @return has the system set this notification to be expanded
510 */
511 public boolean isSystemExpanded() {
512 return mIsSystemExpanded;
513 }
514
515 /**
516 * Set this notification to be expanded by the system.
517 *
518 * @param expand whether the system wants this notification to be expanded.
519 */
520 public void setSystemExpanded(boolean expand) {
Selim Cinek31094df2014-08-14 19:28:15 +0200521 if (expand != mIsSystemExpanded) {
522 final boolean wasExpanded = isExpanded();
523 mIsSystemExpanded = expand;
Selim Cinekb5605e52015-02-20 18:21:41 +0100524 notifyHeightChanged(false /* needsAnimation */);
Selim Cinek31094df2014-08-14 19:28:15 +0200525 logExpansionEvent(false, wasExpanded);
526 }
Jorim Jaggidce3c4c2014-04-29 23:12:24 +0200527 }
528
529 /**
530 * @param expansionDisabled whether to prevent notification expansion
531 */
532 public void setExpansionDisabled(boolean expansionDisabled) {
Selim Cinek31094df2014-08-14 19:28:15 +0200533 if (expansionDisabled != mExpansionDisabled) {
534 final boolean wasExpanded = isExpanded();
535 mExpansionDisabled = expansionDisabled;
536 logExpansionEvent(false, wasExpanded);
537 if (wasExpanded != isExpanded()) {
Selim Cinekb5605e52015-02-20 18:21:41 +0100538 notifyHeightChanged(false /* needsAnimation */);
Selim Cinek31094df2014-08-14 19:28:15 +0200539 }
540 }
Selim Cinek1685e632014-04-08 02:27:49 +0200541 }
542
543 /**
Dan Sandler0d3e62f2014-07-14 17:13:50 -0400544 * @return Can the underlying notification be cleared?
545 */
546 public boolean isClearable() {
Selim Cineka37774f2014-11-11 19:16:18 +0100547 return mStatusBarNotification != null && mStatusBarNotification.isClearable();
Dan Sandler0d3e62f2014-07-14 17:13:50 -0400548 }
549
550 /**
Selim Cinek1685e632014-04-08 02:27:49 +0200551 * Apply an expansion state to the layout.
Selim Cinek1685e632014-04-08 02:27:49 +0200552 */
Jorim Jaggidce3c4c2014-04-29 23:12:24 +0200553 public void applyExpansionToLayout() {
554 boolean expand = isExpanded();
Chris Wren51c75102013-07-16 20:49:17 -0400555 if (expand && mExpandable) {
Selim Cinekb5605e52015-02-20 18:21:41 +0100556 setContentHeight(mMaxExpandHeight);
Chris Wren51c75102013-07-16 20:49:17 -0400557 } else {
Selim Cinekb5605e52015-02-20 18:21:41 +0100558 setContentHeight(mRowMinHeight);
Chris Wren51c75102013-07-16 20:49:17 -0400559 }
Chris Wren51c75102013-07-16 20:49:17 -0400560 }
Dan Sandlera5e0f412014-01-23 15:11:54 -0500561
Jorim Jaggi9cbadd32014-05-01 20:18:31 +0200562 @Override
563 public int getIntrinsicHeight() {
Jorim Jaggibe565df2014-04-28 17:51:23 +0200564 if (isUserLocked()) {
565 return getActualHeight();
566 }
Selim Cinek1685e632014-04-08 02:27:49 +0200567 boolean inExpansionState = isExpanded();
Selim Cinekb5605e52015-02-20 18:21:41 +0100568 int maxContentHeight;
Selim Cinek8d490d42015-04-10 00:05:50 -0700569 if (mIsHeadsUp) {
570 if (inExpansionState) {
571 maxContentHeight = Math.max(mMaxExpandHeight, mHeadsUpHeight);
572 } else {
573 maxContentHeight = Math.max(mRowMinHeight, mHeadsUpHeight);
574 }
575 } else if ((!inExpansionState && !mChildrenExpanded) || mShowingPublicForIntrinsicHeight) {
Selim Cinekb5605e52015-02-20 18:21:41 +0100576 maxContentHeight = mRowMinHeight;
577 } else if (mChildrenExpanded) {
578 maxContentHeight = mChildrenContainer.getIntrinsicHeight();
579 } else {
580 maxContentHeight = getMaxExpandHeight();
Selim Cinek1685e632014-04-08 02:27:49 +0200581 }
Selim Cinekb5605e52015-02-20 18:21:41 +0100582 return maxContentHeight + getBottomDecorHeight();
583 }
Selim Cinek1685e632014-04-08 02:27:49 +0200584
Selim Cinekb5605e52015-02-20 18:21:41 +0100585 @Override
586 protected boolean hasBottomDecor() {
587 return BaseStatusBar.ENABLE_CHILD_NOTIFICATIONS
588 && !mIsHeadsUp && mGroupManager.hasGroupChildren(mStatusBarNotification);
589 }
590
591 @Override
592 protected boolean canHaveBottomDecor() {
593 return BaseStatusBar.ENABLE_CHILD_NOTIFICATIONS && !mIsHeadsUp;
Selim Cinek1685e632014-04-08 02:27:49 +0200594 }
595
Selim Cinek1685e632014-04-08 02:27:49 +0200596 /**
597 * Check whether the view state is currently expanded. This is given by the system in {@link
598 * #setSystemExpanded(boolean)} and can be overridden by user expansion or
599 * collapsing in {@link #setUserExpanded(boolean)}. Note that the visual appearance of this
600 * view can differ from this state, if layout params are modified from outside.
601 *
602 * @return whether the view state is currently expanded.
603 */
604 private boolean isExpanded() {
Jorim Jaggidce3c4c2014-04-29 23:12:24 +0200605 return !mExpansionDisabled
Selim Cinekb5605e52015-02-20 18:21:41 +0100606 && (!hasUserChangedExpansion() && (isSystemExpanded() || isSystemChildExpanded())
607 || isUserExpanded());
608 }
609
610 private boolean isSystemChildExpanded() {
611 return mIsSystemChildExpanded;
612 }
613
614 public void setSystemChildExpanded(boolean expanded) {
615 mIsSystemChildExpanded = expanded;
Selim Cinek1685e632014-04-08 02:27:49 +0200616 }
617
618 @Override
619 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
620 super.onLayout(changed, left, top, right, bottom);
Selim Cineka5e211b2014-08-11 17:35:48 +0200621 boolean updateExpandHeight = mMaxExpandHeight == 0 && !mWasReset;
Selim Cinek8d490d42015-04-10 00:05:50 -0700622 updateMaxHeights();
Jorim Jaggibe565df2014-04-28 17:51:23 +0200623 if (updateExpandHeight) {
Jorim Jaggidce3c4c2014-04-29 23:12:24 +0200624 applyExpansionToLayout();
Jorim Jaggibe565df2014-04-28 17:51:23 +0200625 }
Selim Cineka5e211b2014-08-11 17:35:48 +0200626 mWasReset = false;
Selim Cinek1685e632014-04-08 02:27:49 +0200627 }
628
Selim Cinekb5605e52015-02-20 18:21:41 +0100629 @Override
630 protected boolean isChildInvisible(View child) {
631
632 // We don't want to layout the ChildrenContainer if this is a heads-up view, otherwise the
633 // view will get too high and the shadows will be off.
634 boolean isInvisibleChildContainer = child == mChildrenContainer && mIsHeadsUp;
635 return super.isChildInvisible(child) || isInvisibleChildContainer;
636 }
637
Selim Cinek8d490d42015-04-10 00:05:50 -0700638 private void updateMaxHeights() {
Selim Cinekd2319fb2014-09-01 19:41:54 +0200639 int intrinsicBefore = getIntrinsicHeight();
Selim Cinek8d490d42015-04-10 00:05:50 -0700640 View expandedChild = mPrivateLayout.getExpandedChild();
641 if (expandedChild == null) {
642 expandedChild = mPrivateLayout.getContractedChild();
643 }
644 mMaxExpandHeight = expandedChild.getHeight();
645 View headsUpChild = mPrivateLayout.getHeadsUpChild();
646 if (headsUpChild != null) {
647 mHeadsUpHeight = headsUpChild.getHeight();
648 }
Selim Cinekd2319fb2014-09-01 19:41:54 +0200649 if (intrinsicBefore != getIntrinsicHeight()) {
Selim Cinekb5605e52015-02-20 18:21:41 +0100650 notifyHeightChanged(false /* needsAnimation */);
Selim Cinekd2319fb2014-09-01 19:41:54 +0200651 }
652 }
653
Jorim Jaggiae441282014-08-01 02:45:18 +0200654 public void setSensitive(boolean sensitive) {
655 mSensitive = sensitive;
656 }
657
658 public void setHideSensitiveForIntrinsicHeight(boolean hideSensitive) {
659 mShowingPublicForIntrinsicHeight = mSensitive && hideSensitive;
660 }
661
662 public void setHideSensitive(boolean hideSensitive, boolean animated, long delay,
663 long duration) {
664 boolean oldShowingPublic = mShowingPublic;
665 mShowingPublic = mSensitive && hideSensitive;
666 if (mShowingPublicInitialized && mShowingPublic == oldShowingPublic) {
667 return;
668 }
Dan Sandlera5e0f412014-01-23 15:11:54 -0500669
670 // bail out if no public version
Selim Cinek1685e632014-04-08 02:27:49 +0200671 if (mPublicLayout.getChildCount() == 0) return;
Dan Sandlera5e0f412014-01-23 15:11:54 -0500672
Jorim Jaggiae441282014-08-01 02:45:18 +0200673 if (!animated) {
674 mPublicLayout.animate().cancel();
675 mPrivateLayout.animate().cancel();
676 mPublicLayout.setAlpha(1f);
677 mPrivateLayout.setAlpha(1f);
678 mPublicLayout.setVisibility(mShowingPublic ? View.VISIBLE : View.INVISIBLE);
679 mPrivateLayout.setVisibility(mShowingPublic ? View.INVISIBLE : View.VISIBLE);
680 } else {
681 animateShowingPublic(delay, duration);
682 }
Dan Sandler0d3e62f2014-07-14 17:13:50 -0400683
684 updateVetoButton();
Jorim Jaggiae441282014-08-01 02:45:18 +0200685 mShowingPublicInitialized = true;
686 }
687
688 private void animateShowingPublic(long delay, long duration) {
689 final View source = mShowingPublic ? mPrivateLayout : mPublicLayout;
690 View target = mShowingPublic ? mPublicLayout : mPrivateLayout;
691 source.setVisibility(View.VISIBLE);
692 target.setVisibility(View.VISIBLE);
693 target.setAlpha(0f);
694 source.animate().cancel();
695 target.animate().cancel();
696 source.animate()
697 .alpha(0f)
Jorim Jaggiae441282014-08-01 02:45:18 +0200698 .setStartDelay(delay)
699 .setDuration(duration)
700 .withEndAction(new Runnable() {
701 @Override
702 public void run() {
703 source.setVisibility(View.INVISIBLE);
704 }
705 });
706 target.animate()
707 .alpha(1f)
Jorim Jaggiae441282014-08-01 02:45:18 +0200708 .setStartDelay(delay)
709 .setDuration(duration);
Dan Sandler0d3e62f2014-07-14 17:13:50 -0400710 }
711
712 private void updateVetoButton() {
713 // public versions cannot be dismissed
714 mVetoButton.setVisibility(isClearable() && !mShowingPublic ? View.VISIBLE : View.GONE);
Dan Sandlera5e0f412014-01-23 15:11:54 -0500715 }
Jorim Jaggi251957d2014-04-09 04:24:09 +0200716
Selim Cinekb5605e52015-02-20 18:21:41 +0100717 public void setChildrenExpanded(boolean expanded, boolean animate) {
718 mChildrenExpanded = expanded;
719 updateChildrenVisibility(animate);
720 }
721
722 public void updateExpandButton() {
723 boolean hasExpand = hasBottomDecor();
724 if (hasExpand != mHasExpandAction) {
725 if (hasExpand) {
726 if (mExpandButtonContainer == null) {
727 mExpandButtonStub.inflate();
728 }
729 mExpandButtonContainer.setVisibility(View.VISIBLE);
730 updateExpandButtonAppearance();
731 updateExpandButtonColor();
732 } else if (mExpandButtonContainer != null) {
733 mExpandButtonContainer.setVisibility(View.GONE);
734 }
735 notifyHeightChanged(true /* needsAnimation */);
736 }
737 mHasExpandAction = hasExpand;
738 }
739
740 private void updateExpandButtonAppearance() {
741 if (mExpandButtonContainer == null) {
742 return;
743 }
744 float expandButtonAlpha = 0.0f;
745 float expandButtonTranslation = 0.0f;
746 float containerTranslation = 0.0f;
747 int minHeight = getMinHeight();
748 if (!mChildrenExpanded || mChildExpandAnimator != null) {
749 int expandActionHeight = getBottomDecorHeight();
750 int translationY = getActualHeight() - expandActionHeight;
751 if (translationY > minHeight) {
752 containerTranslation = translationY;
753 expandButtonAlpha = 1.0f;
754 expandButtonTranslation = 0.0f;
755 } else {
756 containerTranslation = minHeight;
757 float progress = expandActionHeight != 0
758 ? (minHeight - translationY) / (float) expandActionHeight
759 : 1.0f;
760 expandButtonTranslation = -progress * expandActionHeight * 0.7f;
761 float alphaProgress = Math.min(progress / 0.7f, 1.0f);
762 alphaProgress = PhoneStatusBar.ALPHA_OUT.getInterpolation(alphaProgress);
763 expandButtonAlpha = 1.0f - alphaProgress;
764 }
765 }
766 if (mChildExpandAnimator != null || mChildrenExpanded) {
767 expandButtonAlpha = (1.0f - mChildrenExpandProgress)
768 * expandButtonAlpha;
769 expandButtonTranslation = (1.0f - mChildrenExpandProgress)
770 * expandButtonTranslation;
771 float newTranslation = -getBottomDecorHeight();
772
773 // We don't want to take the actual height of the view as this is already
774 // interpolated by a custom interpolator leading to a confusing animation. We want
775 // to have a stable end value to interpolate in between
776 float collapsedHeight = !mChildrenExpanded
777 ? Math.max(StackStateAnimator.getFinalActualHeight(this)
778 - getBottomDecorHeight(), minHeight)
779 : mExpandButtonStart;
780 float translationProgress = mFastOutSlowInInterpolator.getInterpolation(
781 mChildrenExpandProgress);
782 containerTranslation = (1.0f - translationProgress) * collapsedHeight
783 + translationProgress * newTranslation;
784 }
785 mExpandButton.setAlpha(expandButtonAlpha);
786 mExpandButtonDivider.setAlpha(expandButtonAlpha);
787 mExpandButton.setTranslationY(expandButtonTranslation);
788 mExpandButtonContainer.setTranslationY(containerTranslation);
789 NotificationContentView showingLayout = getShowingLayout();
790 float layoutTranslation =
791 mExpandButtonContainer.getTranslationY() - showingLayout.getContentHeight();
792 layoutTranslation = Math.min(layoutTranslation, 0);
793 if (!mChildrenExpanded && mChildExpandAnimator == null) {
794 // Needed for the DragDownHelper in order not to jump there, as the position
795 // can be negative for a short time.
796 layoutTranslation = 0;
797 }
798 showingLayout.setTranslationY(layoutTranslation);
799 if (mChildrenContainer != null) {
800 mChildrenContainer.setTranslationY(
801 mExpandButtonContainer.getTranslationY() + getBottomDecorHeight());
802 }
803 }
804
805 private void updateExpandButtonColor() {
806 // TODO: This needs some more baking, currently only the divider is colored according to
807 // the tint, but legacy black doesn't work yet perfectly for the button etc.
808 int color = getRippleColor();
809 if (color == mNormalRippleColor) {
810 color = 0;
811 }
812 if (mExpandButtonDivider != null) {
813 applyTint(mExpandButtonDivider, color);
814 }
815 if (mChildrenContainer != null) {
816 mChildrenContainer.setTintColor(color);
817 }
818 }
819
820 public static void applyTint(View v, int color) {
821 int alpha;
822 if (color != 0) {
823 alpha = COLORED_DIVIDER_ALPHA;
824 } else {
825 color = 0xff000000;
826 alpha = DEFAULT_DIVIDER_ALPHA;
827 }
828 if (v.getBackground() instanceof ColorDrawable) {
829 ColorDrawable background = (ColorDrawable) v.getBackground();
830 background.mutate();
831 background.setColor(color);
832 background.setAlpha(alpha);
833 }
834 }
835
Selim Cinek1685e632014-04-08 02:27:49 +0200836 public int getMaxExpandHeight() {
Selim Cinekb5605e52015-02-20 18:21:41 +0100837 return mMaxExpandHeight;
Chris Wren51c75102013-07-16 20:49:17 -0400838 }
Jorim Jaggi584a7aa2014-04-10 23:26:13 +0200839
Jorim Jaggibe565df2014-04-28 17:51:23 +0200840 @Override
Jorim Jaggi4222d9a2014-04-23 16:13:15 +0200841 public boolean isContentExpandable() {
Selim Cinek2f0df8a2014-06-10 17:40:42 +0200842 NotificationContentView showingLayout = getShowingLayout();
843 return showingLayout.isContentExpandable();
Jorim Jaggi4222d9a2014-04-23 16:13:15 +0200844 }
845
846 @Override
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200847 public void setActualHeight(int height, boolean notifyListeners) {
Selim Cinekb5605e52015-02-20 18:21:41 +0100848 super.setActualHeight(height, notifyListeners);
849 int contentHeight = calculateContentHeightFromActualHeight(height);
850 mPrivateLayout.setContentHeight(contentHeight);
851 mPublicLayout.setContentHeight(contentHeight);
Jorim Jaggib1cd3c12014-09-08 19:55:17 +0200852 if (mGuts != null) {
853 mGuts.setActualHeight(height);
854 }
Jorim Jaggibe565df2014-04-28 17:51:23 +0200855 invalidate();
Selim Cinekb5605e52015-02-20 18:21:41 +0100856 updateExpandButtonAppearance();
Jorim Jaggibe565df2014-04-28 17:51:23 +0200857 }
858
859 @Override
Selim Cinekb5605e52015-02-20 18:21:41 +0100860 public int getMaxContentHeight() {
Selim Cinek2f0df8a2014-06-10 17:40:42 +0200861 NotificationContentView showingLayout = getShowingLayout();
862 return showingLayout.getMaxHeight();
Jorim Jaggibe565df2014-04-28 17:51:23 +0200863 }
864
865 @Override
Jorim Jaggi4222d9a2014-04-23 16:13:15 +0200866 public int getMinHeight() {
Selim Cinek2f0df8a2014-06-10 17:40:42 +0200867 NotificationContentView showingLayout = getShowingLayout();
868 return showingLayout.getMinHeight();
Jorim Jaggi4222d9a2014-04-23 16:13:15 +0200869 }
870
871 @Override
Jorim Jaggibe565df2014-04-28 17:51:23 +0200872 public void setClipTopAmount(int clipTopAmount) {
873 super.setClipTopAmount(clipTopAmount);
874 mPrivateLayout.setClipTopAmount(clipTopAmount);
Selim Cinek2f0df8a2014-06-10 17:40:42 +0200875 mPublicLayout.setClipTopAmount(clipTopAmount);
Jorim Jaggib1cd3c12014-09-08 19:55:17 +0200876 if (mGuts != null) {
877 mGuts.setClipTopAmount(clipTopAmount);
878 }
Jorim Jaggibe565df2014-04-28 17:51:23 +0200879 }
880
881 public void notifyContentUpdated() {
Selim Cinek2f0df8a2014-06-10 17:40:42 +0200882 mPublicLayout.notifyContentUpdated();
Jorim Jaggibe565df2014-04-28 17:51:23 +0200883 mPrivateLayout.notifyContentUpdated();
Selim Cinek343e6e22014-04-11 21:23:30 +0200884 }
Selim Cinek7d447722014-06-10 15:51:59 +0200885
Selim Cinek31094df2014-08-14 19:28:15 +0200886 public boolean isMaxExpandHeightInitialized() {
887 return mMaxExpandHeight != 0;
Selim Cinek7d447722014-06-10 15:51:59 +0200888 }
Selim Cinek2f0df8a2014-06-10 17:40:42 +0200889
890 private NotificationContentView getShowingLayout() {
891 return mShowingPublic ? mPublicLayout : mPrivateLayout;
892 }
Chris Wren78403d72014-07-28 10:23:24 +0100893
894 public void setExpansionLogger(ExpansionLogger logger, String key) {
895 mLogger = logger;
896 mLoggingKey = key;
897 }
898
899
900 private void logExpansionEvent(boolean userAction, boolean wasExpanded) {
901 final boolean nowExpanded = isExpanded();
902 if (wasExpanded != nowExpanded && mLogger != null) {
903 mLogger.logNotificationExpansion(mLoggingKey, userAction, nowExpanded) ;
904 }
905 }
Chris Wren51c75102013-07-16 20:49:17 -0400906}