Steve McKay | c83baa0 | 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 | c83baa0 | 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 | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 25 | import android.app.NotificationManager; |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 26 | import android.app.Service; |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 27 | import android.content.Intent; |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 28 | import android.os.IBinder; |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 29 | import android.os.PowerManager; |
Steve McKay | 97b4be4 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 30 | import android.support.annotation.Nullable; |
Steve McKay | c83baa0 | 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 | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 37 | import com.android.documentsui.services.Job.Factory; |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 38 | |
| 39 | import java.lang.annotation.Retention; |
| 40 | import java.lang.annotation.RetentionPolicy; |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 41 | import java.util.HashMap; |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 42 | import java.util.List; |
Steve McKay | bbeba52 | 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 | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 48 | |
Steve McKay | bbeba52 | 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 | 6b6c16e | 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 | dd2b31c | 2016-01-22 16:22:51 +0900 | [diff] [blame^] | 58 | private static final int NOTIFICATION_ID_WARNING = 2; |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 59 | |
Steve McKay | c83baa0 | 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 | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 63 | public static final String EXTRA_DELAY = "com.android.documentsui.DELAY"; |
Steve McKay | c83baa0 | 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 | dd2b31c | 2016-01-22 16:22:51 +0900 | [diff] [blame^] | 67 | public static final String EXTRA_DIALOG_TYPE = "com.android.documentsui.DIALOG_TYPE"; |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 68 | |
Tomasz Mikolajewski | e009441 | 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 | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 74 | public static final int OPERATION_UNKNOWN = -1; |
| 75 | public static final int OPERATION_COPY = 1; |
| 76 | public static final int OPERATION_MOVE = 2; |
| 77 | public static final int OPERATION_DELETE = 3; |
| 78 | |
| 79 | @IntDef(flag = true, value = { |
| 80 | OPERATION_UNKNOWN, |
| 81 | OPERATION_COPY, |
| 82 | OPERATION_MOVE, |
| 83 | OPERATION_DELETE |
| 84 | }) |
| 85 | @Retention(RetentionPolicy.SOURCE) |
| 86 | public @interface OpType {} |
| 87 | |
| 88 | // TODO: Move it to a shared file when more operations are implemented. |
| 89 | public static final int FAILURE_COPY = 1; |
| 90 | |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 91 | // The executor and job factory are visible for testing and non-final |
| 92 | // so we'll have a way to inject test doubles from the test. It's |
| 93 | // a sub-optimal arrangement. |
| 94 | @VisibleForTesting ScheduledExecutorService executor; |
| 95 | @VisibleForTesting Factory jobFactory; |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 96 | |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 97 | private PowerManager mPowerManager; |
| 98 | private PowerManager.WakeLock mWakeLock; // the wake lock, if held. |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 99 | private NotificationManager mNotificationManager; |
| 100 | |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 101 | @GuardedBy("mRunning") |
| 102 | private Map<String, JobRecord> mRunning = new HashMap<>(); |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 103 | |
Steve McKay | 97b4be4 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 104 | private int mLastServiceId; |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 105 | |
| 106 | @Override |
| 107 | public void onCreate() { |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 108 | // Allow tests to pre-set these with test doubles. |
| 109 | if (executor == null) { |
| 110 | executor = new ScheduledThreadPoolExecutor(POOL_SIZE); |
| 111 | } |
| 112 | |
| 113 | if (jobFactory == null) { |
| 114 | jobFactory = Job.Factory.instance; |
| 115 | } |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 116 | |
| 117 | if (DEBUG) Log.d(TAG, "Created."); |
| 118 | mPowerManager = getSystemService(PowerManager.class); |
| 119 | mNotificationManager = getSystemService(NotificationManager.class); |
| 120 | } |
| 121 | |
| 122 | @Override |
Steve McKay | 97b4be4 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 123 | public void onDestroy() { |
| 124 | if (DEBUG) Log.d(TAG, "Shutting down executor."); |
| 125 | List<Runnable> unfinished = executor.shutdownNow(); |
| 126 | if (!unfinished.isEmpty()) { |
| 127 | Log.w(TAG, "Shutting down, but executor reports running jobs: " + unfinished); |
| 128 | } |
| 129 | executor = null; |
| 130 | if (DEBUG) Log.d(TAG, "Destroyed."); |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public int onStartCommand(Intent intent, int flags, int serviceId) { |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 135 | // TODO: Ensure we're not being called with retry or redeliver. |
| 136 | // checkArgument(flags == 0); // retry and redeliver are not supported. |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 137 | |
| 138 | String jobId = intent.getStringExtra(EXTRA_JOB_ID); |
| 139 | @OpType int operationType = intent.getIntExtra(EXTRA_OPERATION, OPERATION_UNKNOWN); |
| 140 | checkArgument(jobId != null); |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 141 | |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 142 | if (intent.hasExtra(EXTRA_CANCEL)) { |
| 143 | handleCancel(intent); |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 144 | } else { |
| 145 | checkArgument(operationType != OPERATION_UNKNOWN); |
Steve McKay | 97b4be4 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 146 | handleOperation(intent, serviceId, jobId, operationType); |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 147 | } |
| 148 | |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 149 | return START_NOT_STICKY; |
| 150 | } |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 151 | |
Steve McKay | 97b4be4 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 152 | private void handleOperation(Intent intent, int serviceId, String jobId, int operationType) { |
| 153 | if (DEBUG) Log.d(TAG, "onStartCommand: " + jobId + " with serviceId " + serviceId); |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 154 | |
Steve McKay | 97b4be4 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 155 | // Track the service supplied id so we can stop the service once we're out of work to do. |
| 156 | mLastServiceId = serviceId; |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 157 | |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 158 | Job job = null; |
| 159 | synchronized (mRunning) { |
| 160 | if (mWakeLock == null) { |
Tomasz Mikolajewski | e009441 | 2016-01-25 16:20:15 +0900 | [diff] [blame] | 161 | mWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG); |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 162 | } |
| 163 | |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 164 | List<DocumentInfo> srcs = intent.getParcelableArrayListExtra(EXTRA_SRC_LIST); |
Tomasz Mikolajewski | e009441 | 2016-01-25 16:20:15 +0900 | [diff] [blame] | 165 | DocumentInfo srcParent = intent.getParcelableExtra(EXTRA_SRC_PARENT); |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 166 | DocumentStack stack = intent.getParcelableExtra(Shared.EXTRA_STACK); |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 167 | |
Tomasz Mikolajewski | e009441 | 2016-01-25 16:20:15 +0900 | [diff] [blame] | 168 | job = createJob(operationType, jobId, srcs, srcParent, stack); |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 169 | |
Steve McKay | 97b4be4 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 170 | if (job == null) { |
| 171 | return; |
| 172 | } |
| 173 | |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 174 | mWakeLock.acquire(); |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 175 | } |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 176 | |
| 177 | checkState(job != null); |
| 178 | int delay = intent.getIntExtra(EXTRA_DELAY, DEFAULT_DELAY); |
| 179 | checkArgument(delay <= MAX_DELAY); |
Steve McKay | 97b4be4 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 180 | if (DEBUG) Log.d( |
| 181 | TAG, "Scheduling job " + job.id + " to run in " + delay + " milliseconds."); |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 182 | ScheduledFuture<?> future = executor.schedule(job, delay, TimeUnit.MILLISECONDS); |
| 183 | mRunning.put(jobId, new JobRecord(job, future)); |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Cancels the operation corresponding to job id, identified in "EXTRA_JOB_ID". |
| 188 | * |
| 189 | * @param intent The cancellation intent. |
| 190 | */ |
| 191 | private void handleCancel(Intent intent) { |
| 192 | checkArgument(intent.hasExtra(EXTRA_CANCEL)); |
| 193 | String jobId = checkNotNull(intent.getStringExtra(EXTRA_JOB_ID)); |
| 194 | |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 195 | if (DEBUG) Log.d(TAG, "handleCancel: " + jobId); |
| 196 | |
| 197 | synchronized (mRunning) { |
| 198 | // Do nothing if the cancelled ID doesn't match the current job ID. This prevents racey |
| 199 | // cancellation requests from affecting unrelated copy jobs. However, if the current job ID |
| 200 | // is null, the service most likely crashed and was revived by the incoming cancel intent. |
| 201 | // In that case, always allow the cancellation to proceed. |
| 202 | JobRecord record = mRunning.get(jobId); |
| 203 | if (record != null) { |
| 204 | record.job.cancel(); |
| 205 | |
| 206 | // If the job hasn't been started, cancel it and explicitly clean up. |
| 207 | // If it *has* been started, we wait for it to recognize this, then |
| 208 | // allow it stop working in an orderly fashion. |
| 209 | if (record.future.getDelay(TimeUnit.MILLISECONDS) > 0) { |
| 210 | record.future.cancel(false); |
| 211 | onFinished(record.job); |
| 212 | } |
| 213 | } |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | // Dismiss the progress notification here rather than in the copy loop. This preserves |
| 217 | // interactivity for the user in case the copy loop is stalled. |
| 218 | // Try to cancel it even if we don't have a job id...in case there is some sad |
| 219 | // orphan notification. |
Tomasz Mikolajewski | 6b6c16e | 2016-01-21 17:48:03 +0900 | [diff] [blame] | 220 | mNotificationManager.cancel(jobId, NOTIFICATION_ID_PROGRESS); |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 221 | |
| 222 | // TODO: Guarantee the job is being finalized |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 223 | } |
| 224 | |
Steve McKay | 97b4be4 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 225 | /** |
| 226 | * Creates a new job. Returns null if a job with {@code id} already exists. |
| 227 | * @return |
| 228 | */ |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 229 | @GuardedBy("mRunning") |
Steve McKay | 97b4be4 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 230 | private @Nullable Job createJob( |
Tomasz Mikolajewski | e009441 | 2016-01-25 16:20:15 +0900 | [diff] [blame] | 231 | @OpType int operationType, String id, List<DocumentInfo> srcs, DocumentInfo srcParent, |
| 232 | DocumentStack stack) { |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 233 | |
Steve McKay | 97b4be4 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 234 | if (mRunning.containsKey(id)) { |
| 235 | Log.w(TAG, "Duplicate job id: " + id |
| 236 | + ". Ignoring job request for srcs: " + srcs + ", stack: " + stack + "."); |
| 237 | return null; |
| 238 | } |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 239 | |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 240 | Job job = null; |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 241 | switch (operationType) { |
| 242 | case OPERATION_COPY: |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 243 | job = jobFactory.createCopy(this, getApplicationContext(), this, id, stack, srcs); |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 244 | break; |
| 245 | case OPERATION_MOVE: |
Tomasz Mikolajewski | e009441 | 2016-01-25 16:20:15 +0900 | [diff] [blame] | 246 | job = jobFactory.createMove(this, getApplicationContext(), this, id, stack, srcs, |
| 247 | srcParent); |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 248 | break; |
| 249 | case OPERATION_DELETE: |
Tomasz Mikolajewski | e009441 | 2016-01-25 16:20:15 +0900 | [diff] [blame] | 250 | job = jobFactory.createDelete(this, getApplicationContext(), this, id, stack, srcs, |
| 251 | srcParent); |
Steve McKay | 97b4be4 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 252 | break; |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 253 | default: |
| 254 | throw new UnsupportedOperationException(); |
| 255 | } |
| 256 | |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 257 | return checkNotNull(job); |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 258 | } |
| 259 | |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 260 | @GuardedBy("mRunning") |
| 261 | private void deleteJob(Job job) { |
| 262 | if (DEBUG) Log.d(TAG, "deleteJob: " + job.id); |
| 263 | |
| 264 | JobRecord record = mRunning.remove(job.id); |
| 265 | checkArgument(record != null); |
| 266 | record.job.cleanup(); |
| 267 | |
| 268 | if (mRunning.isEmpty()) { |
| 269 | shutdown(); |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * Most likely shuts down. Won't shut down if service has a pending |
Steve McKay | 97b4be4 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 275 | * message. Thread pool is deal with in onDestroy. |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 276 | */ |
| 277 | private void shutdown() { |
Steve McKay | 97b4be4 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 278 | if (DEBUG) Log.d(TAG, "Shutting down. Last serviceId was " + mLastServiceId); |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 279 | mWakeLock.release(); |
| 280 | mWakeLock = null; |
Steve McKay | 97b4be4 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 281 | |
| 282 | // Turns out, for us, stopSelfResult always returns false in tests, |
| 283 | // so we can't guard executor shutdown. For this reason we move |
| 284 | // executor shutdown to #onDestroy. |
| 285 | boolean gonnaStop = stopSelfResult(mLastServiceId); |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 286 | if (DEBUG) Log.d(TAG, "Stopping service: " + gonnaStop); |
| 287 | if (!gonnaStop) { |
| 288 | Log.w(TAG, "Service should be stopping, but reports otherwise."); |
| 289 | } |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | @VisibleForTesting |
| 293 | boolean holdsWakeLock() { |
| 294 | return mWakeLock != null && mWakeLock.isHeld(); |
| 295 | } |
| 296 | |
| 297 | @Override |
| 298 | public void onStart(Job job) { |
| 299 | if (DEBUG) Log.d(TAG, "onStart: " + job.id); |
Tomasz Mikolajewski | 6b6c16e | 2016-01-21 17:48:03 +0900 | [diff] [blame] | 300 | mNotificationManager.notify(job.id, NOTIFICATION_ID_PROGRESS, job.getSetupNotification()); |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | @Override |
| 304 | public void onFinished(Job job) { |
| 305 | if (DEBUG) Log.d(TAG, "onFinished: " + job.id); |
| 306 | |
| 307 | // Dismiss the ongoing copy notification when the copy is done. |
Tomasz Mikolajewski | 6b6c16e | 2016-01-21 17:48:03 +0900 | [diff] [blame] | 308 | mNotificationManager.cancel(job.id, NOTIFICATION_ID_PROGRESS); |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 309 | |
Tomasz Mikolajewski | dd2b31c | 2016-01-22 16:22:51 +0900 | [diff] [blame^] | 310 | if (job.hasFailures()) { |
| 311 | Log.e(TAG, "Job failed on files: " + job.failedFiles.size() + "."); |
| 312 | mNotificationManager.notify( |
| 313 | job.id, NOTIFICATION_ID_FAILURE, job.getFailureNotification()); |
| 314 | } |
| 315 | |
| 316 | if (job.hasWarnings()) { |
| 317 | if (DEBUG) Log.d(TAG, "Job finished with warnings."); |
| 318 | mNotificationManager.notify( |
| 319 | job.id, NOTIFICATION_ID_WARNING, job.getWarningNotification()); |
| 320 | } |
| 321 | |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 322 | synchronized (mRunning) { |
| 323 | deleteJob(job); |
| 324 | } |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | @Override |
| 328 | public void onProgress(CopyJob job) { |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 329 | if (DEBUG) Log.d(TAG, "onProgress: " + job.id); |
Tomasz Mikolajewski | 6b6c16e | 2016-01-21 17:48:03 +0900 | [diff] [blame] | 330 | mNotificationManager.notify( |
| 331 | job.id, NOTIFICATION_ID_PROGRESS, job.getProgressNotification()); |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 332 | } |
| 333 | |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 334 | private static final class JobRecord { |
| 335 | private final Job job; |
| 336 | private final ScheduledFuture<?> future; |
| 337 | |
| 338 | public JobRecord(Job job, ScheduledFuture<?> future) { |
| 339 | this.job = job; |
| 340 | this.future = future; |
| 341 | } |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 342 | } |
| 343 | |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 344 | @Override |
| 345 | public IBinder onBind(Intent intent) { |
| 346 | return null; // Boilerplate. See super#onBind |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 347 | } |
| 348 | } |