Battery stats: wake locks, radio active, cleanup.

- Improve wake lock work source updates to also update the current
  history tag, in case the new work source gets recorded in the
  history.

- Fix bug in recording radio active time that was not distributing
  any time to apps.

- No longer hold a wake lock while dispatching data conn active call,
  since it comes with its own timestamp.

- Fix issue where the top app was not being cleared while the screen
  was off.

- Remove obsolete STATS_LAST stats type.

- Fix bug that was not clearing the total run time when resetting
  the stats.

Change-Id: Iabe17a9edf34f762374ae09fcffb8a819cf72e30
diff --git a/services/core/java/com/android/server/AlarmManagerService.java b/services/core/java/com/android/server/AlarmManagerService.java
index 41ffdc1..168742f 100644
--- a/services/core/java/com/android/server/AlarmManagerService.java
+++ b/services/core/java/com/android/server/AlarmManagerService.java
@@ -1348,25 +1348,22 @@
         try {
             final boolean unimportant = pi == mTimeTickSender;
             mWakeLock.setUnimportantForLogging(unimportant);
+            if (first || mLastWakeLockUnimportantForLogging) {
+                mWakeLock.setHistoryTag(pi.getTag(
+                        type == ELAPSED_REALTIME_WAKEUP || type == RTC_WAKEUP
+                                ? "*walarm*:" : "*alarm*:"));
+            } else {
+                mWakeLock.setHistoryTag(null);
+            }
+            mLastWakeLockUnimportantForLogging = unimportant;
             if (ws != null) {
-                if (first || mLastWakeLockUnimportantForLogging) {
-                    mWakeLock.setHistoryTag(pi.getTag(
-                            type == ELAPSED_REALTIME_WAKEUP || type == RTC_WAKEUP
-                                    ? "*walarm*:" : "*alarm*:"));
-                } else {
-                    mWakeLock.setHistoryTag(null);
-                }
-                mLastWakeLockUnimportantForLogging = unimportant;
                 mWakeLock.setWorkSource(ws);
                 return;
-            } else {
-                mLastWakeLockUnimportantForLogging = false;
             }
 
             final int uid = ActivityManagerNative.getDefault()
                     .getUidForIntentSender(pi.getTarget());
             if (uid >= 0) {
-                mWakeLock.setHistoryTag(null);
                 mWakeLock.setWorkSource(new WorkSource(uid));
                 return;
             }
diff --git a/services/core/java/com/android/server/NativeDaemonConnector.java b/services/core/java/com/android/server/NativeDaemonConnector.java
index 265b957..62eb663 100644
--- a/services/core/java/com/android/server/NativeDaemonConnector.java
+++ b/services/core/java/com/android/server/NativeDaemonConnector.java
@@ -110,7 +110,7 @@
         } catch (Exception e) {
             loge("Error handling '" + event + "': " + e);
         } finally {
-            if (mCallbacks.onCheckHoldWakeLock(msg.what)) {
+            if (mCallbacks.onCheckHoldWakeLock(msg.what) && mWakeLock != null) {
                 mWakeLock.release();
             }
         }
@@ -171,7 +171,8 @@
                                     rawEvent);
                             if (event.isClassUnsolicited()) {
                                 // TODO: migrate to sending NativeDaemonEvent instances
-                                if (mCallbacks.onCheckHoldWakeLock(event.getCode())) {
+                                if (mCallbacks.onCheckHoldWakeLock(event.getCode())
+                                        && mWakeLock != null) {
                                     mWakeLock.acquire();
                                     releaseWl = true;
                                 }
diff --git a/services/core/java/com/android/server/NetworkManagementService.java b/services/core/java/com/android/server/NetworkManagementService.java
index 5273cec..41832eb 100644
--- a/services/core/java/com/android/server/NetworkManagementService.java
+++ b/services/core/java/com/android/server/NetworkManagementService.java
@@ -208,7 +208,11 @@
         }
 
         PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
-        PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, NETD_TAG);
+        // Don't need this wake lock, since we now have a time stamp for when
+        // the network actually went inactive.  (It might be nice to still do this,
+        // but I don't want to do it through the power manager because that pollutes the
+        // battery stats history with pointless noise.)
+        PowerManager.WakeLock wl = null; //pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, NETD_TAG);
 
         mConnector = new NativeDaemonConnector(
                 new NetdCallbackReceiver(), socket, 10, NETD_TAG, 160, wl);
diff --git a/services/core/java/com/android/server/am/ActivityManagerService.java b/services/core/java/com/android/server/am/ActivityManagerService.java
index 5500b9d..04a2fc3 100644
--- a/services/core/java/com/android/server/am/ActivityManagerService.java
+++ b/services/core/java/com/android/server/am/ActivityManagerService.java
@@ -15583,7 +15583,7 @@
         ActivityRecord act = mStackSupervisor.resumedAppLocked();
         String pkg;
         int uid;
-        if (act != null) {
+        if (act != null && !act.sleeping) {
             pkg = act.packageName;
             uid = act.info.applicationInfo.uid;
         } else {
diff --git a/services/core/java/com/android/server/power/PowerManagerService.java b/services/core/java/com/android/server/power/PowerManagerService.java
index fab972f..a388318 100644
--- a/services/core/java/com/android/server/power/PowerManagerService.java
+++ b/services/core/java/com/android/server/power/PowerManagerService.java
@@ -747,7 +747,7 @@
         }
     }
 
-    private void updateWakeLockWorkSourceInternal(IBinder lock, WorkSource ws) {
+    private void updateWakeLockWorkSourceInternal(IBinder lock, WorkSource ws, String historyTag) {
         synchronized (mLock) {
             int index = findWakeLockIndexLocked(lock);
             if (index < 0) {
@@ -767,7 +767,8 @@
             if (!wakeLock.hasSameWorkSource(ws)) {
                 notifyWakeLockChangingLocked(wakeLock, wakeLock.mFlags, wakeLock.mTag,
                         wakeLock.mPackageName, wakeLock.mOwnerUid, wakeLock.mOwnerPid,
-                        ws, wakeLock.mHistoryTag);
+                        ws, historyTag);
+                wakeLock.mHistoryTag = historyTag;
                 wakeLock.updateWorkSource(ws);
             }
         }
@@ -2600,11 +2601,11 @@
                     ws.add(uids[i]);
                 }
             }
-            updateWakeLockWorkSource(lock, ws);
+            updateWakeLockWorkSource(lock, ws, null);
         }
 
         @Override // Binder call
-        public void updateWakeLockWorkSource(IBinder lock, WorkSource ws) {
+        public void updateWakeLockWorkSource(IBinder lock, WorkSource ws, String historyTag) {
             if (lock == null) {
                 throw new IllegalArgumentException("lock must not be null");
             }
@@ -2619,7 +2620,7 @@
 
             final long ident = Binder.clearCallingIdentity();
             try {
-                updateWakeLockWorkSourceInternal(lock, ws);
+                updateWakeLockWorkSourceInternal(lock, ws, historyTag);
             } finally {
                 Binder.restoreCallingIdentity(ident);
             }