blob: 29b322eaff10333f6a2dc6f0c8c31435b6fce8b6 [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
19import android.app.job.JobParameters;
20import android.app.job.JobService;
21import android.content.Intent;
22import android.os.Message;
23import android.os.Messenger;
24import android.os.RemoteException;
25import android.util.Log;
26import android.util.Slog;
27import android.util.SparseArray;
28
29public class SyncJobService extends JobService {
30 private static final String TAG = "SyncManager";
31
32 public static final String EXTRA_MESSENGER = "messenger";
33
34 private Messenger mMessenger;
35 private SparseArray<JobParameters> jobParamsMap = new SparseArray<JobParameters>();
36
Makoto Onukia9dca242017-06-21 17:06:49 -070037 private final SyncLogger mLogger = SyncLogger.getInstance();
38
Shreyas Basarge8c834c02016-01-07 13:53:16 +000039 /**
40 * This service is started by the SyncManager which passes a messenger object to
41 * communicate back with it. It never stops while the device is running.
42 */
43 @Override
44 public int onStartCommand(Intent intent, int flags, int startId) {
45 mMessenger = intent.getParcelableExtra(EXTRA_MESSENGER);
46 Message m = Message.obtain();
47 m.what = SyncManager.SyncHandler.MESSAGE_JOBSERVICE_OBJECT;
48 m.obj = this;
49 sendMessage(m);
50
51 return START_NOT_STICKY;
52 }
53
54 private void sendMessage(Message message) {
55 if (mMessenger == null) {
56 Slog.e(TAG, "Messenger not initialized.");
57 return;
58 }
59 try {
60 mMessenger.send(message);
61 } catch (RemoteException e) {
62 Slog.e(TAG, e.toString());
63 }
64 }
65
66 @Override
67 public boolean onStartJob(JobParameters params) {
Makoto Onukia9dca242017-06-21 17:06:49 -070068
69 mLogger.purgeOldLogs();
70
Shreyas Basarge8c834c02016-01-07 13:53:16 +000071 boolean isLoggable = Log.isLoggable(TAG, Log.VERBOSE);
72 synchronized (jobParamsMap) {
73 jobParamsMap.put(params.getJobId(), params);
74 }
75 Message m = Message.obtain();
76 m.what = SyncManager.SyncHandler.MESSAGE_START_SYNC;
77 SyncOperation op = SyncOperation.maybeCreateFromJobExtras(params.getExtras());
Makoto Onukia9dca242017-06-21 17:06:49 -070078
Makoto Onuki118c0da2017-07-14 10:27:03 -070079 mLogger.log("onStartJob() jobid=", params.getJobId(), " op=", op);
Makoto Onukia9dca242017-06-21 17:06:49 -070080
Shreyas Basarge8c834c02016-01-07 13:53:16 +000081 if (op == null) {
82 Slog.e(TAG, "Got invalid job " + params.getJobId());
83 return false;
84 }
85 if (isLoggable) {
86 Slog.v(TAG, "Got start job message " + op.target);
87 }
88 m.obj = op;
89 sendMessage(m);
90 return true;
91 }
92
93 @Override
94 public boolean onStopJob(JobParameters params) {
95 if (Log.isLoggable(TAG, Log.VERBOSE)) {
96 Slog.v(TAG, "onStopJob called " + params.getJobId() + ", reason: "
97 + params.getStopReason());
98 }
Makoto Onukia9dca242017-06-21 17:06:49 -070099 mLogger.log("onStopJob() ", mLogger.jobParametersToString(params));
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000100 synchronized (jobParamsMap) {
101 jobParamsMap.remove(params.getJobId());
102 }
103 Message m = Message.obtain();
104 m.what = SyncManager.SyncHandler.MESSAGE_STOP_SYNC;
105 m.obj = SyncOperation.maybeCreateFromJobExtras(params.getExtras());
106 if (m.obj == null) {
107 return false;
108 }
109
110 // Reschedule if this job was NOT explicitly canceled.
111 m.arg1 = params.getStopReason() != JobParameters.REASON_CANCELED ? 1 : 0;
112 // Apply backoff only if stop is called due to timeout.
113 m.arg2 = params.getStopReason() == JobParameters.REASON_TIMEOUT ? 1 : 0;
114
115 sendMessage(m);
116 return false;
117 }
118
Makoto Onukia9dca242017-06-21 17:06:49 -0700119 public void callJobFinished(int jobId, boolean needsReschedule, String why) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000120 synchronized (jobParamsMap) {
121 JobParameters params = jobParamsMap.get(jobId);
Makoto Onukia9dca242017-06-21 17:06:49 -0700122 mLogger.log("callJobFinished()",
Makoto Onuki87d42622017-12-13 12:01:45 -0800123 " jobid=", jobId,
Makoto Onukia9dca242017-06-21 17:06:49 -0700124 " needsReschedule=", needsReschedule,
125 " ", mLogger.jobParametersToString(params),
126 " why=", why);
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000127 if (params != null) {
128 jobFinished(params, needsReschedule);
129 jobParamsMap.remove(jobId);
130 } else {
131 Slog.e(TAG, "Job params not found for " + String.valueOf(jobId));
132 }
133 }
134 }
Makoto Onuki118c0da2017-07-14 10:27:03 -0700135}