Fire "dreaming started" and "dreaming stopped" broadcasts.

Dream manager now fires broadcast intents when entering + exiting
dreamland (except when testing).

Power manager can now listen for dreams ending, using polling only
as a backstop.

Also:
 - Bullet-proof dream-manager/dream against known failure modes
 - Add new read/write dream permissions
 - Refactor dream-manager to delegate work + state management into
   a new DreamController class, via a handler

Bug:6999949
Bug:7152024
Change-Id: I986bb7812209d8c95ae1d660a5eee5998a7b08b1
diff --git a/core/res/AndroidManifest.xml b/core/res/AndroidManifest.xml
index 89d78b6..661b70c 100644
--- a/core/res/AndroidManifest.xml
+++ b/core/res/AndroidManifest.xml
@@ -140,6 +140,9 @@
 
     <protected-broadcast android:name="android.os.UpdateLock.UPDATE_LOCK_CHANGED" />
 
+    <protected-broadcast android:name="android.intent.action.DREAMING_STARTED" />
+    <protected-broadcast android:name="android.intent.action.DREAMING_STOPPED" />
+
     <!-- ====================================== -->
     <!-- Permissions for things that cost money -->
     <!-- ====================================== -->
@@ -1929,6 +1932,18 @@
         android:permissionGroup="android.permission-group.SYSTEM_TOOLS"
         android:protectionLevel="signature|system" />
 
+    <!-- Allows applications to read dream settings and dream state.
+         @hide -->
+    <permission android:name="android.permission.READ_DREAM_STATE"
+        android:permissionGroup="android.permission-group.SYSTEM_TOOLS"
+        android:protectionLevel="signature" />
+
+    <!-- Allows applications to write dream settings, and start or stop dreaming.
+         @hide -->
+    <permission android:name="android.permission.WRITE_DREAM_STATE"
+        android:permissionGroup="android.permission-group.SYSTEM_TOOLS"
+        android:protectionLevel="signature" />
+
     <!-- Allow an application to read and write the cache partition.
          @hide -->
     <permission android:name="android.permission.ACCESS_CACHE_FILESYSTEM"