Various work on out of memory managment.

- Improve how we handle processes that have shown UI, to take care
  of more cases where we want to push them into the background LRU
  list.
- New trim memory level for when an application that has done UI
  is no longer visible to the user.
- Add APIs to get new trim memory callback.
- Add a host of new bind flags to tweak how the system will adjust
  the OOM level of the target process.

Change-Id: I23ba354112f411a9f8773a67426b4dff85fa2439
diff --git a/services/java/com/android/server/am/ProcessRecord.java b/services/java/com/android/server/am/ProcessRecord.java
index 5b59363..a896ce4 100644
--- a/services/java/com/android/server/am/ProcessRecord.java
+++ b/services/java/com/android/server/am/ProcessRecord.java
@@ -23,6 +23,7 @@
 import android.app.IApplicationThread;
 import android.app.IInstrumentationWatcher;
 import android.content.ComponentName;
+import android.content.Context;
 import android.content.pm.ApplicationInfo;
 import android.content.res.CompatibilityInfo;
 import android.os.Bundle;
@@ -66,7 +67,10 @@
     boolean setIsForeground;    // Running foreground UI when last set?
     boolean foregroundServices; // Running any services that are foreground?
     boolean foregroundActivities; // Running any activities that are foreground?
+    boolean systemNoUi;         // This is a system process, but not currently showing UI.
     boolean hasShownUi;         // Has UI been shown in this process since it was started?
+    boolean pendingUiClean;     // Want to clean up resources from showing UI?
+    boolean hasAboveClient;     // Bound using BIND_ABOVE_CLIENT, so want to be lower
     boolean bad;                // True if disabled in the bad process list
     boolean killedBackground;   // True when proc has been killed due to too many bg
     String waitingToKill;       // Process is waiting to be killed when in the bg; reason
@@ -185,8 +189,11 @@
                 pw.print(" set="); pw.println(setAdj);
         pw.print(prefix); pw.print("curSchedGroup="); pw.print(curSchedGroup);
                 pw.print(" setSchedGroup="); pw.print(setSchedGroup);
+                pw.print(" systemNoUi="); pw.print(systemNoUi);
                 pw.print(" trimMemoryLevel="); pw.println(trimMemoryLevel);
-                pw.print(" hasShownUi="); pw.println(hasShownUi);
+        pw.print(prefix); pw.print("hasShownUi="); pw.print(hasShownUi);
+                pw.print(" pendingUiClean="); pw.print(pendingUiClean);
+                pw.print(" hasAboveClient="); pw.println(hasAboveClient);
         pw.print(prefix); pw.print("setIsForeground="); pw.print(setIsForeground);
                 pw.print(" foregroundServices="); pw.print(foregroundServices);
                 pw.print(" forcingToForeground="); pw.println(forcingToForeground);
@@ -307,6 +314,18 @@
         deathRecipient = null;
     }
 
+    void updateHasAboveClientLocked() {
+        hasAboveClient = false;
+        if (connections.size() > 0) {
+            for (ConnectionRecord cr : connections) {
+                if ((cr.flags&Context.BIND_ABOVE_CLIENT) != 0) {
+                    hasAboveClient = true;
+                    break;
+                }
+            }
+        }
+    }
+
     public String toShortString() {
         if (shortStringName != null) {
             return shortStringName;