Get rid of the global actions menu on tablets.

We have the system tray for that stuff.  Make long press on power
go directly to the shut down confirmation dialog.  Make that dialog
auto dismiss when the screen goes off.  Reword it when we're skipping
the global actions menu, because it feels too abrupt to say "The
phone will shut down" when they haven't clearly pressed a button
that says shut down.

Change-Id: I8c92a1362bd4ed242e6cd94c2dae6d9ff9c4c34d
diff --git a/core/java/com/android/internal/app/ShutdownThread.java b/core/java/com/android/internal/app/ShutdownThread.java
index e07c54f..8104ece 100644
--- a/core/java/com/android/internal/app/ShutdownThread.java
+++ b/core/java/com/android/internal/app/ShutdownThread.java
@@ -18,15 +18,17 @@
 package com.android.internal.app;
 
 import android.app.ActivityManagerNative;
+import android.app.AlertDialog;
+import android.app.Dialog;
 import android.app.IActivityManager;
 import android.app.ProgressDialog;
-import android.app.AlertDialog;
 import android.bluetooth.BluetoothAdapter;
 import android.bluetooth.IBluetooth;
 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.DialogInterface;
 import android.content.Intent;
+import android.content.IntentFilter;
 import android.os.Handler;
 import android.os.Power;
 import android.os.PowerManager;
@@ -91,13 +93,20 @@
             }
         }
 
-        Log.d(TAG, "Notifying thread to start radio shutdown");
+        final int longPressBehavior = context.getResources().getInteger(
+                        com.android.internal.R.integer.config_longPressOnPowerBehavior);
+        final int resourceId = longPressBehavior == 2
+                ? com.android.internal.R.string.shutdown_confirm_question
+                : com.android.internal.R.string.shutdown_confirm;
+
+        Log.d(TAG, "Notifying thread to start shutdown longPressBehavior=" + longPressBehavior);
 
         if (confirm) {
+            final CloseDialogReceiver closer = new CloseDialogReceiver(context);
             final AlertDialog dialog = new AlertDialog.Builder(context)
                     .setIcon(android.R.drawable.ic_dialog_alert)
                     .setTitle(com.android.internal.R.string.power_off)
-                    .setMessage(com.android.internal.R.string.shutdown_confirm)
+                    .setMessage(resourceId)
                     .setPositiveButton(com.android.internal.R.string.yes, new DialogInterface.OnClickListener() {
                         public void onClick(DialogInterface dialog, int which) {
                             beginShutdownSequence(context);
@@ -105,6 +114,8 @@
                     })
                     .setNegativeButton(com.android.internal.R.string.no, null)
                     .create();
+            closer.dialog = dialog;
+            dialog.setOnDismissListener(closer);
             dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
             if (!context.getResources().getBoolean(
                     com.android.internal.R.bool.config_sf_slowBlur)) {
@@ -116,6 +127,27 @@
         }
     }
 
+    private static class CloseDialogReceiver extends BroadcastReceiver
+            implements DialogInterface.OnDismissListener {
+        private Context mContext;
+        public Dialog dialog;
+
+        CloseDialogReceiver(Context context) {
+            mContext = context;
+            IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
+            context.registerReceiver(this, filter);
+        }
+
+        @Override
+        public void onReceive(Context context, Intent intent) {
+            dialog.cancel();
+        }
+
+        public void onDismiss(DialogInterface unused) {
+            mContext.unregisterReceiver(this);
+        }
+    }
+
     /**
      * Request a clean shutdown, waiting for subsystems to clean up their
      * state etc.  Must be called from a Looper thread in which its UI
diff --git a/core/res/res/values-xlarge/config.xml b/core/res/res/values-xlarge/config.xml
index 7e5a27b..813651e 100644
--- a/core/res/res/values-xlarge/config.xml
+++ b/core/res/res/values-xlarge/config.xml
@@ -25,6 +25,9 @@
     <string name="config_statusBarComponent">com.android.systemui/com.android.systemui.statusbar.tablet.TabletStatusBarService</string>
     <bool name="config_statusBarCanHide">false</bool>
 
+    <!-- see comment in values/config.xml -->
+    <integer name="config_longPressOnPowerBehavior">2</integer>
+
     <!-- Show sliding tab before lockscreen -->
     <bool name="config_enableSlidingTabFirst">false</bool>
     <!-- Enable lockscreen rotation -->
diff --git a/core/res/res/values/config.xml b/core/res/res/values/config.xml
index 71967d4..d353db6 100644
--- a/core/res/res/values/config.xml
+++ b/core/res/res/values/config.xml
@@ -221,6 +221,13 @@
          closed.  The default is 0. -->
     <integer name="config_lidNavigationAccessibility">0</integer>
 
+    <!-- Control the behavior when the user long presses the power button.
+            0 - Nothing
+            1 - Global actions menu
+            2 - Power off (with confirmation)
+    -->
+    <integer name="config_longPressOnPowerBehavior">1</integer>
+
     <!-- Indicate whether the SD card is accessible without removing the battery. -->
     <bool name="config_batterySdCardAccessibility">false</bool>
 
diff --git a/core/res/res/values/strings.xml b/core/res/res/values/strings.xml
index 521a739..0172827 100755
--- a/core/res/res/values/strings.xml
+++ b/core/res/res/values/strings.xml
@@ -280,9 +280,16 @@
     <!-- Shutdown Progress Dialog. This is shown if the user chooses to power off the phone. -->
     <string name="shutdown_progress">Shutting down\u2026</string>
 
-    <!-- Shutdown Confirmation Dialog.  When the user chooses to power off the phone, there will be a confirmation dialog.  This is the message. -->
+    <!-- Shutdown Confirmation Dialog.  When the user chooses to power off the phone, there will
+         be a confirmation dialog.  This is the message. -->
     <string name="shutdown_confirm">Your phone will shut down.</string>
 
+    <!-- Shutdown Confirmation Dialog.  When the user chooses to power off the phone, it asks
+         the user if they'd like to shut down.  This is the message.  This is used instead of
+         shutdown_confirm when the system is configured to use long press to go directly to the
+         power off dialog instead of the global actions menu. -->
+    <string name="shutdown_confirm_question">Would you like to shut down?</string>
+
     <!-- Recent Tasks dialog: title
      TODO: this should move to SystemUI.apk, but the code for the old 
             recent dialog is still in the framework
diff --git a/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java b/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java
index 33685ba..68e0e32 100755
--- a/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java
+++ b/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java
@@ -47,6 +47,7 @@
 import android.provider.Settings;
 
 import com.android.internal.R;
+import com.android.internal.app.ShutdownThread;
 import com.android.internal.policy.PolicyManager;
 import com.android.internal.statusbar.IStatusBarService;
 import com.android.internal.telephony.ITelephony;
@@ -128,6 +129,10 @@
     static final boolean DEBUG_LAYOUT = false;
     static final boolean SHOW_STARTING_ANIMATIONS = true;
     static final boolean SHOW_PROCESSES_ON_ALT_MENU = false;
+
+    static final int LONG_PRESS_POWER_NOTHING = 0;
+    static final int LONG_PRESS_POWER_GLOBAL_ACTIONS = 1;
+    static final int LONG_PRESS_POWER_SHUT_OFF = 2;
     
     // wallpaper is at the bottom, though the window manager may move it.
     static final int WALLPAPER_LAYER = 2;
@@ -224,6 +229,7 @@
     boolean mDeskDockEnablesAccelerometer;
     int mLidKeyboardAccessibility;
     int mLidNavigationAccessibility;
+    int mLongPressOnPowerBehavior = -1;
     boolean mScreenOn = false;
     boolean mOrientationSensorEnabled = false;
     int mCurrentAppOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
@@ -467,10 +473,27 @@
 
     Runnable mPowerLongPress = new Runnable() {
         public void run() {
-            mShouldTurnOffOnKeyUp = false;
-            performHapticFeedbackLw(null, HapticFeedbackConstants.LONG_PRESS, false);
-            sendCloseSystemWindows(SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS);
-            showGlobalActionsDialog();
+            // The context isn't read
+            if (mLongPressOnPowerBehavior < 0) {
+                mLongPressOnPowerBehavior = mContext.getResources().getInteger(
+                        com.android.internal.R.integer.config_longPressOnPowerBehavior);
+            }
+            switch (mLongPressOnPowerBehavior) {
+            case LONG_PRESS_POWER_NOTHING:
+                break;
+            case LONG_PRESS_POWER_GLOBAL_ACTIONS:
+                mShouldTurnOffOnKeyUp = false;
+                performHapticFeedbackLw(null, HapticFeedbackConstants.LONG_PRESS, false);
+                sendCloseSystemWindows(SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS);
+                showGlobalActionsDialog();
+                break;
+            case LONG_PRESS_POWER_SHUT_OFF:
+                mShouldTurnOffOnKeyUp = false;
+                performHapticFeedbackLw(null, HapticFeedbackConstants.LONG_PRESS, false);
+                sendCloseSystemWindows(SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS);
+                ShutdownThread.shutdown(mContext, true);
+                break;
+            }
         }
     };
 
diff --git a/policy/src/com/android/internal/policy/impl/PowerDialog.java b/policy/src/com/android/internal/policy/impl/PowerDialog.java
deleted file mode 100644
index de35bd7..0000000
--- a/policy/src/com/android/internal/policy/impl/PowerDialog.java
+++ /dev/null
@@ -1,182 +0,0 @@
-/*
- * Copyright (C) 2007 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.android.internal.policy.impl;
-
-import com.android.internal.R;
-
-import android.app.Dialog;
-import android.app.StatusBarManager;
-import android.content.Context;
-import android.os.Bundle;
-import android.os.RemoteException;
-import android.os.LocalPowerManager;
-import android.os.ServiceManager;
-import android.os.SystemClock;
-
-import com.android.internal.app.ShutdownThread;
-import com.android.internal.telephony.ITelephony;
-import android.view.KeyEvent;
-import android.util.Log;
-import android.view.View;
-import android.view.WindowManager;
-import android.view.View.OnClickListener;
-import android.view.View.OnKeyListener;
-import android.widget.Button;
-
-/**
- * @deprecated use {@link GlobalActions} instead.
- */
-public class PowerDialog extends Dialog implements OnClickListener,
-        OnKeyListener {
-    private static final String TAG = "PowerDialog";
-
-    static private StatusBarManager sStatusBar;
-    private Button mKeyguard;
-    private Button mPower;
-    private Button mRadioPower;
-    private Button mSilent;
-
-    private LocalPowerManager mPowerManager;
-
-    public PowerDialog(Context context, LocalPowerManager powerManager) {
-        super(context);
-        mPowerManager = powerManager;
-    }
-
-    @Override
-    protected void onCreate(Bundle savedInstanceState) {
-        super.onCreate(savedInstanceState);
-
-        Context context = getContext();
-
-        if (sStatusBar == null) {
-            sStatusBar = (StatusBarManager)context.getSystemService(Context.STATUS_BAR_SERVICE);
-        }
-
-        setContentView(com.android.internal.R.layout.power_dialog);
-
-        getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG);
-        if (!getContext().getResources().getBoolean(
-                com.android.internal.R.bool.config_sf_slowBlur)) {
-            getWindow().setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,
-                    WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
-        }
-        getWindow().setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,
-                WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
-
-        setTitle(context.getText(R.string.power_dialog));
-
-        mKeyguard = (Button) findViewById(R.id.keyguard);
-        mPower = (Button) findViewById(R.id.off);
-        mRadioPower = (Button) findViewById(R.id.radio_power);
-        mSilent = (Button) findViewById(R.id.silent);
-
-        if (mKeyguard != null) {
-            mKeyguard.setOnKeyListener(this);
-            mKeyguard.setOnClickListener(this);
-        }
-        if (mPower != null) {
-            mPower.setOnClickListener(this);
-        }
-        if (mRadioPower != null) {
-            mRadioPower.setOnClickListener(this);
-        }
-        if (mSilent != null) {
-            mSilent.setOnClickListener(this);
-            // XXX: HACK for now hide the silent until we get mute support
-            mSilent.setVisibility(View.GONE);
-        }
-
-        CharSequence text;
-
-        // set the keyguard button's text
-        text = context.getText(R.string.screen_lock);
-        mKeyguard.setText(text);
-        mKeyguard.requestFocus();
-
-        try {
-            ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
-            if (phone != null) {
-                text = phone.isRadioOn() ? context
-                        .getText(R.string.turn_off_radio) : context
-                        .getText(R.string.turn_on_radio);
-            }
-        } catch (RemoteException ex) {
-            // ignore it
-        }
-
-        mRadioPower.setText(text);
-    }
-
-    public void onClick(View v) {
-        this.dismiss();
-        if (v == mPower) {
-            // shutdown by making sure radio and power are handled accordingly.
-            ShutdownThread.shutdown(getContext(), true);
-        } else if (v == mRadioPower) {
-            try {
-                ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
-                if (phone != null) {
-                    phone.toggleRadioOnOff();
-                }
-            } catch (RemoteException ex) {
-                // ignore it
-            }
-        } else if (v == mSilent) {
-            // do something
-        } else if (v == mKeyguard) {
-            if (v.isInTouchMode()) {
-                // only in touch mode for the reasons explained in onKey.
-                this.dismiss();
-                mPowerManager.goToSleep(SystemClock.uptimeMillis() + 1);
-            }
-        }
-    }
-
-    public boolean onKey(View v, int keyCode, KeyEvent event) {
-        // The activate keyguard button needs to put the device to sleep on the
-        // key up event. If we try to put it to sleep on the click or down
-        // action
-        // the the up action will cause the device to wake back up.
-
-        // Log.i(TAG, "keyCode: " + keyCode + " action: " + event.getAction());
-        if (keyCode != KeyEvent.KEYCODE_DPAD_CENTER
-                || event.getAction() != KeyEvent.ACTION_UP) {
-            // Log.i(TAG, "getting out of dodge...");
-            return false;
-        }
-
-        // Log.i(TAG, "Clicked mKeyguard! dimissing dialog");
-        this.dismiss();
-        // Log.i(TAG, "onKey: turning off the screen...");
-        // XXX: This is a hack for now
-        mPowerManager.goToSleep(event.getEventTime() + 1);
-        return true;
-    }
-
-    public void show() {
-        super.show();
-        Log.d(TAG, "show... disabling expand");
-        sStatusBar.disable(StatusBarManager.DISABLE_EXPAND);
-    }
-
-    public void dismiss() {
-        super.dismiss();
-        Log.d(TAG, "dismiss... reenabling expand");
-        sStatusBar.disable(StatusBarManager.DISABLE_NONE);
-    }
-}