/
BaseTransientBottomBar.java
1456 lines (1285 loc) · 50.2 KB
/
BaseTransientBottomBar.java
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
/*
* Copyright (C) 2015 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.google.android.material.snackbar;
import com.google.android.material.R;
import static androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP;
import static com.google.android.material.animation.AnimationUtils.FAST_OUT_SLOW_IN_INTERPOLATOR;
import static com.google.android.material.animation.AnimationUtils.LINEAR_INTERPOLATOR;
import static com.google.android.material.animation.AnimationUtils.LINEAR_OUT_SLOW_IN_INTERPOLATOR;
import static com.google.android.material.theme.overlay.MaterialThemeOverlay.wrap;
import android.accessibilityservice.AccessibilityServiceInfo;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.PorterDuff;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.os.Build;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import androidx.core.graphics.drawable.DrawableCompat;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewGroup.MarginLayoutParams;
import android.view.ViewParent;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.WindowInsets;
import android.view.WindowManager;
import android.view.accessibility.AccessibilityManager;
import android.widget.FrameLayout;
import androidx.annotation.IdRes;
import androidx.annotation.IntDef;
import androidx.annotation.IntRange;
import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.annotation.RestrictTo;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.core.view.AccessibilityDelegateCompat;
import androidx.core.view.OnApplyWindowInsetsListener;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.core.view.accessibility.AccessibilityNodeInfoCompat;
import com.google.android.material.behavior.SwipeDismissBehavior;
import com.google.android.material.color.MaterialColors;
import com.google.android.material.internal.ThemeEnforcement;
import com.google.android.material.internal.ViewUtils;
import com.google.android.material.resources.MaterialResources;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;
/**
* Base class for lightweight transient bars that are displayed along the bottom edge of the
* application window.
*
* @param <B> The transient bottom bar subclass.
*/
public abstract class BaseTransientBottomBar<B extends BaseTransientBottomBar<B>> {
/** Animation mode that corresponds to the slide in and out animations. */
public static final int ANIMATION_MODE_SLIDE = 0;
/** Animation mode that corresponds to the fade in and out animations. */
public static final int ANIMATION_MODE_FADE = 1;
/**
* Animation modes that can be set on the {@link BaseTransientBottomBar}.
*
* @hide
*/
@RestrictTo(LIBRARY_GROUP)
@IntDef({ANIMATION_MODE_SLIDE, ANIMATION_MODE_FADE})
@Retention(RetentionPolicy.SOURCE)
public @interface AnimationMode {}
/**
* Base class for {@link BaseTransientBottomBar} callbacks.
*
* @param <B> The transient bottom bar subclass.
* @see BaseTransientBottomBar#addCallback(BaseCallback)
*/
public abstract static class BaseCallback<B> {
/** Indicates that the Snackbar was dismissed via a swipe. */
public static final int DISMISS_EVENT_SWIPE = 0;
/** Indicates that the Snackbar was dismissed via an action click. */
public static final int DISMISS_EVENT_ACTION = 1;
/** Indicates that the Snackbar was dismissed via a timeout. */
public static final int DISMISS_EVENT_TIMEOUT = 2;
/** Indicates that the Snackbar was dismissed via a call to {@link #dismiss()}. */
public static final int DISMISS_EVENT_MANUAL = 3;
/** Indicates that the Snackbar was dismissed from a new Snackbar being shown. */
public static final int DISMISS_EVENT_CONSECUTIVE = 4;
/**
* Annotation for types of Dismiss events.
*
* @hide
*/
@RestrictTo(LIBRARY_GROUP)
@IntDef({
DISMISS_EVENT_SWIPE,
DISMISS_EVENT_ACTION,
DISMISS_EVENT_TIMEOUT,
DISMISS_EVENT_MANUAL,
DISMISS_EVENT_CONSECUTIVE
})
@Retention(RetentionPolicy.SOURCE)
public @interface DismissEvent {}
/**
* Called when the given {@link BaseTransientBottomBar} has been dismissed, either through a
* time-out, having been manually dismissed, or an action being clicked.
*
* @param transientBottomBar The transient bottom bar which has been dismissed.
* @param event The event which caused the dismissal. One of either: {@link
* #DISMISS_EVENT_SWIPE}, {@link #DISMISS_EVENT_ACTION}, {@link #DISMISS_EVENT_TIMEOUT},
* {@link #DISMISS_EVENT_MANUAL} or {@link #DISMISS_EVENT_CONSECUTIVE}.
* @see BaseTransientBottomBar#dismiss()
*/
public void onDismissed(B transientBottomBar, @DismissEvent int event) {
// empty
}
/**
* Called when the given {@link BaseTransientBottomBar} is visible.
*
* @param transientBottomBar The transient bottom bar which is now visible.
* @see BaseTransientBottomBar#show()
*/
public void onShown(B transientBottomBar) {
// empty
}
}
/**
* Interface that defines the behavior of the main content of a transient bottom bar.
*
* @deprecated Use {@link com.google.android.material.snackbar.ContentViewCallback} instead.
*/
@Deprecated
public interface ContentViewCallback
extends com.google.android.material.snackbar.ContentViewCallback {}
/** @hide */
@RestrictTo(LIBRARY_GROUP)
@IntDef({LENGTH_INDEFINITE, LENGTH_SHORT, LENGTH_LONG})
@IntRange(from = 1)
@Retention(RetentionPolicy.SOURCE)
public @interface Duration {}
/**
* Show the Snackbar indefinitely. This means that the Snackbar will be displayed from the time
* that is {@link #show() shown} until either it is dismissed, or another Snackbar is shown.
*
* @see #setDuration
*/
public static final int LENGTH_INDEFINITE = -2;
/**
* Show the Snackbar for a short period of time.
*
* @see #setDuration
*/
public static final int LENGTH_SHORT = -1;
/**
* Show the Snackbar for a long period of time.
*
* @see #setDuration
*/
public static final int LENGTH_LONG = 0;
// Legacy slide animation duration constant.
static final int ANIMATION_DURATION = 250;
// Legacy slide animation content fade duration constant.
static final int ANIMATION_FADE_DURATION = 180;
// Fade and scale animation constants.
private static final int ANIMATION_FADE_IN_DURATION = 150;
private static final int ANIMATION_FADE_OUT_DURATION = 75;
private static final float ANIMATION_SCALE_FROM_VALUE = 0.8f;
@NonNull static final Handler handler;
static final int MSG_SHOW = 0;
static final int MSG_DISMISS = 1;
// On JB/KK versions of the platform sometimes View.setTranslationY does not result in
// layout / draw pass, and CoordinatorLayout relies on a draw pass to happen to sync vertical
// positioning of all its child views
private static final boolean USE_OFFSET_API =
(Build.VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN)
&& (Build.VERSION.SDK_INT <= VERSION_CODES.KITKAT);
private static final int[] SNACKBAR_STYLE_ATTR = new int[] {R.attr.snackbarStyle};
private static final String TAG = BaseTransientBottomBar.class.getSimpleName();
static {
handler =
new Handler(
Looper.getMainLooper(),
new Handler.Callback() {
@Override
public boolean handleMessage(@NonNull Message message) {
switch (message.what) {
case MSG_SHOW:
((BaseTransientBottomBar) message.obj).showView();
return true;
case MSG_DISMISS:
((BaseTransientBottomBar) message.obj).hideView(message.arg1);
return true;
default:
return false;
}
}
});
}
@NonNull private final ViewGroup targetParent;
private final Context context;
@NonNull protected final SnackbarBaseLayout view;
@NonNull
private final com.google.android.material.snackbar.ContentViewCallback contentViewCallback;
private int duration;
private boolean gestureInsetBottomIgnored;
@Nullable
private Anchor anchor;
private boolean anchorViewLayoutListenerEnabled = false;
@RequiresApi(VERSION_CODES.Q)
private final Runnable bottomMarginGestureInsetRunnable =
new Runnable() {
@Override
public void run() {
if (view == null || context == null) {
return;
}
// Calculate current bottom inset, factoring in translationY to account for where the
// view will likely be animating to.
int currentInsetBottom =
getScreenHeight() - getViewAbsoluteBottom() + (int) view.getTranslationY();
if (currentInsetBottom >= extraBottomMarginGestureInset) {
// No need to add extra offset if view is already outside of bottom gesture area
return;
}
LayoutParams layoutParams = view.getLayoutParams();
if (!(layoutParams instanceof MarginLayoutParams)) {
Log.w(
TAG,
"Unable to apply gesture inset because layout params are not MarginLayoutParams");
return;
}
// Move view outside of bottom gesture area
MarginLayoutParams marginParams = (MarginLayoutParams) layoutParams;
marginParams.bottomMargin += extraBottomMarginGestureInset - currentInsetBottom;
view.requestLayout();
}
};
@Nullable private Rect originalMargins;
private int extraBottomMarginWindowInset;
private int extraLeftMarginWindowInset;
private int extraRightMarginWindowInset;
private int extraBottomMarginGestureInset;
private int extraBottomMarginAnchorView;
private List<BaseCallback<B>> callbacks;
private BaseTransientBottomBar.Behavior behavior;
@Nullable private final AccessibilityManager accessibilityManager;
/** @hide */
// TODO(b/76413401): make package private after the widget migration is finished
@RestrictTo(LIBRARY_GROUP)
protected interface OnLayoutChangeListener {
void onLayoutChange(View view, int left, int top, int right, int bottom);
}
/** @hide */
// TODO(b/76413401): make package private after the widget migration is finished
@RestrictTo(LIBRARY_GROUP)
protected interface OnAttachStateChangeListener {
void onViewAttachedToWindow(View v);
void onViewDetachedFromWindow(View v);
}
/**
* Constructor for the transient bottom bar.
*
* <p>Uses {@link Context} from {@code parent}.
*
* @param parent The parent for this transient bottom bar.
* @param content The content view for this transient bottom bar.
* @param contentViewCallback The content view callback for this transient bottom bar.
*/
protected BaseTransientBottomBar(
@NonNull ViewGroup parent,
@NonNull View content,
@NonNull com.google.android.material.snackbar.ContentViewCallback contentViewCallback) {
this(parent.getContext(), parent, content, contentViewCallback);
}
protected BaseTransientBottomBar(
@NonNull Context context,
@NonNull ViewGroup parent,
@NonNull View content,
@NonNull com.google.android.material.snackbar.ContentViewCallback contentViewCallback) {
if (parent == null) {
throw new IllegalArgumentException("Transient bottom bar must have non-null parent");
}
if (content == null) {
throw new IllegalArgumentException("Transient bottom bar must have non-null content");
}
if (contentViewCallback == null) {
throw new IllegalArgumentException("Transient bottom bar must have non-null callback");
}
targetParent = parent;
this.contentViewCallback = contentViewCallback;
this.context = context;
ThemeEnforcement.checkAppCompatTheme(context);
LayoutInflater inflater = LayoutInflater.from(context);
// Note that for backwards compatibility reasons we inflate a layout that is defined
// in the extending Snackbar class. This is to prevent breakage of apps that have custom
// coordinator layout behaviors that depend on that layout.
view = (SnackbarBaseLayout) inflater.inflate(getSnackbarBaseLayoutResId(), targetParent, false);
if (content instanceof SnackbarContentLayout) {
((SnackbarContentLayout) content)
.updateActionTextColorAlphaIfNeeded(view.getActionTextColorAlpha());
((SnackbarContentLayout) content).setMaxInlineActionWidth(view.getMaxInlineActionWidth());
}
view.addView(content);
LayoutParams layoutParams = view.getLayoutParams();
if (layoutParams instanceof MarginLayoutParams) {
MarginLayoutParams marginParams = (MarginLayoutParams) layoutParams;
originalMargins =
new Rect(
marginParams.leftMargin,
marginParams.topMargin,
marginParams.rightMargin,
marginParams.bottomMargin);
}
ViewCompat.setAccessibilityLiveRegion(view, ViewCompat.ACCESSIBILITY_LIVE_REGION_POLITE);
ViewCompat.setImportantForAccessibility(view, ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_YES);
// Make sure that we fit system windows and have a listener to apply any insets
ViewCompat.setFitsSystemWindows(view, true);
ViewCompat.setOnApplyWindowInsetsListener(
view,
new OnApplyWindowInsetsListener() {
@NonNull
@Override
public WindowInsetsCompat onApplyWindowInsets(
View v, @NonNull WindowInsetsCompat insets) {
// Save window insets for additional margins, e.g., to dodge the system navigation bar
extraBottomMarginWindowInset = insets.getSystemWindowInsetBottom();
extraLeftMarginWindowInset = insets.getSystemWindowInsetLeft();
extraRightMarginWindowInset = insets.getSystemWindowInsetRight();
updateMargins();
return insets;
}
});
// Handle accessibility events
ViewCompat.setAccessibilityDelegate(
view,
new AccessibilityDelegateCompat() {
@Override
public void onInitializeAccessibilityNodeInfo(
View host, @NonNull AccessibilityNodeInfoCompat info) {
super.onInitializeAccessibilityNodeInfo(host, info);
info.addAction(AccessibilityNodeInfoCompat.ACTION_DISMISS);
info.setDismissable(true);
}
@Override
public boolean performAccessibilityAction(View host, int action, Bundle args) {
if (action == AccessibilityNodeInfoCompat.ACTION_DISMISS) {
dismiss();
return true;
}
return super.performAccessibilityAction(host, action, args);
}
});
accessibilityManager =
(AccessibilityManager) context.getSystemService(Context.ACCESSIBILITY_SERVICE);
}
private void updateMargins() {
LayoutParams layoutParams = view.getLayoutParams();
if (!(layoutParams instanceof MarginLayoutParams) || originalMargins == null) {
Log.w(TAG, "Unable to update margins because layout params are not MarginLayoutParams");
return;
}
int extraBottomMargin =
getAnchorView() != null ? extraBottomMarginAnchorView : extraBottomMarginWindowInset;
MarginLayoutParams marginParams = (MarginLayoutParams) layoutParams;
marginParams.bottomMargin = originalMargins.bottom + extraBottomMargin;
marginParams.leftMargin = originalMargins.left + extraLeftMarginWindowInset;
marginParams.rightMargin = originalMargins.right + extraRightMarginWindowInset;
view.requestLayout();
if (VERSION.SDK_INT >= VERSION_CODES.Q && shouldUpdateGestureInset()) {
// Ensure there is only one gesture inset runnable running at a time
view.removeCallbacks(bottomMarginGestureInsetRunnable);
view.post(bottomMarginGestureInsetRunnable);
}
}
private boolean shouldUpdateGestureInset() {
return extraBottomMarginGestureInset > 0 && !gestureInsetBottomIgnored && isSwipeDismissable();
}
private boolean isSwipeDismissable() {
LayoutParams layoutParams = view.getLayoutParams();
return layoutParams instanceof CoordinatorLayout.LayoutParams
&& ((CoordinatorLayout.LayoutParams) layoutParams).getBehavior()
instanceof SwipeDismissBehavior;
}
@LayoutRes
protected int getSnackbarBaseLayoutResId() {
return hasSnackbarStyleAttr() ? R.layout.mtrl_layout_snackbar : R.layout.design_layout_snackbar;
}
/**
* {@link Snackbar}s should still work with AppCompat themes, which don't specify a {@code
* snackbarStyle}. This method helps to check if a valid {@code snackbarStyle} is set within the
* current context, so that we know whether we can use the attribute.
*/
protected boolean hasSnackbarStyleAttr() {
TypedArray a = context.obtainStyledAttributes(SNACKBAR_STYLE_ATTR);
int snackbarStyleResId = a.getResourceId(0, -1);
a.recycle();
return snackbarStyleResId != -1;
}
/**
* Set how long to show the view for.
*
* @param duration How long to display the message. Can be {@link #LENGTH_SHORT}, {@link
* #LENGTH_LONG}, {@link #LENGTH_INDEFINITE}, or a custom duration in milliseconds.
*/
@NonNull
public B setDuration(@Duration int duration) {
this.duration = duration;
return (B) this;
}
/**
* Return the duration.
*
* @see #setDuration
*/
@Duration
public int getDuration() {
return duration;
}
/**
* Sets whether this bottom bar should adjust it's position based on the system gesture area on
* Android Q and above.
*
* <p>Note: the bottom bar will only adjust it's position if it is dismissable via swipe (because
* that would cause a gesture conflict), gesture navigation is enabled, and this {@code
* gestureInsetBottomIgnored} flag is false.
*/
@NonNull
public B setGestureInsetBottomIgnored(boolean gestureInsetBottomIgnored) {
this.gestureInsetBottomIgnored = gestureInsetBottomIgnored;
return (B) this;
}
/**
* Returns whether this bottom bar should adjust it's position based on the system gesture area on
* Android Q and above. See {@link #setGestureInsetBottomIgnored(boolean)}.
*/
public boolean isGestureInsetBottomIgnored() {
return gestureInsetBottomIgnored;
}
/**
* Returns the animation mode.
*
* @see #setAnimationMode(int)
*/
@AnimationMode
public int getAnimationMode() {
return view.getAnimationMode();
}
/**
* Sets the animation mode.
*
* @param animationMode of {@link #ANIMATION_MODE_SLIDE} or {@link #ANIMATION_MODE_FADE}.
* @see #getAnimationMode()
*/
@NonNull
public B setAnimationMode(@AnimationMode int animationMode) {
view.setAnimationMode(animationMode);
return (B) this;
}
/**
* Returns the anchor view for this {@link BaseTransientBottomBar}.
*
* @see #setAnchorView(View)
*/
@Nullable
public View getAnchorView() {
return anchor == null ? null : anchor.getAnchorView();
}
/** Sets the view the {@link BaseTransientBottomBar} should be anchored above. */
@NonNull
public B setAnchorView(@Nullable View anchorView) {
if (this.anchor != null) {
this.anchor.unanchor();
}
this.anchor = anchorView == null ? null : Anchor.anchor(this, anchorView);
return (B) this;
}
/**
* Sets the view the {@link BaseTransientBottomBar} should be anchored above by id.
*
* @throws IllegalArgumentException if the anchor view is not found.
*/
@NonNull
public B setAnchorView(@IdRes int anchorViewId) {
View anchorView = targetParent.findViewById(anchorViewId);
if (anchorView == null) {
throw new IllegalArgumentException("Unable to find anchor view with id: " + anchorViewId);
}
return setAnchorView(anchorView);
}
/**
* Returns whether the anchor view layout listener is enabled.
*
* @see #setAnchorViewLayoutListenerEnabled(boolean)
*/
public boolean isAnchorViewLayoutListenerEnabled() {
return anchorViewLayoutListenerEnabled;
}
/**
* Sets whether the anchor view layout listener is enabled. If enabled, the {@link
* BaseTransientBottomBar} will recalculate and update its position when the position of the
* anchor view is changed.
*/
public void setAnchorViewLayoutListenerEnabled(boolean anchorViewLayoutListenerEnabled) {
this.anchorViewLayoutListenerEnabled = anchorViewLayoutListenerEnabled;
}
/**
* Sets the {@link BaseTransientBottomBar.Behavior} to be used in this {@link
* BaseTransientBottomBar}.
*
* @param behavior {@link BaseTransientBottomBar.Behavior} to be applied.
*/
@NonNull
public B setBehavior(BaseTransientBottomBar.Behavior behavior) {
this.behavior = behavior;
return (B) this;
}
/**
* Return the behavior.
*
* @see #setBehavior(BaseTransientBottomBar.Behavior)
*/
public BaseTransientBottomBar.Behavior getBehavior() {
return behavior;
}
/** Returns the {@link BaseTransientBottomBar}'s context. */
@NonNull
public Context getContext() {
return context;
}
/** Returns the {@link BaseTransientBottomBar}'s view. */
@NonNull
public View getView() {
return view;
}
/** Show the {@link BaseTransientBottomBar}. */
public void show() {
SnackbarManager.getInstance().show(getDuration(), managerCallback);
}
/** Dismiss the {@link BaseTransientBottomBar}. */
public void dismiss() {
dispatchDismiss(BaseCallback.DISMISS_EVENT_MANUAL);
}
protected void dispatchDismiss(@BaseCallback.DismissEvent int event) {
SnackbarManager.getInstance().dismiss(managerCallback, event);
}
/**
* Adds the specified callback to the list of callbacks that will be notified of transient bottom
* bar events.
*
* @param callback Callback to notify when transient bottom bar events occur.
* @see #removeCallback(BaseCallback)
*/
@NonNull
public B addCallback(@Nullable BaseCallback<B> callback) {
if (callback == null) {
return (B) this;
}
if (callbacks == null) {
callbacks = new ArrayList<BaseCallback<B>>();
}
callbacks.add(callback);
return (B) this;
}
/**
* Removes the specified callback from the list of callbacks that will be notified of transient
* bottom bar events.
*
* @param callback Callback to remove from being notified of transient bottom bar events
* @see #addCallback(BaseCallback)
*/
@NonNull
public B removeCallback(@Nullable BaseCallback<B> callback) {
if (callback == null) {
return (B) this;
}
if (callbacks == null) {
// This can happen if this method is called before the first call to addCallback
return (B) this;
}
callbacks.remove(callback);
return (B) this;
}
/** Return whether this {@link BaseTransientBottomBar} is currently being shown. */
public boolean isShown() {
return SnackbarManager.getInstance().isCurrent(managerCallback);
}
/**
* Returns whether this {@link BaseTransientBottomBar} is currently being shown, or is queued to
* be shown next.
*/
public boolean isShownOrQueued() {
return SnackbarManager.getInstance().isCurrentOrNext(managerCallback);
}
@NonNull
SnackbarManager.Callback managerCallback =
new SnackbarManager.Callback() {
@Override
public void show() {
handler.sendMessage(handler.obtainMessage(MSG_SHOW, BaseTransientBottomBar.this));
}
@Override
public void dismiss(int event) {
handler.sendMessage(
handler.obtainMessage(MSG_DISMISS, event, 0, BaseTransientBottomBar.this));
}
};
@NonNull
protected SwipeDismissBehavior<? extends View> getNewBehavior() {
return new Behavior();
}
final void showView() {
this.view.setOnAttachStateChangeListener(
new BaseTransientBottomBar.OnAttachStateChangeListener() {
@Override
public void onViewAttachedToWindow(View v) {
if (VERSION.SDK_INT >= VERSION_CODES.Q) {
WindowInsets insets = view.getRootWindowInsets();
if (insets != null) {
extraBottomMarginGestureInset = insets.getMandatorySystemGestureInsets().bottom;
updateMargins();
}
}
}
@Override
public void onViewDetachedFromWindow(View v) {
if (isShownOrQueued()) {
// If we haven't already been dismissed then this event is coming from a
// non-user initiated action. Hence we need to make sure that we callback
// and keep our state up to date. We need to post the call since
// removeView() will call through to onDetachedFromWindow and thus overflow.
handler.post(
new Runnable() {
@Override
public void run() {
onViewHidden(BaseCallback.DISMISS_EVENT_MANUAL);
}
});
}
}
});
if (this.view.getParent() == null) {
ViewGroup.LayoutParams lp = this.view.getLayoutParams();
if (lp instanceof CoordinatorLayout.LayoutParams) {
setUpBehavior((CoordinatorLayout.LayoutParams) lp);
}
recalculateAndUpdateMargins();
// Set view to INVISIBLE so it doesn't flash on the screen before the inset adjustment is
// handled and the enter animation is started
view.setVisibility(View.INVISIBLE);
targetParent.addView(this.view);
}
if (ViewCompat.isLaidOut(this.view)) {
showViewImpl();
return;
}
// Otherwise, add one of our layout change listeners and show it in when laid out
this.view.setOnLayoutChangeListener(
new OnLayoutChangeListener() {
@Override
public void onLayoutChange(View view, int left, int top, int right, int bottom) {
BaseTransientBottomBar.this.view.setOnLayoutChangeListener(null);
BaseTransientBottomBar.this.showViewImpl();
}
});
}
private void showViewImpl() {
if (shouldAnimate()) {
// If animations are enabled, animate it in
animateViewIn();
} else {
// Else if animations are disabled, just make view VISIBLE and call back now
if (view.getParent() != null) {
view.setVisibility(View.VISIBLE);
}
onViewShown();
}
}
private int getViewAbsoluteBottom() {
int[] absoluteLocation = new int[2];
view.getLocationOnScreen(absoluteLocation);
return absoluteLocation[1] + view.getHeight();
}
@RequiresApi(VERSION_CODES.JELLY_BEAN_MR1)
private int getScreenHeight() {
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics displayMetrics = new DisplayMetrics();
windowManager.getDefaultDisplay().getRealMetrics(displayMetrics);
return displayMetrics.heightPixels;
}
private void setUpBehavior(CoordinatorLayout.LayoutParams lp) {
// If our LayoutParams are from a CoordinatorLayout, we'll setup our Behavior
CoordinatorLayout.LayoutParams clp = lp;
SwipeDismissBehavior<? extends View> behavior =
this.behavior == null ? getNewBehavior() : this.behavior;
if (behavior instanceof BaseTransientBottomBar.Behavior) {
((Behavior) behavior).setBaseTransientBottomBar(this);
}
behavior.setListener(
new SwipeDismissBehavior.OnDismissListener() {
@Override
public void onDismiss(@NonNull View view) {
if (view.getParent() != null) {
view.setVisibility(View.GONE);
}
dispatchDismiss(BaseCallback.DISMISS_EVENT_SWIPE);
}
@Override
public void onDragStateChanged(int state) {
switch (state) {
case SwipeDismissBehavior.STATE_DRAGGING:
case SwipeDismissBehavior.STATE_SETTLING:
// If the view is being dragged or settling, pause the timeout
SnackbarManager.getInstance().pauseTimeout(managerCallback);
break;
case SwipeDismissBehavior.STATE_IDLE:
// If the view has been released and is idle, restore the timeout
SnackbarManager.getInstance().restoreTimeoutIfPaused(managerCallback);
break;
default:
// Any other state is ignored
}
}
});
clp.setBehavior(behavior);
// Also set the inset edge so that views can dodge the bar correctly, but only if there is
// no anchor view.
if (getAnchorView() == null) {
clp.insetEdge = Gravity.BOTTOM;
}
}
private void recalculateAndUpdateMargins() {
extraBottomMarginAnchorView = calculateBottomMarginForAnchorView();
updateMargins();
}
private int calculateBottomMarginForAnchorView() {
if (getAnchorView() == null) {
return 0;
}
int[] anchorViewLocation = new int[2];
getAnchorView().getLocationOnScreen(anchorViewLocation);
int anchorViewAbsoluteYTop = anchorViewLocation[1];
int[] targetParentLocation = new int[2];
targetParent.getLocationOnScreen(targetParentLocation);
int targetParentAbsoluteYBottom = targetParentLocation[1] + targetParent.getHeight();
return targetParentAbsoluteYBottom - anchorViewAbsoluteYTop;
}
void animateViewIn() {
// Post to make sure animation doesn't start until after all inset handling has completed
view.post(
new Runnable() {
@Override
public void run() {
if (view == null) {
return;
}
// Make view VISIBLE now that we are about to start the enter animation
if (view.getParent() != null) {
view.setVisibility(View.VISIBLE);
}
if (view.getAnimationMode() == ANIMATION_MODE_FADE) {
startFadeInAnimation();
} else {
startSlideInAnimation();
}
}
});
}
private void animateViewOut(int event) {
if (view.getAnimationMode() == ANIMATION_MODE_FADE) {
startFadeOutAnimation(event);
} else {
startSlideOutAnimation(event);
}
}
private void startFadeInAnimation() {
ValueAnimator alphaAnimator = getAlphaAnimator(0, 1);
ValueAnimator scaleAnimator = getScaleAnimator(ANIMATION_SCALE_FROM_VALUE, 1);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playTogether(alphaAnimator, scaleAnimator);
animatorSet.setDuration(ANIMATION_FADE_IN_DURATION);
animatorSet.addListener(
new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animator) {
onViewShown();
}
});
animatorSet.start();
}
private void startFadeOutAnimation(final int event) {
ValueAnimator animator = getAlphaAnimator(1, 0);
animator.setDuration(ANIMATION_FADE_OUT_DURATION);
animator.addListener(
new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animator) {
onViewHidden(event);
}
});
animator.start();
}
private ValueAnimator getAlphaAnimator(float... alphaValues) {
ValueAnimator animator = ValueAnimator.ofFloat(alphaValues);
animator.setInterpolator(LINEAR_INTERPOLATOR);
animator.addUpdateListener(
new AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(@NonNull ValueAnimator valueAnimator) {
view.setAlpha((Float) valueAnimator.getAnimatedValue());
}
});
return animator;
}
private ValueAnimator getScaleAnimator(float... scaleValues) {
ValueAnimator animator = ValueAnimator.ofFloat(scaleValues);
animator.setInterpolator(LINEAR_OUT_SLOW_IN_INTERPOLATOR);
animator.addUpdateListener(
new AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(@NonNull ValueAnimator valueAnimator) {
float scale = (float) valueAnimator.getAnimatedValue();
view.setScaleX(scale);
view.setScaleY(scale);
}
});
return animator;
}
private void startSlideInAnimation() {
final int translationYBottom = getTranslationYBottom();
if (USE_OFFSET_API) {
ViewCompat.offsetTopAndBottom(view, translationYBottom);
} else {
view.setTranslationY(translationYBottom);
}
ValueAnimator animator = new ValueAnimator();
animator.setIntValues(translationYBottom, 0);
animator.setInterpolator(FAST_OUT_SLOW_IN_INTERPOLATOR);
animator.setDuration(ANIMATION_DURATION);
animator.addListener(
new AnimatorListenerAdapter() {
@Override
public void onAnimationStart(Animator animator) {
contentViewCallback.animateContentIn(
ANIMATION_DURATION - ANIMATION_FADE_DURATION, ANIMATION_FADE_DURATION);
}
@Override
public void onAnimationEnd(Animator animator) {
onViewShown();
}
});
animator.addUpdateListener(
new ValueAnimator.AnimatorUpdateListener() {
private int previousAnimatedIntValue = translationYBottom;