Cleanup some taskbar/hotseat handoff issues

- Clearly distinguish Home <-> App icon alignment (ForResumedState and ForGestureState) from Home <-> Home states (ForLauncherState) by renaming onIconAlignmentRatioChanged() to onIconAlignmentRatioChangedForAppAndHomeTransition (for the former)
- Make sure initial state is properly applied by treating all flags as changed
- onIconAlignmentRatioChangedForStateTransition only runs when launcher is resumed (fixes 208648294)
- Animate taskbar background alpha when going between home and app, separate from icon alignment (bug: 204657916)
- Only copy hotseat's alpha to taskbar when hotseat is originally visible (also bug: 204657916)

Test: Open an app on small screen, open large screen and ensure taskbar stays at the bottom instead of aligning with non-present hotseat (bug: 208648294)
Test: Swipe up and down on home scren, ensure taskbar background never appears and there's no jump between taskbar and hotseat
Change-Id: Iea59965118f2bdbd8ce49279f76fb01fbd61f60b
diff --git a/quickstep/src/com/android/launcher3/taskbar/TaskbarLauncherStateController.java b/quickstep/src/com/android/launcher3/taskbar/TaskbarLauncherStateController.java
index b9fda31..76088ac 100644
--- a/quickstep/src/com/android/launcher3/taskbar/TaskbarLauncherStateController.java
+++ b/quickstep/src/com/android/launcher3/taskbar/TaskbarLauncherStateController.java
@@ -52,10 +52,13 @@
     public static final int FLAG_TRANSITION_STATE_START_STASHED = 1 << 2;
     public static final int FLAG_TRANSITION_STATE_COMMITTED_STASHED = 1 << 3;
 
+    /** Equivalent to an int with all 1s for binary operation purposes */
+    private static final int FLAGS_ALL = ~0;
+
     private final AnimatedFloat mIconAlignmentForResumedState =
-            new AnimatedFloat(this::onIconAlignmentRatioChanged);
+            new AnimatedFloat(this::onIconAlignmentRatioChangedForAppAndHomeTransition);
     private final AnimatedFloat mIconAlignmentForGestureState =
-            new AnimatedFloat(this::onIconAlignmentRatioChanged);
+            new AnimatedFloat(this::onIconAlignmentRatioChangedForAppAndHomeTransition);
     private final AnimatedFloat mIconAlignmentForLauncherState =
             new AnimatedFloat(this::onIconAlignmentRatioChangedForStateTransition);
 
@@ -64,7 +67,7 @@
     private MultiValueAlpha.AlphaProperty mIconAlphaForHome;
     private BaseQuickstepLauncher mLauncher;
 
-    private int mPrevState;
+    private Integer mPrevState;
     private int mState;
 
     private boolean mIsAnimatingToLauncherViaGesture;
@@ -100,7 +103,7 @@
                 (Consumer<Float>) alpha -> mLauncher.getHotseat().setIconsAlpha(alpha > 0 ? 0 : 1));
 
         mIconAlignmentForResumedState.finishAnimation();
-        onIconAlignmentRatioChanged();
+        onIconAlignmentRatioChangedForAppAndHomeTransition();
 
         mLauncher.getStateManager().addStateListener(mStateListener);
     }
@@ -183,8 +186,9 @@
 
     public Animator applyState(long duration, boolean start) {
         Animator animator = null;
-        if (mPrevState != mState) {
-            int changedFlags = mPrevState ^ mState;
+        if (mPrevState == null || mPrevState != mState) {
+            // If this is our initial state, treat all flags as changed.
+            int changedFlags = mPrevState == null ? FLAGS_ALL : mPrevState ^ mState;
             animator = onStateChangeApplied(changedFlags, duration, start);
             mPrevState = mState;
         }
@@ -240,6 +244,12 @@
             animatorSet.play(animator);
         }
 
+        if (hasAnyFlag(changedFlags, FLAG_RESUMED | FLAG_RECENTS_ANIMATION_RUNNING)) {
+            boolean goingToLauncher = hasAnyFlag(FLAG_RESUMED | FLAG_RECENTS_ANIMATION_RUNNING);
+            animatorSet.play(mTaskbarBackgroundAlpha.animateToValue(goingToLauncher ? 0 : 1)
+                    .setDuration(duration));
+        }
+
         if (hasAnyFlag(changedFlags, FLAG_TRANSITION_STATE_START_STASHED)) {
             playStateTransitionAnim(isTransitionStateStartStashed(), animatorSet, duration,
                     false /* committed */);
@@ -279,7 +289,9 @@
 
                 @Override
                 public void onAnimationStart(Animator animation) {
-                    mIconAlphaForHome.setValue(mLauncher.getHotseat().getIconsAlpha());
+                    if (mLauncher.getHotseat().getIconsAlpha() > 0) {
+                        mIconAlphaForHome.setValue(mLauncher.getHotseat().getIconsAlpha());
+                    }
                 }
             });
             animatorSet.play(stashAnimator);
@@ -306,11 +318,14 @@
     }
 
     private void onIconAlignmentRatioChangedForStateTransition() {
+        if (!isResumed()) {
+            return;
+        }
         onIconAlignmentRatioChanged(this::getCurrentIconAlignmentRatioForLauncherState);
     }
 
-    private void onIconAlignmentRatioChanged() {
-        onIconAlignmentRatioChanged(this::getCurrentIconAlignmentRatio);
+    private void onIconAlignmentRatioChangedForAppAndHomeTransition() {
+        onIconAlignmentRatioChanged(this::getCurrentIconAlignmentRatioBetweenAppAndHome);
     }
 
     private void onIconAlignmentRatioChanged(Supplier<Float> alignmentSupplier) {
@@ -321,13 +336,11 @@
         mControllers.taskbarViewController.setLauncherIconAlignment(
                 alignment, mLauncher.getDeviceProfile());
 
-        mTaskbarBackgroundAlpha.updateValue(1 - alignment);
-
         // Switch taskbar and hotseat in last frame
         setTaskbarViewVisible(alignment < 1);
     }
 
-    private float getCurrentIconAlignmentRatio() {
+    private float getCurrentIconAlignmentRatioBetweenAppAndHome() {
         return Math.max(mIconAlignmentForResumedState.value, mIconAlignmentForGestureState.value);
     }