Allow dock keep screen on to select between AC and USB power.

Change-Id: I2309992f5ad7b93006eb52f31758f71c48ed98f1
diff --git a/policy/com/android/internal/policy/impl/KeyguardViewMediator.java b/policy/com/android/internal/policy/impl/KeyguardViewMediator.java
index adcd86b..0cb69e3 100644
--- a/policy/com/android/internal/policy/impl/KeyguardViewMediator.java
+++ b/policy/com/android/internal/policy/impl/KeyguardViewMediator.java
@@ -718,10 +718,15 @@
      * @see #handleKeyguardDone 
      */
     public void keyguardDone(boolean authenticated) {
+        keyguardDone(authenticated, true);
+    }
+
+    public void keyguardDone(boolean authenticated, boolean wakeup) {
         synchronized (this) {
             EventLog.writeEvent(70000, 2);       
             if (DEBUG) Log.d(TAG, "keyguardDone(" + authenticated + ")");
             Message msg = mHandler.obtainMessage(KEYGUARD_DONE);
+            msg.arg1 = wakeup ? 1 : 0;
             mHandler.sendMessage(msg);
 
             if (authenticated) {
@@ -788,7 +793,7 @@
                     handleWakeWhenReady(msg.arg1);
                     return;
                 case KEYGUARD_DONE:
-                    handleKeyguardDone();
+                    handleKeyguardDone(msg.arg1 != 0);
                     return;
                 case KEYGUARD_DONE_DRAWING:
                     handleKeyguardDoneDrawing();
@@ -804,10 +809,12 @@
      * @see #keyguardDone
      * @see #KEYGUARD_DONE
      */
-    private void handleKeyguardDone() {
+    private void handleKeyguardDone(boolean wakeup) {
         if (DEBUG) Log.d(TAG, "handleKeyguardDone");
         handleHide();
-        mPM.userActivity(SystemClock.uptimeMillis(), true);
+        if (wakeup) {
+            mPM.userActivity(SystemClock.uptimeMillis(), true);
+        }
         mWakeLock.release();
         mContext.sendBroadcast(mUserPresentIntent);
     }
diff --git a/policy/com/android/internal/policy/impl/PhoneWindowManager.java b/policy/com/android/internal/policy/impl/PhoneWindowManager.java
index 59283a2..b5d41d8 100644
--- a/policy/com/android/internal/policy/impl/PhoneWindowManager.java
+++ b/policy/com/android/internal/policy/impl/PhoneWindowManager.java
@@ -177,14 +177,16 @@
     RecentApplicationsDialog mRecentAppsDialog;
     Handler mHandler;
 
+    final IntentFilter mBatteryStatusFilter = new IntentFilter();
+    
     boolean mLidOpen;
-    boolean mPlugged;
+    int mPlugged;
     int mDockState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
     int mLidOpenRotation;
     int mCarDockRotation;
     int mDeskDockRotation;
-    boolean mCarDockKeepsScreenOn;
-    boolean mDeskDockKeepsScreenOn;
+    int mCarDockKeepsScreenOn;
+    int mDeskDockKeepsScreenOn;
     boolean mCarDockEnablesAccelerometer;
     boolean mDeskDockEnablesAccelerometer;
     int mLidKeyboardAccessibility;
@@ -308,7 +310,7 @@
         @Override
         public void onOrientationChanged(int rotation) {
             // Send updates based on orientation value
-            if (true) Log.i(TAG, "onOrientationChanged, rotation changed to " +rotation);
+            if (localLOGV) Log.v(TAG, "onOrientationChanged, rotation changed to " +rotation);
             try {
                 mWindowManager.setRotation(rotation, false,
                         mFancyRotationAnimation);
@@ -393,7 +395,7 @@
         }
         //Could have been invoked due to screen turning on or off or
         //change of the currently visible window's orientation
-        if (localLOGV) Log.i(TAG, "Screen status="+mScreenOn+
+        if (localLOGV) Log.v(TAG, "Screen status="+mScreenOn+
                 ", current orientation="+mCurrentAppOrientation+
                 ", SensorEnabled="+mOrientationSensorEnabled);
         boolean disable = true;
@@ -403,7 +405,7 @@
                 //enable listener if not already enabled
                 if (!mOrientationSensorEnabled) {
                     mOrientationListener.enable();
-                    if(localLOGV) Log.i(TAG, "Enabling listeners");
+                    if(localLOGV) Log.v(TAG, "Enabling listeners");
                     mOrientationSensorEnabled = true;
                 }
             } 
@@ -411,7 +413,7 @@
         //check if sensors need to be disabled
         if (disable && mOrientationSensorEnabled) {
             mOrientationListener.disable();
-            if(localLOGV) Log.i(TAG, "Disabling listeners");
+            if(localLOGV) Log.v(TAG, "Disabling listeners");
             mOrientationSensorEnabled = false;
         }
     }
@@ -507,10 +509,10 @@
                 com.android.internal.R.integer.config_carDockRotation);
         mDeskDockRotation = readRotation(
                 com.android.internal.R.integer.config_deskDockRotation);
-        mCarDockKeepsScreenOn = mContext.getResources().getBoolean(
-                com.android.internal.R.bool.config_carDockKeepsScreenOn);
-        mDeskDockKeepsScreenOn = mContext.getResources().getBoolean(
-                com.android.internal.R.bool.config_deskDockKeepsScreenOn);
+        mCarDockKeepsScreenOn = mContext.getResources().getInteger(
+                com.android.internal.R.integer.config_carDockKeepsScreenOn);
+        mDeskDockKeepsScreenOn = mContext.getResources().getInteger(
+                com.android.internal.R.integer.config_deskDockKeepsScreenOn);
         mCarDockEnablesAccelerometer = mContext.getResources().getBoolean(
                 com.android.internal.R.bool.config_carDockEnablesAccelerometer);
         mDeskDockEnablesAccelerometer = mContext.getResources().getBoolean(
@@ -524,16 +526,22 @@
         intentFilter.addAction(Intent.ACTION_POWER_CONNECTED);
         intentFilter.addAction(Intent.ACTION_POWER_DISCONNECTED);
         context.registerReceiver(mPowerReceiver, intentFilter);
-        Intent powerIntent = context.registerReceiver(null,
-                new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
-        mPlugged = false;
-        if (powerIntent != null) {
-            mPlugged = powerIntent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0) != 0;
-        }
+        mBatteryStatusFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
+        mPlugged = 0;
+        updatePlugged();
         // register for dock events
         context.registerReceiver(mDockReceiver, new IntentFilter(Intent.ACTION_DOCK_EVENT));
     }
 
+    void updatePlugged() {
+        Intent powerIntent = mContext.registerReceiver(null, mBatteryStatusFilter);
+        if (localLOGV) Log.v(TAG, "New battery status: " + powerIntent.getExtras());
+        if (powerIntent != null) {
+            mPlugged = powerIntent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0);
+            if (localLOGV) Log.v(TAG, "PLUGGED: " + mPlugged);
+        }
+    }
+    
     private int readRotation(int resID) {
         try {
             int rotation = mContext.getResources().getInteger(resID);
@@ -1333,12 +1341,12 @@
                 if (DEBUG_LAYOUT) Log.v(TAG, "Fullscreen window: " + win);
                 mTopFullscreenOpaqueWindowState = win;
                 if ((attrs.flags & FLAG_SHOW_WHEN_LOCKED) != 0) {
-                    if (localLOGV) Log.i(TAG, "Setting mHideLockScreen to true by win " + win);
+                    if (localLOGV) Log.v(TAG, "Setting mHideLockScreen to true by win " + win);
                     mHideLockScreen = true;
                 }
             }
             if ((attrs.flags & FLAG_DISMISS_KEYGUARD) != 0) {
-                if (localLOGV) Log.i(TAG, "Setting mDismissKeyguard to true by win " + win);
+                if (localLOGV) Log.v(TAG, "Setting mDismissKeyguard to true by win " + win);
                 mDismissKeyguard = true;
             }
         }
@@ -1393,22 +1401,21 @@
         // Hide the key guard if a visible window explicitly specifies that it wants to be displayed
         // when the screen is locked
         if (mKeyguard != null) {
-            if (localLOGV) Log.i(TAG, "finishLayoutLw::mHideKeyguard="+mHideLockScreen);
-            if (mDismissKeyguard && mKeyguardMediator.isShowing()
-                    && !mKeyguardMediator.isSecure()) {
+            if (localLOGV) Log.v(TAG, "finishLayoutLw::mHideKeyguard="+mHideLockScreen);
+            if (mDismissKeyguard && !mKeyguardMediator.isSecure()) {
                 if (mKeyguard.hideLw(true)) {
                     changes |= FINISH_LAYOUT_REDO_LAYOUT
                             | FINISH_LAYOUT_REDO_CONFIG
                             | FINISH_LAYOUT_REDO_WALLPAPER;
                 }
-                if (!mKeyguardMediator.isSecure()) {
+                if (mKeyguardMediator.isShowing()) {
                     mHandler.post(new Runnable() {
                         public void run() {
-                            mKeyguardMediator.keyguardDone(true);
+                            mKeyguardMediator.keyguardDone(false, false);
                         }
                     });
                 }
-            } else if (mHideLockScreen && mKeyguardMediator.isShowing()) {
+            } else if (mHideLockScreen) {
                 if (mKeyguard.hideLw(true)) {
                     changes |= FINISH_LAYOUT_REDO_LAYOUT
                             | FINISH_LAYOUT_REDO_CONFIG
@@ -1786,9 +1793,9 @@
     BroadcastReceiver mPowerReceiver = new BroadcastReceiver() {
         public void onReceive(Context context, Intent intent) {
             if (Intent.ACTION_POWER_CONNECTED.equals(intent.getAction())) {
-                mPlugged = true;
+                updatePlugged();
             } else if (Intent.ACTION_POWER_DISCONNECTED.equals(intent.getAction())) {
-                mPlugged = false;
+                updatePlugged();
             }
             updateKeepScreenOn();
         }
@@ -1991,15 +1998,19 @@
     }
     
     void updateKeepScreenOn() {
-        if (mPlugged) {
+        if (mPlugged != 0) {
+            if (localLOGV) Log.v(TAG, "Update: mDockState=" + mDockState
+                    + " mPlugged=" + mPlugged
+                    + " mCarDockKeepsScreenOn" + mCarDockKeepsScreenOn
+                    + " mDeskDockKeepsScreenOn" + mDeskDockKeepsScreenOn);
             if (mDockState == Intent.EXTRA_DOCK_STATE_CAR
-                    && mCarDockKeepsScreenOn) {
+                    && (mPlugged&mCarDockKeepsScreenOn) != 0) {
                 if (!mDockWakeLock.isHeld()) {
                     mDockWakeLock.acquire();
                 }
                 return;
             } else if (mDockState == Intent.EXTRA_DOCK_STATE_DESK
-                    && mDeskDockKeepsScreenOn) {
+                    && (mPlugged&mDeskDockKeepsScreenOn) != 0) {
                 if (!mDockWakeLock.isHeld()) {
                     mDockWakeLock.acquire();
                 }