Temporarily whitelist an app for network during doze

API to allow an app to be whitelisted for network and wakelock
access for a short period. So even if the device is in idle
mode, such apps can be given a chance to download the payload
related to a high priority cloud-to-device message.

This API is meant for system apps only.

A new permission CHANGE_DEVICE_IDLE_TEMP_WHITELIST is required
to make this call.

Bug: 21525864
Change-Id: Id7a761a664f21af5d7ff55aa56e8df98d15511ca
diff --git a/core/java/android/app/usage/UsageStatsManager.java b/core/java/android/app/usage/UsageStatsManager.java
index 34699d8..c74b0f2 100644
--- a/core/java/android/app/usage/UsageStatsManager.java
+++ b/core/java/android/app/usage/UsageStatsManager.java
@@ -16,6 +16,7 @@
 
 package android.app.usage;
 
+import android.annotation.SystemApi;
 import android.content.Context;
 import android.content.pm.ParceledListSlice;
 import android.os.RemoteException;
@@ -245,4 +246,25 @@
             // fall through
         }
     }
+
+    /**
+     * {@hide}
+     * Temporarily whitelist the specified app for a short duration. This is to allow an app
+     * receiving a high priority message to be able to access the network and acquire wakelocks
+     * even if the device is in power-save mode or the app is currently considered inactive.
+     * The caller must hold the CHANGE_DEVICE_IDLE_TEMP_WHITELIST permission.
+     * @param packageName The package name of the app to whitelist.
+     * @param duration Duration to whitelist the app for, in milliseconds. It is recommended that
+     * this be limited to 10s of seconds. Requested duration will be clamped to a few minutes.
+     * @param user The user for whom the package should be whitelisted. Passing in a user that is
+     * not the same as the caller's process will require the INTERACT_ACROSS_USERS permission.
+     * @see #isAppInactive(String)
+     */
+    @SystemApi
+    public void whitelistAppTemporarily(String packageName, long duration, UserHandle user) {
+        try {
+            mService.whitelistAppTemporarily(packageName, duration, user.getIdentifier());
+        } catch (RemoteException re) {
+        }
+    }
 }