Disabling synchronous binding when returning home from another app while orientation has changed. (Bug 6792288)

- Fixing issue where we were reverting to the first page of the workspace after launching an application from all apps, rotating, and returning home
- Enabling rotation in master.

Change-Id: I291b9d76b20244e9028b6f62164430bc3606644c
diff --git a/src/com/android/launcher2/Launcher.java b/src/com/android/launcher2/Launcher.java
index 9784f57..f31f82e 100644
--- a/src/com/android/launcher2/Launcher.java
+++ b/src/com/android/launcher2/Launcher.java
@@ -183,7 +183,7 @@
             "com.android.launcher.toolbar_voice_search_icon";
 
     /** The different states that Launcher can be in. */
-    private enum State { WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
+    private enum State { NONE, WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
     private State mState = State.WORKSPACE;
     private AnimatorSet mStateAnimation;
     private AnimatorSet mDividerAnimator;
@@ -231,6 +231,10 @@
     private boolean mAutoAdvanceRunning = false;
 
     private Bundle mSavedState;
+    // We set the state in both onCreate and then onNewIntent in some cases, which causes both
+    // scroll issues (because the workspace may not have been measured yet) and extra work.
+    // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
+    private State mOnResumeState = State.NONE;
 
     private SpannableStringBuilder mDefaultKeySsb = null;
 
@@ -241,6 +245,9 @@
     private boolean mWaitingForResult;
     private boolean mOnResumeNeedsLoad;
 
+    // Keep track of whether the user has left launcher
+    private static boolean sPausedFromUserAction = false;
+
     private Bundle mSavedInstanceState;
 
     private LauncherModel mModel;
@@ -386,7 +393,15 @@
         }
 
         if (!mRestoring) {
-            mModel.startLoader(true, mWorkspace.getCurrentPage());
+            if (sPausedFromUserAction) {
+                // If the user leaves launcher, then we should just load items asynchronously when
+                // they return.
+                mModel.startLoader(true, -1);
+            } else {
+                // We only load the page synchronously if the user rotates (or triggers a
+                // configuration change) while launcher is in the foreground
+                mModel.startLoader(true, mWorkspace.getCurrentPage());
+            }
         }
 
         if (!mModel.isAllAppsLoaded()) {
@@ -407,6 +422,11 @@
         unlockScreenOrientation(true);
     }
 
+    protected void onUserLeaveHint() {
+        super.onUserLeaveHint();
+        sPausedFromUserAction = true;
+    }
+
     private void updateGlobalIcons() {
         boolean searchVisible = false;
         boolean voiceVisible = false;
@@ -692,10 +712,19 @@
     protected void onResume() {
         super.onResume();
 
+        // Restore the previous launcher state
+        if (mOnResumeState == State.WORKSPACE) {
+            showWorkspace(false);
+        } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
+            showAllApps(false);
+        }
+        mOnResumeState = State.NONE;
+
         // Process any items that were added while Launcher was away
         InstallShortcutReceiver.flushInstallQueue(this);
 
         mPaused = false;
+        sPausedFromUserAction = false;
         if (mRestoring || mOnResumeNeedsLoad) {
             mWorkspaceLoading = true;
             mModel.startLoader(true, -1);
@@ -839,7 +868,7 @@
 
         State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
         if (state == State.APPS_CUSTOMIZE) {
-            showAllApps(false);
+            mOnResumeState = State.APPS_CUSTOMIZE;
         }
 
         int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
@@ -1375,7 +1404,14 @@
 
             closeFolder();
             exitSpringLoadedDragMode();
-            showWorkspace(alreadyOnHome);
+
+            // If we are already on home, then just animate back to the workspace, otherwise, just
+            // wait until onResume to set the state back to Workspace
+            if (alreadyOnHome) {
+                showWorkspace(true);
+            } else {
+                mOnResumeState = State.WORKSPACE;
+            }
 
             final View v = getWindow().peekDecorView();
             if (v != null && v.getWindowToken() != null) {