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

Commit f3c514f1 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Fix UI job BS & Doze exemption."

parents 97210100 a832dda2
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -2545,6 +2545,7 @@ public class JobSchedulerService extends com.android.server.SystemService
        final JobStatus rescheduledJob = needsReschedule
                ? getRescheduleJobForFailureLocked(jobStatus, debugStopReason) : null;
        if (rescheduledJob != null
                && !rescheduledJob.shouldTreatAsUserInitiatedJob()
                && (debugStopReason == JobParameters.INTERNAL_STOP_REASON_TIMEOUT
                || debugStopReason == JobParameters.INTERNAL_STOP_REASON_PREEMPT)) {
            rescheduledJob.disallowRunInBatterySaverAndDoze();
+6 −4
Original line number Diff line number Diff line
@@ -1411,15 +1411,17 @@ public final class JobStatus {
    public boolean canRunInDoze() {
        return appHasDozeExemption
                || (getFlags() & JobInfo.FLAG_WILL_BE_FOREGROUND) != 0
                || ((shouldTreatAsExpeditedJob() || startedAsExpeditedJob)
                || shouldTreatAsUserInitiatedJob()
                // EJs can't run in Doze if we explicitly require that the device is not Dozing.
                || ((shouldTreatAsExpeditedJob() || startedAsExpeditedJob)
                        && (mDynamicConstraints & CONSTRAINT_DEVICE_NOT_DOZING) == 0);
    }

    boolean canRunInBatterySaver() {
        return (getInternalFlags() & INTERNAL_FLAG_HAS_FOREGROUND_EXEMPTION) != 0
                || ((shouldTreatAsExpeditedJob() || startedAsExpeditedJob)
                || shouldTreatAsUserInitiatedJob()
                // EJs can't run in Battery Saver if we explicitly require that Battery Saver is off
                || ((shouldTreatAsExpeditedJob() || startedAsExpeditedJob)
                        && (mDynamicConstraints & CONSTRAINT_BACKGROUND_NOT_RESTRICTED) == 0);
    }

+100 −0
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ package com.android.server.job.controllers;
import static com.android.dx.mockito.inline.extended.ExtendedMockito.doReturn;
import static com.android.dx.mockito.inline.extended.ExtendedMockito.mock;
import static com.android.dx.mockito.inline.extended.ExtendedMockito.mockitoSession;
import static com.android.dx.mockito.inline.extended.ExtendedMockito.spyOn;
import static com.android.server.job.JobSchedulerService.ACTIVE_INDEX;
import static com.android.server.job.JobSchedulerService.FREQUENT_INDEX;
import static com.android.server.job.JobSchedulerService.NEVER_INDEX;
@@ -131,6 +132,98 @@ public class JobStatusTest {
        }
    }

    @Test
    public void testCanRunInBatterySaver_regular() {
        final JobInfo jobInfo =
                new JobInfo.Builder(101, new ComponentName("foo", "bar")).build();
        JobStatus job = createJobStatus(jobInfo);
        assertFalse(job.canRunInBatterySaver());
        job.disallowRunInBatterySaverAndDoze();
        assertFalse(job.canRunInBatterySaver());
    }

    @Test
    public void testCanRunInBatterySaver_expedited() {
        final JobInfo jobInfo =
                new JobInfo.Builder(101, new ComponentName("foo", "bar"))
                        .setExpedited(true)
                        .build();
        JobStatus job = createJobStatus(jobInfo);
        markExpeditedQuotaApproved(job, true);
        assertTrue(job.canRunInBatterySaver());
        job.disallowRunInBatterySaverAndDoze();
        assertFalse(job.canRunInBatterySaver());

        // Reset the job
        job = createJobStatus(jobInfo);
        markExpeditedQuotaApproved(job, true);
        spyOn(job);
        when(job.shouldTreatAsExpeditedJob()).thenReturn(false);
        job.startedAsExpeditedJob = true;
        assertTrue(job.canRunInBatterySaver());
        job.disallowRunInBatterySaverAndDoze();
        assertFalse(job.canRunInBatterySaver());
    }

    @Test
    public void testCanRunInBatterySaver_userInitiated() {
        final JobInfo jobInfo =
                new JobInfo.Builder(101, new ComponentName("foo", "bar"))
                        .setUserInitiated(true)
                        .build();
        JobStatus job = createJobStatus(jobInfo);
        assertTrue(job.canRunInBatterySaver());
        // User-initiated privilege should trump bs & doze requirement.
        job.disallowRunInBatterySaverAndDoze();
        assertTrue(job.canRunInBatterySaver());
    }

    @Test
    public void testCanRunInDoze_regular() {
        final JobInfo jobInfo =
                new JobInfo.Builder(101, new ComponentName("foo", "bar")).build();
        JobStatus job = createJobStatus(jobInfo);
        assertFalse(job.canRunInDoze());
        job.disallowRunInBatterySaverAndDoze();
        assertFalse(job.canRunInDoze());
    }

    @Test
    public void testCanRunInDoze_expedited() {
        final JobInfo jobInfo =
                new JobInfo.Builder(101, new ComponentName("foo", "bar"))
                        .setExpedited(true)
                        .build();
        JobStatus job = createJobStatus(jobInfo);
        markExpeditedQuotaApproved(job, true);
        assertTrue(job.canRunInDoze());
        job.disallowRunInBatterySaverAndDoze();
        assertFalse(job.canRunInDoze());

        // Reset the job
        job = createJobStatus(jobInfo);
        markExpeditedQuotaApproved(job, true);
        spyOn(job);
        when(job.shouldTreatAsExpeditedJob()).thenReturn(false);
        job.startedAsExpeditedJob = true;
        assertTrue(job.canRunInDoze());
        job.disallowRunInBatterySaverAndDoze();
        assertFalse(job.canRunInDoze());
    }

    @Test
    public void testCanRunInDoze_userInitiated() {
        final JobInfo jobInfo =
                new JobInfo.Builder(101, new ComponentName("foo", "bar"))
                        .setUserInitiated(true)
                        .build();
        JobStatus job = createJobStatus(jobInfo);
        assertTrue(job.canRunInDoze());
        // User-initiated privilege should trump bs & doze requirement.
        job.disallowRunInBatterySaverAndDoze();
        assertTrue(job.canRunInDoze());
    }

    @Test
    public void testMediaBackupExemption_lateConstraint() {
        final JobInfo triggerContentJob = new JobInfo.Builder(42, TEST_JOB_COMPONENT)
@@ -907,6 +1000,13 @@ public class JobStatusTest {
        assertFalse(job.readinessStatusWithConstraint(CONSTRAINT_FLEXIBLE, false));
    }

    private void markExpeditedQuotaApproved(JobStatus job, boolean isApproved) {
        if (job.isRequestedExpeditedJob()) {
            job.setExpeditedJobQuotaApproved(sElapsedRealtimeClock.millis(), isApproved);
            job.setExpeditedJobTareApproved(sElapsedRealtimeClock.millis(), isApproved);
        }
    }

    private void markImplicitConstraintsSatisfied(JobStatus job, boolean isSatisfied) {
        job.setQuotaConstraintSatisfied(sElapsedRealtimeClock.millis(), isSatisfied);
        job.setTareWealthConstraintSatisfied(sElapsedRealtimeClock.millis(), isSatisfied);