Maybe fix issue #10797796: IllegalStateException in ProcessState...

...caused runtime restart

There were some situations where the package list could be set
with process stats when it shouldn't.  Not sure if this is causing
the problem, since there is no repro.

Also some improvements to debug output -- new commands to clear
all stats, print full details of stats, and print a one-day
summary (which should match what the UI shows).

Change-Id: I9581db4059d7bb094f79f2fe06c1ccff3e1a4e74
diff --git a/services/java/com/android/server/am/ProcessRecord.java b/services/java/com/android/server/am/ProcessRecord.java
index d230779..7a456f7 100644
--- a/services/java/com/android/server/am/ProcessRecord.java
+++ b/services/java/com/android/server/am/ProcessRecord.java
@@ -407,9 +407,9 @@
     }
 
     public void makeInactive(ProcessStatsService tracker) {
-        if (thread != null) {
-            thread = null;
-            final ProcessStats.ProcessState origBase = baseProcessTracker;
+        thread = null;
+        final ProcessStats.ProcessState origBase = baseProcessTracker;
+        if (origBase != null) {
             if (origBase != null) {
                 origBase.setState(ProcessStats.STATE_NOTHING,
                         tracker.getMemFactorLocked(), SystemClock.uptimeMillis(), pkgList);
@@ -567,8 +567,16 @@
      */
     public boolean addPackage(String pkg, ProcessStatsService tracker) {
         if (!pkgList.containsKey(pkg)) {
-            pkgList.put(pkg, baseProcessTracker != null
-                    ? tracker.getProcessStateLocked(pkg, info.uid, processName) : null);
+            if (baseProcessTracker != null) {
+                ProcessStats.ProcessState state = tracker.getProcessStateLocked(
+                        pkg, info.uid, processName);
+                pkgList.put(pkg, state);
+                if (state != baseProcessTracker) {
+                    state.makeActive();
+                }
+            } else {
+                pkgList.put(pkg, null);
+            }
             return true;
         }
         return false;
@@ -610,7 +618,7 @@
                 ProcessStats.ProcessState ps = tracker.getProcessStateLocked(
                         info.packageName, info.uid, processName);
                 pkgList.put(info.packageName, ps);
-                if (thread != null && ps != baseProcessTracker) {
+                if (ps != baseProcessTracker) {
                     ps.makeActive();
                 }
             }