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