Handle hiding bubble right after showing it.

If hide bubble right after showing it, we should cancel animation, remove view and set visibility to HIDEN. Otherwise we can't retrieve correct bubble status later, thus hide/show bubble unexpectedly.

It at least fix some cases of IllegalArgumentException.

Bug: 71746139
Test: NewBubbleTest
PiperOrigin-RevId: 182222155
Change-Id: If020b83cfffd9c643fd6fe3d2879a05e5969281f
diff --git a/java/com/android/newbubble/NewBubble.java b/java/com/android/newbubble/NewBubble.java
index cb7a094..65f7a0a 100644
--- a/java/com/android/newbubble/NewBubble.java
+++ b/java/com/android/newbubble/NewBubble.java
@@ -103,7 +103,7 @@
   @NonNull
   private NewBubbleInfo currentInfo;
 
-  @Visibility private int visibility;
+  @VisibleForTesting @Visibility int visibility;
   private boolean expanded;
   private CharSequence textAfterShow;
   private int collapseEndAction;
@@ -112,6 +112,7 @@
   private AnimatorSet collapseAnimatorSet;
   private Integer overrideGravity;
   @VisibleForTesting AnimatorSet exitAnimatorSet;
+  @VisibleForTesting AnimatorSet enterAnimatorSet;
 
   private final int primaryIconMoveDistance;
   private final int leftBoundary;
@@ -120,15 +121,15 @@
   /** Type of action after bubble collapse */
   @Retention(RetentionPolicy.SOURCE)
   @IntDef({CollapseEnd.NOTHING, CollapseEnd.HIDE})
-  @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
-  public @interface CollapseEnd {
+  private @interface CollapseEnd {
     int NOTHING = 0;
     int HIDE = 1;
   }
 
   @Retention(RetentionPolicy.SOURCE)
+  @VisibleForTesting
   @IntDef({Visibility.ENTERING, Visibility.SHOWING, Visibility.EXITING, Visibility.HIDDEN})
-  private @interface Visibility {
+  @interface Visibility {
     int HIDDEN = 0;
     int ENTERING = 1;
     int SHOWING = 2;
@@ -440,7 +441,7 @@
         ObjectAnimator.ofFloat(viewHolder.getPrimaryAvatar(), "alpha", 1);
     ObjectAnimator iconAlphaAnimator =
         ObjectAnimator.ofFloat(viewHolder.getPrimaryIcon(), "alpha", 1);
-    AnimatorSet enterAnimatorSet = new AnimatorSet();
+    enterAnimatorSet = new AnimatorSet();
     enterAnimatorSet.playTogether(
         scaleXAnimator, scaleYAnimator, avatarAlphaAnimator, iconAlphaAnimator);
     enterAnimatorSet.setInterpolator(new OvershootInterpolator());
@@ -609,6 +610,14 @@
     // Make bubble non clickable to prevent further buggy actions
     viewHolder.setChildClickable(false);
 
+    if (visibility == Visibility.ENTERING) {
+      enterAnimatorSet.removeAllListeners();
+      enterAnimatorSet.cancel();
+      enterAnimatorSet = null;
+      afterHiding.run();
+      return;
+    }
+
     if (collapseAnimatorSet != null) {
       collapseEndAction = CollapseEnd.HIDE;
       return;