Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 092f038d authored by Amith Yamasani's avatar Amith Yamasani Committed by Android (Google) Code Review
Browse files

Merge "Throttle jobs for idle apps"

parents df62ab48 b0ff3224
Loading
Loading
Loading
Loading
+37 −0
Original line number Diff line number Diff line
@@ -57,4 +57,41 @@ public abstract class UsageStatsManagerInternal {
     * 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);
    }

}
+10 −2
Original line number Diff line number Diff line
@@ -3035,7 +3035,7 @@ public final class Settings {
        };

        /**
         * These are all pulbic system settings
         * These are all public system settings
         *
         * @hide
         */
@@ -3135,7 +3135,7 @@ public final class Settings {
        }

        /**
         * These are all pulbic system settings
         * These are all public system settings
         *
         * @hide
         */
@@ -5371,6 +5371,13 @@ public final class Settings {
         */
        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.
         *
@@ -5434,6 +5441,7 @@ public final class Settings {
         * 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);
+1 −0
Original line number Diff line number Diff line
@@ -73,6 +73,7 @@ class ActivityManagerDebugConfig {
    static final boolean DEBUG_URI_PERMISSION = DEBUG_ALL || false;
    static final boolean DEBUG_USER_LEAVING = DEBUG_ALL || false;
    static final boolean DEBUG_VISIBILITY = DEBUG_ALL || false;
    static final boolean DEBUG_USAGE_STATS = DEBUG_ALL || true;

    static final String POSTFIX_BACKUP = (APPEND_CATEGORY_NAME) ? "_Backup" : "";
    static final String POSTFIX_BROADCAST = (APPEND_CATEGORY_NAME) ? "_Broadcast" : "";
+28 −2
Original line number Diff line number Diff line
@@ -43,6 +43,7 @@ import android.app.ITaskStackListener;
import android.app.ProfilerInfo;
import android.app.admin.DevicePolicyManager;
import android.app.usage.UsageEvents;
import android.app.usage.UsageStats;
import android.app.usage.UsageStatsManagerInternal;
import android.appwidget.AppWidgetManager;
import android.content.res.Resources;
@@ -61,8 +62,8 @@ import android.util.ArrayMap;
import android.util.ArraySet;
import android.util.DebugUtils;
import android.util.SparseIntArray;
import android.view.Display;
import com.android.internal.R;
import com.android.internal.annotations.GuardedBy;
import com.android.internal.app.DumpHeapActivity;
@@ -96,7 +97,6 @@ import com.android.server.pm.UserManagerService;
import com.android.server.statusbar.StatusBarManagerInternal;
import com.android.server.wm.AppTransition;
import com.android.server.wm.WindowManagerService;
import com.google.android.collect.Lists;
import com.google.android.collect.Maps;
@@ -17854,6 +17854,10 @@ public final class ActivityManagerService extends ActivityManagerNative
                app.lastCpuTime = app.curCpuTime;
            }
            // Inform UsageStats of important process state change
            // Must be called before updating setProcState
            maybeUpdateUsageStats(app);
            app.setProcState = app.curProcState;
            if (app.setProcState >= ActivityManager.PROCESS_STATE_HOME) {
                app.notCachedSinceIdle = false;
@@ -17916,6 +17920,28 @@ public final class ActivityManagerService extends ActivityManagerNative
        return success;
    }
    private void maybeUpdateUsageStats(ProcessRecord app) {
        if (DEBUG_USAGE_STATS) {
            Slog.d(TAG, "Checking proc [" + Arrays.toString(app.getPackageList())
                    + "] state changes: old = " + app.setProcState + ", new = "
                    + app.curProcState);
        }
        if (mUsageStatsService == null) {
            return;
        }
        if (app.curProcState <= ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND
                && (app.setProcState > ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND
                        || app.setProcState < 0)) {
            String[] packages = app.getPackageList();
            if (packages != null) {
                for (int i = 0; i < packages.length; i++) {
                    mUsageStatsService.reportEvent(packages[i], app.userId,
                            UsageEvents.Event.INTERACTION);
                }
            }
        }
    }
    private final void setProcessTrackerStateLocked(ProcessRecord proc, int memFactor, long now) {
        if (proc.thread != null) {
            if (proc.baseProcessTracker != null) {
+6 −1
Original line number Diff line number Diff line
@@ -51,6 +51,7 @@ import android.util.Slog;
import android.util.SparseArray;

import com.android.internal.app.IBatteryStats;
import com.android.server.job.controllers.AppIdleController;
import com.android.server.job.controllers.BatteryController;
import com.android.server.job.controllers.ConnectivityController;
import com.android.server.job.controllers.IdleController;
@@ -317,6 +318,7 @@ public class JobSchedulerService extends com.android.server.SystemService
        mControllers.add(TimeController.get(this));
        mControllers.add(IdleController.get(this));
        mControllers.add(BatteryController.get(this));
        mControllers.add(AppIdleController.get(this));

        mHandler = new JobHandler(context.getMainLooper());
        mJobSchedulerStub = new JobSchedulerStub();
@@ -688,7 +690,6 @@ public class JobSchedulerService extends com.android.server.SystemService
            final boolean jobPending = mPendingJobs.contains(job);
            final boolean jobActive = isCurrentlyActiveLocked(job);
            final boolean userRunning = mStartedUsers.contains(job.getUserId());

            if (DEBUG) {
                Slog.v(TAG, "isReadyToBeExecutedLocked: " + job.toShortString()
                        + " ready=" + jobReady + " pending=" + jobPending
@@ -738,6 +739,10 @@ public class JobSchedulerService extends com.android.server.SystemService
                        }
                    }
                    if (availableContext != null) {
                        if (DEBUG) {
                            Slog.d(TAG, "About to run job "
                                    + nextPending.getJob().getService().toString());
                        }
                        if (!availableContext.executeRunnableJob(nextPending)) {
                            if (DEBUG) {
                                Slog.d(TAG, "Error executing " + nextPending);
Loading