blob: bfcc629541f851fcdf0dc6559a552001d4dfebd7 [file] [log] [blame]
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001/*
2 * Copyright (C) 2015 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
17package com.android.server.content;
18
Makoto Onuki3ab77812018-07-09 14:29:33 -070019import android.annotation.Nullable;
Shreyas Basarge8c834c02016-01-07 13:53:16 +000020import android.app.job.JobParameters;
21import android.app.job.JobService;
Shreyas Basarge8c834c02016-01-07 13:53:16 +000022import android.os.Message;
Makoto Onuki7273aad2018-01-11 15:51:23 -080023import android.os.SystemClock;
Shreyas Basarge8c834c02016-01-07 13:53:16 +000024import android.util.Log;
25import android.util.Slog;
26import android.util.SparseArray;
Makoto Onuki5397be12017-12-20 16:22:34 +090027import android.util.SparseBooleanArray;
Makoto Onuki7273aad2018-01-11 15:51:23 -080028import android.util.SparseLongArray;
Makoto Onuki5397be12017-12-20 16:22:34 +090029
30import com.android.internal.annotations.GuardedBy;
Shreyas Basarge8c834c02016-01-07 13:53:16 +000031
32public class SyncJobService extends JobService {
33 private static final String TAG = "SyncManager";
34
Makoto Onuki3ab77812018-07-09 14:29:33 -070035 private static final Object sLock = new Object();
Shreyas Basarge8c834c02016-01-07 13:53:16 +000036
Makoto Onuki3ab77812018-07-09 14:29:33 -070037 @GuardedBy("sLock")
38 private static SyncJobService sInstance;
Makoto Onuki5397be12017-12-20 16:22:34 +090039
Makoto Onuki3ab77812018-07-09 14:29:33 -070040 @GuardedBy("sLock")
41 private static final SparseArray<JobParameters> sJobParamsMap = new SparseArray<>();
Makoto Onuki5397be12017-12-20 16:22:34 +090042
Makoto Onuki3ab77812018-07-09 14:29:33 -070043 @GuardedBy("sLock")
44 private static final SparseBooleanArray sStartedSyncs = new SparseBooleanArray();
Makoto Onuki5397be12017-12-20 16:22:34 +090045
Makoto Onuki3ab77812018-07-09 14:29:33 -070046 @GuardedBy("sLock")
47 private static final SparseLongArray sJobStartUptimes = new SparseLongArray();
Shreyas Basarge8c834c02016-01-07 13:53:16 +000048
Makoto Onuki3ab77812018-07-09 14:29:33 -070049 private static final SyncLogger sLogger = SyncLogger.getInstance();
Makoto Onuki7273aad2018-01-11 15:51:23 -080050
Makoto Onuki3ab77812018-07-09 14:29:33 -070051 private void updateInstance() {
52 synchronized (SyncJobService.class) {
53 sInstance = this;
54 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +000055 }
56
Makoto Onuki3ab77812018-07-09 14:29:33 -070057 @Nullable
58 private static SyncJobService getInstance() {
59 synchronized (sLock) {
60 if (sInstance == null) {
61 Slog.wtf(TAG, "sInstance == null");
62 }
63 return sInstance;
Shreyas Basarge8c834c02016-01-07 13:53:16 +000064 }
Makoto Onuki3ab77812018-07-09 14:29:33 -070065 }
66
67 public static boolean isReady() {
68 synchronized (sLock) {
69 return sInstance != null;
Shreyas Basarge8c834c02016-01-07 13:53:16 +000070 }
71 }
72
73 @Override
74 public boolean onStartJob(JobParameters params) {
Makoto Onuki3ab77812018-07-09 14:29:33 -070075 updateInstance();
Makoto Onukia9dca242017-06-21 17:06:49 -070076
Makoto Onuki3ab77812018-07-09 14:29:33 -070077 sLogger.purgeOldLogs();
78
79 SyncOperation op = SyncOperation.maybeCreateFromJobExtras(params.getExtras());
80
81 if (op == null) {
82 Slog.wtf(TAG, "Got invalid job " + params.getJobId());
83 return false;
84 }
85
86 final boolean readyToSync = SyncManager.readyToSync(op.target.userId);
87
88 sLogger.log("onStartJob() jobid=", params.getJobId(), " op=", op,
89 " readyToSync", readyToSync);
90
91 if (!readyToSync) {
92 // If the user isn't unlocked or the device has been provisioned yet, just stop the job
93 // at this point. If it's a non-periodic sync, ask the job scheduler to reschedule it.
94 // If it's a periodic sync, then just wait until the next cycle.
95 final boolean wantsReschedule = !op.isPeriodic;
96 jobFinished(params, wantsReschedule);
97 return true;
98 }
Makoto Onukia9dca242017-06-21 17:06:49 -070099
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000100 boolean isLoggable = Log.isLoggable(TAG, Log.VERBOSE);
Makoto Onuki3ab77812018-07-09 14:29:33 -0700101 synchronized (sLock) {
Makoto Onuki5397be12017-12-20 16:22:34 +0900102 final int jobId = params.getJobId();
Makoto Onuki3ab77812018-07-09 14:29:33 -0700103 sJobParamsMap.put(jobId, params);
Makoto Onuki7273aad2018-01-11 15:51:23 -0800104
Makoto Onuki3ab77812018-07-09 14:29:33 -0700105 sStartedSyncs.delete(jobId);
106 sJobStartUptimes.put(jobId, SystemClock.uptimeMillis());
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000107 }
108 Message m = Message.obtain();
109 m.what = SyncManager.SyncHandler.MESSAGE_START_SYNC;
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000110 if (isLoggable) {
111 Slog.v(TAG, "Got start job message " + op.target);
112 }
113 m.obj = op;
Makoto Onuki3ab77812018-07-09 14:29:33 -0700114 SyncManager.sendMessage(m);
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000115 return true;
116 }
117
118 @Override
119 public boolean onStopJob(JobParameters params) {
120 if (Log.isLoggable(TAG, Log.VERBOSE)) {
121 Slog.v(TAG, "onStopJob called " + params.getJobId() + ", reason: "
122 + params.getStopReason());
123 }
Makoto Onuki3ab77812018-07-09 14:29:33 -0700124 final SyncOperation op = SyncOperation.maybeCreateFromJobExtras(params.getExtras());
125 if (op == null) {
126 Slog.wtf(TAG, "Got invalid job " + params.getJobId());
127 return false;
128 }
Makoto Onuki056a9752018-05-08 15:21:56 -0700129
Makoto Onuki3ab77812018-07-09 14:29:33 -0700130 final boolean readyToSync = SyncManager.readyToSync(op.target.userId);
131
132 sLogger.log("onStopJob() ", sLogger.jobParametersToString(params),
Makoto Onuki056a9752018-05-08 15:21:56 -0700133 " readyToSync=", readyToSync);
Makoto Onuki5397be12017-12-20 16:22:34 +0900134
Makoto Onuki3ab77812018-07-09 14:29:33 -0700135 synchronized (sLock) {
136 final int jobId = params.getJobId();
137 sJobParamsMap.remove(jobId);
138
139 final long startUptime = sJobStartUptimes.get(jobId);
Makoto Onuki7273aad2018-01-11 15:51:23 -0800140 final long nowUptime = SystemClock.uptimeMillis();
Makoto Onukid2bfec62018-01-12 13:58:01 -0800141 final long runtime = nowUptime - startUptime;
142
Makoto Onuki056a9752018-05-08 15:21:56 -0700143
Makoto Onuki7273aad2018-01-11 15:51:23 -0800144 if (startUptime == 0) {
145 wtf("Job " + jobId + " start uptime not found: "
146 + " params=" + jobParametersToString(params));
Makoto Onukid2bfec62018-01-12 13:58:01 -0800147 } else if (runtime > 60 * 1000) {
Makoto Onuki7273aad2018-01-11 15:51:23 -0800148 // WTF if startSyncH() hasn't happened, *unless* onStopJob() was called too soon.
149 // (1 minute threshold.)
Makoto Onuki056a9752018-05-08 15:21:56 -0700150 // Also don't wtf when it's not ready to sync.
Makoto Onuki3ab77812018-07-09 14:29:33 -0700151 if (readyToSync && !sStartedSyncs.get(jobId)) {
Makoto Onuki7273aad2018-01-11 15:51:23 -0800152 wtf("Job " + jobId + " didn't start: "
153 + " startUptime=" + startUptime
154 + " nowUptime=" + nowUptime
155 + " params=" + jobParametersToString(params));
156 }
Makoto Onuki5397be12017-12-20 16:22:34 +0900157 }
158
Makoto Onuki3ab77812018-07-09 14:29:33 -0700159 sStartedSyncs.delete(jobId);
160 sJobStartUptimes.delete(jobId);
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000161 }
162 Message m = Message.obtain();
163 m.what = SyncManager.SyncHandler.MESSAGE_STOP_SYNC;
Makoto Onuki3ab77812018-07-09 14:29:33 -0700164 m.obj = op;
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000165
166 // Reschedule if this job was NOT explicitly canceled.
167 m.arg1 = params.getStopReason() != JobParameters.REASON_CANCELED ? 1 : 0;
168 // Apply backoff only if stop is called due to timeout.
169 m.arg2 = params.getStopReason() == JobParameters.REASON_TIMEOUT ? 1 : 0;
170
Makoto Onuki3ab77812018-07-09 14:29:33 -0700171 SyncManager.sendMessage(m);
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000172 return false;
173 }
174
Makoto Onuki3ab77812018-07-09 14:29:33 -0700175 public static void callJobFinished(int jobId, boolean needsReschedule, String why) {
176 final SyncJobService instance = getInstance();
177 if (instance != null) {
178 instance.callJobFinishedInner(jobId, needsReschedule, why);
179 }
180 }
181
182 public void callJobFinishedInner(int jobId, boolean needsReschedule, String why) {
183 synchronized (sLock) {
184 JobParameters params = sJobParamsMap.get(jobId);
185 sLogger.log("callJobFinished()",
Makoto Onuki87d42622017-12-13 12:01:45 -0800186 " jobid=", jobId,
Makoto Onukia9dca242017-06-21 17:06:49 -0700187 " needsReschedule=", needsReschedule,
Makoto Onuki3ab77812018-07-09 14:29:33 -0700188 " ", sLogger.jobParametersToString(params),
Makoto Onukia9dca242017-06-21 17:06:49 -0700189 " why=", why);
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000190 if (params != null) {
191 jobFinished(params, needsReschedule);
Makoto Onuki3ab77812018-07-09 14:29:33 -0700192 sJobParamsMap.remove(jobId);
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000193 } else {
194 Slog.e(TAG, "Job params not found for " + String.valueOf(jobId));
195 }
196 }
197 }
Makoto Onuki5397be12017-12-20 16:22:34 +0900198
Makoto Onuki3ab77812018-07-09 14:29:33 -0700199 public static void markSyncStarted(int jobId) {
200 synchronized (sLock) {
201 sStartedSyncs.put(jobId, true);
Makoto Onuki5397be12017-12-20 16:22:34 +0900202 }
203 }
204
205 public static String jobParametersToString(JobParameters params) {
206 if (params == null) {
207 return "job:null";
208 } else {
209 return "job:#" + params.getJobId() + ":"
Makoto Onukid2bfec62018-01-12 13:58:01 -0800210 + "sr=[" + params.getStopReason() + "/" + params.getDebugStopReason() + "]:"
Makoto Onuki5397be12017-12-20 16:22:34 +0900211 + SyncOperation.maybeCreateFromJobExtras(params.getExtras());
212 }
213 }
Makoto Onuki7273aad2018-01-11 15:51:23 -0800214
Makoto Onuki3ab77812018-07-09 14:29:33 -0700215 private static void wtf(String message) {
216 sLogger.log(message);
Makoto Onuki7273aad2018-01-11 15:51:23 -0800217 Slog.wtf(TAG, message);
218 }
Makoto Onuki118c0da2017-07-14 10:27:03 -0700219}