auto import from //branches/cupcake_rel/...@141571
diff --git a/services/java/com/android/server/HardwareService.java b/services/java/com/android/server/HardwareService.java
index 7466a37..42367ef 100755
--- a/services/java/com/android/server/HardwareService.java
+++ b/services/java/com/android/server/HardwareService.java
@@ -21,8 +21,10 @@
 import android.content.Intent;
 import android.content.IntentFilter;
 import android.content.pm.PackageManager;
+import android.os.Handler;
 import android.os.Hardware;
 import android.os.IHardwareService;
+import android.os.Message;
 import android.os.Power;
 import android.os.PowerManager;
 import android.os.Process;
@@ -45,6 +47,9 @@
     static final int LIGHT_FLASH_NONE = 0;
     static final int LIGHT_FLASH_TIMED = 1;
 
+    private boolean mAttentionLightOn;
+    private boolean mPulsing;
+
     HardwareService(Context context) {
         // Reset the hardware to a default state, in case this is a runtime
         // restart instead of a fresh boot.
@@ -217,10 +222,48 @@
 
     public void setAttentionLight(boolean on) {
         // Not worthy of a permission.  We shouldn't have a flashlight permission.
-        setLight_native(mNativePointer, LIGHT_ID_ATTENTION, on ? 0xffffffff : 0,
-                LIGHT_FLASH_NONE, 0, 0);
+        synchronized (this) {
+            mAttentionLightOn = on;
+            mPulsing = false;
+            setLight_native(mNativePointer, LIGHT_ID_ATTENTION, on ? 0xffffffff : 0,
+                    LIGHT_FLASH_NONE, 0, 0);
+        }
     }
 
+    public void pulseBreathingLight() {
+        synchronized (this) {
+            // HACK: Added at the last minute of cupcake -- design this better;
+            // Don't reuse the attention light -- make another one.
+            if (false) {
+                Log.d(TAG, "pulseBreathingLight mAttentionLightOn=" + mAttentionLightOn
+                        + " mPulsing=" + mPulsing);
+            }
+            if (!mAttentionLightOn && !mPulsing) {
+                mPulsing = true;
+                setLight_native(mNativePointer, LIGHT_ID_ATTENTION, 0xff101010,
+                        LIGHT_FLASH_NONE, 0, 0);
+                mH.sendMessageDelayed(Message.obtain(mH, 1), 3000);
+            }
+        }
+    }
+
+    private Handler mH = new Handler() {
+        @Override
+        public void handleMessage(Message msg) {
+            synchronized (this) {
+                if (false) {
+                    Log.d(TAG, "pulse cleanup handler firing mPulsing=" + mPulsing);
+                }
+                if (mPulsing) {
+                    mPulsing = false;
+                    setLight_native(mNativePointer, LIGHT_ID_ATTENTION,
+                            mAttentionLightOn ? 0xffffffff : 0,
+                            LIGHT_FLASH_NONE, 0, 0);
+                }
+            }
+        }
+    };
+
     private void doCancelVibrate() {
         synchronized (this) {
             if (mThread != null) {
diff --git a/services/java/com/android/server/NotificationManagerService.java b/services/java/com/android/server/NotificationManagerService.java
index c4b0587..6ed8b4c 100644
--- a/services/java/com/android/server/NotificationManagerService.java
+++ b/services/java/com/android/server/NotificationManagerService.java
@@ -588,6 +588,7 @@
                     long identity = Binder.clearCallingIdentity();
                     try {
                         r.statusBarKey = mStatusBarService.addIcon(icon, n);
+                        mHardware.pulseBreathingLight();
                     }
                     finally {
                         Binder.restoreCallingIdentity(identity);
diff --git a/services/java/com/android/server/PackageManagerService.java b/services/java/com/android/server/PackageManagerService.java
index 2cf47df..7fffcb9 100644
--- a/services/java/com/android/server/PackageManagerService.java
+++ b/services/java/com/android/server/PackageManagerService.java
@@ -3248,12 +3248,18 @@
                     res.removedInfo.sendBroadcast(false, true);
                     Bundle extras = new Bundle(1);
                     extras.putInt(Intent.EXTRA_UID, res.uid);
-                    if (res.removedInfo.removedPackage != null) {
+                    final boolean update = res.removedInfo.removedPackage != null;
+                    if (update) {
                         extras.putBoolean(Intent.EXTRA_REPLACING, true);
                     }
                     sendPackageBroadcast(Intent.ACTION_PACKAGE_ADDED,
                                          res.pkg.applicationInfo.packageName,
                                          extras);
+                    if (update) {
+                        sendPackageBroadcast(Intent.ACTION_PACKAGE_REPLACED,
+                                res.pkg.applicationInfo.packageName,
+                                extras);
+                    }
                 }
                 Runtime.getRuntime().gc();
             }
@@ -3894,14 +3900,26 @@
     private boolean deletePackageX(String packageName, boolean sendBroadCast,
                                    boolean deleteCodeAndResources, int flags) {
         PackageRemovedInfo info = new PackageRemovedInfo();
-        boolean res = false;
+        boolean res;
 
         synchronized (mInstallLock) {
             res = deletePackageLI(packageName, deleteCodeAndResources, flags, info);
         }
         
         if(res && sendBroadCast) {
-            info.sendBroadcast(deleteCodeAndResources, false);
+            boolean systemUpdate = info.isRemovedPackageSystemUpdate;
+            info.sendBroadcast(deleteCodeAndResources, systemUpdate);
+
+            // If the removed package was a system update, the old system packaged
+            // was re-enabled; we need to broadcast this information
+            if (systemUpdate) {
+                Bundle extras = new Bundle(1);
+                extras.putInt(Intent.EXTRA_UID, info.removedUid >= 0 ? info.removedUid : info.uid);
+                extras.putBoolean(Intent.EXTRA_REPLACING, true);
+
+                sendPackageBroadcast(Intent.ACTION_PACKAGE_ADDED, packageName, extras);
+                sendPackageBroadcast(Intent.ACTION_PACKAGE_REPLACED, packageName, extras);
+            }
         }
         return res;
     }
@@ -3910,7 +3928,8 @@
         String removedPackage;
         int uid = -1;
         int removedUid = -1;
-        
+        boolean isRemovedPackageSystemUpdate = false;
+
         void sendBroadcast(boolean fullRemove, boolean replacing) {
             Bundle extras = new Bundle(1);
             extras.putInt(Intent.EXTRA_UID, removedUid >= 0 ? removedUid : uid);
@@ -3996,6 +4015,7 @@
             Log.i(TAG, "Deleting system pkg from data partition");
         }
         // Delete the updated package
+        outInfo.isRemovedPackageSystemUpdate = true;
         boolean ret = deleteInstalledPackageLI(p, deleteCodeAndResources, flags, outInfo);
         if (!ret) {
             return false;
diff --git a/services/java/com/android/server/PowerManagerService.java b/services/java/com/android/server/PowerManagerService.java
index 5c7ab93..a900b3e 100644
--- a/services/java/com/android/server/PowerManagerService.java
+++ b/services/java/com/android/server/PowerManagerService.java
@@ -135,8 +135,8 @@
 
     private boolean mDoneBooting = false;
     private int mStayOnConditions = 0;
-    private int mNotificationQueue = -1;
-    private int mNotificationWhy;
+    private int[] mBroadcastQueue = new int[] { -1, -1, -1 };
+    private int[] mBroadcastWhy = new int[3];
     private int mPartialCount = 0;
     private int mPowerState;
     private boolean mOffBecauseOfUser;
@@ -170,6 +170,7 @@
             = new BrightnessState(KEYBOARD_BRIGHT_BIT);
     private final BrightnessState mButtonBrightness
             = new BrightnessState(BUTTON_BRIGHT_BIT);
+    private boolean mStillNeedSleepNotification;
     private boolean mIsPowered = false;
     private IActivityManager mActivityService;
     private IBatteryStats mBatteryStats;
@@ -390,7 +391,7 @@
         mHandler = new Handler();
 
         mBroadcastWakeLock = new UnsynchronizedWakeLock(
-                                PowerManager.PARTIAL_WAKE_LOCK, "sleep_notification", true);
+                                PowerManager.PARTIAL_WAKE_LOCK, "sleep_broadcast", true);
         mStayOnWhilePluggedInScreenDimLock = new UnsynchronizedWakeLock(
                                 PowerManager.SCREEN_DIM_WAKE_LOCK, "StayOnWhilePluggedIn Screen Dim", false);
         mStayOnWhilePluggedInPartialLock = new UnsynchronizedWakeLock(
@@ -798,8 +799,10 @@
                 + " mStayOnConditions=" + mStayOnConditions);
         pw.println("  mOffBecauseOfUser=" + mOffBecauseOfUser
                 + " mUserState=" + mUserState);
-        pw.println("  mNotificationQueue=" + mNotificationQueue
-                + " mNotificationWhy=" + mNotificationWhy);
+        pw.println("  mBroadcastQueue={" + mBroadcastQueue[0] + ',' + mBroadcastQueue[1]
+                + ',' + mBroadcastQueue[2] + "}");
+        pw.println("  mBroadcastWhy={" + mBroadcastWhy[0] + ',' + mBroadcastWhy[1]
+                + ',' + mBroadcastWhy[2] + "}");
         pw.println("  mPokey=" + mPokey + " mPokeAwakeonSet=" + mPokeAwakeOnSet);
         pw.println("  mKeyboardVisible=" + mKeyboardVisible
                 + " mUserActivityAllowed=" + mUserActivityAllowed);
@@ -840,6 +843,8 @@
             pw.println("    poke lock '" + p.tag + "':"
                     + ((p.pokey & POKE_LOCK_IGNORE_CHEEK_EVENTS) != 0
                             ? " POKE_LOCK_IGNORE_CHEEK_EVENTS" : "")
+                    + ((p.pokey & POKE_LOCK_IGNORE_TOUCH_AND_CHEEK_EVENTS) != 0
+                            ? " POKE_LOCK_IGNORE_TOUCH_AND_CHEEK_EVENTS" : "")
                     + ((p.pokey & POKE_LOCK_SHORT_TIMEOUT) != 0
                             ? " POKE_LOCK_SHORT_TIMEOUT" : "")
                     + ((p.pokey & POKE_LOCK_MEDIUM_TIMEOUT) != 0
@@ -924,29 +929,56 @@
 
     private void sendNotificationLocked(boolean on, int why)
     {
-                    
         if (!on) {
-            mNotificationWhy = why;
+            mStillNeedSleepNotification = false;
         }
 
-        int value = on ? 1 : 0;
-        if (mNotificationQueue == -1) {
-            // empty
-            // Acquire the broadcast wake lock before changing the power
-            // state. It will be release after the broadcast is sent.
-            mBroadcastWakeLock.acquire();
-            EventLog.writeEvent(LOG_POWER_SCREEN_BROADCAST_SEND, mBroadcastWakeLock.mCount);
-            mNotificationQueue = value;
-            mHandler.post(mNotificationTask);
-        } else if (mNotificationQueue != value) {
-            // it's a pair, so cancel it
-            mNotificationQueue = -1;
-            mHandler.removeCallbacks(mNotificationTask);
+        // Add to the queue.
+        int index = 0;
+        while (mBroadcastQueue[index] != -1) {
+            index++;
+        }
+        mBroadcastQueue[index] = on ? 1 : 0;
+        mBroadcastWhy[index] = why;
+
+        // If we added it position 2, then there is a pair that can be stripped.
+        // If we added it position 1 and we're turning the screen off, we can strip
+        // the pair and do nothing, because the screen is already off, and therefore
+        // keyguard has already been enabled.
+        // However, if we added it at position 1 and we're turning it on, then position
+        // 0 was to turn it off, and we can't strip that, because keyguard needs to come
+        // on, so have to run the queue then.
+        if (index == 2) {
+            // Also, while we're collapsing them, if it's going to be an "off," and one
+            // is off because of user, then use that, regardless of whether it's the first
+            // or second one.
+            if (!on && why == WindowManagerPolicy.OFF_BECAUSE_OF_USER) {
+                mBroadcastWhy[0] = WindowManagerPolicy.OFF_BECAUSE_OF_USER;
+            }
+            mBroadcastQueue[0] = on ? 1 : 0;
+            mBroadcastQueue[1] = -1;
+            mBroadcastQueue[2] = -1;
+            index = 0;
+        }
+        if (index == 1 && !on) {
+            mBroadcastQueue[0] = -1;
+            mBroadcastQueue[1] = -1;
+            index = -1;
+            // The wake lock was being held, but we're not actually going to do any
+            // broadcasts, so release the wake lock.
             EventLog.writeEvent(LOG_POWER_SCREEN_BROADCAST_STOP, 1, mBroadcastWakeLock.mCount);
             mBroadcastWakeLock.release();
-        } else {
-            // else, same so do nothing -- maybe we should warn?
-            Log.w(TAG, "Duplicate notification: on=" + on + " why=" + why);
+        }
+
+        // Now send the message.
+        if (index >= 0) {
+            // Acquire the broadcast wake lock before changing the power
+            // state. It will be release after the broadcast is sent.
+            // We always increment the ref count for each notification in the queue
+            // and always decrement when that notification is handled.
+            mBroadcastWakeLock.acquire();
+            EventLog.writeEvent(LOG_POWER_SCREEN_BROADCAST_SEND, mBroadcastWakeLock.mCount);
+            mHandler.post(mNotificationTask);
         }
     }
 
@@ -954,64 +986,69 @@
     {
         public void run()
         {
-            int value;
-            int why;
-            WindowManagerPolicy policy;
-            synchronized (mLocks) {
-                policy = getPolicyLocked();
-                value = mNotificationQueue;
-                why = mNotificationWhy;
-                mNotificationQueue = -1;
-            }
-            if (value == 1) {
-                mScreenOnStart = SystemClock.uptimeMillis();
-                
-                policy.screenTurnedOn();
-                try {
-                    ActivityManagerNative.getDefault().wakingUp();
-                } catch (RemoteException e) {
-                    // ignore it
+            while (true) {
+                int value;
+                int why;
+                WindowManagerPolicy policy;
+                synchronized (mLocks) {
+                    value = mBroadcastQueue[0];
+                    why = mBroadcastWhy[0];
+                    for (int i=0; i<2; i++) {
+                        mBroadcastQueue[i] = mBroadcastQueue[i+1];
+                        mBroadcastWhy[i] = mBroadcastWhy[i+1];
+                    }
+                    policy = getPolicyLocked();
                 }
+                if (value == 1) {
+                    mScreenOnStart = SystemClock.uptimeMillis();
+                    
+                    policy.screenTurnedOn();
+                    try {
+                        ActivityManagerNative.getDefault().wakingUp();
+                    } catch (RemoteException e) {
+                        // ignore it
+                    }
 
-                if (mSpew) {
-                    Log.d(TAG, "mBroadcastWakeLock=" + mBroadcastWakeLock);
-                }
-                if (mContext != null && ActivityManagerNative.isSystemReady()) {
-                    mContext.sendOrderedBroadcast(mScreenOnIntent, null,
-                            mScreenOnBroadcastDone, mHandler, 0, null, null);
-                } else {
-                    synchronized (mLocks) {
-                        EventLog.writeEvent(LOG_POWER_SCREEN_BROADCAST_STOP, 2,
-                                mBroadcastWakeLock.mCount);
-                        mBroadcastWakeLock.release();
+                    if (mSpew) {
+                        Log.d(TAG, "mBroadcastWakeLock=" + mBroadcastWakeLock);
+                    }
+                    if (mContext != null && ActivityManagerNative.isSystemReady()) {
+                        mContext.sendOrderedBroadcast(mScreenOnIntent, null,
+                                mScreenOnBroadcastDone, mHandler, 0, null, null);
+                    } else {
+                        synchronized (mLocks) {
+                            EventLog.writeEvent(LOG_POWER_SCREEN_BROADCAST_STOP, 2,
+                                    mBroadcastWakeLock.mCount);
+                            mBroadcastWakeLock.release();
+                        }
                     }
                 }
-            }
-            else if (value == 0) {
-                mScreenOffStart = SystemClock.uptimeMillis();
-                
-                policy.screenTurnedOff(why);
-                try {
-                    ActivityManagerNative.getDefault().goingToSleep();
-                } catch (RemoteException e) {
-                    // ignore it.
-                }
+                else if (value == 0) {
+                    mScreenOffStart = SystemClock.uptimeMillis();
+                    
+                    policy.screenTurnedOff(why);
+                    try {
+                        ActivityManagerNative.getDefault().goingToSleep();
+                    } catch (RemoteException e) {
+                        // ignore it.
+                    }
 
-                if (mContext != null && ActivityManagerNative.isSystemReady()) {
-                    mContext.sendOrderedBroadcast(mScreenOffIntent, null,
-                            mScreenOffBroadcastDone, mHandler, 0, null, null);
-                } else {
-                    synchronized (mLocks) {
-                        EventLog.writeEvent(LOG_POWER_SCREEN_BROADCAST_STOP, 3,
-                                mBroadcastWakeLock.mCount);
-                        mBroadcastWakeLock.release();
+                    if (mContext != null && ActivityManagerNative.isSystemReady()) {
+                        mContext.sendOrderedBroadcast(mScreenOffIntent, null,
+                                mScreenOffBroadcastDone, mHandler, 0, null, null);
+                    } else {
+                        synchronized (mLocks) {
+                            EventLog.writeEvent(LOG_POWER_SCREEN_BROADCAST_STOP, 3,
+                                    mBroadcastWakeLock.mCount);
+                            mBroadcastWakeLock.release();
+                        }
                     }
                 }
-            }
-            else {
-                // If we're in this case, then this handler is running for a previous
-                // paired transaction.  mBroadcastWakeLock will already have been released
-                // in sendNotificationLocked.
+                else {
+                    // If we're in this case, then this handler is running for a previous
+                    // paired transaction.  mBroadcastWakeLock will already have been released.
+                    break;
+                }
             }
         }
     };
@@ -1235,6 +1272,14 @@
 
             if (oldScreenOn != newScreenOn) {
                 if (newScreenOn) {
+                    // When the user presses the power button, we need to always send out the
+                    // notification that it's going to sleep so the keyguard goes on.  But
+                    // we can't do that until the screen fades out, so we don't show the keyguard
+                    // too early.
+                    if (mStillNeedSleepNotification) {
+                        sendNotificationLocked(false, WindowManagerPolicy.OFF_BECAUSE_OF_USER);
+                    }
+
                     // Turn on the screen UNLESS there was a prior
                     // preventScreenOn(true) request.  (Note that the lifetime
                     // of a single preventScreenOn() request is limited to 5
@@ -1290,7 +1335,7 @@
                     }
                     mPowerState &= ~SCREEN_ON_BIT;
                     if (!mScreenBrightness.animating) {
-                        err = screenOffFinishedAnimating(becauseOfUser);
+                        err = screenOffFinishedAnimatingLocked(becauseOfUser);
                     } else {
                         mOffBecauseOfUser = becauseOfUser;
                         err = 0;
@@ -1301,7 +1346,7 @@
         }
     }
     
-    private int screenOffFinishedAnimating(boolean becauseOfUser) {
+    private int screenOffFinishedAnimatingLocked(boolean becauseOfUser) {
         // I don't think we need to check the current state here because all of these
         // Power.setScreenState and sendNotificationLocked can both handle being 
         // called multiple times in the same state. -joeo
@@ -1345,10 +1390,12 @@
             if (ANIMATE_KEYBOARD_LIGHTS) {
                 if ((newState & KEYBOARD_BRIGHT_BIT) == 0) {
                     mKeyboardBrightness.setTargetLocked(Power.BRIGHTNESS_OFF,
-                            ANIM_STEPS, INITIAL_KEYBOARD_BRIGHTNESS);
+                            ANIM_STEPS, INITIAL_KEYBOARD_BRIGHTNESS,
+                            preferredBrightness);
                 } else {
                     mKeyboardBrightness.setTargetLocked(preferredBrightness,
-                            ANIM_STEPS, INITIAL_KEYBOARD_BRIGHTNESS);
+                            ANIM_STEPS, INITIAL_KEYBOARD_BRIGHTNESS,
+                            Power.BRIGHTNESS_OFF);
                 }
                 startAnimation = true;
             } else {
@@ -1364,10 +1411,12 @@
             if (ANIMATE_BUTTON_LIGHTS) {
                 if ((newState & BUTTON_BRIGHT_BIT) == 0) {
                     mButtonBrightness.setTargetLocked(Power.BRIGHTNESS_OFF,
-                            ANIM_STEPS, INITIAL_BUTTON_BRIGHTNESS);
+                            ANIM_STEPS, INITIAL_BUTTON_BRIGHTNESS,
+                            preferredBrightness);
                 } else {
                     mButtonBrightness.setTargetLocked(preferredBrightness,
-                            ANIM_STEPS, INITIAL_BUTTON_BRIGHTNESS);
+                            ANIM_STEPS, INITIAL_BUTTON_BRIGHTNESS,
+                            Power.BRIGHTNESS_OFF);
                 }
                 startAnimation = true;
             } else {
@@ -1381,6 +1430,23 @@
 
         if ((difference & (SCREEN_ON_BIT | SCREEN_BRIGHT_BIT)) != 0) {
             if (ANIMATE_SCREEN_LIGHTS) {
+                int nominalCurrentValue;
+                switch (oldState & (SCREEN_BRIGHT_BIT|SCREEN_ON_BIT)) {
+                    case SCREEN_BRIGHT_BIT | SCREEN_ON_BIT:
+                        nominalCurrentValue = preferredBrightness;
+                        break;
+                    case SCREEN_ON_BIT:
+                        nominalCurrentValue = Power.BRIGHTNESS_DIM;
+                        break;
+                    case 0:
+                        nominalCurrentValue = Power.BRIGHTNESS_OFF;
+                        break;
+                    case SCREEN_BRIGHT_BIT:
+                    default:
+                        // not possible
+                        nominalCurrentValue = (int)mScreenBrightness.curValue;
+                        break;
+                }
                 if ((newState & SCREEN_BRIGHT_BIT) == 0) {
                     // dim or turn off backlight, depending on if the screen is on
                     // the scale is because the brightness ramp isn't linear and this biases
@@ -1398,7 +1464,7 @@
                             steps = (int)(ANIM_STEPS*ratio*scale);
                         }
                         mScreenBrightness.setTargetLocked(Power.BRIGHTNESS_OFF,
-                                steps, INITIAL_SCREEN_BRIGHTNESS);
+                                steps, INITIAL_SCREEN_BRIGHTNESS, nominalCurrentValue);
                     } else {
                         int steps;
                         if ((oldState & SCREEN_ON_BIT) != 0) {
@@ -1417,11 +1483,11 @@
                             mScreenOffTime = SystemClock.elapsedRealtime();
                         }
                         mScreenBrightness.setTargetLocked(Power.BRIGHTNESS_DIM,
-                                steps, INITIAL_SCREEN_BRIGHTNESS);
+                                steps, INITIAL_SCREEN_BRIGHTNESS, nominalCurrentValue);
                     }
                 } else {
                     mScreenBrightness.setTargetLocked(preferredBrightness,
-                            ANIM_STEPS, INITIAL_SCREEN_BRIGHTNESS);
+                            ANIM_STEPS, INITIAL_SCREEN_BRIGHTNESS, nominalCurrentValue);
                 }
                 startAnimation = true;
             } else {
@@ -1502,16 +1568,20 @@
                     + " delta=" + delta);
         }
         
-        void setTargetLocked(int target, int stepsToTarget, int initialValue) {
+        void setTargetLocked(int target, int stepsToTarget, int initialValue,
+                int nominalCurrentValue) {
             if (!initialized) {
                 initialized = true;
                 curValue = (float)initialValue;
             }
             targetValue = target;
-            delta = (targetValue-curValue) / stepsToTarget;
+            delta = (targetValue-nominalCurrentValue) / stepsToTarget;
             if (mSpew) {
+                String noticeMe = nominalCurrentValue == curValue ? "" : "  ******************";
                 Log.i(TAG, "Setting target " + mask + ": cur=" + curValue
-                        + " target=" + targetValue + " delta=" + delta);
+                        + " target=" + targetValue + " delta=" + delta
+                        + " nominalCurrentValue=" + nominalCurrentValue
+                        + noticeMe);
             }
             animating = true;
         }
@@ -1543,7 +1613,7 @@
             animating = more;
             if (!more) {
                 if (mask == SCREEN_BRIGHT_BIT && curIntValue == Power.BRIGHTNESS_OFF) {
-                    screenOffFinishedAnimating(mOffBecauseOfUser);
+                    screenOffFinishedAnimatingLocked(mOffBecauseOfUser);
                 }
             }
             return more;
@@ -1611,13 +1681,23 @@
         //mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
 
         if (((mPokey & POKE_LOCK_IGNORE_CHEEK_EVENTS) != 0)
-            && !((eventType == OTHER_EVENT) || (eventType == BUTTON_EVENT))) {
+                && (eventType == CHEEK_EVENT || eventType == TOUCH_EVENT)) {
             if (false) {
-                Log.d(TAG, "dropping mPokey=0x" + Integer.toHexString(mPokey));
+                Log.d(TAG, "dropping cheek or short event mPokey=0x" + Integer.toHexString(mPokey));
             }
             return;
         }
 
+        if (((mPokey & POKE_LOCK_IGNORE_TOUCH_AND_CHEEK_EVENTS) != 0)
+                && (eventType == TOUCH_EVENT || eventType == TOUCH_UP_EVENT
+                    || eventType == LONG_TOUCH_EVENT || eventType == CHEEK_EVENT)) {
+            if (false) {
+                Log.d(TAG, "dropping touch mPokey=0x" + Integer.toHexString(mPokey));
+            }
+            return;
+        }
+
+
         if (false) {
             if (((mPokey & POKE_LOCK_IGNORE_CHEEK_EVENTS) != 0)) {
                 Log.d(TAG, "userActivity !!!");//, new RuntimeException());
@@ -1695,6 +1775,7 @@
                 }
             }
             EventLog.writeEvent(LOG_POWER_SLEEP_REQUESTED, numCleared);
+            mStillNeedSleepNotification = true;
             mUserState = SCREEN_OFF;
             setPowerState(SCREEN_OFF, false, true);
             cancelTimerLocked();
diff --git a/services/java/com/android/server/WifiService.java b/services/java/com/android/server/WifiService.java
index ddf92e2..6323e2f 100644
--- a/services/java/com/android/server/WifiService.java
+++ b/services/java/com/android/server/WifiService.java
@@ -50,7 +50,6 @@
 import android.os.RemoteException;
 import android.provider.Settings;
 import android.util.Log;
-import android.telephony.TelephonyManager;
 import android.text.TextUtils;
 
 import java.util.ArrayList;
@@ -64,7 +63,6 @@
 import java.io.PrintWriter;
 
 import com.android.internal.app.IBatteryStats;
-import com.android.internal.os.BatteryStatsImpl;
 import com.android.server.am.BatteryStatsService;
 
 /**
@@ -1874,24 +1872,26 @@
     }
 
     private boolean releaseWifiLockLocked(IBinder lock) {
-        boolean result;
+        boolean hadLock;
         
         WifiLock wifiLock = mLocks.removeLock(lock);
-        result = (wifiLock != null);
-        
-        int uid = Binder.getCallingUid();
-        long ident = Binder.clearCallingIdentity();
-        try {
-            switch(wifiLock.mLockMode) {
-            case (WifiManager.WIFI_MODE_FULL): mBatteryStats.noteFullWifiLockReleased(uid);
-            case (WifiManager.WIFI_MODE_SCAN_ONLY): mBatteryStats.noteScanWifiLockReleased(uid);
+        hadLock = (wifiLock != null);
+
+        if (hadLock) {
+            int uid = Binder.getCallingUid();
+            long ident = Binder.clearCallingIdentity();
+            try {
+                switch(wifiLock.mLockMode) {
+                    case (WifiManager.WIFI_MODE_FULL): mBatteryStats.noteFullWifiLockReleased(uid);
+                    case (WifiManager.WIFI_MODE_SCAN_ONLY): mBatteryStats.noteScanWifiLockReleased(uid);
+                }
+            } catch (RemoteException e) {
+            } finally {
+                Binder.restoreCallingIdentity(ident);
             }
-        } catch (RemoteException e) {
-        } finally {
-            Binder.restoreCallingIdentity(ident);
         }
         
         updateWifiState();
-        return result;
+        return hadLock;
     }
 }
diff --git a/services/java/com/android/server/WindowManagerService.java b/services/java/com/android/server/WindowManagerService.java
index 5623b02..18fd74f 100644
--- a/services/java/com/android/server/WindowManagerService.java
+++ b/services/java/com/android/server/WindowManagerService.java
@@ -19,6 +19,8 @@
 import static android.os.LocalPowerManager.CHEEK_EVENT;
 import static android.os.LocalPowerManager.OTHER_EVENT;
 import static android.os.LocalPowerManager.TOUCH_EVENT;
+import static android.os.LocalPowerManager.LONG_TOUCH_EVENT;
+import static android.os.LocalPowerManager.TOUCH_UP_EVENT;
 import static android.view.WindowManager.LayoutParams.FIRST_APPLICATION_WINDOW;
 import static android.view.WindowManager.LayoutParams.FIRST_SUB_WINDOW;
 import static android.view.WindowManager.LayoutParams.FLAG_BLUR_BEHIND;
@@ -2363,6 +2365,7 @@
                         ttoken.startingWindow = null;
                         ttoken.startingMoved = true;
                         startingWindow.mToken = wtoken;
+                        startingWindow.mRootToken = wtoken;
                         startingWindow.mAppToken = wtoken;
                         mWindows.remove(startingWindow);
                         ttoken.windows.remove(startingWindow);
@@ -3678,6 +3681,7 @@
     private static final float CHEEK_THRESHOLD = 0.6f;
     private int mEventState = EVENT_NONE;
     private float mEventSize;
+
     private int eventType(MotionEvent ev) {
         float size = ev.getSize();
         switch (ev.getAction()) {
@@ -3686,7 +3690,7 @@
             return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_EVENT;
         case MotionEvent.ACTION_UP:
             if (size > mEventSize) mEventSize = size;
-            return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : OTHER_EVENT;
+            return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_UP_EVENT;
         case MotionEvent.ACTION_MOVE:
             final int N = ev.getHistorySize();
             if (size > mEventSize) mEventSize = size;
@@ -3699,7 +3703,7 @@
             if (ev.getEventTime() < ev.getDownTime() + EVENT_IGNORE_DURATION) {
                 return TOUCH_EVENT;
             } else {
-                return OTHER_EVENT;
+                return LONG_TOUCH_EVENT;
             }
         default:
             // not good
diff --git a/services/java/com/android/server/am/ActivityManagerService.java b/services/java/com/android/server/am/ActivityManagerService.java
index 9e2ecba..a7dac61 100644
--- a/services/java/com/android/server/am/ActivityManagerService.java
+++ b/services/java/com/android/server/am/ActivityManagerService.java
@@ -3427,7 +3427,7 @@
      */
     private final boolean requestFinishActivityLocked(IBinder token, int resultCode,
             Intent resultData, String reason) {
-        if (localLOGV) Log.v(
+        if (DEBUG_RESULTS) Log.v(
             TAG, "Finishing activity: token=" + token
             + ", result=" + resultCode + ", data=" + resultData);
 
@@ -3490,7 +3490,9 @@
         // send the result
         HistoryRecord resultTo = r.resultTo;
         if (resultTo != null) {
-            if (DEBUG_RESULTS) Log.v(TAG, "Adding result to " + resultTo);
+            if (DEBUG_RESULTS) Log.v(TAG, "Adding result to " + resultTo
+                    + " who=" + r.resultWho + " req=" + r.requestCode
+                    + " res=" + resultCode + " data=" + resultData);
             if (r.info.applicationInfo.uid > 0) {
                 grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
                         r.packageName, resultData, r);
@@ -3499,6 +3501,7 @@
                                      resultData);
             r.resultTo = null;
         }
+        else if (DEBUG_RESULTS) Log.v(TAG, "No result destination from " + r);
 
         // Make sure this HistoryRecord is not holding on to other resources,
         // because clients have remote IPC references to this object so we
@@ -10142,11 +10145,7 @@
             // this decision.
             boolean skip = false;
             if (intent.ACTION_PACKAGE_ADDED.equals(intent.getAction())) {
-                // If this is replacing an existing package, then we allow it
-                // to see the broadcast for it to restart itself.
-                if (!intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) {
-                    skip = true;
-                }
+                skip = true;
             } else if (intent.ACTION_PACKAGE_RESTARTED.equals(intent.getAction())) {
                 skip = true;
             } else if (intent.ACTION_PACKAGE_DATA_CLEARED.equals(intent.getAction())) {
diff --git a/services/java/com/android/server/am/BatteryStatsService.java b/services/java/com/android/server/am/BatteryStatsService.java
index cc9a0af..ee89c09 100644
--- a/services/java/com/android/server/am/BatteryStatsService.java
+++ b/services/java/com/android/server/am/BatteryStatsService.java
@@ -162,7 +162,21 @@
             mStats.noteWifiOffLocked();
         }
     }
-    
+
+    public void noteWifiRunning() {
+        enforceCallingPermission();
+        synchronized (mStats) {
+            mStats.noteWifiRunningLocked();
+        }
+    }
+
+    public void noteWifiStopped() {
+        enforceCallingPermission();
+        synchronized (mStats) {
+            mStats.noteWifiStoppedLocked();
+        }
+    }
+
     public void noteBluetoothOn() {
         enforceCallingPermission();
         synchronized (mStats) {
diff --git a/services/java/com/android/server/status/DateView.java b/services/java/com/android/server/status/DateView.java
index 7c44d67..78bfd5e 100644
--- a/services/java/com/android/server/status/DateView.java
+++ b/services/java/com/android/server/status/DateView.java
@@ -4,12 +4,12 @@
 import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
-import android.text.format.DateFormat;
 import android.util.AttributeSet;
 import android.util.Log;
 import android.widget.TextView;
 import android.view.MotionEvent;
 
+import java.text.DateFormat;
 import java.util.Date;
 
 public final class DateView extends TextView {
@@ -51,7 +51,7 @@
 
     private final void updateClock() {
         Date now = new Date();
-        setText(DateFormat.getLongDateFormat(getContext()).format(now));
+        setText(DateFormat.getDateInstance(DateFormat.LONG).format(now));
     }
 
     void setUpdates(boolean update) {