Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [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.documentsui.services; |
| 18 | |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 19 | import static com.android.documentsui.Shared.DEBUG; |
| 20 | import static com.android.internal.util.Preconditions.checkArgument; |
| 21 | import static com.android.internal.util.Preconditions.checkNotNull; |
| 22 | import static com.android.internal.util.Preconditions.checkState; |
| 23 | |
| 24 | import android.annotation.IntDef; |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 25 | import android.app.NotificationManager; |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 26 | import android.app.Service; |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 27 | import android.content.Intent; |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 28 | import android.os.IBinder; |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 29 | import android.os.PowerManager; |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 30 | import android.support.annotation.Nullable; |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 31 | import android.support.annotation.VisibleForTesting; |
| 32 | import android.util.Log; |
| 33 | |
| 34 | import com.android.documentsui.Shared; |
| 35 | import com.android.documentsui.model.DocumentInfo; |
| 36 | import com.android.documentsui.model.DocumentStack; |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 37 | import com.android.documentsui.services.Job.Factory; |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 38 | |
| 39 | import java.lang.annotation.Retention; |
| 40 | import java.lang.annotation.RetentionPolicy; |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 41 | import java.util.HashMap; |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 42 | import java.util.List; |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 43 | import java.util.Map; |
| 44 | import java.util.concurrent.ScheduledExecutorService; |
| 45 | import java.util.concurrent.ScheduledFuture; |
| 46 | import java.util.concurrent.ScheduledThreadPoolExecutor; |
| 47 | import java.util.concurrent.TimeUnit; |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 48 | |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 49 | import javax.annotation.concurrent.GuardedBy; |
| 50 | |
| 51 | public 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 Mikolajewski | fe0c735 | 2016-01-21 17:48:03 +0900 | [diff] [blame] | 55 | 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 Mikolajewski | 748ea8c | 2016-01-22 16:22:51 +0900 | [diff] [blame] | 58 | private static final int NOTIFICATION_ID_WARNING = 2; |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 59 | |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 60 | public static final String TAG = "FileOperationService"; |
| 61 | |
| 62 | public static final String EXTRA_JOB_ID = "com.android.documentsui.JOB_ID"; |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 63 | public static final String EXTRA_DELAY = "com.android.documentsui.DELAY"; |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 64 | 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 Mikolajewski | 748ea8c | 2016-01-22 16:22:51 +0900 | [diff] [blame] | 67 | public static final String EXTRA_DIALOG_TYPE = "com.android.documentsui.DIALOG_TYPE"; |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 68 | |
Tomasz Mikolajewski | b8436af | 2016-01-25 16:20:15 +0900 | [diff] [blame] | 69 | // 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 McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 74 | @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 McKay | a6bbeab | 2016-02-17 15:02:01 -0800 | [diff] [blame] | 82 | 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 McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 86 | |
| 87 | // TODO: Move it to a shared file when more operations are implemented. |
| 88 | public static final int FAILURE_COPY = 1; |
| 89 | |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 90 | // 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 McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 95 | |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 96 | private PowerManager mPowerManager; |
| 97 | private PowerManager.WakeLock mWakeLock; // the wake lock, if held. |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 98 | private NotificationManager mNotificationManager; |
| 99 | |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 100 | @GuardedBy("mRunning") |
| 101 | private Map<String, JobRecord> mRunning = new HashMap<>(); |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 102 | |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 103 | private int mLastServiceId; |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 104 | |
| 105 | @Override |
| 106 | public void onCreate() { |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 107 | // 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 McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 115 | |
| 116 | if (DEBUG) Log.d(TAG, "Created."); |
| 117 | mPowerManager = getSystemService(PowerManager.class); |
| 118 | mNotificationManager = getSystemService(NotificationManager.class); |
| 119 | } |
| 120 | |
| 121 | @Override |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 122 | 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 McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 134 | // TODO: Ensure we're not being called with retry or redeliver. |
| 135 | // checkArgument(flags == 0); // retry and redeliver are not supported. |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 136 | |
| 137 | String jobId = intent.getStringExtra(EXTRA_JOB_ID); |
| 138 | @OpType int operationType = intent.getIntExtra(EXTRA_OPERATION, OPERATION_UNKNOWN); |
| 139 | checkArgument(jobId != null); |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 140 | |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 141 | if (intent.hasExtra(EXTRA_CANCEL)) { |
| 142 | handleCancel(intent); |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 143 | } else { |
| 144 | checkArgument(operationType != OPERATION_UNKNOWN); |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 145 | handleOperation(intent, serviceId, jobId, operationType); |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 146 | } |
| 147 | |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 148 | return START_NOT_STICKY; |
| 149 | } |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 150 | |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 151 | private void handleOperation(Intent intent, int serviceId, String jobId, int operationType) { |
| 152 | if (DEBUG) Log.d(TAG, "onStartCommand: " + jobId + " with serviceId " + serviceId); |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 153 | |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 154 | // Track the service supplied id so we can stop the service once we're out of work to do. |
| 155 | mLastServiceId = serviceId; |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 156 | |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 157 | Job job = null; |
| 158 | synchronized (mRunning) { |
| 159 | if (mWakeLock == null) { |
Tomasz Mikolajewski | b8436af | 2016-01-25 16:20:15 +0900 | [diff] [blame] | 160 | mWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG); |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 161 | } |
| 162 | |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 163 | List<DocumentInfo> srcs = intent.getParcelableArrayListExtra(EXTRA_SRC_LIST); |
Tomasz Mikolajewski | b8436af | 2016-01-25 16:20:15 +0900 | [diff] [blame] | 164 | DocumentInfo srcParent = intent.getParcelableExtra(EXTRA_SRC_PARENT); |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 165 | DocumentStack stack = intent.getParcelableExtra(Shared.EXTRA_STACK); |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 166 | |
Tomasz Mikolajewski | b8436af | 2016-01-25 16:20:15 +0900 | [diff] [blame] | 167 | job = createJob(operationType, jobId, srcs, srcParent, stack); |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 168 | |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 169 | if (job == null) { |
| 170 | return; |
| 171 | } |
| 172 | |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 173 | mWakeLock.acquire(); |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 174 | } |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 175 | |
| 176 | checkState(job != null); |
| 177 | int delay = intent.getIntExtra(EXTRA_DELAY, DEFAULT_DELAY); |
| 178 | checkArgument(delay <= MAX_DELAY); |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 179 | if (DEBUG) Log.d( |
| 180 | TAG, "Scheduling job " + job.id + " to run in " + delay + " milliseconds."); |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 181 | ScheduledFuture<?> future = executor.schedule(job, delay, TimeUnit.MILLISECONDS); |
| 182 | mRunning.put(jobId, new JobRecord(job, future)); |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 183 | } |
| 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 McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 194 | 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 McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 213 | } |
| 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 Mikolajewski | fe0c735 | 2016-01-21 17:48:03 +0900 | [diff] [blame] | 219 | mNotificationManager.cancel(jobId, NOTIFICATION_ID_PROGRESS); |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 220 | |
| 221 | // TODO: Guarantee the job is being finalized |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 222 | } |
| 223 | |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 224 | /** |
| 225 | * Creates a new job. Returns null if a job with {@code id} already exists. |
| 226 | * @return |
| 227 | */ |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 228 | @GuardedBy("mRunning") |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 229 | private @Nullable Job createJob( |
Tomasz Mikolajewski | b8436af | 2016-01-25 16:20:15 +0900 | [diff] [blame] | 230 | @OpType int operationType, String id, List<DocumentInfo> srcs, DocumentInfo srcParent, |
| 231 | DocumentStack stack) { |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 232 | |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 233 | 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 McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 238 | |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 239 | Job job = null; |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 240 | switch (operationType) { |
| 241 | case OPERATION_COPY: |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 242 | job = jobFactory.createCopy(this, getApplicationContext(), this, id, stack, srcs); |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 243 | break; |
| 244 | case OPERATION_MOVE: |
Tomasz Mikolajewski | b8436af | 2016-01-25 16:20:15 +0900 | [diff] [blame] | 245 | job = jobFactory.createMove(this, getApplicationContext(), this, id, stack, srcs, |
| 246 | srcParent); |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 247 | break; |
| 248 | case OPERATION_DELETE: |
Tomasz Mikolajewski | b8436af | 2016-01-25 16:20:15 +0900 | [diff] [blame] | 249 | job = jobFactory.createDelete(this, getApplicationContext(), this, id, stack, srcs, |
| 250 | srcParent); |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 251 | break; |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 252 | default: |
| 253 | throw new UnsupportedOperationException(); |
| 254 | } |
| 255 | |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 256 | return checkNotNull(job); |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 257 | } |
| 258 | |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 259 | @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 McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 274 | * message. Thread pool is deal with in onDestroy. |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 275 | */ |
| 276 | private void shutdown() { |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 277 | if (DEBUG) Log.d(TAG, "Shutting down. Last serviceId was " + mLastServiceId); |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 278 | mWakeLock.release(); |
| 279 | mWakeLock = null; |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 280 | |
| 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 McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 285 | if (DEBUG) Log.d(TAG, "Stopping service: " + gonnaStop); |
| 286 | if (!gonnaStop) { |
| 287 | Log.w(TAG, "Service should be stopping, but reports otherwise."); |
| 288 | } |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 289 | } |
| 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 Mikolajewski | fe0c735 | 2016-01-21 17:48:03 +0900 | [diff] [blame] | 299 | mNotificationManager.notify(job.id, NOTIFICATION_ID_PROGRESS, job.getSetupNotification()); |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 300 | } |
| 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 Mikolajewski | fe0c735 | 2016-01-21 17:48:03 +0900 | [diff] [blame] | 307 | mNotificationManager.cancel(job.id, NOTIFICATION_ID_PROGRESS); |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 308 | |
Tomasz Mikolajewski | 748ea8c | 2016-01-22 16:22:51 +0900 | [diff] [blame] | 309 | 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 McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 321 | synchronized (mRunning) { |
| 322 | deleteJob(job); |
| 323 | } |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | @Override |
| 327 | public void onProgress(CopyJob job) { |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 328 | if (DEBUG) Log.d(TAG, "onProgress: " + job.id); |
Tomasz Mikolajewski | fe0c735 | 2016-01-21 17:48:03 +0900 | [diff] [blame] | 329 | mNotificationManager.notify( |
| 330 | job.id, NOTIFICATION_ID_PROGRESS, job.getProgressNotification()); |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 331 | } |
| 332 | |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 333 | 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 McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 341 | } |
| 342 | |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 343 | @Override |
| 344 | public IBinder onBind(Intent intent) { |
| 345 | return null; // Boilerplate. See super#onBind |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 346 | } |
| 347 | } |