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 | |
Jeff Sharkey | 2c0b485 | 2019-02-15 15:53:47 -0700 | [diff] [blame] | 19 | import static android.content.ContentResolver.wrap; |
| 20 | |
Steve McKay | 97b4be4 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 21 | import static com.android.documentsui.DocumentsApplication.acquireUnstableProviderOrThrow; |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 22 | import static com.android.documentsui.services.FileOperationService.EXTRA_CANCEL; |
Tomasz Mikolajewski | dd2b31c | 2016-01-22 16:22:51 +0900 | [diff] [blame] | 23 | import static com.android.documentsui.services.FileOperationService.EXTRA_DIALOG_TYPE; |
Steve McKay | 99f1dc3 | 2016-12-29 16:02:01 -0800 | [diff] [blame] | 24 | import static com.android.documentsui.services.FileOperationService.EXTRA_FAILED_DOCS; |
| 25 | import static com.android.documentsui.services.FileOperationService.EXTRA_FAILED_URIS; |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 26 | import static com.android.documentsui.services.FileOperationService.EXTRA_JOB_ID; |
Garfield, Tan | 4833477 | 2016-06-28 17:17:38 -0700 | [diff] [blame] | 27 | import static com.android.documentsui.services.FileOperationService.EXTRA_OPERATION_TYPE; |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 28 | import static com.android.documentsui.services.FileOperationService.OPERATION_UNKNOWN; |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 29 | |
Jeff Sharkey | a4ff00f | 2018-07-09 14:57:51 -0600 | [diff] [blame] | 30 | import androidx.annotation.DrawableRes; |
| 31 | import androidx.annotation.IntDef; |
| 32 | import androidx.annotation.PluralsRes; |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 33 | import android.app.Notification; |
| 34 | import android.app.Notification.Builder; |
| 35 | import android.app.PendingIntent; |
Steve McKay | 97b4be4 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 36 | import android.content.ContentProviderClient; |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 37 | import android.content.ContentResolver; |
| 38 | import android.content.Context; |
| 39 | import android.content.Intent; |
Tomasz Mikolajewski | 63e2aae | 2016-02-01 12:01:14 +0900 | [diff] [blame] | 40 | import android.net.Uri; |
Jeff Sharkey | 2e3a6f6 | 2018-02-01 16:04:14 -0700 | [diff] [blame] | 41 | import android.os.CancellationSignal; |
Jeff Sharkey | bb68a65 | 2019-02-19 11:17:30 -0700 | [diff] [blame] | 42 | import android.os.FileUtils; |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 43 | import android.os.Parcelable; |
| 44 | import android.os.RemoteException; |
| 45 | import android.provider.DocumentsContract; |
Steve McKay | 97b4be4 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 46 | import android.util.Log; |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 47 | |
Ben Kwa | faa2720 | 2016-01-28 16:39:57 -0800 | [diff] [blame] | 48 | import com.android.documentsui.Metrics; |
Tomasz Mikolajewski | dd2b31c | 2016-01-22 16:22:51 +0900 | [diff] [blame] | 49 | import com.android.documentsui.OperationDialogFragment; |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 50 | import com.android.documentsui.R; |
Steve McKay | d080506 | 2016-09-15 14:30:38 -0700 | [diff] [blame] | 51 | import com.android.documentsui.base.DocumentInfo; |
| 52 | import com.android.documentsui.base.DocumentStack; |
Garfield Tan | 3a968cd | 2017-04-25 10:30:45 -0700 | [diff] [blame] | 53 | import com.android.documentsui.base.Features; |
Steve McKay | d9caa6a | 2016-09-15 16:36:45 -0700 | [diff] [blame] | 54 | import com.android.documentsui.base.Shared; |
Steve McKay | 99f1dc3 | 2016-12-29 16:02:01 -0800 | [diff] [blame] | 55 | import com.android.documentsui.clipping.UrisSupplier; |
| 56 | import com.android.documentsui.files.FilesActivity; |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 57 | import com.android.documentsui.services.FileOperationService.OpType; |
| 58 | |
Jeff Sharkey | 844989e | 2018-12-07 15:16:08 -0700 | [diff] [blame] | 59 | import java.io.FileNotFoundException; |
Garfield, Tan | 48ef36f | 2016-06-09 12:04:22 -0700 | [diff] [blame] | 60 | import java.lang.annotation.Retention; |
| 61 | import java.lang.annotation.RetentionPolicy; |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 62 | import java.util.ArrayList; |
Steve McKay | 97b4be4 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 63 | import java.util.HashMap; |
Steve McKay | 97b4be4 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 64 | import java.util.Map; |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 65 | |
Steve McKay | 99f1dc3 | 2016-12-29 16:02:01 -0800 | [diff] [blame] | 66 | import javax.annotation.Nullable; |
| 67 | |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 68 | /** |
| 69 | * A mashup of work item and ui progress update factory. Used by {@link FileOperationService} |
| 70 | * to do work and show progress relating to this work. |
| 71 | */ |
Tomasz Mikolajewski | dd2b31c | 2016-01-22 16:22:51 +0900 | [diff] [blame] | 72 | abstract public class Job implements Runnable { |
Steve McKay | 97b4be4 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 73 | private static final String TAG = "Job"; |
Tomasz Mikolajewski | 63e2aae | 2016-02-01 12:01:14 +0900 | [diff] [blame] | 74 | |
Garfield, Tan | 48ef36f | 2016-06-09 12:04:22 -0700 | [diff] [blame] | 75 | @Retention(RetentionPolicy.SOURCE) |
Garfield, Tan | edce554 | 2016-06-17 15:32:28 -0700 | [diff] [blame] | 76 | @IntDef({STATE_CREATED, STATE_STARTED, STATE_SET_UP, STATE_COMPLETED, STATE_CANCELED}) |
Garfield, Tan | 48ef36f | 2016-06-09 12:04:22 -0700 | [diff] [blame] | 77 | @interface State {} |
| 78 | static final int STATE_CREATED = 0; |
| 79 | static final int STATE_STARTED = 1; |
Garfield, Tan | edce554 | 2016-06-17 15:32:28 -0700 | [diff] [blame] | 80 | static final int STATE_SET_UP = 2; |
| 81 | static final int STATE_COMPLETED = 3; |
Garfield, Tan | 48ef36f | 2016-06-09 12:04:22 -0700 | [diff] [blame] | 82 | /** |
| 83 | * A job is in canceled state as long as {@link #cancel()} is called on it, even after it is |
| 84 | * completed. |
| 85 | */ |
Garfield, Tan | edce554 | 2016-06-17 15:32:28 -0700 | [diff] [blame] | 86 | static final int STATE_CANCELED = 4; |
Garfield, Tan | 48ef36f | 2016-06-09 12:04:22 -0700 | [diff] [blame] | 87 | |
Tomasz Mikolajewski | 63e2aae | 2016-02-01 12:01:14 +0900 | [diff] [blame] | 88 | static final String INTENT_TAG_WARNING = "warning"; |
| 89 | static final String INTENT_TAG_FAILURE = "failure"; |
| 90 | static final String INTENT_TAG_PROGRESS = "progress"; |
| 91 | static final String INTENT_TAG_CANCEL = "cancel"; |
| 92 | |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 93 | final Context service; |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 94 | final Context appContext; |
| 95 | final Listener listener; |
| 96 | |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 97 | final @OpType int operationType; |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 98 | final String id; |
| 99 | final DocumentStack stack; |
| 100 | |
Steve McKay | 99f1dc3 | 2016-12-29 16:02:01 -0800 | [diff] [blame] | 101 | final UrisSupplier mResourceUris; |
| 102 | |
| 103 | int failureCount = 0; |
| 104 | final ArrayList<DocumentInfo> failedDocs = new ArrayList<>(); |
| 105 | final ArrayList<Uri> failedUris = new ArrayList<>(); |
| 106 | |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 107 | final Notification.Builder mProgressBuilder; |
| 108 | |
Jeff Sharkey | 2e3a6f6 | 2018-02-01 16:04:14 -0700 | [diff] [blame] | 109 | final CancellationSignal mSignal = new CancellationSignal(); |
| 110 | |
Steve McKay | 97b4be4 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 111 | private final Map<String, ContentProviderClient> mClients = new HashMap<>(); |
Garfield Tan | 3a968cd | 2017-04-25 10:30:45 -0700 | [diff] [blame] | 112 | private final Features mFeatures; |
| 113 | |
Garfield, Tan | 48ef36f | 2016-06-09 12:04:22 -0700 | [diff] [blame] | 114 | private volatile @State int mState = STATE_CREATED; |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 115 | |
| 116 | /** |
| 117 | * A simple progressable job, much like an AsyncTask, but with support |
| 118 | * for providing various related notification, progress and navigation information. |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 119 | * @param service The service context in which this job is running. |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 120 | * @param listener |
| 121 | * @param id Arbitrary string ID |
| 122 | * @param stack The documents stack context relating to this request. This is the |
| 123 | * destination in the Files app where the user will be take when the |
| 124 | * navigation intent is invoked (presumably from notification). |
Garfield, Tan | 4833477 | 2016-06-28 17:17:38 -0700 | [diff] [blame] | 125 | * @param srcs the list of docs to operate on |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 126 | */ |
Garfield, Tan | 4833477 | 2016-06-28 17:17:38 -0700 | [diff] [blame] | 127 | Job(Context service, Listener listener, String id, |
Garfield Tan | 3a968cd | 2017-04-25 10:30:45 -0700 | [diff] [blame] | 128 | @OpType int opType, DocumentStack stack, UrisSupplier srcs, Features features) { |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 129 | |
Garfield, Tan | 4833477 | 2016-06-28 17:17:38 -0700 | [diff] [blame] | 130 | assert(opType != OPERATION_UNKNOWN); |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 131 | |
| 132 | this.service = service; |
Garfield, Tan | 4833477 | 2016-06-28 17:17:38 -0700 | [diff] [blame] | 133 | this.appContext = service.getApplicationContext(); |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 134 | this.listener = listener; |
Garfield, Tan | 4833477 | 2016-06-28 17:17:38 -0700 | [diff] [blame] | 135 | this.operationType = opType; |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 136 | |
| 137 | this.id = id; |
| 138 | this.stack = stack; |
Steve McKay | 99f1dc3 | 2016-12-29 16:02:01 -0800 | [diff] [blame] | 139 | this.mResourceUris = srcs; |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 140 | |
Garfield Tan | 3a968cd | 2017-04-25 10:30:45 -0700 | [diff] [blame] | 141 | mFeatures = features; |
| 142 | |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 143 | mProgressBuilder = createProgressBuilder(); |
| 144 | } |
| 145 | |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 146 | @Override |
| 147 | public final void run() { |
Garfield, Tan | 48ef36f | 2016-06-09 12:04:22 -0700 | [diff] [blame] | 148 | if (isCanceled()) { |
| 149 | // Canceled before running |
| 150 | return; |
| 151 | } |
| 152 | |
| 153 | mState = STATE_STARTED; |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 154 | listener.onStart(this); |
Steve McKay | 99f1dc3 | 2016-12-29 16:02:01 -0800 | [diff] [blame] | 155 | |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 156 | try { |
Garfield, Tan | edce554 | 2016-06-17 15:32:28 -0700 | [diff] [blame] | 157 | boolean result = setUp(); |
| 158 | if (result && !isCanceled()) { |
| 159 | mState = STATE_SET_UP; |
| 160 | start(); |
| 161 | } |
Tomasz Mikolajewski | c515150 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 162 | } catch (RuntimeException e) { |
| 163 | // No exceptions should be thrown here, as all calls to the provider must be |
| 164 | // handled within Job implementations. However, just in case catch them here. |
| 165 | Log.e(TAG, "Operation failed due to an unhandled runtime exception.", e); |
shawnlin | 9cee68f | 2019-01-25 11:20:18 +0800 | [diff] [blame] | 166 | Metrics.logFileOperationErrors(operationType, failedDocs, failedUris); |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 167 | } finally { |
Garfield, Tan | edce554 | 2016-06-17 15:32:28 -0700 | [diff] [blame] | 168 | mState = (mState == STATE_STARTED || mState == STATE_SET_UP) ? STATE_COMPLETED : mState; |
Tomasz Mikolajewski | ed11f1c | 2017-01-30 11:07:04 +0900 | [diff] [blame] | 169 | finish(); |
Tomasz Mikolajewski | dd2b31c | 2016-01-22 16:22:51 +0900 | [diff] [blame] | 170 | listener.onFinished(this); |
Garfield, Tan | edce554 | 2016-06-17 15:32:28 -0700 | [diff] [blame] | 171 | |
| 172 | // NOTE: If this details is a JumboClipDetails, and it's still referred in primary clip |
| 173 | // at this point, user won't be able to paste it to anywhere else because the underlying |
Steve McKay | 99f1dc3 | 2016-12-29 16:02:01 -0800 | [diff] [blame] | 174 | mResourceUris.dispose(); |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 175 | } |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 176 | } |
| 177 | |
Garfield, Tan | edce554 | 2016-06-17 15:32:28 -0700 | [diff] [blame] | 178 | boolean setUp() { |
| 179 | return true; |
| 180 | } |
Steve McKay | 99f1dc3 | 2016-12-29 16:02:01 -0800 | [diff] [blame] | 181 | |
Søren Gjesse | 09bd5bc | 2017-12-01 10:48:17 +0100 | [diff] [blame] | 182 | abstract void finish(); |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 183 | |
Tomasz Mikolajewski | ed11f1c | 2017-01-30 11:07:04 +0900 | [diff] [blame] | 184 | abstract void start(); |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 185 | abstract Notification getSetupNotification(); |
Garfield, Tan | 48ef36f | 2016-06-09 12:04:22 -0700 | [diff] [blame] | 186 | abstract Notification getProgressNotification(); |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 187 | abstract Notification getFailureNotification(); |
| 188 | |
Tomasz Mikolajewski | dd2b31c | 2016-01-22 16:22:51 +0900 | [diff] [blame] | 189 | abstract Notification getWarningNotification(); |
| 190 | |
Tomasz Mikolajewski | 63e2aae | 2016-02-01 12:01:14 +0900 | [diff] [blame] | 191 | Uri getDataUriForIntent(String tag) { |
| 192 | return Uri.parse(String.format("data,%s-%s", tag, id)); |
| 193 | } |
| 194 | |
Tomasz Mikolajewski | a011586 | 2017-03-02 15:42:08 +0900 | [diff] [blame] | 195 | ContentProviderClient getClient(Uri uri) throws RemoteException { |
| 196 | ContentProviderClient client = mClients.get(uri.getAuthority()); |
Steve McKay | 97b4be4 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 197 | if (client == null) { |
| 198 | // Acquire content providers. |
| 199 | client = acquireUnstableProviderOrThrow( |
| 200 | getContentResolver(), |
Tomasz Mikolajewski | a011586 | 2017-03-02 15:42:08 +0900 | [diff] [blame] | 201 | uri.getAuthority()); |
Steve McKay | 97b4be4 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 202 | |
Tomasz Mikolajewski | a011586 | 2017-03-02 15:42:08 +0900 | [diff] [blame] | 203 | mClients.put(uri.getAuthority(), client); |
Steve McKay | 97b4be4 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 204 | } |
| 205 | |
Steve McKay | 0af8afd | 2016-02-25 13:34:03 -0800 | [diff] [blame] | 206 | assert(client != null); |
| 207 | return client; |
Steve McKay | 97b4be4 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 208 | } |
| 209 | |
Tomasz Mikolajewski | a011586 | 2017-03-02 15:42:08 +0900 | [diff] [blame] | 210 | ContentProviderClient getClient(DocumentInfo doc) throws RemoteException { |
| 211 | return getClient(doc.derivedUri); |
| 212 | } |
| 213 | |
Steve McKay | 97b4be4 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 214 | final void cleanup() { |
| 215 | for (ContentProviderClient client : mClients.values()) { |
Jeff Sharkey | bb68a65 | 2019-02-19 11:17:30 -0700 | [diff] [blame] | 216 | FileUtils.closeQuietly(client); |
Steve McKay | 97b4be4 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | |
Garfield, Tan | 48ef36f | 2016-06-09 12:04:22 -0700 | [diff] [blame] | 220 | final @State int getState() { |
| 221 | return mState; |
| 222 | } |
| 223 | |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 224 | final void cancel() { |
Garfield, Tan | 48ef36f | 2016-06-09 12:04:22 -0700 | [diff] [blame] | 225 | mState = STATE_CANCELED; |
Jeff Sharkey | 2e3a6f6 | 2018-02-01 16:04:14 -0700 | [diff] [blame] | 226 | mSignal.cancel(); |
shawnlin | 9cee68f | 2019-01-25 11:20:18 +0800 | [diff] [blame] | 227 | Metrics.logFileOperationCancelled(operationType); |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | final boolean isCanceled() { |
Garfield, Tan | 48ef36f | 2016-06-09 12:04:22 -0700 | [diff] [blame] | 231 | return mState == STATE_CANCELED; |
| 232 | } |
| 233 | |
| 234 | final boolean isFinished() { |
| 235 | return mState == STATE_CANCELED || mState == STATE_COMPLETED; |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | final ContentResolver getContentResolver() { |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 239 | return service.getContentResolver(); |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | void onFileFailed(DocumentInfo file) { |
Steve McKay | 99f1dc3 | 2016-12-29 16:02:01 -0800 | [diff] [blame] | 243 | failureCount++; |
| 244 | failedDocs.add(file); |
| 245 | } |
| 246 | |
| 247 | void onResolveFailed(Uri uri) { |
| 248 | failureCount++; |
| 249 | failedUris.add(uri); |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 250 | } |
| 251 | |
Tomasz Mikolajewski | dd2b31c | 2016-01-22 16:22:51 +0900 | [diff] [blame] | 252 | final boolean hasFailures() { |
Steve McKay | 99f1dc3 | 2016-12-29 16:02:01 -0800 | [diff] [blame] | 253 | return failureCount > 0; |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 254 | } |
| 255 | |
Tomasz Mikolajewski | dd2b31c | 2016-01-22 16:22:51 +0900 | [diff] [blame] | 256 | boolean hasWarnings() { |
| 257 | return false; |
| 258 | } |
| 259 | |
Garfield Tan | 03a3a39 | 2016-12-12 14:06:45 -0800 | [diff] [blame] | 260 | final void deleteDocument(DocumentInfo doc, @Nullable DocumentInfo parent) |
| 261 | throws ResourceException { |
Steve McKay | 97b4be4 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 262 | try { |
Garfield Tan | 03a3a39 | 2016-12-12 14:06:45 -0800 | [diff] [blame] | 263 | if (parent != null && doc.isRemoveSupported()) { |
Jeff Sharkey | 2c0b485 | 2019-02-15 15:53:47 -0700 | [diff] [blame] | 264 | DocumentsContract.removeDocument(wrap(getClient(doc)), doc.derivedUri, |
| 265 | parent.derivedUri); |
Tomasz Mikolajewski | 8fe5498 | 2016-02-23 15:12:54 +0900 | [diff] [blame] | 266 | } else if (doc.isDeleteSupported()) { |
Jeff Sharkey | 2c0b485 | 2019-02-15 15:53:47 -0700 | [diff] [blame] | 267 | DocumentsContract.deleteDocument(wrap(getClient(doc)), doc.derivedUri); |
Tomasz Mikolajewski | 8fe5498 | 2016-02-23 15:12:54 +0900 | [diff] [blame] | 268 | } else { |
Steve McKay | 99f1dc3 | 2016-12-29 16:02:01 -0800 | [diff] [blame] | 269 | throw new ResourceException("Unable to delete source document. " |
| 270 | + "File is not deletable or removable: %s.", doc.derivedUri); |
Tomasz Mikolajewski | 8fe5498 | 2016-02-23 15:12:54 +0900 | [diff] [blame] | 271 | } |
Jeff Sharkey | 844989e | 2018-12-07 15:16:08 -0700 | [diff] [blame] | 272 | } catch (FileNotFoundException | RemoteException | RuntimeException e) { |
Tomasz Mikolajewski | c515150 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 273 | throw new ResourceException("Failed to delete file %s due to an exception.", |
| 274 | doc.derivedUri, e); |
Steve McKay | 97b4be4 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 275 | } |
Steve McKay | 97b4be4 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 276 | } |
| 277 | |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 278 | Notification getSetupNotification(String content) { |
Steve McKay | 003097d | 2016-02-23 10:06:50 -0800 | [diff] [blame] | 279 | mProgressBuilder.setProgress(0, 0, true) |
| 280 | .setContentText(content); |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 281 | return mProgressBuilder.build(); |
| 282 | } |
| 283 | |
| 284 | Notification getFailureNotification(@PluralsRes int titleId, @DrawableRes int icon) { |
Tomasz Mikolajewski | 63e2aae | 2016-02-01 12:01:14 +0900 | [diff] [blame] | 285 | final Intent navigateIntent = buildNavigateIntent(INTENT_TAG_FAILURE); |
Tomasz Mikolajewski | dd2b31c | 2016-01-22 16:22:51 +0900 | [diff] [blame] | 286 | navigateIntent.putExtra(EXTRA_DIALOG_TYPE, OperationDialogFragment.DIALOG_TYPE_FAILURE); |
Garfield, Tan | 4833477 | 2016-06-28 17:17:38 -0700 | [diff] [blame] | 287 | navigateIntent.putExtra(EXTRA_OPERATION_TYPE, operationType); |
Steve McKay | 99f1dc3 | 2016-12-29 16:02:01 -0800 | [diff] [blame] | 288 | navigateIntent.putParcelableArrayListExtra(EXTRA_FAILED_DOCS, failedDocs); |
| 289 | navigateIntent.putParcelableArrayListExtra(EXTRA_FAILED_URIS, failedUris); |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 290 | |
Garfield Tan | 3a968cd | 2017-04-25 10:30:45 -0700 | [diff] [blame] | 291 | final Notification.Builder errorBuilder = createNotificationBuilder() |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 292 | .setContentTitle(service.getResources().getQuantityString(titleId, |
Steve McKay | 99f1dc3 | 2016-12-29 16:02:01 -0800 | [diff] [blame] | 293 | failureCount, failureCount)) |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 294 | .setContentText(service.getString(R.string.notification_touch_for_details)) |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 295 | .setContentIntent(PendingIntent.getActivity(appContext, 0, navigateIntent, |
| 296 | PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT)) |
| 297 | .setCategory(Notification.CATEGORY_ERROR) |
| 298 | .setSmallIcon(icon) |
| 299 | .setAutoCancel(true); |
Steve McKay | 003097d | 2016-02-23 10:06:50 -0800 | [diff] [blame] | 300 | |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 301 | return errorBuilder.build(); |
| 302 | } |
| 303 | |
| 304 | abstract Builder createProgressBuilder(); |
| 305 | |
| 306 | final Builder createProgressBuilder( |
| 307 | String title, @DrawableRes int icon, |
| 308 | String actionTitle, @DrawableRes int actionIcon) { |
Garfield Tan | 3a968cd | 2017-04-25 10:30:45 -0700 | [diff] [blame] | 309 | Notification.Builder progressBuilder = createNotificationBuilder() |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 310 | .setContentTitle(title) |
| 311 | .setContentIntent( |
Tomasz Mikolajewski | 63e2aae | 2016-02-01 12:01:14 +0900 | [diff] [blame] | 312 | PendingIntent.getActivity(appContext, 0, |
| 313 | buildNavigateIntent(INTENT_TAG_PROGRESS), 0)) |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 314 | .setCategory(Notification.CATEGORY_PROGRESS) |
| 315 | .setSmallIcon(icon) |
| 316 | .setOngoing(true); |
| 317 | |
| 318 | final Intent cancelIntent = createCancelIntent(); |
| 319 | |
| 320 | progressBuilder.addAction( |
| 321 | actionIcon, |
| 322 | actionTitle, |
| 323 | PendingIntent.getService( |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 324 | service, |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 325 | 0, |
| 326 | cancelIntent, |
| 327 | PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_CANCEL_CURRENT)); |
| 328 | |
| 329 | return progressBuilder; |
| 330 | } |
| 331 | |
Garfield Tan | 3a968cd | 2017-04-25 10:30:45 -0700 | [diff] [blame] | 332 | Notification.Builder createNotificationBuilder() { |
| 333 | return mFeatures.isNotificationChannelEnabled() |
| 334 | ? new Notification.Builder(service, FileOperationService.NOTIFICATION_CHANNEL_ID) |
| 335 | : new Notification.Builder(service); |
| 336 | } |
| 337 | |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 338 | /** |
| 339 | * Creates an intent for navigating back to the destination directory. |
| 340 | */ |
Tomasz Mikolajewski | 63e2aae | 2016-02-01 12:01:14 +0900 | [diff] [blame] | 341 | Intent buildNavigateIntent(String tag) { |
Garfield Tan | c5efea0 | 2017-02-22 12:58:29 -0800 | [diff] [blame] | 342 | // TODO (b/35721285): Reuse an existing task rather than creating a new one every time. |
Steve McKay | b6006b2 | 2016-09-29 09:23:45 -0700 | [diff] [blame] | 343 | Intent intent = new Intent(service, FilesActivity.class); |
Tomasz Mikolajewski | 63e2aae | 2016-02-01 12:01:14 +0900 | [diff] [blame] | 344 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
Tomasz Mikolajewski | 63e2aae | 2016-02-01 12:01:14 +0900 | [diff] [blame] | 345 | intent.setData(getDataUriForIntent(tag)); |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 346 | intent.putExtra(Shared.EXTRA_STACK, (Parcelable) stack); |
| 347 | return intent; |
| 348 | } |
| 349 | |
| 350 | Intent createCancelIntent() { |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 351 | final Intent cancelIntent = new Intent(service, FileOperationService.class); |
Tomasz Mikolajewski | 63e2aae | 2016-02-01 12:01:14 +0900 | [diff] [blame] | 352 | cancelIntent.setData(getDataUriForIntent(INTENT_TAG_CANCEL)); |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 353 | cancelIntent.putExtra(EXTRA_CANCEL, true); |
| 354 | cancelIntent.putExtra(EXTRA_JOB_ID, id); |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 355 | return cancelIntent; |
| 356 | } |
| 357 | |
Steve McKay | 97b4be4 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 358 | @Override |
| 359 | public String toString() { |
| 360 | return new StringBuilder() |
| 361 | .append("Job") |
| 362 | .append("{") |
| 363 | .append("id=" + id) |
| 364 | .append("}") |
| 365 | .toString(); |
| 366 | } |
| 367 | |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 368 | /** |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 369 | * Listener interface employed by the service that owns us as well as tests. |
| 370 | */ |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 371 | interface Listener { |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 372 | void onStart(Job job); |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 373 | void onFinished(Job job); |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 374 | } |
lumark | dbf6512 | 2018-05-28 18:42:58 +0800 | [diff] [blame] | 375 | |
| 376 | /** |
| 377 | * Interface for tracking job progress. |
| 378 | */ |
| 379 | interface ProgressTracker { |
| 380 | default double getProgress() { return -1; } |
| 381 | default long getRemainingTimeEstimate() { |
| 382 | return -1; |
| 383 | } |
| 384 | } |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 385 | } |