From e7641e3b7c5e13d6cf11672bcd59b63c2c8bdce5 Mon Sep 17 00:00:00 2001 From: Varun Shah Date: Tue, 19 Nov 2024 00:04:13 +0000 Subject: Ensure thermal restriction is included in getJobPendingReasons API. Currently, if a job is pending because of multiple constraints, thermal restriction will not be included in the pending reasons since it was evaluated later. Update this logic so that apps know the device is in a bad state which could also be leading to the job not being executed. Bug: 372031023 Flag: android.app.job.get_pending_job_reasons_history_api Test: atest JobSchedulingTest Change-Id: Ia4c796725472dcb0cf9b0bd1ba4c98f739b1985c --- .../com/android/server/job/JobSchedulerService.java | 20 ++++++-------------- .../android/server/job/controllers/JobStatus.java | 12 +++++++++++- 2 files changed, 17 insertions(+), 15 deletions(-) (limited to 'apex') diff --git a/apex/jobscheduler/service/java/com/android/server/job/JobSchedulerService.java b/apex/jobscheduler/service/java/com/android/server/job/JobSchedulerService.java index 1c6e40e25a92..963307b110cf 100644 --- a/apex/jobscheduler/service/java/com/android/server/job/JobSchedulerService.java +++ b/apex/jobscheduler/service/java/com/android/server/job/JobSchedulerService.java @@ -2085,8 +2085,12 @@ public class JobSchedulerService extends com.android.server.SystemService if (DEBUG) { Slog.v(TAG, debugPrefix + " ready=" + jobReady); } - if (!jobReady) { - return job.getPendingJobReasons(); + final JobRestriction restriction = checkIfRestricted(job); + if (DEBUG) { + Slog.v(TAG, debugPrefix + " restriction=" + restriction); + } + if (!jobReady || restriction != null) { + return job.getPendingJobReasons(restriction); } final boolean userStarted = areUsersStartedLocked(job); @@ -2106,18 +2110,6 @@ public class JobSchedulerService extends com.android.server.SystemService return new int[] { JobScheduler.PENDING_JOB_REASON_APP }; } - final JobRestriction restriction = checkIfRestricted(job); - if (DEBUG) { - Slog.v(TAG, debugPrefix + " restriction=" + restriction); - } - if (restriction != null) { - // Currently this will return _DEVICE_STATE because of thermal reasons. - // TODO (b/372031023): does it make sense to move this along with the - // pendingJobReasons() call above and also get the pending reasons from - // all of the restriction controllers? - return new int[] { restriction.getPendingReason() }; - } - // The following can be a little more expensive, so we are doing it later, // but still before checking with the package manager! final boolean jobPending = mPendingJobQueue.contains(job); diff --git a/apex/jobscheduler/service/java/com/android/server/job/controllers/JobStatus.java b/apex/jobscheduler/service/java/com/android/server/job/controllers/JobStatus.java index b0784f1c69fd..a3eaefd5f057 100644 --- a/apex/jobscheduler/service/java/com/android/server/job/controllers/JobStatus.java +++ b/apex/jobscheduler/service/java/com/android/server/job/controllers/JobStatus.java @@ -66,6 +66,7 @@ import com.android.server.job.JobSchedulerService; import com.android.server.job.JobServerProtoEnums; import com.android.server.job.JobStatusDumpProto; import com.android.server.job.JobStatusShortInfoProto; +import com.android.server.job.restrictions.JobRestriction; import dalvik.annotation.optimization.NeverCompile; @@ -2179,11 +2180,20 @@ public final class JobStatus { * This will return all potential reasons why the job is pending. */ @NonNull - public int[] getPendingJobReasons() { + public int[] getPendingJobReasons(@Nullable JobRestriction restriction) { final int unsatisfiedConstraints = ~satisfiedConstraints & (requiredConstraints | mDynamicConstraints | IMPLICIT_CONSTRAINTS); final ArrayList reasons = constraintsToPendingJobReasons(unsatisfiedConstraints); + if (restriction != null) { + // Currently only ThermalStatusRestriction extends the JobRestriction class and + // returns PENDING_JOB_REASON_DEVICE_STATE if the job is restricted because of thermal. + @JobScheduler.PendingJobReason final int reason = restriction.getPendingReason(); + if (!reasons.contains(reason)) { + reasons.addLast(reason); + } + } + if (reasons.isEmpty()) { if (getEffectiveStandbyBucket() == NEVER_INDEX) { Slog.wtf(TAG, "App in NEVER bucket querying pending job reason"); -- cgit v1.2.3-59-g8ed1b