• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
33  * {@link JobInfo#BIAS_FOREGROUND_SERVICE} bias 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 JobRestrictions} are
58      * fine with it).
59      *
60      * @param job to be checked
61      * @return false if the {@link JobSchedulerService} should not schedule this job at the moment,
62      * true - otherwise
63      */
isJobRestricted(JobStatus job)64     public abstract boolean isJobRestricted(JobStatus job);
65 
66     /** Dump any internal constants the Restriction may have. */
dumpConstants(IndentingPrintWriter pw)67     public abstract void dumpConstants(IndentingPrintWriter pw);
68 
69     /** Dump any internal constants the Restriction may have. */
dumpConstants(ProtoOutputStream proto)70     public void dumpConstants(ProtoOutputStream proto) {
71     }
72 
73     /** @return reason code for the Restriction. */
74     @JobParameters.StopReason
getReason()75     public final int getReason() {
76         return mReason;
77     }
78 
getInternalReason()79     public final int getInternalReason() {
80         return mInternalReason;
81     }
82 }
83