1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
|
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License
*/
package com.android.systemui.statusbar;
import static com.android.keyguard.BouncerPanelExpansionCalculator.aboutToShowBouncerProgress;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.IndentingPrintWriter;
import android.util.MathUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.accessibility.AccessibilityNodeInfo;
import android.view.animation.Interpolator;
import android.view.animation.PathInterpolator;
import androidx.annotation.NonNull;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.policy.SystemBarUtils;
import com.android.systemui.R;
import com.android.systemui.animation.Interpolators;
import com.android.systemui.animation.ShadeInterpolation;
import com.android.systemui.flags.FeatureFlags;
import com.android.systemui.flags.Flags;
import com.android.systemui.plugins.statusbar.StatusBarStateController.StateListener;
import com.android.systemui.shade.transition.LargeScreenShadeInterpolator;
import com.android.systemui.statusbar.notification.LegacySourceType;
import com.android.systemui.statusbar.notification.NotificationUtils;
import com.android.systemui.statusbar.notification.SourceType;
import com.android.systemui.statusbar.notification.row.ActivatableNotificationView;
import com.android.systemui.statusbar.notification.row.ExpandableNotificationRow;
import com.android.systemui.statusbar.notification.row.ExpandableView;
import com.android.systemui.statusbar.notification.stack.AmbientState;
import com.android.systemui.statusbar.notification.stack.AnimationProperties;
import com.android.systemui.statusbar.notification.stack.ExpandableViewState;
import com.android.systemui.statusbar.notification.stack.NotificationStackScrollLayoutController;
import com.android.systemui.statusbar.notification.stack.StackScrollAlgorithm;
import com.android.systemui.statusbar.notification.stack.ViewState;
import com.android.systemui.statusbar.phone.NotificationIconContainer;
import com.android.systemui.util.DumpUtilsKt;
import java.io.PrintWriter;
/**
* A notification shelf view that is placed inside the notification scroller. It manages the
* overflow icons that don't fit into the regular list anymore.
*/
public class NotificationShelf extends ActivatableNotificationView implements
View.OnLayoutChangeListener, StateListener {
private static final int TAG_CONTINUOUS_CLIPPING = R.id.continuous_clipping_tag;
private static final String TAG = "NotificationShelf";
// More extreme version of SLOW_OUT_LINEAR_IN which keeps the icon nearly invisible until after
// the next icon has translated out of the way, to avoid overlapping.
private static final Interpolator ICON_ALPHA_INTERPOLATOR =
new PathInterpolator(0.6f, 0f, 0.6f, 0f);
private static final SourceType BASE_VALUE = SourceType.from("BaseValue");
private static final SourceType SHELF_SCROLL = SourceType.from("ShelfScroll");
private NotificationIconContainer mShelfIcons;
private int[] mTmp = new int[2];
private boolean mHideBackground;
private int mStatusBarHeight;
private boolean mEnableNotificationClipping;
private AmbientState mAmbientState;
private NotificationStackScrollLayoutController mHostLayoutController;
private int mPaddingBetweenElements;
private int mNotGoneIndex;
private boolean mHasItemsInStableShelf;
private NotificationIconContainer mCollapsedIcons;
private int mScrollFastThreshold;
private int mStatusBarState;
private boolean mInteractive;
private boolean mAnimationsEnabled = true;
private boolean mShowNotificationShelf;
private Rect mClipRect = new Rect();
private int mIndexOfFirstViewInShelf = -1;
private float mCornerAnimationDistance;
private NotificationShelfController mController;
private float mActualWidth = -1;
public NotificationShelf(Context context, AttributeSet attrs) {
super(context, attrs);
}
@VisibleForTesting
public NotificationShelf(Context context, AttributeSet attrs, boolean showNotificationShelf) {
super(context, attrs);
mShowNotificationShelf = showNotificationShelf;
}
@Override
@VisibleForTesting
public void onFinishInflate() {
super.onFinishInflate();
mShelfIcons = findViewById(R.id.content);
mShelfIcons.setClipChildren(false);
mShelfIcons.setClipToPadding(false);
setClipToActualHeight(false);
setClipChildren(false);
setClipToPadding(false);
mShelfIcons.setIsStaticLayout(false);
requestRoundness(/* top = */ 1f, /* bottom = */ 1f, BASE_VALUE, /* animate = */ false);
if (!mUseRoundnessSourceTypes) {
// Setting this to first in section to get the clipping to the top roundness correct.
// This value determines the way we are clipping to the top roundness of the overall
// shade
setFirstInSection(true);
}
updateResources();
}
public void bind(AmbientState ambientState,
NotificationStackScrollLayoutController hostLayoutController) {
mAmbientState = ambientState;
mHostLayoutController = hostLayoutController;
hostLayoutController.setOnNotificationRemovedListener((child, isTransferInProgress) -> {
child.requestRoundnessReset(SHELF_SCROLL);
});
}
private void updateResources() {
Resources res = getResources();
mStatusBarHeight = SystemBarUtils.getStatusBarHeight(mContext);
mPaddingBetweenElements = res.getDimensionPixelSize(R.dimen.notification_divider_height);
ViewGroup.LayoutParams layoutParams = getLayoutParams();
final int newShelfHeight = res.getDimensionPixelOffset(R.dimen.notification_shelf_height);
if (newShelfHeight != layoutParams.height) {
layoutParams.height = newShelfHeight;
setLayoutParams(layoutParams);
}
final int padding = res.getDimensionPixelOffset(R.dimen.shelf_icon_container_padding);
mShelfIcons.setPadding(padding, 0, padding, 0);
mScrollFastThreshold = res.getDimensionPixelOffset(R.dimen.scroll_fast_threshold);
mShowNotificationShelf = res.getBoolean(R.bool.config_showNotificationShelf);
mCornerAnimationDistance = res.getDimensionPixelSize(
R.dimen.notification_corner_animation_distance);
mEnableNotificationClipping = res.getBoolean(R.bool.notification_enable_clipping);
mShelfIcons.setInNotificationIconShelf(true);
if (!mShowNotificationShelf) {
setVisibility(GONE);
}
}
@Override
protected void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
updateResources();
}
@Override
protected View getContentView() {
return mShelfIcons;
}
public NotificationIconContainer getShelfIcons() {
return mShelfIcons;
}
@Override
@NonNull
public ExpandableViewState createExpandableViewState() {
return new ShelfState();
}
@Override
public String toString() {
return "NotificationShelf("
+ "hideBackground=" + mHideBackground + " notGoneIndex=" + mNotGoneIndex
+ " hasItemsInStableShelf=" + mHasItemsInStableShelf
+ " statusBarState=" + mStatusBarState + " interactive=" + mInteractive
+ " animationsEnabled=" + mAnimationsEnabled
+ " showNotificationShelf=" + mShowNotificationShelf
+ " indexOfFirstViewInShelf=" + mIndexOfFirstViewInShelf + ')';
}
/**
* Update the state of the shelf.
*/
public void updateState(StackScrollAlgorithm.StackScrollAlgorithmState algorithmState,
AmbientState ambientState) {
ExpandableView lastView = ambientState.getLastVisibleBackgroundChild();
ShelfState viewState = (ShelfState) getViewState();
if (mShowNotificationShelf && lastView != null) {
ExpandableViewState lastViewState = lastView.getViewState();
viewState.copyFrom(lastViewState);
viewState.height = getIntrinsicHeight();
viewState.setZTranslation(ambientState.getBaseZHeight());
viewState.clipTopAmount = 0;
if (ambientState.isExpansionChanging() && !ambientState.isOnKeyguard()) {
float expansion = ambientState.getExpansionFraction();
if (ambientState.isBouncerInTransit()) {
viewState.setAlpha(aboutToShowBouncerProgress(expansion));
} else {
FeatureFlags flags = ambientState.getFeatureFlags();
if (ambientState.isSmallScreen() || !flags.isEnabled(
Flags.LARGE_SHADE_GRANULAR_ALPHA_INTERPOLATION)) {
viewState.setAlpha(ShadeInterpolation.getContentAlpha(expansion));
} else {
LargeScreenShadeInterpolator interpolator =
ambientState.getLargeScreenShadeInterpolator();
viewState.setAlpha(interpolator.getNotificationContentAlpha(expansion));
}
}
} else {
viewState.setAlpha(1f - ambientState.getHideAmount());
}
viewState.belowSpeedBump = mHostLayoutController.getSpeedBumpIndex() == 0;
viewState.hideSensitive = false;
viewState.setXTranslation(getTranslationX());
viewState.hasItemsInStableShelf = lastViewState.inShelf;
viewState.firstViewInShelf = algorithmState.firstViewInShelf;
if (mNotGoneIndex != -1) {
viewState.notGoneIndex = Math.min(viewState.notGoneIndex, mNotGoneIndex);
}
viewState.hidden = !mAmbientState.isShadeExpanded()
|| algorithmState.firstViewInShelf == null;
final int indexOfFirstViewInShelf = algorithmState.visibleChildren.indexOf(
algorithmState.firstViewInShelf);
if (mAmbientState.isExpansionChanging()
&& algorithmState.firstViewInShelf != null
&& indexOfFirstViewInShelf > 0) {
// Show shelf if section before it is showing.
final ExpandableView viewBeforeShelf = algorithmState.visibleChildren.get(
indexOfFirstViewInShelf - 1);
if (viewBeforeShelf.getViewState().hidden) {
viewState.hidden = true;
}
}
final float stackEnd = ambientState.getStackY() + ambientState.getStackHeight();
viewState.setYTranslation(stackEnd - viewState.height);
} else {
viewState.hidden = true;
viewState.location = ExpandableViewState.LOCATION_GONE;
viewState.hasItemsInStableShelf = false;
}
}
/**
* @param fractionToShade Fraction of lockscreen to shade transition
* @param shortestWidth Shortest width to use for lockscreen shelf
*/
@VisibleForTesting
public void updateActualWidth(float fractionToShade, float shortestWidth) {
final float actualWidth = mAmbientState.isOnKeyguard()
? MathUtils.lerp(shortestWidth, getWidth(), fractionToShade)
: getWidth();
setBackgroundWidth((int) actualWidth);
if (mShelfIcons != null) {
mShelfIcons.setActualLayoutWidth((int) actualWidth);
}
mActualWidth = actualWidth;
}
@Override
public void getBoundsOnScreen(Rect outRect, boolean clipToParent) {
super.getBoundsOnScreen(outRect, clipToParent);
final int actualWidth = getActualWidth();
if (isLayoutRtl()) {
outRect.left = outRect.right - actualWidth;
} else {
outRect.right = outRect.left + actualWidth;
}
}
/**
* @return Actual width of shelf, accounting for possible ongoing width animation
*/
public int getActualWidth() {
return mActualWidth > -1 ? (int) mActualWidth : getWidth();
}
/**
* @param localX Click x from left of screen
* @param slop Margin of error within which we count x for valid click
* @param left Left of shelf, from left of screen
* @param right Right of shelf, from left of screen
* @return Whether click x was in view
*/
@VisibleForTesting
public boolean isXInView(float localX, float slop, float left, float right) {
return (left - slop) <= localX && localX < (right + slop);
}
/**
* @param localY Click y from top of shelf
* @param slop Margin of error within which we count y for valid click
* @param top Top of shelf
* @param bottom Height of shelf
* @return Whether click y was in view
*/
@VisibleForTesting
public boolean isYInView(float localY, float slop, float top, float bottom) {
return (top - slop) <= localY && localY < (bottom + slop);
}
/**
* @param localX Click x
* @param localY Click y
* @param slop Margin of error for valid click
* @return Whether this click was on the visible (non-clipped) part of the shelf
*/
@Override
public boolean pointInView(float localX, float localY, float slop) {
final float containerWidth = getWidth();
final float shelfWidth = getActualWidth();
final float left = isLayoutRtl() ? containerWidth - shelfWidth : 0;
final float right = isLayoutRtl() ? containerWidth : shelfWidth;
final float top = mClipTopAmount;
final float bottom = getActualHeight();
return isXInView(localX, slop, left, right)
&& isYInView(localY, slop, top, bottom);
}
/**
* Update the shelf appearance based on the other notifications around it. This transforms
* the icons from the notification area into the shelf.
*/
public void updateAppearance() {
// If the shelf should not be shown, then there is no need to update anything.
if (!mShowNotificationShelf) {
return;
}
mShelfIcons.resetViewStates();
float shelfStart = getTranslationY();
float numViewsInShelf = 0.0f;
View lastChild = mAmbientState.getLastVisibleBackgroundChild();
mNotGoneIndex = -1;
// find the first view that doesn't overlap with the shelf
int notGoneIndex = 0;
int colorOfViewBeforeLast = NO_COLOR;
boolean backgroundForceHidden = false;
if (mHideBackground && !((ShelfState) getViewState()).hasItemsInStableShelf) {
backgroundForceHidden = true;
}
int colorTwoBefore = NO_COLOR;
int previousColor = NO_COLOR;
float transitionAmount = 0.0f;
float currentScrollVelocity = mAmbientState.getCurrentScrollVelocity();
boolean scrollingFast = currentScrollVelocity > mScrollFastThreshold
|| (mAmbientState.isExpansionChanging()
&& Math.abs(mAmbientState.getExpandingVelocity()) > mScrollFastThreshold);
boolean expandingAnimated = mAmbientState.isExpansionChanging()
&& !mAmbientState.isPanelTracking();
int baseZHeight = mAmbientState.getBaseZHeight();
int clipTopAmount = 0;
for (int i = 0; i < mHostLayoutController.getChildCount(); i++) {
ExpandableView child = mHostLayoutController.getChildAt(i);
if (!child.needsClippingToShelf() || child.getVisibility() == GONE) {
continue;
}
float notificationClipEnd;
boolean aboveShelf = ViewState.getFinalTranslationZ(child) > baseZHeight
|| child.isPinned();
boolean isLastChild = child == lastChild;
final float viewStart = child.getTranslationY();
final float shelfClipStart = getTranslationY() - mPaddingBetweenElements;
final float inShelfAmount = getAmountInShelf(i, child, scrollingFast,
expandingAnimated, isLastChild, shelfClipStart);
// TODO(b/172289889) scale mPaddingBetweenElements with expansion amount
if ((isLastChild && !child.isInShelf()) || aboveShelf || backgroundForceHidden) {
notificationClipEnd = shelfStart + getIntrinsicHeight();
} else {
notificationClipEnd = shelfStart - mPaddingBetweenElements;
}
int clipTop = updateNotificationClipHeight(child, notificationClipEnd, notGoneIndex);
clipTopAmount = Math.max(clipTop, clipTopAmount);
// If the current row is an ExpandableNotificationRow, update its color, roundedness,
// and icon state.
if (child instanceof ExpandableNotificationRow) {
ExpandableNotificationRow expandableRow = (ExpandableNotificationRow) child;
numViewsInShelf += inShelfAmount;
int ownColorUntinted = expandableRow.getBackgroundColorWithoutTint();
if (viewStart >= shelfStart && mNotGoneIndex == -1) {
mNotGoneIndex = notGoneIndex;
setTintColor(previousColor);
setOverrideTintColor(colorTwoBefore, transitionAmount);
} else if (mNotGoneIndex == -1) {
colorTwoBefore = previousColor;
transitionAmount = inShelfAmount;
}
// We don't want to modify the color if the notification is hun'd
if (isLastChild && mController.canModifyColorOfNotifications()) {
if (colorOfViewBeforeLast == NO_COLOR) {
colorOfViewBeforeLast = ownColorUntinted;
}
expandableRow.setOverrideTintColor(colorOfViewBeforeLast, inShelfAmount);
} else {
colorOfViewBeforeLast = ownColorUntinted;
expandableRow.setOverrideTintColor(NO_COLOR, 0 /* overrideAmount */);
}
if (notGoneIndex != 0 || !aboveShelf) {
expandableRow.setAboveShelf(false);
}
previousColor = ownColorUntinted;
notGoneIndex++;
}
if (child instanceof ActivatableNotificationView) {
ActivatableNotificationView anv =
(ActivatableNotificationView) child;
// Because we show whole notifications on the lockscreen, the bottom notification is
// always "just about to enter the shelf" by normal scrolling rules. This is fine
// if the shelf is visible, but if the shelf is hidden, it causes incorrect curling.
// notificationClipEnd handles the discrepancy between a visible and hidden shelf,
// so we use that when on the keyguard (and while animating away) to reduce curling.
final float keyguardSafeShelfStart =
mAmbientState.isOnKeyguard() ? notificationClipEnd : shelfStart;
updateCornerRoundnessOnScroll(anv, viewStart, keyguardSafeShelfStart);
}
}
clipTransientViews();
setClipTopAmount(clipTopAmount);
boolean isHidden = getViewState().hidden
|| clipTopAmount >= getIntrinsicHeight()
|| !mShowNotificationShelf
|| numViewsInShelf < 1f;
final float fractionToShade = Interpolators.STANDARD.getInterpolation(
mAmbientState.getFractionToShade());
final float shortestWidth = mShelfIcons.calculateWidthFor(numViewsInShelf);
updateActualWidth(fractionToShade, shortestWidth);
// TODO(b/172289889) transition last icon in shelf to notification icon and vice versa.
setVisibility(isHidden ? View.INVISIBLE : View.VISIBLE);
mShelfIcons.setSpeedBumpIndex(mHostLayoutController.getSpeedBumpIndex());
mShelfIcons.calculateIconXTranslations();
mShelfIcons.applyIconStates();
for (int i = 0; i < mHostLayoutController.getChildCount(); i++) {
View child = mHostLayoutController.getChildAt(i);
if (!(child instanceof ExpandableNotificationRow)
|| child.getVisibility() == GONE) {
continue;
}
ExpandableNotificationRow row = (ExpandableNotificationRow) child;
updateContinuousClipping(row);
}
boolean hideBackground = isHidden;
setHideBackground(hideBackground);
if (mNotGoneIndex == -1) {
mNotGoneIndex = notGoneIndex;
}
}
private void updateCornerRoundnessOnScroll(
ActivatableNotificationView anv,
float viewStart,
float shelfStart) {
final boolean isUnlockedHeadsUp = !mAmbientState.isOnKeyguard()
&& !mAmbientState.isShadeExpanded()
&& anv instanceof ExpandableNotificationRow
&& anv.isHeadsUp();
final boolean isHunGoingToShade = mAmbientState.isShadeExpanded()
&& anv == mAmbientState.getTrackedHeadsUpRow();
final boolean shouldUpdateCornerRoundness = viewStart < shelfStart
&& !mHostLayoutController.isViewAffectedBySwipe(anv)
&& !isUnlockedHeadsUp
&& !isHunGoingToShade
&& !anv.isAboveShelf()
&& !mAmbientState.isPulsing()
&& !mAmbientState.isDozing();
if (!shouldUpdateCornerRoundness) {
return;
}
final float viewEnd = viewStart + anv.getActualHeight();
final float cornerAnimationDistance = mCornerAnimationDistance
* mAmbientState.getExpansionFraction();
final float cornerAnimationTop = shelfStart - cornerAnimationDistance;
final SourceType sourceType;
if (mUseRoundnessSourceTypes) {
sourceType = SHELF_SCROLL;
} else {
sourceType = LegacySourceType.OnScroll;
}
final float topValue;
if (!mUseRoundnessSourceTypes && anv.isFirstInSection()) {
topValue = 1f;
} else if (viewStart >= cornerAnimationTop) {
// Round top corners within animation bounds
topValue = MathUtils.saturate(
(viewStart - cornerAnimationTop) / cornerAnimationDistance);
} else {
// Fast scroll skips frames and leaves corners with unfinished rounding.
// Reset top and bottom corners outside of animation bounds.
topValue = 0f;
}
anv.requestTopRoundness(topValue, sourceType, /* animate = */ false);
final float bottomValue;
if (!mUseRoundnessSourceTypes && anv.isLastInSection()) {
bottomValue = 1f;
} else if (viewEnd >= cornerAnimationTop) {
// Round bottom corners within animation bounds
bottomValue = MathUtils.saturate(
(viewEnd - cornerAnimationTop) / cornerAnimationDistance);
} else {
// Fast scroll skips frames and leaves corners with unfinished rounding.
// Reset top and bottom corners outside of animation bounds.
bottomValue = 0f;
}
anv.requestBottomRoundness(bottomValue, sourceType, /* animate = */ false);
}
/**
* Clips transient views to the top of the shelf - Transient views are only used for
* disappearing views/animations and need to be clipped correctly by the shelf to ensure they
* don't show underneath the notification stack when something is animating and the user
* swipes quickly.
*/
private void clipTransientViews() {
for (int i = 0; i < mHostLayoutController.getTransientViewCount(); i++) {
View transientView = mHostLayoutController.getTransientView(i);
if (transientView instanceof ExpandableView) {
ExpandableView transientExpandableView = (ExpandableView) transientView;
updateNotificationClipHeight(transientExpandableView, getTranslationY(), -1);
}
}
}
private void updateIconClipAmount(ExpandableNotificationRow row) {
float maxTop = row.getTranslationY();
if (getClipTopAmount() != 0) {
// if the shelf is clipped, lets make sure we also clip the icon
maxTop = Math.max(maxTop, getTranslationY() + getClipTopAmount());
}
StatusBarIconView icon = row.getEntry().getIcons().getShelfIcon();
float shelfIconPosition = getTranslationY() + icon.getTop() + icon.getTranslationY();
if (shelfIconPosition < maxTop && !mAmbientState.isFullyHidden()) {
int top = (int) (maxTop - shelfIconPosition);
Rect clipRect = new Rect(0, top, icon.getWidth(), Math.max(top, icon.getHeight()));
icon.setClipBounds(clipRect);
} else {
icon.setClipBounds(null);
}
}
private void updateContinuousClipping(final ExpandableNotificationRow row) {
StatusBarIconView icon = row.getEntry().getIcons().getShelfIcon();
boolean needsContinuousClipping = ViewState.isAnimatingY(icon) && !mAmbientState.isDozing();
boolean isContinuousClipping = icon.getTag(TAG_CONTINUOUS_CLIPPING) != null;
if (needsContinuousClipping && !isContinuousClipping) {
final ViewTreeObserver observer = icon.getViewTreeObserver();
ViewTreeObserver.OnPreDrawListener predrawListener =
new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
boolean animatingY = ViewState.isAnimatingY(icon);
if (!animatingY) {
if (observer.isAlive()) {
observer.removeOnPreDrawListener(this);
}
icon.setTag(TAG_CONTINUOUS_CLIPPING, null);
return true;
}
updateIconClipAmount(row);
return true;
}
};
observer.addOnPreDrawListener(predrawListener);
icon.addOnAttachStateChangeListener(new OnAttachStateChangeListener() {
@Override
public void onViewAttachedToWindow(View v) {
}
@Override
public void onViewDetachedFromWindow(View v) {
if (v == icon) {
if (observer.isAlive()) {
observer.removeOnPreDrawListener(predrawListener);
}
icon.setTag(TAG_CONTINUOUS_CLIPPING, null);
}
}
});
icon.setTag(TAG_CONTINUOUS_CLIPPING, predrawListener);
}
}
/**
* Update the clipping of this view.
*
* @return the amount that our own top should be clipped
*/
private int updateNotificationClipHeight(ExpandableView view,
float notificationClipEnd, int childIndex) {
float viewEnd = view.getTranslationY() + view.getActualHeight();
boolean isPinned = (view.isPinned() || view.isHeadsUpAnimatingAway())
&& !mAmbientState.isDozingAndNotPulsing(view);
boolean shouldClipOwnTop;
if (mAmbientState.isPulseExpanding()) {
shouldClipOwnTop = childIndex == 0;
} else {
shouldClipOwnTop = view.showingPulsing();
}
if (!isPinned) {
if (viewEnd > notificationClipEnd && !shouldClipOwnTop) {
int clipBottomAmount =
mEnableNotificationClipping ? (int) (viewEnd - notificationClipEnd) : 0;
view.setClipBottomAmount(clipBottomAmount);
} else {
view.setClipBottomAmount(0);
}
}
if (shouldClipOwnTop) {
return (int) (viewEnd - getTranslationY());
} else {
return 0;
}
}
@Override
public void setFakeShadowIntensity(float shadowIntensity, float outlineAlpha, int shadowYEnd,
int outlineTranslation) {
if (!mHasItemsInStableShelf) {
shadowIntensity = 0.0f;
}
super.setFakeShadowIntensity(shadowIntensity, outlineAlpha, shadowYEnd, outlineTranslation);
}
/**
* @param i Index of the view in the host layout.
* @param view The current ExpandableView.
* @param scrollingFast Whether we are scrolling fast.
* @param expandingAnimated Whether we are expanding a notification.
* @param isLastChild Whether this is the last view.
* @param shelfClipStart The point at which notifications start getting clipped by the shelf.
* @return The amount how much this notification is in the shelf.
* 0f is not in shelf. 1f is completely in shelf.
*/
@VisibleForTesting
public float getAmountInShelf(
int i,
ExpandableView view,
boolean scrollingFast,
boolean expandingAnimated,
boolean isLastChild,
float shelfClipStart
) {
// Let's calculate how much the view is in the shelf
float viewStart = view.getTranslationY();
int fullHeight = view.getActualHeight() + mPaddingBetweenElements;
float iconTransformStart = calculateIconTransformationStart(view);
// Let's make sure the transform distance is
// at most to the icon (relevant for conversations)
float transformDistance = Math.min(
viewStart + fullHeight - iconTransformStart,
getIntrinsicHeight());
if (isLastChild) {
fullHeight = Math.min(fullHeight, view.getMinHeight() - getIntrinsicHeight());
transformDistance = Math.min(
transformDistance,
view.getMinHeight() - getIntrinsicHeight());
}
float viewEnd = viewStart + fullHeight;
float fullTransitionAmount = 0.0f;
float iconTransitionAmount = 0.0f;
// Don't animate shelf icons during shade expansion.
if (mAmbientState.isExpansionChanging() && !mAmbientState.isOnKeyguard()) {
// TODO(b/172289889) handle icon placement for notification that is clipped by the shelf
if (mIndexOfFirstViewInShelf != -1 && i >= mIndexOfFirstViewInShelf) {
fullTransitionAmount = 1f;
iconTransitionAmount = 1f;
}
} else if (viewEnd >= shelfClipStart
&& (!mAmbientState.isUnlockHintRunning() || view.isInShelf())
&& (mAmbientState.isShadeExpanded()
|| (!view.isPinned() && !view.isHeadsUpAnimatingAway()))) {
if (viewStart < shelfClipStart && Math.abs(viewStart - shelfClipStart) > 0.001f) {
// Partially clipped by shelf.
float fullAmount = (shelfClipStart - viewStart) / fullHeight;
fullAmount = Math.min(1.0f, fullAmount);
fullTransitionAmount = 1.0f - fullAmount;
if (isLastChild) {
// Reduce icon transform distance to completely fade in shelf icon
// by the time the notification icon fades out, and vice versa
iconTransitionAmount = (shelfClipStart - viewStart)
/ (iconTransformStart - viewStart);
} else {
iconTransitionAmount = (shelfClipStart - iconTransformStart)
/ transformDistance;
}
iconTransitionAmount = MathUtils.constrain(iconTransitionAmount, 0.0f, 1.0f);
iconTransitionAmount = 1.0f - iconTransitionAmount;
} else {
// Fully in shelf.
fullTransitionAmount = 1.0f;
iconTransitionAmount = 1.0f;
}
}
updateIconPositioning(view, iconTransitionAmount,
scrollingFast, expandingAnimated, isLastChild);
return fullTransitionAmount;
}
/**
* @return the location where the transformation into the shelf should start.
*/
private float calculateIconTransformationStart(ExpandableView view) {
View target = view.getShelfTransformationTarget();
if (target == null) {
return view.getTranslationY();
}
float start = view.getTranslationY() + view.getRelativeTopPadding(target);
// Let's not start the transformation right at the icon but by the padding before it.
start -= view.getShelfIcon().getTop();
return start;
}
private void updateIconPositioning(
ExpandableView view,
float iconTransitionAmount,
boolean scrollingFast,
boolean expandingAnimated,
boolean isLastChild
) {
StatusBarIconView icon = view.getShelfIcon();
NotificationIconContainer.IconState iconState = getIconState(icon);
if (iconState == null) {
return;
}
boolean clampInShelf = iconTransitionAmount > 0.5f || isTargetClipped(view);
float clampedAmount = clampInShelf ? 1.0f : 0.0f;
if (iconTransitionAmount == clampedAmount) {
iconState.noAnimations = (scrollingFast || expandingAnimated) && !isLastChild;
}
if (!isLastChild
&& (scrollingFast || (expandingAnimated && !ViewState.isAnimatingY(icon)))) {
iconState.cancelAnimations(icon);
iconState.noAnimations = true;
}
float transitionAmount;
if (mAmbientState.isHiddenAtAll() && !view.isInShelf()) {
transitionAmount = mAmbientState.isFullyHidden() ? 1 : 0;
} else {
transitionAmount = iconTransitionAmount;
iconState.needsCannedAnimation = iconState.clampedAppearAmount != clampedAmount;
}
iconState.clampedAppearAmount = clampedAmount;
setIconTransformationAmount(view, transitionAmount);
}
private boolean isTargetClipped(ExpandableView view) {
View target = view.getShelfTransformationTarget();
if (target == null) {
return false;
}
// We should never clip the target, let's instead put it into the shelf!
float endOfTarget = view.getTranslationY()
+ view.getContentTranslation()
+ view.getRelativeTopPadding(target)
+ target.getHeight();
return endOfTarget >= getTranslationY() - mPaddingBetweenElements;
}
private void setIconTransformationAmount(ExpandableView view, float transitionAmount) {
if (!(view instanceof ExpandableNotificationRow)) {
return;
}
ExpandableNotificationRow row = (ExpandableNotificationRow) view;
StatusBarIconView icon = row.getShelfIcon();
NotificationIconContainer.IconState iconState = getIconState(icon);
if (iconState == null) {
return;
}
iconState.setAlpha(ICON_ALPHA_INTERPOLATOR.getInterpolation(transitionAmount));
boolean isAppearing = row.isDrawingAppearAnimation() && !row.isInShelf();
iconState.hidden = isAppearing
|| (view instanceof ExpandableNotificationRow
&& ((ExpandableNotificationRow) view).isLowPriority()
&& mShelfIcons.areIconsOverflowing())
|| (transitionAmount == 0.0f && !iconState.isAnimating(icon))
|| row.isAboveShelf()
|| row.showingPulsing()
|| row.getTranslationZ() > mAmbientState.getBaseZHeight();
iconState.iconAppearAmount = iconState.hidden ? 0f : transitionAmount;
// Fade in icons at shelf start
// This is important for conversation icons, which are badged and need x reset
iconState.setXTranslation(mShelfIcons.getActualPaddingStart());
final boolean stayingInShelf = row.isInShelf() && !row.isTransformingIntoShelf();
if (stayingInShelf) {
iconState.iconAppearAmount = 1.0f;
iconState.setAlpha(1.0f);
iconState.hidden = false;
}
int backgroundColor = getBackgroundColorWithoutTint();
int shelfColor = icon.getContrastedStaticDrawableColor(backgroundColor);
if (row.isShowingIcon() && shelfColor != StatusBarIconView.NO_COLOR) {
int iconColor = row.getOriginalIconColor();
shelfColor = NotificationUtils.interpolateColors(iconColor, shelfColor,
iconState.iconAppearAmount);
}
iconState.iconColor = shelfColor;
}
private NotificationIconContainer.IconState getIconState(StatusBarIconView icon) {
if (mShelfIcons == null) {
return null;
}
return mShelfIcons.getIconState(icon);
}
private float getFullyClosedTranslation() {
return -(getIntrinsicHeight() - mStatusBarHeight) / 2;
}
@Override
public boolean hasNoContentHeight() {
return true;
}
private void setHideBackground(boolean hideBackground) {
if (mHideBackground != hideBackground) {
mHideBackground = hideBackground;
updateOutline();
}
}
@Override
protected boolean needsOutline() {
return !mHideBackground && super.needsOutline();
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
updateRelativeOffset();
// we always want to clip to our sides, such that nothing can draw outside of these bounds
int height = getResources().getDisplayMetrics().heightPixels;
mClipRect.set(0, -height, getWidth(), height);
if (mShelfIcons != null) {
mShelfIcons.setClipBounds(mClipRect);
}
}
private void updateRelativeOffset() {
if (mCollapsedIcons != null) {
mCollapsedIcons.getLocationOnScreen(mTmp);
}
getLocationOnScreen(mTmp);
}
/**
* @return the index of the notification at which the shelf visually resides
*/
public int getNotGoneIndex() {
return mNotGoneIndex;
}
private void setHasItemsInStableShelf(boolean hasItemsInStableShelf) {
if (mHasItemsInStableShelf != hasItemsInStableShelf) {
mHasItemsInStableShelf = hasItemsInStableShelf;
updateInteractiveness();
}
}
/**
* @return whether the shelf has any icons in it when a potential animation has finished, i.e
* if the current state would be applied right now
*/
public boolean hasItemsInStableShelf() {
return mHasItemsInStableShelf;
}
public void setCollapsedIcons(NotificationIconContainer collapsedIcons) {
mCollapsedIcons = collapsedIcons;
mCollapsedIcons.addOnLayoutChangeListener(this);
}
@Override
public void onStateChanged(int newState) {
mStatusBarState = newState;
updateInteractiveness();
}
private void updateInteractiveness() {
mInteractive = mStatusBarState == StatusBarState.KEYGUARD && mHasItemsInStableShelf;
setClickable(mInteractive);
setFocusable(mInteractive);
setImportantForAccessibility(mInteractive ? View.IMPORTANT_FOR_ACCESSIBILITY_YES
: View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
}
@Override
protected boolean isInteractive() {
return mInteractive;
}
public void setAnimationsEnabled(boolean enabled) {
mAnimationsEnabled = enabled;
if (!enabled) {
// we need to wait with enabling the animations until the first frame has passed
mShelfIcons.setAnimationsEnabled(false);
}
}
@Override
public boolean hasOverlappingRendering() {
return false; // Shelf only uses alpha for transitions where the difference can't be seen.
}
@Override
public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
super.onInitializeAccessibilityNodeInfo(info);
if (mInteractive) {
info.addAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_EXPAND);
AccessibilityNodeInfo.AccessibilityAction unlock
= new AccessibilityNodeInfo.AccessibilityAction(
AccessibilityNodeInfo.ACTION_CLICK,
getContext().getString(R.string.accessibility_overflow_action));
info.addAction(unlock);
}
}
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft,
int oldTop, int oldRight, int oldBottom) {
updateRelativeOffset();
}
@Override
public boolean needsClippingToShelf() {
return false;
}
public void setController(NotificationShelfController notificationShelfController) {
mController = notificationShelfController;
}
public void setIndexOfFirstViewInShelf(ExpandableView firstViewInShelf) {
mIndexOfFirstViewInShelf = mHostLayoutController.indexOfChild(firstViewInShelf);
}
/**
* This method resets the OnScroll roundness of a view to 0f
* <p>
* Note: This should be the only class that handles roundness {@code SourceType.OnScroll}
*/
public static void resetLegacyOnScrollRoundness(ExpandableView expandableView) {
expandableView.requestRoundnessReset(LegacySourceType.OnScroll);
}
@Override
public void dump(PrintWriter pwOriginal, String[] args) {
IndentingPrintWriter pw = DumpUtilsKt.asIndenting(pwOriginal);
super.dump(pw, args);
if (DUMP_VERBOSE) {
DumpUtilsKt.withIncreasedIndent(pw, () -> {
pw.println("mActualWidth: " + mActualWidth);
pw.println("mStatusBarHeight: " + mStatusBarHeight);
});
}
}
public class ShelfState extends ExpandableViewState {
private boolean hasItemsInStableShelf;
private ExpandableView firstViewInShelf;
@Override
public void applyToView(View view) {
if (!mShowNotificationShelf) {
return;
}
super.applyToView(view);
setIndexOfFirstViewInShelf(firstViewInShelf);
updateAppearance();
setHasItemsInStableShelf(hasItemsInStableShelf);
mShelfIcons.setAnimationsEnabled(mAnimationsEnabled);
}
@Override
public void animateTo(View view, AnimationProperties properties) {
if (!mShowNotificationShelf) {
return;
}
super.animateTo(view, properties);
setIndexOfFirstViewInShelf(firstViewInShelf);
updateAppearance();
setHasItemsInStableShelf(hasItemsInStableShelf);
mShelfIcons.setAnimationsEnabled(mAnimationsEnabled);
}
}
}
|