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