1 /* 2 * Copyright (C) 2014 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.controllers; 18 19 import android.content.Context; 20 import android.util.proto.ProtoOutputStream; 21 22 import com.android.internal.util.IndentingPrintWriter; 23 import com.android.server.job.JobSchedulerService; 24 import com.android.server.job.JobSchedulerService.Constants; 25 import com.android.server.job.StateChangedListener; 26 27 import java.util.function.Predicate; 28 29 /** 30 * Incorporates shared controller logic between the various controllers of the JobManager. 31 * These are solely responsible for tracking a list of jobs, and notifying the JM when these 32 * are ready to run, or whether they must be stopped. 33 */ 34 public abstract class StateController { 35 protected final JobSchedulerService mService; 36 protected final StateChangedListener mStateChangedListener; 37 protected final Context mContext; 38 protected final Object mLock; 39 protected final Constants mConstants; 40 41 StateController(JobSchedulerService service) { 42 mService = service; 43 mStateChangedListener = service; 44 mContext = service.getTestableContext(); 45 mLock = service.getLock(); 46 mConstants = service.getConstants(); 47 } 48 49 /** 50 * Implement the logic here to decide whether a job should be tracked by this controller. 51 * This logic is put here so the JobManager can be completely agnostic of Controller logic. 52 * Also called when updating a task, so implementing controllers have to be aware of 53 * preexisting tasks. 54 */ 55 public abstract void maybeStartTrackingJobLocked(JobStatus jobStatus, JobStatus lastJob); 56 /** 57 * Optionally implement logic here to prepare the job to be executed. 58 */ 59 public void prepareForExecutionLocked(JobStatus jobStatus) { 60 } 61 /** 62 * Remove task - this will happen if the task is cancelled, completed, etc. 63 */ 64 public abstract void maybeStopTrackingJobLocked(JobStatus jobStatus, JobStatus incomingJob, 65 boolean forUpdate); 66 /** 67 * Called when a new job is being created to reschedule an old failed job. 68 */ 69 public void rescheduleForFailureLocked(JobStatus newJob, JobStatus failureToReschedule) { 70 } 71 72 public abstract void dumpControllerStateLocked(IndentingPrintWriter pw, 73 Predicate<JobStatus> predicate); 74 public abstract void dumpControllerStateLocked(ProtoOutputStream proto, long fieldId, 75 Predicate<JobStatus> predicate); 76 } 77