Introduce "IdleService" API to expose idle-time maintenance to apps

When an application wishes to do low-priority background work when the
device is otherwise idle (e.g. in a desk dock overnight), it declares
a service in its manifest that requires this permission:

     android:permission="android.permission.BIND_IDLE_SERVICE

to launch, and which publishes this intent filter:

    <intent-filter>
        <action android:name="android.service.idle.IdleService" />
    </intent-filter>

This string is declared in the API as IdleService.SERVICE_INTERFACE.

The service must be implemented by extending the new "IdleService"
class, which provides the API through which the system will communicate
with the app.

IdleService declares three methods, two of which are lifecycle callbacks
to the service, and the third of which is for the service itself to
invoke when appropriate.  The lifecycle callbacks are

    public abstract boolean onIdleStart();
    public abstract void onIdleStop();

The first of these is a notification to the service that an idle
maintenance interval has begun.  The service can then spin off
whatever non-UI work it wishes.  When the interval is over, or if
the OS determines that idle services should be shut down immediately,
the onIdleStop() method will be invoked.  The service must shut down
any background processing immediately when this method is called.

Both of these methods must return immediately.  However, the OS
holds a wakelock on the application's behalf for the entire period
between the onIdleStart() and onIdleStop() callbacks.  This means
that for system-arbitrated idle-time operation, the application does
not need to do any of its own wakelock management, and does not need
to hold any wakelock permissions.

The third method in IdleService is

    public final void finishIdle();

Calling this method notifies the OS that the application has finished
whatever idle-time operation it needed to perform, and the OS is thus
free to release the wakelock and return to normal operation (or to
allow other apps to run their own idle services).

Currently the idle window granted to each idle service is ten minutes.
The OS is rather conservative about when these services are run; low
battery or any user activity will suppress them, and the OS will not
choose to run them particularly often.

Idle services are granted their execution windows in round-robin
fashion.

Bug 9680213

Change-Id: Idd6f35940c938c31b94aa4269a67870abf7125b6
diff --git a/core/res/AndroidManifest.xml b/core/res/AndroidManifest.xml
index 2151d4c..bba62d3 100644
--- a/core/res/AndroidManifest.xml
+++ b/core/res/AndroidManifest.xml
@@ -1727,6 +1727,13 @@
         android:label="@string/permlab_recovery"
         android:description="@string/permdesc_recovery" />
 
+    <!-- Allows the system to bind to an application's idle services
+         @hide -->
+    <permission android:name="android.permission.BIND_IDLE_SERVICE"
+        android:protectionLevel="signature"
+        android:label="@string/permlab_bindIdleService"
+        android:description="@string/permdesc_bindIdleService" />
+
     <!-- ========================================= -->
     <!-- Permissions for special development tools -->
     <!-- ========================================= -->
@@ -2728,6 +2735,14 @@
             </intent-filter>
         </service>
 
+        <service android:name="com.android.server.MountServiceIdler"
+                 android:exported="false"
+                 android:permission="android.permission.BIND_IDLE_SERVICE" >
+            <intent-filter>
+                <action android:name="android.service.idle.IdleService" />
+            </intent-filter>
+        </service>
+
     </application>
 
 </manifest>