Merge "Add support for using the lid switch to turn off the screen." into jb-dev
diff --git a/core/res/res/values/config.xml b/core/res/res/values/config.xml
index 472b19c..229f518 100755
--- a/core/res/res/values/config.xml
+++ b/core/res/res/values/config.xml
@@ -287,11 +287,6 @@
          Default value is 2 minutes. -->
     <integer translatable="false" name="config_wifi_driver_stop_delay">120000</integer>
 
-    <!-- Flag indicating whether the keyguard should be bypassed when
-         the slider is open.  This can be set or unset depending how easily
-         the slider can be opened (for example, in a pocket or purse). -->
-    <bool name="config_bypass_keyguard_if_slider_open">true</bool>
-
     <!-- Flag indicating whether the we should enable the automatic brightness in Settings.
          Software implementation will be used if config_hardware_auto_brightness_available is not set -->
     <bool name="config_automatic_brightness_available">false</bool>
@@ -374,11 +369,16 @@
     <integer name="config_lidKeyboardAccessibility">0</integer>
 
     <!-- Indicate whether the lid state impacts the accessibility of
-         the physical keyboard.  0 means it doesn't, 1 means it is accessible
+         the navigation buttons.  0 means it doesn't, 1 means it is accessible
          when the lid is open, 2 means it is accessible when the lid is
          closed.  The default is 0. -->
     <integer name="config_lidNavigationAccessibility">0</integer>
 
+    <!-- Indicate whether closing the lid causes the device to go to sleep and opening
+         it causes the device to wake up.
+         The default is false. -->
+    <bool name="config_lidControlsSleep">false</bool>
+
     <!-- Control the behavior when the user long presses the power button.
             0 - Nothing
             1 - Global actions menu
diff --git a/core/res/res/values/public.xml b/core/res/res/values/public.xml
index f851e62..14584df 100644
--- a/core/res/res/values/public.xml
+++ b/core/res/res/values/public.xml
@@ -1174,12 +1174,13 @@
   <java-symbol type="attr" name="dialogTitleDecorLayout" />
   <java-symbol type="attr" name="dialogTitleIconsDecorLayout" />
   <java-symbol type="bool" name="config_allowAllRotations" />
-  <java-symbol type="bool" name="config_bypass_keyguard_if_slider_open" />
+  <java-symbol type="bool" name="config_annoy_dianne" />
   <java-symbol type="bool" name="config_carDockEnablesAccelerometer" />
   <java-symbol type="bool" name="config_deskDockEnablesAccelerometer" />
   <java-symbol type="bool" name="config_disableMenuKeyInLockScreen" />
   <java-symbol type="bool" name="config_enableLockBeforeUnlockScreen" />
   <java-symbol type="bool" name="config_enableLockScreenRotation" />
+  <java-symbol type="bool" name="config_lidControlsSleep" />
   <java-symbol type="bool" name="config_reverseDefaultRotation" />
   <java-symbol type="bool" name="config_showNavigationBar" />
   <java-symbol type="bool" name="target_honeycomb_needs_options_menu" />
diff --git a/core/tests/overlaytests/OverlayTest/src/com/android/overlaytest/OverlayBaseTest.java b/core/tests/overlaytests/OverlayTest/src/com/android/overlaytest/OverlayBaseTest.java
index 85b49ce..6211c1c 100644
--- a/core/tests/overlaytests/OverlayTest/src/com/android/overlaytest/OverlayBaseTest.java
+++ b/core/tests/overlaytests/OverlayTest/src/com/android/overlaytest/OverlayBaseTest.java
@@ -64,8 +64,8 @@
     }
 
     public void testBoolean() throws Throwable {
-        // config_bypass_keyguard_if_slider_open has no overlay
-        final int resId = com.android.internal.R.bool.config_bypass_keyguard_if_slider_open;
+        // config_annoy_dianne has no overlay
+        final int resId = com.android.internal.R.bool.config_annoy_dianne;
         assertResource(resId, true, true);
     }
 
diff --git a/policy/src/com/android/internal/policy/impl/KeyguardUpdateMonitor.java b/policy/src/com/android/internal/policy/impl/KeyguardUpdateMonitor.java
index d340516..7238fdf 100644
--- a/policy/src/com/android/internal/policy/impl/KeyguardUpdateMonitor.java
+++ b/policy/src/com/android/internal/policy/impl/KeyguardUpdateMonitor.java
@@ -72,8 +72,6 @@
 
     private IccCard.State mSimState = IccCard.State.READY;
 
-    private boolean mKeyguardBypassEnabled;
-
     private boolean mDeviceProvisioned;
 
     private BatteryStatus mBatteryStatus;
@@ -217,9 +215,6 @@
             }
         };
 
-        mKeyguardBypassEnabled = context.getResources().getBoolean(
-                com.android.internal.R.bool.config_bypass_keyguard_if_slider_open);
-
         mDeviceProvisioned = Settings.Secure.getInt(
                 mContext.getContentResolver(), Settings.Secure.DEVICE_PROVISIONED, 0) != 0;
 
@@ -660,10 +655,6 @@
         handleSimStateChange(new SimArgs(IccCard.State.READY));
     }
 
-    public boolean isKeyguardBypassEnabled() {
-        return mKeyguardBypassEnabled;
-    }
-
     public boolean isDevicePluggedIn() {
         return isPluggedIn(mBatteryStatus);
     }
diff --git a/policy/src/com/android/internal/policy/impl/KeyguardViewMediator.java b/policy/src/com/android/internal/policy/impl/KeyguardViewMediator.java
index fec9530..11a1cd6 100644
--- a/policy/src/com/android/internal/policy/impl/KeyguardViewMediator.java
+++ b/policy/src/com/android/internal/policy/impl/KeyguardViewMediator.java
@@ -121,13 +121,6 @@
      */
     protected static final int AWAKE_INTERVAL_DEFAULT_MS = 10000;
 
-
-    /**
-     * The default amount of time we stay awake (used for all key input) when
-     * the keyboard is open
-     */
-    protected static final int AWAKE_INTERVAL_DEFAULT_KEYBOARD_OPEN_MS = 10000;
-
     /**
      * How long to wait after the screen turns off due to timeout before
      * turning on the keyguard (i.e, the user has this much time to turn
@@ -237,8 +230,6 @@
 
     private KeyguardUpdateMonitor mUpdateMonitor;
 
-    private boolean mKeyboardOpen = false;
-
     private boolean mScreenOn = false;
 
     // last known state of the cellular connection
@@ -590,22 +581,6 @@
     }
 
     /**
-     * Returns true if the change is resulting in the keyguard beign dismissed,
-     * meaning the screen can turn on immediately.  Otherwise returns false.
-     */
-    public boolean doLidChangeTq(boolean isLidOpen) {
-        mKeyboardOpen = isLidOpen;
-
-        if (mUpdateMonitor.isKeyguardBypassEnabled() && mKeyboardOpen
-                && !mKeyguardViewProperties.isSecure() && mKeyguardViewManager.isShowing()) {
-            if (DEBUG) Log.d(TAG, "bypassing keyguard on sliding open of keyboard with non-secure keyguard");
-            mHandler.sendEmptyMessage(KEYGUARD_DONE_AUTHENTICATING);
-            return true;
-        }
-        return false;
-    }
-
-    /**
      * Enable the keyguard if the settings are appropriate.  Return true if all
      * work that will happen is done; returns false if the caller can wait for
      * the keyguard to be shown.
@@ -955,8 +930,7 @@
 
     /** {@inheritDoc} */
     public void pokeWakelock() {
-        pokeWakelock(mKeyboardOpen ?
-                AWAKE_INTERVAL_DEFAULT_KEYBOARD_OPEN_MS : AWAKE_INTERVAL_DEFAULT_MS);
+        pokeWakelock(AWAKE_INTERVAL_DEFAULT_MS);
     }
 
     /** {@inheritDoc} */
diff --git a/policy/src/com/android/internal/policy/impl/LockScreen.java b/policy/src/com/android/internal/policy/impl/LockScreen.java
index e8ba21d..dacaeb5 100644
--- a/policy/src/com/android/internal/policy/impl/LockScreen.java
+++ b/policy/src/com/android/internal/policy/impl/LockScreen.java
@@ -69,7 +69,6 @@
     private boolean mEnableRingSilenceFallback = false;
 
     // current configuration state of keyboard and display
-    private int mKeyboardHidden;
     private int mCreationOrientation;
 
     private boolean mSilentMode;
@@ -404,7 +403,6 @@
         mCallback = callback;
         mEnableMenuKeyInLockScreen = shouldEnableMenuKey();
         mCreationOrientation = configuration.orientation;
-        mKeyboardHidden = configuration.hardKeyboardHidden;
 
         if (LockPatternKeyguardView.DEBUG_CONFIGURATION) {
             Log.v(TAG, "***** CREATING LOCK SCREEN", new RuntimeException());
@@ -506,12 +504,6 @@
         Configuration newConfig = getResources().getConfiguration();
         if (newConfig.orientation != mCreationOrientation) {
             mCallback.recreateMe(newConfig);
-        } else if (newConfig.hardKeyboardHidden != mKeyboardHidden) {
-            mKeyboardHidden = newConfig.hardKeyboardHidden;
-            final boolean isKeyboardOpen = mKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO;
-            if (mUpdateMonitor.isKeyguardBypassEnabled() && isKeyboardOpen) {
-                mCallback.goToUnlockScreen();
-            }
         }
     }
 
diff --git a/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java b/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java
index 319eb36..d9a5e5f 100755
--- a/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java
+++ b/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java
@@ -353,6 +353,7 @@
     boolean mDeskDockEnablesAccelerometer;
     int mLidKeyboardAccessibility;
     int mLidNavigationAccessibility;
+    boolean mLidControlsSleep;
     int mLongPressOnPowerBehavior = -1;
     boolean mScreenOnEarly = false;
     boolean mScreenOnFully = false;
@@ -897,6 +898,8 @@
                 com.android.internal.R.integer.config_lidKeyboardAccessibility);
         mLidNavigationAccessibility = mContext.getResources().getInteger(
                 com.android.internal.R.integer.config_lidNavigationAccessibility);
+        mLidControlsSleep = mContext.getResources().getBoolean(
+                com.android.internal.R.bool.config_lidControlsSleep);
         // register for dock events
         IntentFilter filter = new IntentFilter();
         filter.addAction(UiModeManager.ACTION_ENTER_CAR_MODE);
@@ -1274,10 +1277,15 @@
         return visibleValue;
     }
 
+    private boolean isKeyboardVisible() {
+        return determineHiddenState(mLidKeyboardAccessibility, 0, 1) == 1
+                || determineHiddenState(mLidNavigationAccessibility, 0, 1) == 1;
+    }
+
     /** {@inheritDoc} */
     public void adjustConfigurationLw(Configuration config) {
         readLidState();
-        updateKeyboardVisibility();
+        applyLidSwitchState();
 
         if (config.keyboard == Configuration.KEYBOARD_NOKEYS) {
             config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_YES;
@@ -2852,33 +2860,26 @@
         if (mHeadless) return;
 
         // lid changed state
-        mLidState = lidOpen ? LID_OPEN : LID_CLOSED;
-        updateKeyboardVisibility();
+        final int newLidState = lidOpen ? LID_OPEN : LID_CLOSED;
+        if (newLidState == mLidState) {
+            return;
+        }
 
-        boolean awakeNow = mKeyguardMediator.doLidChangeTq(lidOpen);
+        mLidState = newLidState;
+        applyLidSwitchState();
         updateRotation(true);
-        if (awakeNow) {
-            // If the lid is opening and we don't have to keep the
-            // keyguard up, then we can turn on the screen
-            // immediately.
-            mKeyguardMediator.pokeWakelock();
-        } else if (keyguardIsShowingTq()) {
-            if (lidOpen) {
-                // If we are opening the lid and not hiding the
-                // keyguard, then we need to have it turn on the
-                // screen once it is shown.
+
+        if (lidOpen) {
+            if (keyguardIsShowingTq()) {
                 mKeyguardMediator.onWakeKeyWhenKeyguardShowingTq(
                         KeyEvent.KEYCODE_POWER, mDockMode != Intent.EXTRA_DOCK_STATE_UNDOCKED);
-            }
-        } else {
-            // Light up the keyboard if we are sliding up.
-            if (lidOpen) {
-                mPowerManager.userActivity(SystemClock.uptimeMillis(), false,
-                        LocalPowerManager.BUTTON_EVENT);
             } else {
                 mPowerManager.userActivity(SystemClock.uptimeMillis(), false,
-                        LocalPowerManager.OTHER_EVENT);
+                        LocalPowerManager.BUTTON_EVENT);
             }
+        } else if (!mLidControlsSleep) {
+            mPowerManager.userActivity(SystemClock.uptimeMillis(), false,
+                    LocalPowerManager.OTHER_EVENT);
         }
     }
 
@@ -3927,13 +3928,15 @@
     /** {@inheritDoc} */
     public void enableScreenAfterBoot() {
         readLidState();
-        updateKeyboardVisibility();
-
+        applyLidSwitchState();
         updateRotation(true);
     }
 
-    private void updateKeyboardVisibility() {
-        mPowerManager.setKeyboardVisibility(mLidState == LID_OPEN);
+    private void applyLidSwitchState() {
+        mPowerManager.setKeyboardVisibility(isKeyboardVisible());
+        if (mLidState == LID_CLOSED && mLidControlsSleep) {
+            mPowerManager.goToSleep(SystemClock.uptimeMillis());
+        }
     }
 
     void updateRotation(boolean alwaysSendConfiguration) {
@@ -4219,6 +4222,7 @@
         pw.print(prefix); pw.print("mLidKeyboardAccessibility=");
                 pw.print(mLidKeyboardAccessibility);
                 pw.print(" mLidNavigationAccessibility="); pw.print(mLidNavigationAccessibility);
+                pw.print(" mLidControlsSleep="); pw.print(mLidControlsSleep);
                 pw.print(" mLongPressOnPowerBehavior="); pw.println(mLongPressOnPowerBehavior);
         pw.print(prefix); pw.print("mScreenOnEarly="); pw.print(mScreenOnEarly);
                 pw.print(" mScreenOnFully="); pw.print(mScreenOnFully);
diff --git a/policy/src/com/android/internal/policy/impl/SimPukUnlockScreen.java b/policy/src/com/android/internal/policy/impl/SimPukUnlockScreen.java
index f3e7d4a..3b2a473 100644
--- a/policy/src/com/android/internal/policy/impl/SimPukUnlockScreen.java
+++ b/policy/src/com/android/internal/policy/impl/SimPukUnlockScreen.java
@@ -304,11 +304,6 @@
             mCallback.recreateMe(newConfig);
         } else if (newConfig.hardKeyboardHidden != mKeyboardHidden) {
             mKeyboardHidden = newConfig.hardKeyboardHidden;
-            final boolean isKeyboardOpen =
-                (mKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO);
-            if (mUpdateMonitor.isKeyguardBypassEnabled() && isKeyboardOpen) {
-                mCallback.goToUnlockScreen();
-            }
         }
 
     }
diff --git a/policy/src/com/android/internal/policy/impl/SimUnlockScreen.java b/policy/src/com/android/internal/policy/impl/SimUnlockScreen.java
index 9604cdc..80407f5 100644
--- a/policy/src/com/android/internal/policy/impl/SimUnlockScreen.java
+++ b/policy/src/com/android/internal/policy/impl/SimUnlockScreen.java
@@ -282,10 +282,6 @@
             mCallback.recreateMe(newConfig);
         } else if (newConfig.hardKeyboardHidden != mKeyboardHidden) {
             mKeyboardHidden = newConfig.hardKeyboardHidden;
-            final boolean isKeyboardOpen = mKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO;
-            if (mUpdateMonitor.isKeyguardBypassEnabled() && isKeyboardOpen) {
-                mCallback.goToUnlockScreen();
-            }
         }
     }
 
diff --git a/services/java/com/android/server/PowerManagerService.java b/services/java/com/android/server/PowerManagerService.java
index 9a371c6..bd50e22 100644
--- a/services/java/com/android/server/PowerManagerService.java
+++ b/services/java/com/android/server/PowerManagerService.java
@@ -2521,7 +2521,8 @@
             return val;
         } catch (Exception e) {
             // guard against null pointer or index out of bounds errors
-            Slog.e(TAG, "getAutoBrightnessValue", e);
+            Slog.e(TAG, "Values array must be non-empty and must be the same length "
+                    + "as the auto-brightness levels array.  Check config.xml.", e);
             return 255;
         }
     }
diff --git a/services/java/com/android/server/input/InputManagerService.java b/services/java/com/android/server/input/InputManagerService.java
index 189a9c7..aece7d25 100644
--- a/services/java/com/android/server/input/InputManagerService.java
+++ b/services/java/com/android/server/input/InputManagerService.java
@@ -199,6 +199,15 @@
     /** The key is down but is a virtual key press that is being emulated by the system. */
     public static final int KEY_STATE_VIRTUAL = 2;
 
+    /** Scan code: Mouse / trackball button. */
+    public static final int BTN_MOUSE = 0x110;
+
+    /** Switch code: Lid switch.  When set, lid is shut. */
+    public static final int SW_LID = 0x00;
+
+    /** Switch code: Keypad slide.  When set, keyboard is exposed. */
+    public static final int SW_KEYPAD_SLIDE = 0x0a;
+
     public InputManagerService(Context context, Callbacks callbacks) {
         this.mContext = context;
         this.mCallbacks = callbacks;
diff --git a/services/java/com/android/server/wm/WindowManagerService.java b/services/java/com/android/server/wm/WindowManagerService.java
index 16aeb35..28fca69 100755
--- a/services/java/com/android/server/wm/WindowManagerService.java
+++ b/services/java/com/android/server/wm/WindowManagerService.java
@@ -4964,8 +4964,8 @@
     // Called by window manager policy. Not exposed externally.
     @Override
     public int getLidState() {
-        final int SW_LID = 0x00;
-        int sw = mInputManager.getSwitchState(-1, InputDevice.SOURCE_ANY, SW_LID);
+        int sw = mInputManager.getSwitchState(-1, InputDevice.SOURCE_ANY,
+                InputManagerService.SW_LID);
         if (sw > 0) {
             // Switch state: AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL.
             return LID_CLOSED;
@@ -6511,14 +6511,13 @@
                    + " milliseconds before attempting to detect safe mode.");
         }
 
-        final int BTN_MOUSE = 0x110;
         int menuState = mInputManager.getKeyCodeState(-1, InputDevice.SOURCE_ANY,
                 KeyEvent.KEYCODE_MENU);
         int sState = mInputManager.getKeyCodeState(-1, InputDevice.SOURCE_ANY, KeyEvent.KEYCODE_S);
         int dpadState = mInputManager.getKeyCodeState(-1, InputDevice.SOURCE_DPAD,
                 KeyEvent.KEYCODE_DPAD_CENTER);
         int trackballState = mInputManager.getScanCodeState(-1, InputDevice.SOURCE_TRACKBALL,
-                BTN_MOUSE);
+                InputManagerService.BTN_MOUSE);
         int volumeDownState = mInputManager.getKeyCodeState(-1, InputDevice.SOURCE_ANY,
                 KeyEvent.KEYCODE_VOLUME_DOWN);
         mSafeMode = menuState > 0 || sState > 0 || dpadState > 0 || trackballState > 0