Merge "Moving mState from Workspace to StateManager" into ub-launcher3-master
diff --git a/src/com/android/launcher3/Launcher.java b/src/com/android/launcher3/Launcher.java
index cd9fffc..2593c49 100644
--- a/src/com/android/launcher3/Launcher.java
+++ b/src/com/android/launcher3/Launcher.java
@@ -772,7 +772,7 @@
 
         if (!mAppLaunchSuccess) {
             getUserEventDispatcher().logActionCommand(Action.Command.STOP,
-                    mWorkspace.getState().containerType);
+                    mStateManager.getState().containerType);
         }
         NotificationListener.removeNotificationsChangedListener();
     }
@@ -1014,7 +1014,7 @@
     }
 
     public boolean isInState(LauncherState state) {
-        return mWorkspace.getState() == state;
+        return mStateManager.getState() == state;
     }
 
     /**
@@ -1426,7 +1426,7 @@
             if (topOpenView != null) {
                 topOpenView.logActionCommand(Action.Command.HOME_INTENT);
             } else if (alreadyOnHome) {
-                Target target = newContainerTarget(mWorkspace.getState().containerType);
+                Target target = newContainerTarget(mStateManager.getState().containerType);
                 target.pageIndex = mWorkspace.getCurrentPage();
                 ued.logActionCommand(Action.Command.HOME_INTENT, target);
             }
@@ -1489,7 +1489,7 @@
             outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
 
         }
-        outState.putInt(RUNTIME_STATE, mWorkspace.getState().ordinal);
+        outState.putInt(RUNTIME_STATE, mStateManager.getState().ordinal);
 
 
         AbstractFloatingView widgets = AbstractFloatingView
@@ -1884,7 +1884,7 @@
         if (topView != null) {
             topView.onBackPressed();
         } else if (!isInState(NORMAL)) {
-            ued.logActionCommand(Action.Command.BACK, mWorkspace.getState().containerType);
+            ued.logActionCommand(Action.Command.BACK, mStateManager.getState().containerType);
             mStateManager.goToState(NORMAL);
         } else {
             // Back button is a no-op here, but give at least some feedback for the button press
@@ -2440,7 +2440,7 @@
         // TODO: When can workspace be null?
         text.add(mWorkspace == null
                 ? getString(R.string.all_apps_home_button_label)
-                : mWorkspace.getState().getDescription(this));
+                : mStateManager.getState().getDescription(this));
         return result;
     }
 
diff --git a/src/com/android/launcher3/LauncherStateManager.java b/src/com/android/launcher3/LauncherStateManager.java
index 4298174..863cae7 100644
--- a/src/com/android/launcher3/LauncherStateManager.java
+++ b/src/com/android/launcher3/LauncherStateManager.java
@@ -16,6 +16,8 @@
 
 package com.android.launcher3;
 
+import static com.android.launcher3.LauncherState.NORMAL;
+
 import android.animation.Animator;
 import android.animation.AnimatorListenerAdapter;
 import android.animation.AnimatorSet;
@@ -77,6 +79,8 @@
     private final Launcher mLauncher;
     private final AllAppsTransitionController mAllAppsController;
 
+    private LauncherState mState = NORMAL;
+
     public LauncherStateManager(
             Launcher l, AllAppsTransitionController allAppsController) {
         mUiHandler = new Handler(Looper.getMainLooper());
@@ -84,6 +88,10 @@
         mAllAppsController = allAppsController;
     }
 
+    public LauncherState getState() {
+        return mState;
+    }
+
     /**
      * @see #goToState(LauncherState, boolean, Runnable)
      */
@@ -139,6 +147,7 @@
         mConfig.reset();
 
         if (!animated) {
+            setState(state);
             mAllAppsController.setFinalProgress(state.verticalProgress);
             mLauncher.getWorkspace().setState(state);
             mLauncher.getUserEventDispatcher().resetElapsedContainerMillis();
@@ -161,9 +170,10 @@
         }
     }
 
-    protected AnimatorSet createAnimationToNewWorkspace(LauncherState state,
+    protected AnimatorSet createAnimationToNewWorkspace(final LauncherState state,
             final Runnable onCompleteRunnable) {
         mConfig.reset();
+        mConfig.duration = state == NORMAL ? mState.transitionDuration : state.transitionDuration;
 
         final AnimatorSet animation = LauncherAnimUtils.createAnimatorSet();
         final AnimationLayerSet layerViews = new AnimationLayerSet();
@@ -175,6 +185,13 @@
 
         animation.addListener(layerViews);
         animation.addListener(new AnimationSuccessListener() {
+
+            @Override
+            public void onAnimationStart(Animator animation) {
+                // Change the internal state only when the transition actually starts
+                setState(state);
+            }
+
             @Override
             public void onAnimationSuccess(Animator animator) {
                 // Run any queued runnables
@@ -189,6 +206,12 @@
         return mConfig.mCurrentAnimation;
     }
 
+    private void setState(LauncherState state) {
+        mState.onStateDisabled(mLauncher);
+        mState = state;
+        mState.onStateEnabled(mLauncher);
+    }
+
     /**
      * Cancels the current animation.
      */
@@ -220,13 +243,13 @@
 
     public static class AnimationConfig extends AnimatorListenerAdapter {
         public boolean shouldPost;
+        public long duration;
 
-        private long mOverriddenDuration = -1;
         private AnimatorSet mCurrentAnimation;
 
         public void reset() {
-            shouldPost = false;
-            mOverriddenDuration = -1;
+            shouldPost = true;
+            duration = 0;
 
             if (mCurrentAnimation != null) {
                 mCurrentAnimation.setDuration(0);
@@ -235,14 +258,6 @@
             }
         }
 
-        public void overrideDuration(long duration) {
-            mOverriddenDuration = duration;
-        }
-
-        public long getDuration(long defaultDuration) {
-            return mOverriddenDuration >= 0 ? mOverriddenDuration : defaultDuration;
-        }
-
         @Override
         public void onAnimationEnd(Animator animation) {
             if (mCurrentAnimation == animation) {
diff --git a/src/com/android/launcher3/Workspace.java b/src/com/android/launcher3/Workspace.java
index 900e5bf..71e8c55 100644
--- a/src/com/android/launcher3/Workspace.java
+++ b/src/com/android/launcher3/Workspace.java
@@ -206,8 +206,6 @@
      */
     private final float[] mHotseatAlpha = new float[] {1, 1, 1};
 
-    @ViewDebug.ExportedProperty(category = "launcher")
-    private LauncherState mState = NORMAL;
     private boolean mIsSwitchingState = false;
 
     boolean mChildrenLayersEnabled = true;
@@ -576,7 +574,8 @@
         mWorkspaceScreens.put(screenId, newScreen);
         mScreenOrder.add(insertIndex, screenId);
         addView(newScreen, insertIndex);
-        mStateTransitionAnimation.applyChildState(mState, newScreen, insertIndex);
+        mStateTransitionAnimation.applyChildState(
+                mLauncher.getStateManager().getState(), newScreen, insertIndex);
 
         if (mLauncher.getAccessibilityDelegate().isInAccessibleDrag()) {
             newScreen.enableAccessibleDrag(true, CellLayout.WORKSPACE_ACCESSIBILITY_DRAG);
@@ -1411,13 +1410,13 @@
         return super.getDescendantFocusability();
     }
 
-    public boolean workspaceInModalState() {
-        return mState != NORMAL;
+    private boolean workspaceInModalState() {
+        return !mLauncher.isInState(NORMAL);
     }
 
     /** Returns whether a drag should be allowed to be started from the current workspace state. */
     public boolean workspaceIconsCanBeDragged() {
-        return mState == NORMAL || mState == SPRING_LOADED;
+        return mLauncher.isInState(NORMAL) || mLauncher.isInState(SPRING_LOADED);
     }
 
     private void updateChildrenLayersEnabled() {
@@ -1545,11 +1544,6 @@
     }
 
     private void onStartStateTransition(LauncherState state) {
-        // Change the internal state only when the transition actually starts
-        mState.onStateDisabled(mLauncher);
-        mState = state;
-        mState.onStateEnabled(mLauncher);
-
         mIsSwitchingState = true;
         mTransitionProgress = 0;
 
@@ -1570,7 +1564,7 @@
      */
     public void setState(LauncherState toState) {
         onStartStateTransition(toState);
-        mStateTransitionAnimation.setState(mState);
+        mStateTransitionAnimation.setState(toState);
         onEndStateTransition();
     }
 
@@ -1580,7 +1574,7 @@
     public void setStateWithAnimation(LauncherState toState, AnimationLayerSet layerViews,
             AnimatorSet anim, AnimationConfig config) {
         StateTransitionListener listener = new StateTransitionListener(toState);
-        mStateTransitionAnimation.setStateWithAnimation(mState, toState, anim, layerViews, config);
+        mStateTransitionAnimation.setStateWithAnimation(toState, anim, layerViews, config);
 
         // Invalidate the pages now, so that we have the visible pages before the
         // animation is started
@@ -1595,22 +1589,19 @@
         anim.addListener(listener);
     }
 
-    public LauncherState getState() {
-        return mState;
-    }
-
     public void updateAccessibilityFlags() {
         // TODO: Update the accessibility flags appropriately when dragging.
+        int accessibilityFlag = mLauncher.getStateManager().getState().workspaceAccessibilityFlag;
         if (!mLauncher.getAccessibilityDelegate().isInAccessibleDrag()) {
             int total = getPageCount();
             for (int i = 0; i < total; i++) {
-                updateAccessibilityFlags((CellLayout) getPageAt(i), i);
+                updateAccessibilityFlags(accessibilityFlag, (CellLayout) getPageAt(i), i);
             }
-            setImportantForAccessibility(mState.workspaceAccessibilityFlag);
+            setImportantForAccessibility(accessibilityFlag);
         }
     }
 
-    private void updateAccessibilityFlags(CellLayout page, int pageNo) {
+    private void updateAccessibilityFlags(int accessibilityFlag, CellLayout page, int pageNo) {
         if (isPageRearrangeEnabled()) {
             page.setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
             page.getShortcutsAndWidgets().setImportantForAccessibility(
@@ -1626,8 +1617,7 @@
             }
         } else {
             page.setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO);
-            page.getShortcutsAndWidgets()
-                    .setImportantForAccessibility(mState.workspaceAccessibilityFlag);
+            page.getShortcutsAndWidgets().setImportantForAccessibility(accessibilityFlag);
             page.setContentDescription(null);
             page.setAccessibilityDelegate(null);
         }
@@ -2687,7 +2677,7 @@
         final long screenId = getIdForScreen(cellLayout);
         if (!mLauncher.isHotseatLayout(cellLayout)
                 && screenId != getScreenIdForPageIndex(mCurrentPage)
-                && mState != SPRING_LOADED) {
+                && !mLauncher.isInState(SPRING_LOADED)) {
             snapToPage(getPageIndexForScreenId(screenId));
         }
 
diff --git a/src/com/android/launcher3/WorkspaceStateTransitionAnimation.java b/src/com/android/launcher3/WorkspaceStateTransitionAnimation.java
index 0ccb8ad..9ae0fe4 100644
--- a/src/com/android/launcher3/WorkspaceStateTransitionAnimation.java
+++ b/src/com/android/launcher3/WorkspaceStateTransitionAnimation.java
@@ -161,12 +161,10 @@
         setWorkspaceProperty(toState, NO_ANIM_PROPERTY_SETTER);
     }
 
-    public void setStateWithAnimation(LauncherState fromState, LauncherState toState,
-            AnimatorSet anim, AnimationLayerSet layerViews, AnimationConfig config) {
-        long duration = config.getDuration(toState == LauncherState.NORMAL
-                ? fromState.transitionDuration : toState.transitionDuration);
+    public void setStateWithAnimation(LauncherState toState, AnimatorSet anim,
+            AnimationLayerSet layerViews, AnimationConfig config) {
         AnimatedPropertySetter propertySetter =
-                new AnimatedPropertySetter(duration, layerViews, anim);
+                new AnimatedPropertySetter(config.duration, layerViews, anim);
         setWorkspaceProperty(toState, propertySetter);
     }
 
diff --git a/src/com/android/launcher3/allapps/AllAppsTransitionController.java b/src/com/android/launcher3/allapps/AllAppsTransitionController.java
index d62cd4b..7de11bb 100644
--- a/src/com/android/launcher3/allapps/AllAppsTransitionController.java
+++ b/src/com/android/launcher3/allapps/AllAppsTransitionController.java
@@ -133,9 +133,9 @@
         if (ev.getAction() == MotionEvent.ACTION_DOWN) {
             mNoIntercept = false;
             mTouchEventStartedOnHotseat = mLauncher.getDragLayer().isEventOverHotseat(ev);
-            if (!mLauncher.isInState(LauncherState.ALL_APPS) && mLauncher.getWorkspace().workspaceInModalState()) {
+            if (!mLauncher.isInState(ALL_APPS) && !mLauncher.isInState(NORMAL)) {
                 mNoIntercept = true;
-            } else if (mLauncher.isInState(LauncherState.ALL_APPS) &&
+            } else if (mLauncher.isInState(ALL_APPS) &&
                     !mAppsView.shouldContainerScroll(ev)) {
                 mNoIntercept = true;
             } else if (AbstractFloatingView.getTopOpenView(mLauncher) != null) {
@@ -147,7 +147,7 @@
                 boolean ignoreSlopWhenSettling = false;
 
                 if (mDetector.isIdleState()) {
-                    if (mLauncher.isInState(LauncherState.ALL_APPS)) {
+                    if (mLauncher.isInState(ALL_APPS)) {
                         directionsToDetectScroll |= SwipeDetector.DIRECTION_NEGATIVE;
                     } else {
                         directionsToDetectScroll |= SwipeDetector.DIRECTION_POSITIVE;
@@ -230,7 +230,7 @@
         if (fling) {
             if (velocity < 0) {
                 calculateDuration(velocity, mAppsView.getTranslationY());
-                if (!mLauncher.isInState(LauncherState.ALL_APPS)) {
+                if (!mLauncher.isInState(ALL_APPS)) {
                     logSwipeOnContainer(Touch.FLING, Direction.UP, containerType);
                 }
                 mLauncher.getStateManager().goToState(ALL_APPS);
@@ -241,7 +241,7 @@
                 }
             } else {
                 calculateDuration(velocity, Math.abs(mShiftRange - mAppsView.getTranslationY()));
-                if (mLauncher.isInState(LauncherState.ALL_APPS)) {
+                if (mLauncher.isInState(ALL_APPS)) {
                     logSwipeOnContainer(Touch.FLING, Direction.DOWN, ContainerType.ALLAPPS);
                 }
                 mLauncher.getStateManager().goToState(NORMAL);
@@ -250,13 +250,13 @@
         } else {
             if (mAppsView.getTranslationY() > mShiftRange / 2) {
                 calculateDuration(velocity, Math.abs(mShiftRange - mAppsView.getTranslationY()));
-                if (mLauncher.isInState(LauncherState.ALL_APPS)) {
+                if (mLauncher.isInState(ALL_APPS)) {
                     logSwipeOnContainer(Touch.SWIPE, Direction.DOWN, ContainerType.ALLAPPS);
                 }
                 mLauncher.getStateManager().goToState(NORMAL);
             } else {
                 calculateDuration(velocity, Math.abs(mAppsView.getTranslationY()));
-                if (!mLauncher.isInState(LauncherState.ALL_APPS)) {
+                if (!mLauncher.isInState(ALL_APPS)) {
                     logSwipeOnContainer(Touch.SWIPE, Direction.UP, containerType);
                 }
                 mLauncher.getStateManager().goToState(ALL_APPS);
@@ -403,7 +403,7 @@
             outConfig.shouldPost = false;
         }
 
-        outConfig.overrideDuration(mAnimationDuration);
+        outConfig.duration = mAnimationDuration;
         ObjectAnimator anim = ObjectAnimator.ofFloat(this, PROGRESS, mProgress, progress);
         anim.setDuration(mAnimationDuration);
         anim.setInterpolator(interpolator);