blob: 05a3f119c7d60d48780be2a1d361707bed3a406b [file] [log] [blame]
Steve McKay14e827a2016-01-06 18:32:13 -08001/*
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.documentsui.services;
18
Steve McKay14e827a2016-01-06 18:32:13 -080019import static com.android.documentsui.Shared.DEBUG;
20import static com.android.internal.util.Preconditions.checkArgument;
21import static com.android.internal.util.Preconditions.checkNotNull;
22import static com.android.internal.util.Preconditions.checkState;
23
24import android.annotation.IntDef;
Steve McKay14e827a2016-01-06 18:32:13 -080025import android.app.NotificationManager;
Steve McKayecbf3c52016-01-13 17:17:39 -080026import android.app.Service;
Steve McKay14e827a2016-01-06 18:32:13 -080027import android.content.Intent;
Steve McKayecbf3c52016-01-13 17:17:39 -080028import android.os.IBinder;
Steve McKay14e827a2016-01-06 18:32:13 -080029import android.os.PowerManager;
Steve McKay35645432016-01-20 15:09:35 -080030import android.support.annotation.Nullable;
Steve McKay14e827a2016-01-06 18:32:13 -080031import android.support.annotation.VisibleForTesting;
32import android.util.Log;
33
34import com.android.documentsui.Shared;
35import com.android.documentsui.model.DocumentInfo;
36import com.android.documentsui.model.DocumentStack;
Steve McKayecbf3c52016-01-13 17:17:39 -080037import com.android.documentsui.services.Job.Factory;
Steve McKay14e827a2016-01-06 18:32:13 -080038
39import java.lang.annotation.Retention;
40import java.lang.annotation.RetentionPolicy;
Steve McKayecbf3c52016-01-13 17:17:39 -080041import java.util.HashMap;
Steve McKay14e827a2016-01-06 18:32:13 -080042import java.util.List;
Steve McKayecbf3c52016-01-13 17:17:39 -080043import java.util.Map;
44import java.util.concurrent.ScheduledExecutorService;
45import java.util.concurrent.ScheduledFuture;
46import java.util.concurrent.ScheduledThreadPoolExecutor;
47import java.util.concurrent.TimeUnit;
Steve McKay14e827a2016-01-06 18:32:13 -080048
Steve McKayecbf3c52016-01-13 17:17:39 -080049import javax.annotation.concurrent.GuardedBy;
50
51public class FileOperationService extends Service implements Job.Listener {
52
53 private static final int DEFAULT_DELAY = 0;
54 private static final int MAX_DELAY = 10 * 1000; // ten seconds
Tomasz Mikolajewskife0c7352016-01-21 17:48:03 +090055 private static final int POOL_SIZE = 2; // "pool size", not *max* "pool size".
56 private static final int NOTIFICATION_ID_PROGRESS = 0;
57 private static final int NOTIFICATION_ID_FAILURE = 1;
Tomasz Mikolajewski748ea8c2016-01-22 16:22:51 +090058 private static final int NOTIFICATION_ID_WARNING = 2;
Steve McKayecbf3c52016-01-13 17:17:39 -080059
Steve McKay14e827a2016-01-06 18:32:13 -080060 public static final String TAG = "FileOperationService";
61
62 public static final String EXTRA_JOB_ID = "com.android.documentsui.JOB_ID";
Steve McKayecbf3c52016-01-13 17:17:39 -080063 public static final String EXTRA_DELAY = "com.android.documentsui.DELAY";
Steve McKay14e827a2016-01-06 18:32:13 -080064 public static final String EXTRA_OPERATION = "com.android.documentsui.OPERATION";
65 public static final String EXTRA_CANCEL = "com.android.documentsui.CANCEL";
66 public static final String EXTRA_SRC_LIST = "com.android.documentsui.SRC_LIST";
Tomasz Mikolajewski748ea8c2016-01-22 16:22:51 +090067 public static final String EXTRA_DIALOG_TYPE = "com.android.documentsui.DIALOG_TYPE";
Steve McKay14e827a2016-01-06 18:32:13 -080068
Tomasz Mikolajewskib8436af2016-01-25 16:20:15 +090069 // This extra is used only for moving and deleting. Currently it's not the case,
70 // but in the future those files may be from multiple different parents. In
71 // such case, this needs to be replaced with pairs of parent and child.
72 public static final String EXTRA_SRC_PARENT = "com.android.documentsui.SRC_PARENT";
73
Steve McKay14e827a2016-01-06 18:32:13 -080074 @IntDef(flag = true, value = {
75 OPERATION_UNKNOWN,
76 OPERATION_COPY,
77 OPERATION_MOVE,
78 OPERATION_DELETE
79 })
80 @Retention(RetentionPolicy.SOURCE)
81 public @interface OpType {}
Steve McKaya6bbeab2016-02-17 15:02:01 -080082 public static final int OPERATION_UNKNOWN = -1;
83 public static final int OPERATION_COPY = 1;
84 public static final int OPERATION_MOVE = 2;
85 public static final int OPERATION_DELETE = 3;
Steve McKay14e827a2016-01-06 18:32:13 -080086
87 // TODO: Move it to a shared file when more operations are implemented.
88 public static final int FAILURE_COPY = 1;
89
Steve McKayecbf3c52016-01-13 17:17:39 -080090 // The executor and job factory are visible for testing and non-final
91 // so we'll have a way to inject test doubles from the test. It's
92 // a sub-optimal arrangement.
93 @VisibleForTesting ScheduledExecutorService executor;
94 @VisibleForTesting Factory jobFactory;
Steve McKay14e827a2016-01-06 18:32:13 -080095
Steve McKayecbf3c52016-01-13 17:17:39 -080096 private PowerManager mPowerManager;
97 private PowerManager.WakeLock mWakeLock; // the wake lock, if held.
Steve McKay14e827a2016-01-06 18:32:13 -080098 private NotificationManager mNotificationManager;
99
Steve McKayecbf3c52016-01-13 17:17:39 -0800100 @GuardedBy("mRunning")
101 private Map<String, JobRecord> mRunning = new HashMap<>();
Steve McKay14e827a2016-01-06 18:32:13 -0800102
Steve McKay35645432016-01-20 15:09:35 -0800103 private int mLastServiceId;
Steve McKay14e827a2016-01-06 18:32:13 -0800104
105 @Override
106 public void onCreate() {
Steve McKayecbf3c52016-01-13 17:17:39 -0800107 // Allow tests to pre-set these with test doubles.
108 if (executor == null) {
109 executor = new ScheduledThreadPoolExecutor(POOL_SIZE);
110 }
111
112 if (jobFactory == null) {
113 jobFactory = Job.Factory.instance;
114 }
Steve McKay14e827a2016-01-06 18:32:13 -0800115
116 if (DEBUG) Log.d(TAG, "Created.");
117 mPowerManager = getSystemService(PowerManager.class);
118 mNotificationManager = getSystemService(NotificationManager.class);
119 }
120
121 @Override
Steve McKay35645432016-01-20 15:09:35 -0800122 public void onDestroy() {
123 if (DEBUG) Log.d(TAG, "Shutting down executor.");
124 List<Runnable> unfinished = executor.shutdownNow();
125 if (!unfinished.isEmpty()) {
126 Log.w(TAG, "Shutting down, but executor reports running jobs: " + unfinished);
127 }
128 executor = null;
129 if (DEBUG) Log.d(TAG, "Destroyed.");
130 }
131
132 @Override
133 public int onStartCommand(Intent intent, int flags, int serviceId) {
Steve McKayecbf3c52016-01-13 17:17:39 -0800134 // TODO: Ensure we're not being called with retry or redeliver.
135 // checkArgument(flags == 0); // retry and redeliver are not supported.
Steve McKay14e827a2016-01-06 18:32:13 -0800136
137 String jobId = intent.getStringExtra(EXTRA_JOB_ID);
138 @OpType int operationType = intent.getIntExtra(EXTRA_OPERATION, OPERATION_UNKNOWN);
139 checkArgument(jobId != null);
Steve McKayecbf3c52016-01-13 17:17:39 -0800140
Steve McKay14e827a2016-01-06 18:32:13 -0800141 if (intent.hasExtra(EXTRA_CANCEL)) {
142 handleCancel(intent);
Steve McKayecbf3c52016-01-13 17:17:39 -0800143 } else {
144 checkArgument(operationType != OPERATION_UNKNOWN);
Steve McKay35645432016-01-20 15:09:35 -0800145 handleOperation(intent, serviceId, jobId, operationType);
Steve McKay14e827a2016-01-06 18:32:13 -0800146 }
147
Steve McKayecbf3c52016-01-13 17:17:39 -0800148 return START_NOT_STICKY;
149 }
Steve McKay14e827a2016-01-06 18:32:13 -0800150
Steve McKay35645432016-01-20 15:09:35 -0800151 private void handleOperation(Intent intent, int serviceId, String jobId, int operationType) {
152 if (DEBUG) Log.d(TAG, "onStartCommand: " + jobId + " with serviceId " + serviceId);
Steve McKay14e827a2016-01-06 18:32:13 -0800153
Steve McKay35645432016-01-20 15:09:35 -0800154 // Track the service supplied id so we can stop the service once we're out of work to do.
155 mLastServiceId = serviceId;
Steve McKay14e827a2016-01-06 18:32:13 -0800156
Steve McKayecbf3c52016-01-13 17:17:39 -0800157 Job job = null;
158 synchronized (mRunning) {
159 if (mWakeLock == null) {
Tomasz Mikolajewskib8436af2016-01-25 16:20:15 +0900160 mWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
Steve McKay14e827a2016-01-06 18:32:13 -0800161 }
162
Steve McKayecbf3c52016-01-13 17:17:39 -0800163 List<DocumentInfo> srcs = intent.getParcelableArrayListExtra(EXTRA_SRC_LIST);
Tomasz Mikolajewskib8436af2016-01-25 16:20:15 +0900164 DocumentInfo srcParent = intent.getParcelableExtra(EXTRA_SRC_PARENT);
Steve McKayecbf3c52016-01-13 17:17:39 -0800165 DocumentStack stack = intent.getParcelableExtra(Shared.EXTRA_STACK);
Steve McKay14e827a2016-01-06 18:32:13 -0800166
Tomasz Mikolajewskib8436af2016-01-25 16:20:15 +0900167 job = createJob(operationType, jobId, srcs, srcParent, stack);
Steve McKayecbf3c52016-01-13 17:17:39 -0800168
Steve McKay35645432016-01-20 15:09:35 -0800169 if (job == null) {
170 return;
171 }
172
Steve McKayecbf3c52016-01-13 17:17:39 -0800173 mWakeLock.acquire();
Steve McKay14e827a2016-01-06 18:32:13 -0800174 }
Steve McKayecbf3c52016-01-13 17:17:39 -0800175
176 checkState(job != null);
177 int delay = intent.getIntExtra(EXTRA_DELAY, DEFAULT_DELAY);
178 checkArgument(delay <= MAX_DELAY);
Steve McKay35645432016-01-20 15:09:35 -0800179 if (DEBUG) Log.d(
180 TAG, "Scheduling job " + job.id + " to run in " + delay + " milliseconds.");
Steve McKayecbf3c52016-01-13 17:17:39 -0800181 ScheduledFuture<?> future = executor.schedule(job, delay, TimeUnit.MILLISECONDS);
182 mRunning.put(jobId, new JobRecord(job, future));
Steve McKay14e827a2016-01-06 18:32:13 -0800183 }
184
185 /**
186 * Cancels the operation corresponding to job id, identified in "EXTRA_JOB_ID".
187 *
188 * @param intent The cancellation intent.
189 */
190 private void handleCancel(Intent intent) {
191 checkArgument(intent.hasExtra(EXTRA_CANCEL));
192 String jobId = checkNotNull(intent.getStringExtra(EXTRA_JOB_ID));
193
Steve McKayecbf3c52016-01-13 17:17:39 -0800194 if (DEBUG) Log.d(TAG, "handleCancel: " + jobId);
195
196 synchronized (mRunning) {
197 // Do nothing if the cancelled ID doesn't match the current job ID. This prevents racey
198 // cancellation requests from affecting unrelated copy jobs. However, if the current job ID
199 // is null, the service most likely crashed and was revived by the incoming cancel intent.
200 // In that case, always allow the cancellation to proceed.
201 JobRecord record = mRunning.get(jobId);
202 if (record != null) {
203 record.job.cancel();
204
205 // If the job hasn't been started, cancel it and explicitly clean up.
206 // If it *has* been started, we wait for it to recognize this, then
207 // allow it stop working in an orderly fashion.
208 if (record.future.getDelay(TimeUnit.MILLISECONDS) > 0) {
209 record.future.cancel(false);
210 onFinished(record.job);
211 }
212 }
Steve McKay14e827a2016-01-06 18:32:13 -0800213 }
214
215 // Dismiss the progress notification here rather than in the copy loop. This preserves
216 // interactivity for the user in case the copy loop is stalled.
217 // Try to cancel it even if we don't have a job id...in case there is some sad
218 // orphan notification.
Tomasz Mikolajewskife0c7352016-01-21 17:48:03 +0900219 mNotificationManager.cancel(jobId, NOTIFICATION_ID_PROGRESS);
Steve McKayecbf3c52016-01-13 17:17:39 -0800220
221 // TODO: Guarantee the job is being finalized
Steve McKay14e827a2016-01-06 18:32:13 -0800222 }
223
Steve McKay35645432016-01-20 15:09:35 -0800224 /**
225 * Creates a new job. Returns null if a job with {@code id} already exists.
226 * @return
227 */
Steve McKayecbf3c52016-01-13 17:17:39 -0800228 @GuardedBy("mRunning")
Steve McKay35645432016-01-20 15:09:35 -0800229 private @Nullable Job createJob(
Tomasz Mikolajewskib8436af2016-01-25 16:20:15 +0900230 @OpType int operationType, String id, List<DocumentInfo> srcs, DocumentInfo srcParent,
231 DocumentStack stack) {
Steve McKay14e827a2016-01-06 18:32:13 -0800232
Steve McKay35645432016-01-20 15:09:35 -0800233 if (mRunning.containsKey(id)) {
234 Log.w(TAG, "Duplicate job id: " + id
235 + ". Ignoring job request for srcs: " + srcs + ", stack: " + stack + ".");
236 return null;
237 }
Steve McKay14e827a2016-01-06 18:32:13 -0800238
Steve McKayecbf3c52016-01-13 17:17:39 -0800239 Job job = null;
Steve McKay14e827a2016-01-06 18:32:13 -0800240 switch (operationType) {
241 case OPERATION_COPY:
Steve McKayecbf3c52016-01-13 17:17:39 -0800242 job = jobFactory.createCopy(this, getApplicationContext(), this, id, stack, srcs);
Steve McKay14e827a2016-01-06 18:32:13 -0800243 break;
244 case OPERATION_MOVE:
Tomasz Mikolajewskib8436af2016-01-25 16:20:15 +0900245 job = jobFactory.createMove(this, getApplicationContext(), this, id, stack, srcs,
246 srcParent);
Steve McKay14e827a2016-01-06 18:32:13 -0800247 break;
248 case OPERATION_DELETE:
Tomasz Mikolajewskib8436af2016-01-25 16:20:15 +0900249 job = jobFactory.createDelete(this, getApplicationContext(), this, id, stack, srcs,
250 srcParent);
Steve McKay35645432016-01-20 15:09:35 -0800251 break;
Steve McKay14e827a2016-01-06 18:32:13 -0800252 default:
253 throw new UnsupportedOperationException();
254 }
255
Steve McKayecbf3c52016-01-13 17:17:39 -0800256 return checkNotNull(job);
Steve McKay14e827a2016-01-06 18:32:13 -0800257 }
258
Steve McKayecbf3c52016-01-13 17:17:39 -0800259 @GuardedBy("mRunning")
260 private void deleteJob(Job job) {
261 if (DEBUG) Log.d(TAG, "deleteJob: " + job.id);
262
263 JobRecord record = mRunning.remove(job.id);
264 checkArgument(record != null);
265 record.job.cleanup();
266
267 if (mRunning.isEmpty()) {
268 shutdown();
269 }
270 }
271
272 /**
273 * Most likely shuts down. Won't shut down if service has a pending
Steve McKay35645432016-01-20 15:09:35 -0800274 * message. Thread pool is deal with in onDestroy.
Steve McKayecbf3c52016-01-13 17:17:39 -0800275 */
276 private void shutdown() {
Steve McKay35645432016-01-20 15:09:35 -0800277 if (DEBUG) Log.d(TAG, "Shutting down. Last serviceId was " + mLastServiceId);
Steve McKayecbf3c52016-01-13 17:17:39 -0800278 mWakeLock.release();
279 mWakeLock = null;
Steve McKay35645432016-01-20 15:09:35 -0800280
281 // Turns out, for us, stopSelfResult always returns false in tests,
282 // so we can't guard executor shutdown. For this reason we move
283 // executor shutdown to #onDestroy.
284 boolean gonnaStop = stopSelfResult(mLastServiceId);
Steve McKayecbf3c52016-01-13 17:17:39 -0800285 if (DEBUG) Log.d(TAG, "Stopping service: " + gonnaStop);
286 if (!gonnaStop) {
287 Log.w(TAG, "Service should be stopping, but reports otherwise.");
288 }
Steve McKayecbf3c52016-01-13 17:17:39 -0800289 }
290
291 @VisibleForTesting
292 boolean holdsWakeLock() {
293 return mWakeLock != null && mWakeLock.isHeld();
294 }
295
296 @Override
297 public void onStart(Job job) {
298 if (DEBUG) Log.d(TAG, "onStart: " + job.id);
Tomasz Mikolajewskife0c7352016-01-21 17:48:03 +0900299 mNotificationManager.notify(job.id, NOTIFICATION_ID_PROGRESS, job.getSetupNotification());
Steve McKayecbf3c52016-01-13 17:17:39 -0800300 }
301
302 @Override
303 public void onFinished(Job job) {
304 if (DEBUG) Log.d(TAG, "onFinished: " + job.id);
305
306 // Dismiss the ongoing copy notification when the copy is done.
Tomasz Mikolajewskife0c7352016-01-21 17:48:03 +0900307 mNotificationManager.cancel(job.id, NOTIFICATION_ID_PROGRESS);
Steve McKayecbf3c52016-01-13 17:17:39 -0800308
Tomasz Mikolajewski748ea8c2016-01-22 16:22:51 +0900309 if (job.hasFailures()) {
310 Log.e(TAG, "Job failed on files: " + job.failedFiles.size() + ".");
311 mNotificationManager.notify(
312 job.id, NOTIFICATION_ID_FAILURE, job.getFailureNotification());
313 }
314
315 if (job.hasWarnings()) {
316 if (DEBUG) Log.d(TAG, "Job finished with warnings.");
317 mNotificationManager.notify(
318 job.id, NOTIFICATION_ID_WARNING, job.getWarningNotification());
319 }
320
Steve McKayecbf3c52016-01-13 17:17:39 -0800321 synchronized (mRunning) {
322 deleteJob(job);
323 }
Steve McKay14e827a2016-01-06 18:32:13 -0800324 }
325
326 @Override
327 public void onProgress(CopyJob job) {
Steve McKayecbf3c52016-01-13 17:17:39 -0800328 if (DEBUG) Log.d(TAG, "onProgress: " + job.id);
Tomasz Mikolajewskife0c7352016-01-21 17:48:03 +0900329 mNotificationManager.notify(
330 job.id, NOTIFICATION_ID_PROGRESS, job.getProgressNotification());
Steve McKay14e827a2016-01-06 18:32:13 -0800331 }
332
Steve McKayecbf3c52016-01-13 17:17:39 -0800333 private static final class JobRecord {
334 private final Job job;
335 private final ScheduledFuture<?> future;
336
337 public JobRecord(Job job, ScheduledFuture<?> future) {
338 this.job = job;
339 this.future = future;
340 }
Steve McKay14e827a2016-01-06 18:32:13 -0800341 }
342
Steve McKayecbf3c52016-01-13 17:17:39 -0800343 @Override
344 public IBinder onBind(Intent intent) {
345 return null; // Boilerplate. See super#onBind
Steve McKay14e827a2016-01-06 18:32:13 -0800346 }
347}