Fix issue #21571700: Need to be smarter about how foreground...

...services get out of app idle

Introduce a new process state to even better distinguish foreground
services from other states.  Rework the INTERACTION reporting to
usage stats to do it less when the screen is off -- require that
an app sit in the foreground service or top activity state for
at least 30 minutes before we consider it an interaction.

Also eradicate a bunch of logging in package manager.

Change-Id: I94249e67f9a9c62e9a92ae104710e6747b16327e
diff --git a/services/core/java/com/android/server/am/ProcessRecord.java b/services/core/java/com/android/server/am/ProcessRecord.java
index 14759c3..3acd3a3 100644
--- a/services/core/java/com/android/server/am/ProcessRecord.java
+++ b/services/core/java/com/android/server/am/ProcessRecord.java
@@ -112,6 +112,8 @@
     boolean killedByAm;         // True when proc has been killed by activity manager, not for RAM
     boolean killed;             // True once we know the process has been killed
     boolean procStateChanged;   // Keep track of whether we changed 'setAdj'.
+    boolean reportedInteraction;// Whether we have told usage stats about it being an interaction
+    long fgInteractionTime;     // When we became foreground for interaction purposes
     String waitingToKill;       // Process is waiting to be killed when in the bg, and reason
     IBinder forcingToForeground;// Token that is forcing this process to be foreground
     int adjSeq;                 // Sequence id for identifying oom_adj assignment cycles
@@ -291,6 +293,15 @@
                     pw.print(" foregroundServices="); pw.print(foregroundServices);
                     pw.print(" forcingToForeground="); pw.println(forcingToForeground);
         }
+        if (reportedInteraction || fgInteractionTime != 0) {
+            pw.print(prefix); pw.print("reportedInteraction=");
+            pw.print(reportedInteraction);
+            if (fgInteractionTime != 0) {
+                pw.print(" fgInteractionTime=");
+                TimeUtils.formatDuration(fgInteractionTime, SystemClock.elapsedRealtime(), pw);
+            }
+            pw.println();
+        }
         if (persistent || removed) {
             pw.print(prefix); pw.print("persistent="); pw.print(persistent);
                     pw.print(" removed="); pw.println(removed);