Fix bug 2250432 - all apps should dismiss when you power off or phone locks

Also cherry-picks this from launcher 1

Fix issue #2133206: dialogs/menus should auto-dismiss when screen turns off

Close things out in various ways.  This should be done for Launcher2 as well.

Change-Id: Id4f1c78e35180b437144c54ddcbf10069cc09071

Conflicts:

	AndroidManifest.xml
	src/com/android/launcher2/Launcher.java
diff --git a/src/com/android/launcher2/Launcher.java b/src/com/android/launcher2/Launcher.java
index f718c5b..6a0978b 100644
--- a/src/com/android/launcher2/Launcher.java
+++ b/src/com/android/launcher2/Launcher.java
@@ -25,12 +25,14 @@
 import android.app.WallpaperInfo;
 import android.app.WallpaperManager;
 import android.content.ActivityNotFoundException;
+import android.content.BroadcastReceiver;
 import android.content.ComponentName;
 import android.content.ContentResolver;
 import android.content.Context;
 import android.content.DialogInterface;
 import android.content.Intent;
 import android.content.Intent.ShortcutIconResource;
+import android.content.IntentFilter;
 import android.content.pm.ActivityInfo;
 import android.content.pm.LabeledIntent;
 import android.content.pm.PackageManager;
@@ -159,6 +161,8 @@
     private static final Object sLock = new Object();
     private static int sScreen = DEFAULT_SCREEN;
 
+    private final BroadcastReceiver mCloseSystemDialogsReceiver
+            = new CloseSystemDialogsIntentReceiver();
     private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
 
     private LayoutInflater mInflater;
@@ -211,6 +215,9 @@
         mDragController = new DragController(this);
         mInflater = getLayoutInflater();
 
+        IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
+        registerReceiver(mCloseSystemDialogsReceiver, filter);
+
         mAppWidgetManager = AppWidgetManager.getInstance(this);
         mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
         mAppWidgetHost.startListening();
@@ -814,13 +821,32 @@
         return info;
     }
 
+    void closeSystemDialogs() {
+        closeAllApps(false);
+        getWindow().closeAllPanels();
+
+        try {
+            dismissDialog(DIALOG_CREATE_SHORTCUT);
+            // Unlock the workspace if the dialog was showing
+        } catch (Exception e) {
+            // An exception is thrown if the dialog is not visible, which is fine
+        }
+
+        try {
+            dismissDialog(DIALOG_RENAME_FOLDER);
+            // Unlock the workspace if the dialog was showing
+        } catch (Exception e) {
+            // An exception is thrown if the dialog is not visible, which is fine
+        }
+    }
+
     @Override
     protected void onNewIntent(Intent intent) {
         super.onNewIntent(intent);
 
         // Close the menu
         if (Intent.ACTION_MAIN.equals(intent.getAction())) {
-            getWindow().closeAllPanels();
+            closeSystemDialogs();
 
             // Whatever we were doing is hereby canceled.
             mWaitingForResult = false;
@@ -830,20 +856,6 @@
             // for example onResume being called when the user pressed the 'back' button.
             mIsNewIntent = true;
 
-            try {
-                dismissDialog(DIALOG_CREATE_SHORTCUT);
-                // Unlock the workspace if the dialog was showing
-            } catch (Exception e) {
-                // An exception is thrown if the dialog is not visible, which is fine
-            }
-
-            try {
-                dismissDialog(DIALOG_RENAME_FOLDER);
-                // Unlock the workspace if the dialog was showing
-            } catch (Exception e) {
-                // An exception is thrown if the dialog is not visible, which is fine
-            }
-
             if ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) !=
                     Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) {
 
@@ -939,6 +951,8 @@
         
         dismissPreview(mPreviousView);
         dismissPreview(mNextView);
+
+        unregisterReceiver(mCloseSystemDialogsReceiver);
     }
 
     @Override
@@ -1305,6 +1319,10 @@
         startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
     }
 
+    /**
+     * Registers various content observers. The current implementation registers
+     * only a favorites observer to keep track of the favorites applications.
+     */
     private void registerContentObservers() {
         ContentResolver resolver = getContentResolver();
         resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
@@ -1954,6 +1972,17 @@
     }
 
     /**
+     * Receives notifications when applications are added/removed.
+     */
+    private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
+        @Override
+        public void onReceive(Context context, Intent intent) {
+            Log.d(TAG, "CloseSystemDialogsIntentReceiver.onReceiver intent=" + intent);
+            closeSystemDialogs();
+        }
+    }
+
+    /**
      * Receives notifications whenever the appwidgets are reset.
      */
     private class AppWidgetResetObserver extends ContentObserver {