Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | 97b4be4 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 19 | import static com.android.documentsui.DocumentsApplication.acquireUnstableProviderOrThrow; |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 20 | import static com.android.documentsui.services.FileOperationService.EXTRA_CANCEL; |
Tomasz Mikolajewski | dd2b31c | 2016-01-22 16:22:51 +0900 | [diff] [blame^] | 21 | import static com.android.documentsui.services.FileOperationService.EXTRA_DIALOG_TYPE; |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 22 | import static com.android.documentsui.services.FileOperationService.EXTRA_JOB_ID; |
| 23 | import static com.android.documentsui.services.FileOperationService.EXTRA_OPERATION; |
| 24 | import static com.android.documentsui.services.FileOperationService.EXTRA_SRC_LIST; |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 25 | import static com.android.documentsui.services.FileOperationService.OPERATION_UNKNOWN; |
| 26 | import static com.android.internal.util.Preconditions.checkArgument; |
Steve McKay | 97b4be4 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 27 | import static com.android.internal.util.Preconditions.checkNotNull; |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 28 | |
| 29 | import android.annotation.DrawableRes; |
| 30 | import android.annotation.PluralsRes; |
| 31 | import android.app.Notification; |
| 32 | import android.app.Notification.Builder; |
| 33 | import android.app.PendingIntent; |
Steve McKay | 97b4be4 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 34 | import android.content.ContentProviderClient; |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 35 | import android.content.ContentResolver; |
| 36 | import android.content.Context; |
| 37 | import android.content.Intent; |
| 38 | import android.os.Parcelable; |
| 39 | import android.os.RemoteException; |
| 40 | import android.provider.DocumentsContract; |
Steve McKay | 97b4be4 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 41 | import android.util.Log; |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 42 | |
| 43 | import com.android.documentsui.FilesActivity; |
Tomasz Mikolajewski | dd2b31c | 2016-01-22 16:22:51 +0900 | [diff] [blame^] | 44 | import com.android.documentsui.OperationDialogFragment; |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 45 | import com.android.documentsui.R; |
| 46 | import com.android.documentsui.Shared; |
| 47 | import com.android.documentsui.model.DocumentInfo; |
| 48 | import com.android.documentsui.model.DocumentStack; |
| 49 | import com.android.documentsui.services.FileOperationService.OpType; |
| 50 | |
| 51 | import java.util.ArrayList; |
Steve McKay | 97b4be4 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 52 | import java.util.HashMap; |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 53 | import java.util.List; |
Steve McKay | 97b4be4 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 54 | import java.util.Map; |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 55 | |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 56 | /** |
| 57 | * A mashup of work item and ui progress update factory. Used by {@link FileOperationService} |
| 58 | * to do work and show progress relating to this work. |
| 59 | */ |
Tomasz Mikolajewski | dd2b31c | 2016-01-22 16:22:51 +0900 | [diff] [blame^] | 60 | abstract public class Job implements Runnable { |
Steve McKay | 97b4be4 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 61 | private static final String TAG = "Job"; |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 62 | final Context service; |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 63 | final Context appContext; |
| 64 | final Listener listener; |
| 65 | |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 66 | final @OpType int operationType; |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 67 | final String id; |
| 68 | final DocumentStack stack; |
| 69 | |
| 70 | final ArrayList<DocumentInfo> failedFiles = new ArrayList<>(); |
| 71 | final Notification.Builder mProgressBuilder; |
| 72 | |
Steve McKay | 97b4be4 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 73 | private final Map<String, ContentProviderClient> mClients = new HashMap<>(); |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 74 | private volatile boolean mCanceled; |
| 75 | |
| 76 | /** |
| 77 | * A simple progressable job, much like an AsyncTask, but with support |
| 78 | * for providing various related notification, progress and navigation information. |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 79 | * @param operationType |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 80 | * |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 81 | * @param service The service context in which this job is running. |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 82 | * @param appContext The context of the invoking application. This is usually |
| 83 | * just {@code getApplicationContext()}. |
| 84 | * @param listener |
| 85 | * @param id Arbitrary string ID |
| 86 | * @param stack The documents stack context relating to this request. This is the |
| 87 | * destination in the Files app where the user will be take when the |
| 88 | * navigation intent is invoked (presumably from notification). |
| 89 | */ |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 90 | Job(Context service, Context appContext, Listener listener, |
| 91 | @OpType int operationType, String id, DocumentStack stack) { |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 92 | |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 93 | checkArgument(operationType != OPERATION_UNKNOWN); |
| 94 | |
| 95 | this.service = service; |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 96 | this.appContext = appContext; |
| 97 | this.listener = listener; |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 98 | this.operationType = operationType; |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 99 | |
| 100 | this.id = id; |
| 101 | this.stack = stack; |
| 102 | |
| 103 | mProgressBuilder = createProgressBuilder(); |
| 104 | } |
| 105 | |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 106 | @Override |
| 107 | public final void run() { |
| 108 | listener.onStart(this); |
| 109 | try { |
| 110 | start(); |
| 111 | } catch (Exception e) { |
| 112 | // In the case of an unmanaged failure, we still want |
| 113 | // to resolve business in an orderly fashion. That'll |
| 114 | // ensure the service is shut down and notifications |
| 115 | // shown/closed. |
Tomasz Mikolajewski | dd2b31c | 2016-01-22 16:22:51 +0900 | [diff] [blame^] | 116 | Log.e(TAG, "Operation failed due to an exception.", e); |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 117 | } finally { |
Tomasz Mikolajewski | dd2b31c | 2016-01-22 16:22:51 +0900 | [diff] [blame^] | 118 | listener.onFinished(this); |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 119 | } |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 120 | } |
| 121 | |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 122 | abstract void start() throws RemoteException; |
| 123 | |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 124 | abstract Notification getSetupNotification(); |
| 125 | // TODO: Progress notification for deletes. |
| 126 | // abstract Notification getProgressNotification(long bytesCopied); |
| 127 | abstract Notification getFailureNotification(); |
| 128 | |
Tomasz Mikolajewski | dd2b31c | 2016-01-22 16:22:51 +0900 | [diff] [blame^] | 129 | abstract Notification getWarningNotification(); |
| 130 | |
Steve McKay | 97b4be4 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 131 | ContentProviderClient getClient(DocumentInfo doc) throws RemoteException { |
| 132 | ContentProviderClient client = mClients.get(doc.authority); |
| 133 | if (client == null) { |
| 134 | // Acquire content providers. |
| 135 | client = acquireUnstableProviderOrThrow( |
| 136 | getContentResolver(), |
| 137 | doc.authority); |
| 138 | |
| 139 | mClients.put(doc.authority, client); |
| 140 | } |
| 141 | |
| 142 | return checkNotNull(client); |
| 143 | } |
| 144 | |
| 145 | final void cleanup() { |
| 146 | for (ContentProviderClient client : mClients.values()) { |
| 147 | ContentProviderClient.releaseQuietly(client); |
| 148 | } |
| 149 | } |
| 150 | |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 151 | final void cancel() { |
| 152 | mCanceled = true; |
| 153 | } |
| 154 | |
| 155 | final boolean isCanceled() { |
| 156 | return mCanceled; |
| 157 | } |
| 158 | |
| 159 | final ContentResolver getContentResolver() { |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 160 | return service.getContentResolver(); |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | void onFileFailed(DocumentInfo file) { |
| 164 | failedFiles.add(file); |
| 165 | } |
| 166 | |
Tomasz Mikolajewski | dd2b31c | 2016-01-22 16:22:51 +0900 | [diff] [blame^] | 167 | final boolean hasFailures() { |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 168 | return !failedFiles.isEmpty(); |
| 169 | } |
| 170 | |
Tomasz Mikolajewski | dd2b31c | 2016-01-22 16:22:51 +0900 | [diff] [blame^] | 171 | boolean hasWarnings() { |
| 172 | return false; |
| 173 | } |
| 174 | |
Steve McKay | 97b4be4 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 175 | final boolean deleteDocument(DocumentInfo doc) { |
| 176 | try { |
| 177 | DocumentsContract.deleteDocument(getClient(doc), doc.derivedUri); |
| 178 | } catch (RemoteException e) { |
| 179 | Log.w(TAG, "Failed to delete file: " + doc.derivedUri, e); |
| 180 | return false; |
| 181 | } |
| 182 | |
| 183 | return true; // victory dance! |
| 184 | } |
| 185 | |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 186 | Notification getSetupNotification(String content) { |
| 187 | mProgressBuilder.setProgress(0, 0, true); |
| 188 | mProgressBuilder.setContentText(content); |
| 189 | return mProgressBuilder.build(); |
| 190 | } |
| 191 | |
| 192 | Notification getFailureNotification(@PluralsRes int titleId, @DrawableRes int icon) { |
| 193 | final Intent navigateIntent = buildNavigateIntent(); |
Tomasz Mikolajewski | dd2b31c | 2016-01-22 16:22:51 +0900 | [diff] [blame^] | 194 | navigateIntent.putExtra(EXTRA_DIALOG_TYPE, OperationDialogFragment.DIALOG_TYPE_FAILURE); |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 195 | navigateIntent.putExtra(EXTRA_OPERATION, operationType); |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 196 | |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 197 | navigateIntent.putParcelableArrayListExtra(EXTRA_SRC_LIST, failedFiles); |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 198 | |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 199 | final Notification.Builder errorBuilder = new Notification.Builder(service) |
| 200 | .setContentTitle(service.getResources().getQuantityString(titleId, |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 201 | failedFiles.size(), failedFiles.size())) |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 202 | .setContentText(service.getString(R.string.notification_touch_for_details)) |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 203 | .setContentIntent(PendingIntent.getActivity(appContext, 0, navigateIntent, |
| 204 | PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT)) |
| 205 | .setCategory(Notification.CATEGORY_ERROR) |
| 206 | .setSmallIcon(icon) |
| 207 | .setAutoCancel(true); |
| 208 | return errorBuilder.build(); |
| 209 | } |
| 210 | |
| 211 | abstract Builder createProgressBuilder(); |
| 212 | |
| 213 | final Builder createProgressBuilder( |
| 214 | String title, @DrawableRes int icon, |
| 215 | String actionTitle, @DrawableRes int actionIcon) { |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 216 | Notification.Builder progressBuilder = new Notification.Builder(service) |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 217 | .setContentTitle(title) |
| 218 | .setContentIntent( |
| 219 | PendingIntent.getActivity(appContext, 0, buildNavigateIntent(), 0)) |
| 220 | .setCategory(Notification.CATEGORY_PROGRESS) |
| 221 | .setSmallIcon(icon) |
| 222 | .setOngoing(true); |
| 223 | |
| 224 | final Intent cancelIntent = createCancelIntent(); |
| 225 | |
| 226 | progressBuilder.addAction( |
| 227 | actionIcon, |
| 228 | actionTitle, |
| 229 | PendingIntent.getService( |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 230 | service, |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 231 | 0, |
| 232 | cancelIntent, |
| 233 | PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_CANCEL_CURRENT)); |
| 234 | |
| 235 | return progressBuilder; |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * Creates an intent for navigating back to the destination directory. |
| 240 | */ |
| 241 | Intent buildNavigateIntent() { |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 242 | Intent intent = new Intent(service, FilesActivity.class); |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 243 | intent.setAction(DocumentsContract.ACTION_BROWSE); |
| 244 | intent.putExtra(Shared.EXTRA_STACK, (Parcelable) stack); |
| 245 | return intent; |
| 246 | } |
| 247 | |
| 248 | Intent createCancelIntent() { |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 249 | final Intent cancelIntent = new Intent(service, FileOperationService.class); |
| 250 | cancelIntent.putExtra(EXTRA_CANCEL, true); |
| 251 | cancelIntent.putExtra(EXTRA_JOB_ID, id); |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 252 | return cancelIntent; |
| 253 | } |
| 254 | |
Steve McKay | 97b4be4 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 255 | @Override |
| 256 | public String toString() { |
| 257 | return new StringBuilder() |
| 258 | .append("Job") |
| 259 | .append("{") |
| 260 | .append("id=" + id) |
| 261 | .append("}") |
| 262 | .toString(); |
| 263 | } |
| 264 | |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 265 | /** |
| 266 | * Factory class that facilitates our testing FileOperationService. |
| 267 | */ |
| 268 | static class Factory { |
| 269 | |
| 270 | static final Factory instance = new Factory(); |
| 271 | |
| 272 | Job createCopy(Context service, Context appContext, Listener listener, |
| 273 | String id, DocumentStack stack, List<DocumentInfo> srcs) { |
| 274 | return new CopyJob(service, appContext, listener, id, stack, srcs); |
| 275 | } |
| 276 | |
| 277 | Job createMove(Context service, Context appContext, Listener listener, |
Tomasz Mikolajewski | e009441 | 2016-01-25 16:20:15 +0900 | [diff] [blame] | 278 | String id, DocumentStack stack, List<DocumentInfo> srcs, |
| 279 | DocumentInfo srcParent) { |
| 280 | return new MoveJob(service, appContext, listener, id, stack, srcs, srcParent); |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 281 | } |
Steve McKay | 97b4be4 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 282 | |
| 283 | Job createDelete(Context service, Context appContext, Listener listener, |
Tomasz Mikolajewski | e009441 | 2016-01-25 16:20:15 +0900 | [diff] [blame] | 284 | String id, DocumentStack stack, List<DocumentInfo> srcs, |
| 285 | DocumentInfo srcParent) { |
| 286 | return new DeleteJob(service, appContext, listener, id, stack, srcs, srcParent); |
Steve McKay | 97b4be4 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 287 | } |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | /** |
| 291 | * Listener interface employed by the service that owns us as well as tests. |
| 292 | */ |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 293 | interface Listener { |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 294 | void onStart(Job job); |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 295 | void onFinished(Job job); |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 296 | void onProgress(CopyJob job); |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 297 | } |
| 298 | } |