Throttle jobs for idle apps
First pass at delaying jobs from apps that are idle.
TODO: Throttle syncs
TODO: Provide a periodic point at which apps are checked for idleness.
Apps that switch to foreground process state are tracked by UsageStats
as an INTERACTION event that affects the last-used timestamp.
JobScheduler's logic for when an app is ready is trumped by the idleness
of the app, and only if the battery is not charging. When charging state
changes, we update the idle state of all the tracked jobs.
android package is whitelisted.
Bug: 20066058
Change-Id: I0a0acb517b100a5c7b11e3f435f4141375f3451f
diff --git a/core/java/android/app/usage/UsageStatsManagerInternal.java b/core/java/android/app/usage/UsageStatsManagerInternal.java
index 0122069..8b3fc2e 100644
--- a/core/java/android/app/usage/UsageStatsManagerInternal.java
+++ b/core/java/android/app/usage/UsageStatsManagerInternal.java
@@ -57,4 +57,41 @@
* Prepares the UsageStatsService for shutdown.
*/
public abstract void prepareShutdown();
+
+ /**
+ * Returns true if the app has not been used for a certain amount of time. How much time?
+ * Could be hours, could be days, who knows?
+ *
+ * @param packageName
+ * @param userId
+ * @return
+ */
+ public abstract boolean isAppIdle(String packageName, int userId);
+
+ /**
+ * Returns the most recent time that the specified package was active for the given user.
+ * @param packageName The package to search.
+ * @param userId The user id of the user of interest.
+ * @return The timestamp of when the package was last used, or -1 if it hasn't been used.
+ */
+ public abstract long getLastPackageAccessTime(String packageName, int userId);
+
+ /**
+ * Sets up a listener for changes to packages being accessed.
+ * @param listener A listener within the system process.
+ */
+ public abstract void addAppIdleStateChangeListener(
+ AppIdleStateChangeListener listener);
+
+ /**
+ * Removes a listener that was previously added for package usage state changes.
+ * @param listener The listener within the system process to remove.
+ */
+ public abstract void removeAppIdleStateChangeListener(
+ AppIdleStateChangeListener listener);
+
+ public interface AppIdleStateChangeListener {
+ void onAppIdleStateChanged(String packageName, int userId, boolean idle);
+ }
+
}
diff --git a/core/java/android/provider/Settings.java b/core/java/android/provider/Settings.java
index ec7e8b2..f488ea0 100644
--- a/core/java/android/provider/Settings.java
+++ b/core/java/android/provider/Settings.java
@@ -3026,7 +3026,7 @@
};
/**
- * These are all pulbic system settings
+ * These are all public system settings
*
* @hide
*/
@@ -3126,7 +3126,7 @@
}
/**
- * These are all pulbic system settings
+ * These are all public system settings
*
* @hide
*/
@@ -5363,6 +5363,13 @@
public static final String SLEEP_TIMEOUT = "sleep_timeout";
/**
+ * Duration in milliseconds that an app should be inactive before it is considered idle.
+ * <p/>Type: Long
+ * @hide
+ */
+ public static final String APP_IDLE_DURATION = "app_idle_duration";
+
+ /**
* This are the settings to be backed up.
*
* NOTE: Settings are backed up and restored in the order they appear
@@ -5425,6 +5432,7 @@
* since the managed profile doesn't get to change them.
*/
private static final Set<String> CLONE_TO_MANAGED_PROFILE = new ArraySet<>();
+
static {
CLONE_TO_MANAGED_PROFILE.add(ACCESSIBILITY_ENABLED);
CLONE_TO_MANAGED_PROFILE.add(ALLOW_MOCK_LOCATION);