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      * @return false if the {@link JobSchedulerService} should not schedule this job at the moment,
66      * true - otherwise
67      */
isJobRestricted(JobStatus job)68     public abstract boolean isJobRestricted(JobStatus job);
69 
70     /** Dump any internal constants the Restriction may have. */
dumpConstants(IndentingPrintWriter pw)71     public abstract void dumpConstants(IndentingPrintWriter pw);
72 
73     /** Dump any internal constants the Restriction may have. */
dumpConstants(ProtoOutputStream proto)74     public void dumpConstants(ProtoOutputStream proto) {
75     }
76 
77     @JobScheduler.PendingJobReason
getPendingReason()78     public final int getPendingReason() {
79         return mPendingReason;
80     }
81 
82     /** @return stop reason code for the Restriction. */
83     @JobParameters.StopReason
getStopReason()84     public final int getStopReason() {
85         return mStopReason;
86     }
87 
getInternalReason()88     public final int getInternalReason() {
89         return mInternalReason;
90     }
91 }
92