1 /* 2 * Copyright (C) 2021 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.car.builtin.job; 18 19 import android.annotation.SystemApi; 20 import android.app.job.JobInfo; 21 import android.app.job.JobScheduler; 22 import android.app.job.JobSnapshot; 23 import android.car.builtin.annotation.AddedIn; 24 import android.car.builtin.annotation.PlatformVersion; 25 import android.content.Context; 26 27 import java.util.List; 28 29 /** 30 * Helper for JobScheduler related operations. 31 * 32 * @hide 33 */ 34 @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) 35 public final class JobSchedulerHelper { 36 JobSchedulerHelper()37 private JobSchedulerHelper() { 38 throw new UnsupportedOperationException("contains only static members"); 39 } 40 41 /** Gets the number of running jobs which are executed when a device goes idle. */ 42 @AddedIn(PlatformVersion.TIRAMISU_0) getNumberOfRunningJobsAtIdle(Context context)43 public static int getNumberOfRunningJobsAtIdle(Context context) { 44 List<JobInfo> startedJobs = context.getSystemService(JobScheduler.class).getStartedJobs(); 45 if (startedJobs == null) { 46 return 0; 47 } 48 int jobCount = 0; 49 for (int idx = 0; idx < startedJobs.size(); idx++) { 50 JobInfo jobInfo = startedJobs.get(idx); 51 if (jobInfo.isRequireDeviceIdle()) { 52 jobCount++; 53 } 54 } 55 return jobCount; 56 } 57 58 /** Gets the number of jobs which are scheduled for execution at idle but not finished. */ 59 @AddedIn(PlatformVersion.TIRAMISU_0) getNumberOfPendingJobs(Context context)60 public static int getNumberOfPendingJobs(Context context) { 61 List<JobSnapshot> allScheduledJobs = 62 context.getSystemService(JobScheduler.class).getAllJobSnapshots(); 63 if (allScheduledJobs == null) { 64 return 0; 65 } 66 int jobCount = 0; 67 for (int idx = 0; idx < jobCount; idx++) { 68 JobSnapshot scheduledJob = allScheduledJobs.get(idx); 69 JobInfo jobInfo = scheduledJob.getJobInfo(); 70 if (scheduledJob.isRunnable() && jobInfo.isRequireDeviceIdle()) { 71 jobCount++; 72 } 73 } 74 return jobCount; 75 } 76 } 77