Make activity manager sleep top activity when dreaming.

Added the concept of a sleep token to represent a demand to sleep.
We grab this token whenever a dream window is visible.

This change also deletes a special condition in shouldSleepLocked()
which would tend to keep activities running when the lock screen
was shown unless they had previously been sleeping.  I believe this
condition is no longer necessary and the policy is much simpler
without it.  Let's see if anything breaks.

NOTE: This can currently only be used within the system server process
but it might make sense to move it to IActivityManager (guarded by
DEVICE_POWER permission) so that keyguard can use it.  If we added
a callback on release to inform the client that the underlying activity
has finished drawing then we could untangle a bunch of keyguard
specific logic.

Bug: 18866521
Change-Id: I84b2118f5b990a71a1647d1cee536ad3d99f3a24
diff --git a/core/java/android/app/ActivityManagerInternal.java b/core/java/android/app/ActivityManagerInternal.java
index d56dc1e..bde8f39 100644
--- a/core/java/android/app/ActivityManagerInternal.java
+++ b/core/java/android/app/ActivityManagerInternal.java
@@ -16,6 +16,8 @@
 
 package android.app;
 
+import android.annotation.NonNull;
+
 /**
  * Activity manager local system service interface.
  *
@@ -27,4 +29,23 @@
 
     public abstract int startIsolatedProcess(String entryPoint, String[] mainArgs,
             String processName, String abiOverride, int uid, Runnable crashHandler);
+
+    /**
+     * Acquires a sleep token with the specified tag.
+     *
+     * @param tag A string identifying the purpose of the token (eg. "Dream").
+     */
+    public abstract SleepToken acquireSleepToken(@NonNull String tag);
+
+    /**
+     * Sleep tokens cause the activity manager to put the top activity to sleep.
+     * They are used by components such as dreams that may hide and block interaction
+     * with underlying activities.
+     */
+    public static abstract class SleepToken {
+        /**
+         * Releases the sleep token.
+         */
+        public abstract void release();
+    }
 }