Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server.content; |
| 18 | |
| 19 | import android.app.job.JobParameters; |
| 20 | import android.app.job.JobService; |
| 21 | import android.content.Intent; |
| 22 | import android.os.Message; |
| 23 | import android.os.Messenger; |
| 24 | import android.os.RemoteException; |
| 25 | import android.util.Log; |
| 26 | import android.util.Slog; |
| 27 | import android.util.SparseArray; |
| 28 | |
| 29 | public 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 Onuki | a9dca24 | 2017-06-21 17:06:49 -0700 | [diff] [blame] | 37 | private final SyncLogger mLogger = SyncLogger.getInstance(); |
| 38 | |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 39 | /** |
| 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 Onuki | a9dca24 | 2017-06-21 17:06:49 -0700 | [diff] [blame] | 68 | |
| 69 | mLogger.purgeOldLogs(); |
| 70 | |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 71 | 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 Onuki | a9dca24 | 2017-06-21 17:06:49 -0700 | [diff] [blame] | 78 | |
Makoto Onuki | 118c0da | 2017-07-14 10:27:03 -0700 | [diff] [blame] | 79 | mLogger.log("onStartJob() jobid=", params.getJobId(), " op=", op); |
Makoto Onuki | a9dca24 | 2017-06-21 17:06:49 -0700 | [diff] [blame] | 80 | |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 81 | 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 Onuki | a9dca24 | 2017-06-21 17:06:49 -0700 | [diff] [blame] | 99 | mLogger.log("onStopJob() ", mLogger.jobParametersToString(params)); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 100 | 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 Onuki | a9dca24 | 2017-06-21 17:06:49 -0700 | [diff] [blame] | 119 | public void callJobFinished(int jobId, boolean needsReschedule, String why) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 120 | synchronized (jobParamsMap) { |
| 121 | JobParameters params = jobParamsMap.get(jobId); |
Makoto Onuki | a9dca24 | 2017-06-21 17:06:49 -0700 | [diff] [blame] | 122 | mLogger.log("callJobFinished()", |
Makoto Onuki | 87d4262 | 2017-12-13 12:01:45 -0800 | [diff] [blame] | 123 | " jobid=", jobId, |
Makoto Onuki | a9dca24 | 2017-06-21 17:06:49 -0700 | [diff] [blame] | 124 | " needsReschedule=", needsReschedule, |
| 125 | " ", mLogger.jobParametersToString(params), |
| 126 | " why=", why); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 127 | 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 Onuki | 118c0da | 2017-07-14 10:27:03 -0700 | [diff] [blame] | 135 | } |