Update the uid correctly.

use the pending intent to find out the uid rather than
from binder which is calling the api. If other alarms
get updated internally due to this call from the app, other
alarms will have invalid uid.

Update the worksource correctly with the next alarm that
will be processed, The blocked UID alarm processing will be delayed,
if we set that as the worksource that will not be correct.

During Alarm delivery also do a check if wake lock needs to be released.
As the UID might have been blocked already.

Change-Id: I954f09b87d73387e1a7afd862638d8a0e2a88a53
diff --git a/services/core/java/com/android/server/AlarmManagerService.java b/services/core/java/com/android/server/AlarmManagerService.java
index edbb289..9de6f2a 100644
--- a/services/core/java/com/android/server/AlarmManagerService.java
+++ b/services/core/java/com/android/server/AlarmManagerService.java
@@ -2089,7 +2089,7 @@
             workSource = _ws;
             flags = _flags;
             alarmClock = _info;
-            uid = _uid;
+            uid = operation.getCreatorUid();
             pid = Binder.getCallingPid();
         }
 
@@ -2244,6 +2244,12 @@
                 mInFlight.add(inflight);
                 mBroadcastRefCount++;
                 mTriggeredUids.add(new Integer(alarm.uid));
+                if (checkReleaseWakeLock()) {
+                    if (mWakeLock.isHeld()) {
+                        mWakeLock.release();
+                        if (localLOGV) Slog.v(TAG, "AM WakeLock Released Internally deliverAlarms");
+                    }
+                }
 
                 if (allowWhileIdle) {
                     // Record the last time this uid handled an ALLOW_WHILE_IDLE alarm.
@@ -2712,8 +2718,12 @@
                     }
                 } else {
                     // the next of our alarms is now in flight.  reattribute the wakelock.
+                    InFlight inFlight = null;
                     if (mInFlight.size() > 0) {
-                        InFlight inFlight = mInFlight.get(0);
+                        for(int index = 0; index < mInFlight.size(); index++){
+                            inFlight = mInFlight.get(index);
+                            if(!mBlockedUids.contains(inFlight.mUid)) break;
+                        }
                         setWakelockWorkSource(inFlight.mPendingIntent, inFlight.mWorkSource,
                                 inFlight.mAlarmType, inFlight.mTag, false);
                     } else {