1 /* 2 * Copyright (C) 2019 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 com.android.server.job.restrictions; 18 19 import android.app.job.JobInfo; 20 import android.app.job.JobParameters; 21 import android.app.job.JobScheduler; 22 import android.util.IndentingPrintWriter; 23 import android.util.proto.ProtoOutputStream; 24 25 import com.android.server.job.JobSchedulerService; 26 import com.android.server.job.controllers.JobStatus; 27 28 /** 29 * Used by {@link JobSchedulerService} to impose additional restrictions regarding whether jobs 30 * should be scheduled or not based on the state of the system/device. 31 * Every restriction is associated with exactly one stop reason, which could be retrieved using 32 * {@link #getStopReason()}, one pending reason (retrievable via {@link #getPendingReason()}, 33 * (and the internal reason via {@link #getInternalReason()}). 34 * Note, that this is not taken into account for the jobs that have 35 * {@link JobInfo#BIAS_FOREGROUND_SERVICE} bias or higher. 36 */ 37 public abstract class JobRestriction { 38 39 final JobSchedulerService mService; 40 private final int mStopReason; 41 private final int mPendingReason; 42 private final int mInternalReason; 43 JobRestriction(JobSchedulerService service, @JobParameters.StopReason int stopReason, @JobScheduler.PendingJobReason int pendingReason, int internalReason)44 protected JobRestriction(JobSchedulerService service, @JobParameters.StopReason int stopReason, 45 @JobScheduler.PendingJobReason int pendingReason, int internalReason) { 46 mService = service; 47 mPendingReason = pendingReason; 48 mStopReason = stopReason; 49 mInternalReason = internalReason; 50 } 51 52 /** 53 * Called when the system boot phase has reached 54 * {@link com.android.server.SystemService#PHASE_SYSTEM_SERVICES_READY}. 55 */ onSystemServicesReady()56 public void onSystemServicesReady() { 57 } 58 59 /** 60 * Called by {@link JobSchedulerService} to check if it may proceed with scheduling the job (in 61 * case all constraints are satisfied and all other {@link JobRestriction JobRestrictions} are 62 * fine with it). 63 * 64 * @param job to be checked 65 * @param bias job bias to be checked 66 * @return false if the {@link JobSchedulerService} should not schedule this job at the moment, 67 * true - otherwise 68 */ isJobRestricted(JobStatus job, int bias)69 public abstract boolean isJobRestricted(JobStatus job, int bias); 70 71 /** Dump any internal constants the Restriction may have. */ dumpConstants(IndentingPrintWriter pw)72 public abstract void dumpConstants(IndentingPrintWriter pw); 73 74 /** Dump any internal constants the Restriction may have. */ dumpConstants(ProtoOutputStream proto)75 public void dumpConstants(ProtoOutputStream proto) { 76 } 77 78 @JobScheduler.PendingJobReason getPendingReason()79 public final int getPendingReason() { 80 return mPendingReason; 81 } 82 83 /** @return stop reason code for the Restriction. */ 84 @JobParameters.StopReason getStopReason()85 public final int getStopReason() { 86 return mStopReason; 87 } 88 getInternalReason()89 public final int getInternalReason() { 90 return mInternalReason; 91 } 92 } 93