Merge branch 'eclair' into eclair-release
diff --git a/src/com/android/launcher2/Launcher.java b/src/com/android/launcher2/Launcher.java
index cbf09b0..52d89c3e 100644
--- a/src/com/android/launcher2/Launcher.java
+++ b/src/com/android/launcher2/Launcher.java
@@ -589,12 +589,16 @@
 
     @SuppressWarnings({"UnusedDeclaration"})
     public void previousScreen(View v) {
-        mWorkspace.scrollLeft();
+        if (!isAllAppsVisible()) {
+            mWorkspace.scrollLeft();
+        }
     }
 
     @SuppressWarnings({"UnusedDeclaration"})
     public void nextScreen(View v) {
-        mWorkspace.scrollRight();
+        if (!isAllAppsVisible()) {
+            mWorkspace.scrollRight();
+        }
     }
     
     /**
@@ -815,7 +819,7 @@
     }
 
     void closeSystemDialogs() {
-        closeAllApps(false);
+        closeAllApps(true);
         getWindow().closeAllPanels();
 
         try {
@@ -831,6 +835,9 @@
         } catch (Exception e) {
             // An exception is thrown if the dialog is not visible, which is fine
         }
+
+        // Whatever we were doing is hereby canceled.
+        mWaitingForResult = false;
     }
 
     @Override
@@ -839,21 +846,21 @@
 
         // Close the menu
         if (Intent.ACTION_MAIN.equals(intent.getAction())) {
+            // also will cancel mWaitingForResult.
             closeSystemDialogs();
 
-            // Whatever we were doing is hereby canceled.
-            mWaitingForResult = false;
-
             // Set this flag so that onResume knows to close the search dialog if it's open,
             // because this was a new intent (thus a press of 'home' or some such) rather than
             // for example onResume being called when the user pressed the 'back' button.
             mIsNewIntent = true;
 
+            boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
+                        != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
+            boolean allAppsVisible = isAllAppsVisible();
             if (!mWorkspace.isDefaultScreenShowing()) {
-                mWorkspace.moveToDefaultScreen();
+                mWorkspace.moveToDefaultScreen(alreadyOnHome && !allAppsVisible);
             }
-
-            closeAllApps(false);
+            closeAllApps(alreadyOnHome && allAppsVisible);
 
             final View v = getWindow().peekDecorView();
             if (v != null && v.getWindowToken() != null) {
@@ -1318,8 +1325,6 @@
     public boolean dispatchKeyEvent(KeyEvent event) {
         if (event.getAction() == KeyEvent.ACTION_DOWN) {
             switch (event.getKeyCode()) {
-                case KeyEvent.KEYCODE_BACK:
-                    return true;
                 case KeyEvent.KEYCODE_HOME:
                     return true;
                 case KeyEvent.KEYCODE_VOLUME_DOWN:
@@ -1331,18 +1336,6 @@
             }
         } else if (event.getAction() == KeyEvent.ACTION_UP) {
             switch (event.getKeyCode()) {
-                case KeyEvent.KEYCODE_BACK:
-                    if (event.isTracking() && !event.isCanceled()) {
-                        mWorkspace.dispatchKeyEvent(event);
-                        if (isAllAppsVisible()) {
-                            closeAllApps(true);
-                        } else {
-                            closeFolder();
-                        }
-                        dismissPreview(mPreviousView);
-                        dismissPreview(mNextView);
-                    }
-                    return true;
                 case KeyEvent.KEYCODE_HOME:
                     return true;
             }
@@ -1351,6 +1344,17 @@
         return super.dispatchKeyEvent(event);
     }
 
+    @Override
+    public void onBackPressed() {
+        if (isAllAppsVisible()) {
+            closeAllApps(true);
+        } else {
+            closeFolder();
+        }
+        dismissPreview(mPreviousView);
+        dismissPreview(mNextView);
+    }
+
     private void closeFolder() {
         Folder folder = mWorkspace.getOpenFolder();
         if (folder != null) {
@@ -1904,7 +1908,11 @@
         }
 
         private void cleanup() {
-            dismissDialog(DIALOG_CREATE_SHORTCUT);
+            try {
+                dismissDialog(DIALOG_CREATE_SHORTCUT);
+            } catch (Exception e) {
+                // An exception is thrown if the dialog is not visible, which is fine
+            }
         }
 
         /**
diff --git a/src/com/android/launcher2/LauncherModel.java b/src/com/android/launcher2/LauncherModel.java
index 85d47a9..d9017da 100644
--- a/src/com/android/launcher2/LauncherModel.java
+++ b/src/com/android/launcher2/LauncherModel.java
@@ -278,8 +278,6 @@
         ArrayList<ApplicationInfo> added = null;
         ArrayList<ApplicationInfo> removed = null;
         ArrayList<ApplicationInfo> modified = null;
-        boolean update = false;
-        boolean remove = false;
 
         synchronized (mLock) {
             if (mBeforeFirstLoad) {
@@ -298,11 +296,9 @@
 
             if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
                 mAllAppsList.updatePackage(context, packageName);
-                update = true;
             } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
                 if (!replacing) {
                     mAllAppsList.removePackage(packageName);
-                    remove = true;
                 }
                 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
                 // later, we will update the package at this time
@@ -311,7 +307,6 @@
                     mAllAppsList.addPackage(context, packageName);
                 } else {
                     mAllAppsList.updatePackage(context, packageName);
-                    update = true;
                 }
             }
 
@@ -345,7 +340,7 @@
                     }
                 });
             }
-            if (update || modified != null) {
+            if (modified != null) {
                 final ArrayList<ApplicationInfo> modifiedFinal = modified;
                 mHandler.post(new Runnable() {
                     public void run() {
@@ -353,7 +348,7 @@
                     }
                 });
             }
-            if (remove || removed != null) {
+            if (removed != null) {
                 final ArrayList<ApplicationInfo> removedFinal = removed;
                 mHandler.post(new Runnable() {
                     public void run() {
@@ -980,7 +975,9 @@
 
             private void bindAllApps() {
                 synchronized (mLock) {
-                    final ArrayList<ApplicationInfo> results = mAllAppsList.added;
+                    final ArrayList<ApplicationInfo> results
+                            = (ArrayList<ApplicationInfo>)mAllAppsList.data.clone();
+                    // We're adding this now, so clear out this so we don't re-send them.
                     mAllAppsList.added = new ArrayList<ApplicationInfo>();
                     mHandler.post(new Runnable() {
                         public void run() {
diff --git a/src/com/android/launcher2/Workspace.java b/src/com/android/launcher2/Workspace.java
index d0cec8a..ac94cca 100644
--- a/src/com/android/launcher2/Workspace.java
+++ b/src/com/android/launcher2/Workspace.java
@@ -947,6 +947,10 @@
     }
 
     void snapToScreen(int whichScreen) {
+        snapToScreen(whichScreen, true);
+    }
+
+    void snapToScreen(int whichScreen, boolean animate) {
         //if (!mScroller.isFinished()) return;
 
         whichScreen = Math.max(0, Math.min(whichScreen, getChildCount() - 1));
@@ -970,7 +974,8 @@
         final int delta = newX - mScrollX;
         final int duration = screenDelta * 300;
         awakenScrollBars(duration);
-        mScroller.startScroll(mScrollX, 0, delta, 0, duration);
+        // 1ms is close to don't animate
+        mScroller.startScroll(mScrollX, 0, delta, 0, animate ? duration : 1);
         invalidate();
     }
 
@@ -1420,8 +1425,8 @@
         }
     }
 
-    void moveToDefaultScreen() {
-        snapToScreen(mDefaultScreen);
+    void moveToDefaultScreen(boolean animate) {
+        snapToScreen(mDefaultScreen, animate);
         getChildAt(mDefaultScreen).requestFocus();
     }