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

Commit a8456872 authored by Kweku Adams's avatar Kweku Adams
Browse files

Stop overrunning jobs when doze and battery saver turn on.

If we let a job run longer than its minimum runtime guarantee, make sure
we stop it as soon as battery saver or doze turns on.

Bug: 171305774
Test: atest CtsJobSchedulerTestCases
Change-Id: Ibe378da7393740dfa3712394dd8bca8c23315027
parent 156864ee
Loading
Loading
Loading
Loading
+28 −0
Original line number Diff line number Diff line
@@ -317,6 +317,8 @@ class JobConcurrencyManager {

        final IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        filter.addAction(PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED);
        filter.addAction(PowerManager.ACTION_POWER_SAVE_MODE_CHANGED);
        mContext.registerReceiver(mReceiver, filter);
        try {
            ActivityManager.getService().registerUserSwitchObserver(mGracePeriodObserver, TAG);
@@ -340,6 +342,20 @@ class JobConcurrencyManager {
                case Intent.ACTION_SCREEN_OFF:
                    onInteractiveStateChanged(false);
                    break;
                case PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED:
                    if (mPowerManager != null && mPowerManager.isDeviceIdleMode()) {
                        synchronized (mLock) {
                            stopLongRunningJobsLocked("deep doze");
                        }
                    }
                    break;
                case PowerManager.ACTION_POWER_SAVE_MODE_CHANGED:
                    if (mPowerManager != null && mPowerManager.isPowerSaveMode()) {
                        synchronized (mLock) {
                            stopLongRunningJobsLocked("battery saver");
                        }
                    }
                    break;
            }
        }
    };
@@ -634,6 +650,18 @@ class JobConcurrencyManager {
        noteConcurrency();
    }

    @GuardedBy("mLock")
    private void stopLongRunningJobsLocked(@NonNull String debugReason) {
        for (int i = 0; i < MAX_JOB_CONTEXTS_COUNT; ++i) {
            final JobServiceContext jsc = mService.mActiveServices.get(i);
            final JobStatus jobStatus = jsc.getRunningJobLocked();

            if (jobStatus != null && !jsc.isWithinExecutionGuaranteeTime()) {
                jsc.cancelExecutingJobLocked(JobParameters.REASON_TIMEOUT, debugReason);
            }
        }
    }

    private void noteConcurrency() {
        mService.mJobPackageTracker.noteConcurrency(mRunningJobs.size(),
                // TODO: log per type instead of only TOP