Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [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; |
| 18 | |
Ben Kwa | f527c63 | 2015-04-08 15:03:35 -0700 | [diff] [blame] | 19 | import static com.android.documentsui.model.DocumentInfo.getCursorLong; |
| 20 | import static com.android.documentsui.model.DocumentInfo.getCursorString; |
| 21 | |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 22 | import android.app.IntentService; |
| 23 | import android.app.Notification; |
| 24 | import android.app.NotificationManager; |
| 25 | import android.app.PendingIntent; |
Ben Kwa | f527c63 | 2015-04-08 15:03:35 -0700 | [diff] [blame] | 26 | import android.content.ContentProviderClient; |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 27 | import android.content.Context; |
| 28 | import android.content.Intent; |
Tomasz Mikolajewski | 9452c44 | 2015-04-14 16:32:41 +0900 | [diff] [blame] | 29 | import android.content.res.Resources; |
Ben Kwa | f527c63 | 2015-04-08 15:03:35 -0700 | [diff] [blame] | 30 | import android.database.Cursor; |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 31 | import android.net.Uri; |
Ben Kwa | f585893 | 2015-04-07 15:43:39 -0700 | [diff] [blame] | 32 | import android.os.CancellationSignal; |
Ben Kwa | f585893 | 2015-04-07 15:43:39 -0700 | [diff] [blame] | 33 | import android.os.ParcelFileDescriptor; |
Tomasz Mikolajewski | 2023fcf | 2015-04-10 10:30:33 +0900 | [diff] [blame] | 34 | import android.os.Parcelable; |
Ben Kwa | f527c63 | 2015-04-08 15:03:35 -0700 | [diff] [blame] | 35 | import android.os.RemoteException; |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 36 | import android.os.SystemClock; |
Ben Kwa | f585893 | 2015-04-07 15:43:39 -0700 | [diff] [blame] | 37 | import android.provider.DocumentsContract; |
Ben Kwa | f527c63 | 2015-04-08 15:03:35 -0700 | [diff] [blame] | 38 | import android.provider.DocumentsContract.Document; |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 39 | import android.text.format.DateUtils; |
| 40 | import android.util.Log; |
Tomasz Mikolajewski | 9452c44 | 2015-04-14 16:32:41 +0900 | [diff] [blame] | 41 | import android.widget.Toast; |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 42 | |
| 43 | import com.android.documentsui.model.DocumentInfo; |
Tomasz Mikolajewski | 2023fcf | 2015-04-10 10:30:33 +0900 | [diff] [blame] | 44 | import com.android.documentsui.model.DocumentStack; |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 45 | |
| 46 | import libcore.io.IoUtils; |
| 47 | |
Tomasz Mikolajewski | 9452c44 | 2015-04-14 16:32:41 +0900 | [diff] [blame] | 48 | import java.io.FileNotFoundException; |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 49 | import java.io.IOException; |
| 50 | import java.io.InputStream; |
| 51 | import java.io.OutputStream; |
| 52 | import java.text.NumberFormat; |
| 53 | import java.util.ArrayList; |
Ben Kwa | f527c63 | 2015-04-08 15:03:35 -0700 | [diff] [blame] | 54 | import java.util.List; |
| 55 | import java.util.Objects; |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 56 | |
| 57 | public class CopyService extends IntentService { |
| 58 | public static final String TAG = "CopyService"; |
Tomasz Mikolajewski | 2023fcf | 2015-04-10 10:30:33 +0900 | [diff] [blame] | 59 | |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 60 | private static final String EXTRA_CANCEL = "com.android.documentsui.CANCEL"; |
Tomasz Mikolajewski | 2023fcf | 2015-04-10 10:30:33 +0900 | [diff] [blame] | 61 | public static final String EXTRA_SRC_LIST = "com.android.documentsui.SRC_LIST"; |
| 62 | public static final String EXTRA_STACK = "com.android.documentsui.STACK"; |
Tomasz Mikolajewski | 332d819 | 2015-04-13 19:38:43 +0900 | [diff] [blame] | 63 | public static final String EXTRA_FAILURE = "com.android.documentsui.FAILURE"; |
Ben Kwa | cb4461f | 2015-05-05 11:50:11 -0700 | [diff] [blame^] | 64 | public static final String EXTRA_TRANSFER_MODE = "com.android.documentsui.TRANSFER_MODE"; |
| 65 | |
| 66 | public static final int TRANSFER_MODE_NONE = 0; |
| 67 | public static final int TRANSFER_MODE_COPY = 1; |
| 68 | public static final int TRANSFER_MODE_MOVE = 2; |
Tomasz Mikolajewski | 332d819 | 2015-04-13 19:38:43 +0900 | [diff] [blame] | 69 | |
| 70 | // TODO: Move it to a shared file when more operations are implemented. |
| 71 | public static final int FAILURE_COPY = 1; |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 72 | |
| 73 | private NotificationManager mNotificationManager; |
| 74 | private Notification.Builder mProgressBuilder; |
| 75 | |
| 76 | // Jobs are serialized but a job ID is used, to avoid mixing up cancellation requests. |
| 77 | private String mJobId; |
| 78 | private volatile boolean mIsCancelled; |
| 79 | // Parameters of the copy job. Requests to an IntentService are serialized so this code only |
| 80 | // needs to deal with one job at a time. |
Tomasz Mikolajewski | 9452c44 | 2015-04-14 16:32:41 +0900 | [diff] [blame] | 81 | private final ArrayList<DocumentInfo> mFailedFiles; |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 82 | private long mBatchSize; |
| 83 | private long mBytesCopied; |
| 84 | private long mStartTime; |
| 85 | private long mLastNotificationTime; |
| 86 | // Speed estimation |
| 87 | private long mBytesCopiedSample; |
| 88 | private long mSampleTime; |
| 89 | private long mSpeed; |
| 90 | private long mRemainingTime; |
Ben Kwa | f527c63 | 2015-04-08 15:03:35 -0700 | [diff] [blame] | 91 | // Provider clients are acquired for the duration of each copy job. Note that there is an |
| 92 | // implicit assumption that all srcs come from the same authority. |
| 93 | private ContentProviderClient mSrcClient; |
| 94 | private ContentProviderClient mDstClient; |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 95 | |
| 96 | public CopyService() { |
| 97 | super("CopyService"); |
Ben Kwa | f527c63 | 2015-04-08 15:03:35 -0700 | [diff] [blame] | 98 | |
Tomasz Mikolajewski | 9452c44 | 2015-04-14 16:32:41 +0900 | [diff] [blame] | 99 | mFailedFiles = new ArrayList<DocumentInfo>(); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Starts the service for a copy operation. |
| 104 | * |
| 105 | * @param context Context for the intent. |
| 106 | * @param srcDocs A list of src files to copy. |
| 107 | * @param dstStack The copy destination stack. |
| 108 | */ |
Ben Kwa | cb4461f | 2015-05-05 11:50:11 -0700 | [diff] [blame^] | 109 | public static void start(Context context, List<DocumentInfo> srcDocs, DocumentStack dstStack, |
| 110 | int mode) { |
Tomasz Mikolajewski | 9452c44 | 2015-04-14 16:32:41 +0900 | [diff] [blame] | 111 | final Resources res = context.getResources(); |
| 112 | final Intent copyIntent = new Intent(context, CopyService.class); |
| 113 | copyIntent.putParcelableArrayListExtra( |
| 114 | EXTRA_SRC_LIST, new ArrayList<DocumentInfo>(srcDocs)); |
| 115 | copyIntent.putExtra(EXTRA_STACK, (Parcelable) dstStack); |
Ben Kwa | cb4461f | 2015-05-05 11:50:11 -0700 | [diff] [blame^] | 116 | copyIntent.putExtra(EXTRA_TRANSFER_MODE, mode); |
Tomasz Mikolajewski | 9452c44 | 2015-04-14 16:32:41 +0900 | [diff] [blame] | 117 | |
Ben Kwa | cb4461f | 2015-05-05 11:50:11 -0700 | [diff] [blame^] | 118 | int toastMessage = (mode == TRANSFER_MODE_COPY) ? R.plurals.copy_begin |
| 119 | : R.plurals.move_begin; |
Tomasz Mikolajewski | 9452c44 | 2015-04-14 16:32:41 +0900 | [diff] [blame] | 120 | Toast.makeText(context, |
Ben Kwa | cb4461f | 2015-05-05 11:50:11 -0700 | [diff] [blame^] | 121 | res.getQuantityString(toastMessage, srcDocs.size(), srcDocs.size()), |
Tomasz Mikolajewski | 9452c44 | 2015-04-14 16:32:41 +0900 | [diff] [blame] | 122 | Toast.LENGTH_SHORT).show(); |
| 123 | context.startService(copyIntent); |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public int onStartCommand(Intent intent, int flags, int startId) { |
| 128 | if (intent.hasExtra(EXTRA_CANCEL)) { |
| 129 | handleCancel(intent); |
| 130 | } |
| 131 | return super.onStartCommand(intent, flags, startId); |
| 132 | } |
| 133 | |
| 134 | @Override |
| 135 | protected void onHandleIntent(Intent intent) { |
| 136 | if (intent.hasExtra(EXTRA_CANCEL)) { |
| 137 | handleCancel(intent); |
| 138 | return; |
| 139 | } |
| 140 | |
Tomasz Mikolajewski | 2023fcf | 2015-04-10 10:30:33 +0900 | [diff] [blame] | 141 | final ArrayList<DocumentInfo> srcs = intent.getParcelableArrayListExtra(EXTRA_SRC_LIST); |
| 142 | final DocumentStack stack = intent.getParcelableExtra(EXTRA_STACK); |
Ben Kwa | cb4461f | 2015-05-05 11:50:11 -0700 | [diff] [blame^] | 143 | // Copy by default. |
| 144 | final int transferMode = intent.getIntExtra(EXTRA_TRANSFER_MODE, TRANSFER_MODE_COPY); |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 145 | |
Ben Kwa | f527c63 | 2015-04-08 15:03:35 -0700 | [diff] [blame] | 146 | try { |
| 147 | // Acquire content providers. |
| 148 | mSrcClient = DocumentsApplication.acquireUnstableProviderOrThrow(getContentResolver(), |
| 149 | srcs.get(0).authority); |
| 150 | mDstClient = DocumentsApplication.acquireUnstableProviderOrThrow(getContentResolver(), |
Tomasz Mikolajewski | 2023fcf | 2015-04-10 10:30:33 +0900 | [diff] [blame] | 151 | stack.peek().authority); |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 152 | |
Tomasz Mikolajewski | 2023fcf | 2015-04-10 10:30:33 +0900 | [diff] [blame] | 153 | setupCopyJob(srcs, stack); |
Ben Kwa | f527c63 | 2015-04-08 15:03:35 -0700 | [diff] [blame] | 154 | |
| 155 | for (int i = 0; i < srcs.size() && !mIsCancelled; ++i) { |
Ben Kwa | cb4461f | 2015-05-05 11:50:11 -0700 | [diff] [blame^] | 156 | copy(srcs.get(i), stack.peek(), transferMode); |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 157 | } |
Ben Kwa | f527c63 | 2015-04-08 15:03:35 -0700 | [diff] [blame] | 158 | } catch (Exception e) { |
| 159 | // Catch-all to prevent any copy errors from wedging the app. |
| 160 | Log.e(TAG, "Exceptions occurred during copying", e); |
| 161 | } finally { |
| 162 | ContentProviderClient.releaseQuietly(mSrcClient); |
| 163 | ContentProviderClient.releaseQuietly(mDstClient); |
| 164 | |
| 165 | // Dismiss the ongoing copy notification when the copy is done. |
| 166 | mNotificationManager.cancel(mJobId, 0); |
| 167 | |
| 168 | if (mFailedFiles.size() > 0) { |
Tomasz Mikolajewski | 332d819 | 2015-04-13 19:38:43 +0900 | [diff] [blame] | 169 | final Context context = getApplicationContext(); |
| 170 | final Intent navigateIntent = new Intent(context, StandaloneActivity.class); |
| 171 | navigateIntent.putExtra(EXTRA_STACK, (Parcelable) stack); |
| 172 | navigateIntent.putExtra(EXTRA_FAILURE, FAILURE_COPY); |
| 173 | navigateIntent.putParcelableArrayListExtra(EXTRA_SRC_LIST, mFailedFiles); |
| 174 | |
| 175 | final Notification.Builder errorBuilder = new Notification.Builder(this) |
| 176 | .setContentTitle(context.getResources(). |
| 177 | getQuantityString(R.plurals.copy_error_notification_title, |
| 178 | mFailedFiles.size(), mFailedFiles.size())) |
| 179 | .setContentText(getString(R.string.notification_touch_for_details)) |
| 180 | .setContentIntent(PendingIntent.getActivity(context, 0, navigateIntent, |
| 181 | PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT)) |
| 182 | .setCategory(Notification.CATEGORY_ERROR) |
| 183 | .setSmallIcon(R.drawable.ic_menu_copy) |
| 184 | .setAutoCancel(true); |
| 185 | mNotificationManager.notify(mJobId, 0, errorBuilder.build()); |
Ben Kwa | f527c63 | 2015-04-08 15:03:35 -0700 | [diff] [blame] | 186 | } |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 187 | } |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | @Override |
| 191 | public void onCreate() { |
| 192 | super.onCreate(); |
| 193 | mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Sets up the CopyService to start tracking and sending notifications for the given batch of |
| 198 | * files. |
| 199 | * |
| 200 | * @param srcs A list of src files to copy. |
Tomasz Mikolajewski | 2023fcf | 2015-04-10 10:30:33 +0900 | [diff] [blame] | 201 | * @param stack The copy destination stack. |
Ben Kwa | f527c63 | 2015-04-08 15:03:35 -0700 | [diff] [blame] | 202 | * @throws RemoteException |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 203 | */ |
Tomasz Mikolajewski | 2023fcf | 2015-04-10 10:30:33 +0900 | [diff] [blame] | 204 | private void setupCopyJob(ArrayList<DocumentInfo> srcs, DocumentStack stack) |
Ben Kwa | f527c63 | 2015-04-08 15:03:35 -0700 | [diff] [blame] | 205 | throws RemoteException { |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 206 | // Create an ID for this copy job. Use the timestamp. |
| 207 | mJobId = String.valueOf(SystemClock.elapsedRealtime()); |
| 208 | // Reset the cancellation flag. |
| 209 | mIsCancelled = false; |
| 210 | |
Tomasz Mikolajewski | 2023fcf | 2015-04-10 10:30:33 +0900 | [diff] [blame] | 211 | final Context context = getApplicationContext(); |
| 212 | final Intent navigateIntent = new Intent(context, StandaloneActivity.class); |
Tomasz Mikolajewski | 332d819 | 2015-04-13 19:38:43 +0900 | [diff] [blame] | 213 | navigateIntent.putExtra(EXTRA_STACK, (Parcelable) stack); |
Tomasz Mikolajewski | 2023fcf | 2015-04-10 10:30:33 +0900 | [diff] [blame] | 214 | |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 215 | mProgressBuilder = new Notification.Builder(this) |
| 216 | .setContentTitle(getString(R.string.copy_notification_title)) |
Tomasz Mikolajewski | 2023fcf | 2015-04-10 10:30:33 +0900 | [diff] [blame] | 217 | .setContentIntent(PendingIntent.getActivity(context, 0, navigateIntent, 0)) |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 218 | .setCategory(Notification.CATEGORY_PROGRESS) |
Tomasz Mikolajewski | 332d819 | 2015-04-13 19:38:43 +0900 | [diff] [blame] | 219 | .setSmallIcon(R.drawable.ic_menu_copy) |
| 220 | .setOngoing(true); |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 221 | |
Tomasz Mikolajewski | 2023fcf | 2015-04-10 10:30:33 +0900 | [diff] [blame] | 222 | final Intent cancelIntent = new Intent(this, CopyService.class); |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 223 | cancelIntent.putExtra(EXTRA_CANCEL, mJobId); |
| 224 | mProgressBuilder.addAction(R.drawable.ic_cab_cancel, |
Daichi Hirono | 677ce6c | 2015-04-15 14:33:52 +0900 | [diff] [blame] | 225 | getString(android.R.string.cancel), PendingIntent.getService(this, 0, |
Ben Kwa | c89eb7c | 2015-04-15 12:13:32 -0700 | [diff] [blame] | 226 | cancelIntent, |
| 227 | PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_CANCEL_CURRENT)); |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 228 | |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 229 | // Send an initial progress notification. |
Ben Kwa | f527c63 | 2015-04-08 15:03:35 -0700 | [diff] [blame] | 230 | mProgressBuilder.setProgress(0, 0, true); // Indeterminate progress while setting up. |
| 231 | mProgressBuilder.setContentText(getString(R.string.copy_preparing)); |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 232 | mNotificationManager.notify(mJobId, 0, mProgressBuilder.build()); |
| 233 | |
| 234 | // Reset batch parameters. |
Ben Kwa | f527c63 | 2015-04-08 15:03:35 -0700 | [diff] [blame] | 235 | mFailedFiles.clear(); |
| 236 | mBatchSize = calculateFileSizes(srcs); |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 237 | mBytesCopied = 0; |
| 238 | mStartTime = SystemClock.elapsedRealtime(); |
| 239 | mLastNotificationTime = 0; |
| 240 | mBytesCopiedSample = 0; |
| 241 | mSampleTime = 0; |
| 242 | mSpeed = 0; |
| 243 | mRemainingTime = 0; |
| 244 | |
| 245 | // TODO: Check preconditions for copy. |
| 246 | // - check that the destination has enough space and is writeable? |
| 247 | // - check MIME types? |
| 248 | } |
| 249 | |
| 250 | /** |
Ben Kwa | f527c63 | 2015-04-08 15:03:35 -0700 | [diff] [blame] | 251 | * Calculates the cumulative size of all the documents in the list. Directories are recursed |
| 252 | * into and totaled up. |
| 253 | * |
| 254 | * @param srcs |
| 255 | * @return Size in bytes. |
| 256 | * @throws RemoteException |
| 257 | */ |
| 258 | private long calculateFileSizes(List<DocumentInfo> srcs) throws RemoteException { |
| 259 | long result = 0; |
| 260 | for (DocumentInfo src : srcs) { |
| 261 | if (Document.MIME_TYPE_DIR.equals(src.mimeType)) { |
| 262 | // Directories need to be recursed into. |
| 263 | result += calculateFileSizesHelper(src.derivedUri); |
| 264 | } else { |
| 265 | result += src.size; |
| 266 | } |
| 267 | } |
| 268 | return result; |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * Calculates (recursively) the cumulative size of all the files under the given directory. |
| 273 | * |
| 274 | * @throws RemoteException |
| 275 | */ |
| 276 | private long calculateFileSizesHelper(Uri uri) throws RemoteException { |
| 277 | final String authority = uri.getAuthority(); |
| 278 | final Uri queryUri = DocumentsContract.buildChildDocumentsUri(authority, |
| 279 | DocumentsContract.getDocumentId(uri)); |
| 280 | final String queryColumns[] = new String[] { |
| 281 | Document.COLUMN_DOCUMENT_ID, |
| 282 | Document.COLUMN_MIME_TYPE, |
| 283 | Document.COLUMN_SIZE |
| 284 | }; |
| 285 | |
| 286 | long result = 0; |
| 287 | Cursor cursor = null; |
| 288 | try { |
| 289 | cursor = mSrcClient.query(queryUri, queryColumns, null, null, null); |
| 290 | while (cursor.moveToNext()) { |
| 291 | if (Document.MIME_TYPE_DIR.equals( |
| 292 | getCursorString(cursor, Document.COLUMN_MIME_TYPE))) { |
| 293 | // Recurse into directories. |
| 294 | final Uri subdirUri = DocumentsContract.buildDocumentUri(authority, |
| 295 | getCursorString(cursor, Document.COLUMN_DOCUMENT_ID)); |
| 296 | result += calculateFileSizesHelper(subdirUri); |
| 297 | } else { |
| 298 | // This may return -1 if the size isn't defined. Ignore those cases. |
| 299 | long size = getCursorLong(cursor, Document.COLUMN_SIZE); |
| 300 | result += size > 0 ? size : 0; |
| 301 | } |
| 302 | } |
| 303 | } finally { |
| 304 | IoUtils.closeQuietly(cursor); |
| 305 | } |
| 306 | |
| 307 | return result; |
| 308 | } |
| 309 | |
| 310 | /** |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 311 | * Cancels the current copy job, if its ID matches the given ID. |
| 312 | * |
| 313 | * @param intent The cancellation intent. |
| 314 | */ |
| 315 | private void handleCancel(Intent intent) { |
| 316 | final String cancelledId = intent.getStringExtra(EXTRA_CANCEL); |
| 317 | // Do nothing if the cancelled ID doesn't match the current job ID. This prevents racey |
| 318 | // cancellation requests from affecting unrelated copy jobs. |
Ben Kwa | f527c63 | 2015-04-08 15:03:35 -0700 | [diff] [blame] | 319 | if (Objects.equals(mJobId, cancelledId)) { |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 320 | // Set the cancel flag. This causes the copy loops to exit. |
| 321 | mIsCancelled = true; |
| 322 | // Dismiss the progress notification here rather than in the copy loop. This preserves |
| 323 | // interactivity for the user in case the copy loop is stalled. |
| 324 | mNotificationManager.cancel(mJobId, 0); |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | /** |
| 329 | * Logs progress on the current copy operation. Displays/Updates the progress notification. |
| 330 | * |
| 331 | * @param bytesCopied |
| 332 | */ |
| 333 | private void makeProgress(long bytesCopied) { |
| 334 | mBytesCopied += bytesCopied; |
| 335 | double done = (double) mBytesCopied / mBatchSize; |
| 336 | String percent = NumberFormat.getPercentInstance().format(done); |
| 337 | |
| 338 | // Update time estimate |
| 339 | long currentTime = SystemClock.elapsedRealtime(); |
| 340 | long elapsedTime = currentTime - mStartTime; |
| 341 | |
| 342 | // Send out progress notifications once a second. |
| 343 | if (currentTime - mLastNotificationTime > 1000) { |
| 344 | updateRemainingTimeEstimate(elapsedTime); |
| 345 | mProgressBuilder.setProgress(100, (int) (done * 100), false); |
| 346 | mProgressBuilder.setContentInfo(percent); |
| 347 | if (mRemainingTime > 0) { |
| 348 | mProgressBuilder.setContentText(getString(R.string.copy_remaining, |
| 349 | DateUtils.formatDuration(mRemainingTime))); |
| 350 | } else { |
| 351 | mProgressBuilder.setContentText(null); |
| 352 | } |
| 353 | mNotificationManager.notify(mJobId, 0, mProgressBuilder.build()); |
| 354 | mLastNotificationTime = currentTime; |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | /** |
| 359 | * Generates an estimate of the remaining time in the copy. |
| 360 | * |
| 361 | * @param elapsedTime The time elapsed so far. |
| 362 | */ |
| 363 | private void updateRemainingTimeEstimate(long elapsedTime) { |
| 364 | final long sampleDuration = elapsedTime - mSampleTime; |
| 365 | final long sampleSpeed = ((mBytesCopied - mBytesCopiedSample) * 1000) / sampleDuration; |
| 366 | if (mSpeed == 0) { |
| 367 | mSpeed = sampleSpeed; |
| 368 | } else { |
| 369 | mSpeed = ((3 * mSpeed) + sampleSpeed) / 4; |
| 370 | } |
| 371 | |
| 372 | if (mSampleTime > 0 && mSpeed > 0) { |
| 373 | mRemainingTime = ((mBatchSize - mBytesCopied) * 1000) / mSpeed; |
| 374 | } else { |
| 375 | mRemainingTime = 0; |
| 376 | } |
| 377 | |
| 378 | mSampleTime = elapsedTime; |
| 379 | mBytesCopiedSample = mBytesCopied; |
| 380 | } |
| 381 | |
| 382 | /** |
Ben Kwa | f527c63 | 2015-04-08 15:03:35 -0700 | [diff] [blame] | 383 | * Copies a the given documents to the given location. |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 384 | * |
Ben Kwa | f527c63 | 2015-04-08 15:03:35 -0700 | [diff] [blame] | 385 | * @param srcInfo DocumentInfos for the documents to copy. |
Tomasz Mikolajewski | 2023fcf | 2015-04-10 10:30:33 +0900 | [diff] [blame] | 386 | * @param dstDirInfo The destination directory. |
Ben Kwa | f527c63 | 2015-04-08 15:03:35 -0700 | [diff] [blame] | 387 | * @throws RemoteException |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 388 | */ |
Ben Kwa | cb4461f | 2015-05-05 11:50:11 -0700 | [diff] [blame^] | 389 | private void copy(DocumentInfo srcInfo, DocumentInfo dstDirInfo, int mode) |
| 390 | throws RemoteException { |
Tomasz Mikolajewski | 2023fcf | 2015-04-10 10:30:33 +0900 | [diff] [blame] | 391 | final Uri dstUri = DocumentsContract.createDocument(mDstClient, dstDirInfo.derivedUri, |
Ben Kwa | f585893 | 2015-04-07 15:43:39 -0700 | [diff] [blame] | 392 | srcInfo.mimeType, srcInfo.displayName); |
Ben Kwa | f527c63 | 2015-04-08 15:03:35 -0700 | [diff] [blame] | 393 | if (dstUri == null) { |
| 394 | // If this is a directory, the entire subdir will not be copied over. |
| 395 | Log.e(TAG, "Error while copying " + srcInfo.displayName); |
Tomasz Mikolajewski | 9452c44 | 2015-04-14 16:32:41 +0900 | [diff] [blame] | 396 | mFailedFiles.add(srcInfo); |
Ben Kwa | f527c63 | 2015-04-08 15:03:35 -0700 | [diff] [blame] | 397 | return; |
| 398 | } |
Ben Kwa | f585893 | 2015-04-07 15:43:39 -0700 | [diff] [blame] | 399 | |
Ben Kwa | f527c63 | 2015-04-08 15:03:35 -0700 | [diff] [blame] | 400 | if (Document.MIME_TYPE_DIR.equals(srcInfo.mimeType)) { |
Ben Kwa | cb4461f | 2015-05-05 11:50:11 -0700 | [diff] [blame^] | 401 | copyDirectoryHelper(srcInfo.derivedUri, dstUri, mode); |
Ben Kwa | f527c63 | 2015-04-08 15:03:35 -0700 | [diff] [blame] | 402 | } else { |
Ben Kwa | cb4461f | 2015-05-05 11:50:11 -0700 | [diff] [blame^] | 403 | copyFileHelper(srcInfo.derivedUri, dstUri, mode); |
Ben Kwa | f527c63 | 2015-04-08 15:03:35 -0700 | [diff] [blame] | 404 | } |
| 405 | } |
| 406 | |
| 407 | /** |
| 408 | * Handles recursion into a directory and copying its contents. Note that in linux terms, this |
| 409 | * does the equivalent of "cp src/* dst", not "cp -r src dst". |
| 410 | * |
| 411 | * @param srcDirUri URI of the directory to copy from. The routine will copy the directory's |
| 412 | * contents, not the directory itself. |
| 413 | * @param dstDirUri URI of the directory to copy to. Must be created beforehand. |
| 414 | * @throws RemoteException |
| 415 | */ |
Ben Kwa | cb4461f | 2015-05-05 11:50:11 -0700 | [diff] [blame^] | 416 | private void copyDirectoryHelper(Uri srcDirUri, Uri dstDirUri, int mode) |
| 417 | throws RemoteException { |
Ben Kwa | f527c63 | 2015-04-08 15:03:35 -0700 | [diff] [blame] | 418 | // Recurse into directories. Copy children into the new subdirectory. |
| 419 | final String queryColumns[] = new String[] { |
| 420 | Document.COLUMN_DISPLAY_NAME, |
| 421 | Document.COLUMN_DOCUMENT_ID, |
| 422 | Document.COLUMN_MIME_TYPE, |
| 423 | Document.COLUMN_SIZE |
| 424 | }; |
| 425 | final Uri queryUri = DocumentsContract.buildChildDocumentsUri(srcDirUri.getAuthority(), |
| 426 | DocumentsContract.getDocumentId(srcDirUri)); |
| 427 | Cursor cursor = null; |
| 428 | try { |
| 429 | // Iterate over srcs in the directory; copy to the destination directory. |
| 430 | cursor = mSrcClient.query(queryUri, queryColumns, null, null, null); |
| 431 | while (cursor.moveToNext()) { |
| 432 | final String childMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE); |
| 433 | final Uri dstUri = DocumentsContract.createDocument(mDstClient, dstDirUri, |
| 434 | childMimeType, getCursorString(cursor, Document.COLUMN_DISPLAY_NAME)); |
| 435 | final Uri childUri = DocumentsContract.buildDocumentUri(srcDirUri.getAuthority(), |
| 436 | getCursorString(cursor, Document.COLUMN_DOCUMENT_ID)); |
| 437 | if (Document.MIME_TYPE_DIR.equals(childMimeType)) { |
Ben Kwa | cb4461f | 2015-05-05 11:50:11 -0700 | [diff] [blame^] | 438 | copyDirectoryHelper(childUri, dstUri, mode); |
Ben Kwa | f527c63 | 2015-04-08 15:03:35 -0700 | [diff] [blame] | 439 | } else { |
Ben Kwa | cb4461f | 2015-05-05 11:50:11 -0700 | [diff] [blame^] | 440 | copyFileHelper(childUri, dstUri, mode); |
| 441 | } |
| 442 | } |
| 443 | if (mode == TRANSFER_MODE_MOVE) { |
| 444 | try { |
| 445 | DocumentsContract.deleteDocument(mSrcClient, srcDirUri); |
| 446 | } catch (RemoteException e) { |
| 447 | // RemoteExceptions usually signal that the connection is dead, so there's no |
| 448 | // point attempting to continue. Propagate the exception up so the copy job is |
| 449 | // cancelled. |
| 450 | Log.w(TAG, "Failed to clean up after move: " + srcDirUri, e); |
| 451 | throw e; |
Ben Kwa | f527c63 | 2015-04-08 15:03:35 -0700 | [diff] [blame] | 452 | } |
| 453 | } |
| 454 | } finally { |
| 455 | IoUtils.closeQuietly(cursor); |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | /** |
| 460 | * Handles copying a single file. |
| 461 | * |
| 462 | * @param srcUri URI of the file to copy from. |
| 463 | * @param dstUri URI of the *file* to copy to. Must be created beforehand. |
| 464 | * @throws RemoteException |
| 465 | */ |
Ben Kwa | cb4461f | 2015-05-05 11:50:11 -0700 | [diff] [blame^] | 466 | private void copyFileHelper(Uri srcUri, Uri dstUri, int mode) |
| 467 | throws RemoteException { |
Ben Kwa | f527c63 | 2015-04-08 15:03:35 -0700 | [diff] [blame] | 468 | // Copy an individual file. |
Ben Kwa | f585893 | 2015-04-07 15:43:39 -0700 | [diff] [blame] | 469 | CancellationSignal canceller = new CancellationSignal(); |
| 470 | ParcelFileDescriptor srcFile = null; |
| 471 | ParcelFileDescriptor dstFile = null; |
| 472 | InputStream src = null; |
| 473 | OutputStream dst = null; |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 474 | |
Ben Kwa | 37c7659 | 2015-04-24 15:35:25 -0700 | [diff] [blame] | 475 | IOException copyError = null; |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 476 | try { |
Ben Kwa | f527c63 | 2015-04-08 15:03:35 -0700 | [diff] [blame] | 477 | srcFile = mSrcClient.openFile(srcUri, "r", canceller); |
| 478 | dstFile = mDstClient.openFile(dstUri, "w", canceller); |
Ben Kwa | f585893 | 2015-04-07 15:43:39 -0700 | [diff] [blame] | 479 | src = new ParcelFileDescriptor.AutoCloseInputStream(srcFile); |
| 480 | dst = new ParcelFileDescriptor.AutoCloseOutputStream(dstFile); |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 481 | |
| 482 | byte[] buffer = new byte[8192]; |
| 483 | int len; |
Ben Kwa | f585893 | 2015-04-07 15:43:39 -0700 | [diff] [blame] | 484 | while (!mIsCancelled && ((len = src.read(buffer)) != -1)) { |
| 485 | dst.write(buffer, 0, len); |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 486 | makeProgress(len); |
| 487 | } |
Ben Kwa | 37c7659 | 2015-04-24 15:35:25 -0700 | [diff] [blame] | 488 | |
Ben Kwa | f585893 | 2015-04-07 15:43:39 -0700 | [diff] [blame] | 489 | srcFile.checkError(); |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 490 | } catch (IOException e) { |
Ben Kwa | 37c7659 | 2015-04-24 15:35:25 -0700 | [diff] [blame] | 491 | copyError = e; |
Ben Kwa | 2d88d49 | 2015-05-05 11:58:38 -0700 | [diff] [blame] | 492 | try { |
| 493 | dstFile.closeWithError(copyError.getMessage()); |
| 494 | } catch (IOException closeError) { |
| 495 | Log.e(TAG, "Error closing destination", closeError); |
Ben Kwa | 37c7659 | 2015-04-24 15:35:25 -0700 | [diff] [blame] | 496 | } |
Ben Kwa | 2d88d49 | 2015-05-05 11:58:38 -0700 | [diff] [blame] | 497 | } finally { |
Ben Kwa | f585893 | 2015-04-07 15:43:39 -0700 | [diff] [blame] | 498 | // This also ensures the file descriptors are closed. |
| 499 | IoUtils.closeQuietly(src); |
| 500 | IoUtils.closeQuietly(dst); |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 501 | } |
| 502 | |
Ben Kwa | 37c7659 | 2015-04-24 15:35:25 -0700 | [diff] [blame] | 503 | if (copyError != null) { |
| 504 | // Log errors. |
| 505 | Log.e(TAG, "Error while copying " + srcUri.toString(), copyError); |
| 506 | try { |
| 507 | mFailedFiles.add(DocumentInfo.fromUri(getContentResolver(), srcUri)); |
| 508 | } catch (FileNotFoundException ignore) { |
| 509 | Log.w(TAG, "Source file gone: " + srcUri, copyError); |
Ben Kwa | cb4461f | 2015-05-05 11:50:11 -0700 | [diff] [blame^] | 510 | // The source file is gone. |
Ben Kwa | 37c7659 | 2015-04-24 15:35:25 -0700 | [diff] [blame] | 511 | } |
| 512 | } |
| 513 | |
| 514 | if (copyError != null || mIsCancelled) { |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 515 | // Clean up half-copied files. |
Ben Kwa | f585893 | 2015-04-07 15:43:39 -0700 | [diff] [blame] | 516 | canceller.cancel(); |
Ben Kwa | f527c63 | 2015-04-08 15:03:35 -0700 | [diff] [blame] | 517 | try { |
| 518 | DocumentsContract.deleteDocument(mDstClient, dstUri); |
| 519 | } catch (RemoteException e) { |
Ben Kwa | cb4461f | 2015-05-05 11:50:11 -0700 | [diff] [blame^] | 520 | Log.w(TAG, "Failed to clean up after copy error: " + dstUri, e); |
Ben Kwa | f527c63 | 2015-04-08 15:03:35 -0700 | [diff] [blame] | 521 | // RemoteExceptions usually signal that the connection is dead, so there's no point |
| 522 | // attempting to continue. Propagate the exception up so the copy job is cancelled. |
| 523 | throw e; |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 524 | } |
Ben Kwa | cb4461f | 2015-05-05 11:50:11 -0700 | [diff] [blame^] | 525 | } else if (mode == TRANSFER_MODE_MOVE) { |
| 526 | // Clean up src files after a successful move. |
| 527 | try { |
| 528 | DocumentsContract.deleteDocument(mSrcClient, srcUri); |
| 529 | } catch (RemoteException e) { |
| 530 | Log.w(TAG, "Failed to clean up after move: " + srcUri, e); |
| 531 | throw e; |
| 532 | } |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 533 | } |
| 534 | } |
| 535 | } |