blob: 54ccc2a29f3401e94fd7c3a4f3e427d93977a365 [file] [log] [blame]
Steve McKay14e827a2016-01-06 18:32:13 -08001/*
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
17package com.android.documentsui.services;
18
19import static android.os.SystemClock.elapsedRealtime;
Steve McKayecbf3c52016-01-13 17:17:39 -080020import static android.provider.DocumentsContract.buildChildDocumentsUri;
21import static android.provider.DocumentsContract.buildDocumentUri;
22import static android.provider.DocumentsContract.getDocumentId;
23import static android.provider.DocumentsContract.isChildDocument;
Garfield, Tan4a7aba22016-06-09 12:04:22 -070024
Tomasz Mikolajewski748ea8c2016-01-22 16:22:51 +090025import static com.android.documentsui.OperationDialogFragment.DIALOG_TYPE_CONVERTED;
Steve McKay14e827a2016-01-06 18:32:13 -080026import static com.android.documentsui.Shared.DEBUG;
27import static com.android.documentsui.model.DocumentInfo.getCursorLong;
28import static com.android.documentsui.model.DocumentInfo.getCursorString;
Tomasz Mikolajewski748ea8c2016-01-22 16:22:51 +090029import static com.android.documentsui.services.FileOperationService.EXTRA_DIALOG_TYPE;
Garfield, Tan05df8ec2016-06-28 17:17:38 -070030import static com.android.documentsui.services.FileOperationService.EXTRA_OPERATION_TYPE;
Tomasz Mikolajewski748ea8c2016-01-22 16:22:51 +090031import static com.android.documentsui.services.FileOperationService.EXTRA_SRC_LIST;
Garfield, Tan05df8ec2016-06-28 17:17:38 -070032import static com.android.documentsui.services.FileOperationService.OPERATION_COPY;
Steve McKay14e827a2016-01-06 18:32:13 -080033
34import android.annotation.StringRes;
35import android.app.Notification;
36import android.app.Notification.Builder;
Tomasz Mikolajewski748ea8c2016-01-22 16:22:51 +090037import android.app.PendingIntent;
Steve McKay14e827a2016-01-06 18:32:13 -080038import android.content.ContentProviderClient;
Garfield, Tanf46958b2016-06-17 15:32:28 -070039import android.content.ContentResolver;
Steve McKay14e827a2016-01-06 18:32:13 -080040import android.content.Context;
Tomasz Mikolajewski748ea8c2016-01-22 16:22:51 +090041import android.content.Intent;
Steve McKay14e827a2016-01-06 18:32:13 -080042import android.content.res.AssetFileDescriptor;
43import android.database.Cursor;
44import android.net.Uri;
45import android.os.CancellationSignal;
46import android.os.ParcelFileDescriptor;
47import android.os.RemoteException;
48import android.provider.DocumentsContract;
49import android.provider.DocumentsContract.Document;
50import android.text.format.DateUtils;
51import android.util.Log;
52import android.webkit.MimeTypeMap;
53
Garfield, Tan71ac8a02016-07-06 17:24:20 -070054import com.android.documentsui.DocumentsApplication;
Ben Kwad5b2af12016-01-28 16:39:57 -080055import com.android.documentsui.Metrics;
Steve McKay14e827a2016-01-06 18:32:13 -080056import com.android.documentsui.R;
Garfield, Tan71ac8a02016-07-06 17:24:20 -070057import com.android.documentsui.RootsCache;
58import com.android.documentsui.UrisSupplier;
Steve McKay14e827a2016-01-06 18:32:13 -080059import com.android.documentsui.model.DocumentInfo;
60import com.android.documentsui.model.DocumentStack;
Garfield, Tanf46958b2016-06-17 15:32:28 -070061import com.android.documentsui.model.RootInfo;
Garfield, Tan05df8ec2016-06-28 17:17:38 -070062import com.android.documentsui.services.FileOperationService.OpType;
Steve McKay14e827a2016-01-06 18:32:13 -080063
64import libcore.io.IoUtils;
65
66import java.io.FileNotFoundException;
67import java.io.IOException;
68import java.io.InputStream;
Steve McKay14e827a2016-01-06 18:32:13 -080069import java.text.NumberFormat;
Tomasz Mikolajewski748ea8c2016-01-22 16:22:51 +090070import java.util.ArrayList;
Steve McKay14e827a2016-01-06 18:32:13 -080071import java.util.List;
72
73class CopyJob extends Job {
Steve McKay7a3b8112016-02-23 10:06:50 -080074
Steve McKay14e827a2016-01-06 18:32:13 -080075 private static final String TAG = "CopyJob";
Steve McKay7a3b8112016-02-23 10:06:50 -080076
Steve McKay35645432016-01-20 15:09:35 -080077 final List<DocumentInfo> mSrcs;
Tomasz Mikolajewski748ea8c2016-01-22 16:22:51 +090078 final ArrayList<DocumentInfo> convertedFiles = new ArrayList<>();
Steve McKay14e827a2016-01-06 18:32:13 -080079
80 private long mStartTime = -1;
Steve McKay7a3b8112016-02-23 10:06:50 -080081
Steve McKay14e827a2016-01-06 18:32:13 -080082 private long mBatchSize;
Garfield, Tan4a7aba22016-06-09 12:04:22 -070083 private volatile long mBytesCopied;
Steve McKay14e827a2016-01-06 18:32:13 -080084 // Speed estimation
85 private long mBytesCopiedSample;
86 private long mSampleTime;
87 private long mSpeed;
88 private long mRemainingTime;
89
90 /**
Steve McKay14e827a2016-01-06 18:32:13 -080091 * @see @link {@link Job} constructor for most param descriptions.
Steve McKay14e827a2016-01-06 18:32:13 -080092 */
Garfield, Tan05df8ec2016-06-28 17:17:38 -070093 CopyJob(Context service, Listener listener, String id, DocumentStack destination,
94 UrisSupplier srcs) {
95 this(service, listener, id, OPERATION_COPY, destination, srcs);
96 }
Steve McKayecbf3c52016-01-13 17:17:39 -080097
Garfield, Tan05df8ec2016-06-28 17:17:38 -070098 CopyJob(Context service, Listener listener, String id, @OpType int opType,
99 DocumentStack destination, UrisSupplier srcs) {
100 super(service, listener, id, opType, destination, srcs);
101
102 assert(srcs.getItemCount() > 0);
Steve McKayecbf3c52016-01-13 17:17:39 -0800103
Garfield, Tanf46958b2016-06-17 15:32:28 -0700104 // delay the initialization of it to setUp() because it may be IO extensive.
Garfield, Tan05df8ec2016-06-28 17:17:38 -0700105 mSrcs = new ArrayList<>(srcs.getItemCount());
Steve McKay14e827a2016-01-06 18:32:13 -0800106 }
107
108 @Override
109 Builder createProgressBuilder() {
110 return super.createProgressBuilder(
Steve McKayecbf3c52016-01-13 17:17:39 -0800111 service.getString(R.string.copy_notification_title),
Steve McKay14e827a2016-01-06 18:32:13 -0800112 R.drawable.ic_menu_copy,
Steve McKayecbf3c52016-01-13 17:17:39 -0800113 service.getString(android.R.string.cancel),
Steve McKay14e827a2016-01-06 18:32:13 -0800114 R.drawable.ic_cab_cancel);
115 }
116
117 @Override
118 public Notification getSetupNotification() {
Steve McKayecbf3c52016-01-13 17:17:39 -0800119 return getSetupNotification(service.getString(R.string.copy_preparing));
Steve McKay14e827a2016-01-06 18:32:13 -0800120 }
121
Steve McKay14e827a2016-01-06 18:32:13 -0800122 Notification getProgressNotification(@StringRes int msgId) {
Garfield, Tan4a7aba22016-06-09 12:04:22 -0700123 updateRemainingTimeEstimate();
124
Tomasz Mikolajewski0fa97e82016-02-18 16:45:44 +0900125 if (mBatchSize >= 0) {
126 double completed = (double) this.mBytesCopied / mBatchSize;
127 mProgressBuilder.setProgress(100, (int) (completed * 100), false);
Garfield, Tan4a7aba22016-06-09 12:04:22 -0700128 mProgressBuilder.setSubText(
Tomasz Mikolajewski0fa97e82016-02-18 16:45:44 +0900129 NumberFormat.getPercentInstance().format(completed));
130 } else {
131 // If the total file size failed to compute on some files, then show
132 // an indeterminate spinner. CopyJob would most likely fail on those
133 // files while copying, but would continue with another files.
134 // Also, if the total size is 0 bytes, show an indeterminate spinner.
135 mProgressBuilder.setProgress(0, 0, true);
136 }
137
Steve McKay14e827a2016-01-06 18:32:13 -0800138 if (mRemainingTime > 0) {
Steve McKayecbf3c52016-01-13 17:17:39 -0800139 mProgressBuilder.setContentText(service.getString(msgId,
Steve McKay14e827a2016-01-06 18:32:13 -0800140 DateUtils.formatDuration(mRemainingTime)));
141 } else {
142 mProgressBuilder.setContentText(null);
143 }
144
Steve McKay14e827a2016-01-06 18:32:13 -0800145 return mProgressBuilder.build();
146 }
147
Garfield, Tan4a7aba22016-06-09 12:04:22 -0700148 @Override
Steve McKay14e827a2016-01-06 18:32:13 -0800149 public Notification getProgressNotification() {
150 return getProgressNotification(R.string.copy_remaining);
151 }
152
153 void onBytesCopied(long numBytes) {
154 this.mBytesCopied += numBytes;
155 }
156
157 /**
158 * Generates an estimate of the remaining time in the copy.
159 */
Garfield, Tan4a7aba22016-06-09 12:04:22 -0700160 private void updateRemainingTimeEstimate() {
Steve McKay14e827a2016-01-06 18:32:13 -0800161 long elapsedTime = elapsedRealtime() - mStartTime;
162
Garfield, Tan4a7aba22016-06-09 12:04:22 -0700163 // mBytesCopied is modified in worker thread, but this method is called in monitor thread,
164 // so take a snapshot of mBytesCopied to make sure the updated estimate is consistent.
165 final long bytesCopied = mBytesCopied;
Garfield, Tanf46958b2016-06-17 15:32:28 -0700166 final long sampleDuration = Math.max(elapsedTime - mSampleTime, 1L); // avoid dividing 0
Garfield, Tan4a7aba22016-06-09 12:04:22 -0700167 final long sampleSpeed = ((bytesCopied - mBytesCopiedSample) * 1000) / sampleDuration;
Steve McKay14e827a2016-01-06 18:32:13 -0800168 if (mSpeed == 0) {
169 mSpeed = sampleSpeed;
170 } else {
171 mSpeed = ((3 * mSpeed) + sampleSpeed) / 4;
172 }
173
174 if (mSampleTime > 0 && mSpeed > 0) {
Garfield, Tan4a7aba22016-06-09 12:04:22 -0700175 mRemainingTime = ((mBatchSize - bytesCopied) * 1000) / mSpeed;
Steve McKay14e827a2016-01-06 18:32:13 -0800176 } else {
177 mRemainingTime = 0;
178 }
179
180 mSampleTime = elapsedTime;
Garfield, Tan4a7aba22016-06-09 12:04:22 -0700181 mBytesCopiedSample = bytesCopied;
Steve McKay14e827a2016-01-06 18:32:13 -0800182 }
183
184 @Override
185 Notification getFailureNotification() {
186 return getFailureNotification(
187 R.plurals.copy_error_notification_title, R.drawable.ic_menu_copy);
188 }
189
190 @Override
Tomasz Mikolajewski748ea8c2016-01-22 16:22:51 +0900191 Notification getWarningNotification() {
Tomasz Mikolajewskicd270152016-02-01 12:01:14 +0900192 final Intent navigateIntent = buildNavigateIntent(INTENT_TAG_WARNING);
Tomasz Mikolajewski748ea8c2016-01-22 16:22:51 +0900193 navigateIntent.putExtra(EXTRA_DIALOG_TYPE, DIALOG_TYPE_CONVERTED);
Garfield, Tan05df8ec2016-06-28 17:17:38 -0700194 navigateIntent.putExtra(EXTRA_OPERATION_TYPE, operationType);
Tomasz Mikolajewski748ea8c2016-01-22 16:22:51 +0900195
196 navigateIntent.putParcelableArrayListExtra(EXTRA_SRC_LIST, convertedFiles);
197
198 // TODO: Consider adding a dialog on tapping the notification with a list of
199 // converted files.
200 final Notification.Builder warningBuilder = new Notification.Builder(service)
201 .setContentTitle(service.getResources().getString(
202 R.string.notification_copy_files_converted_title))
203 .setContentText(service.getString(
204 R.string.notification_touch_for_details))
205 .setContentIntent(PendingIntent.getActivity(appContext, 0, navigateIntent,
206 PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT))
207 .setCategory(Notification.CATEGORY_ERROR)
208 .setSmallIcon(R.drawable.ic_menu_copy)
209 .setAutoCancel(true);
210 return warningBuilder.build();
211 }
212
213 @Override
Garfield, Tanf46958b2016-06-17 15:32:28 -0700214 boolean setUp() {
Garfield, Tanf46958b2016-06-17 15:32:28 -0700215 try {
216 buildDocumentList();
217 } catch (ResourceException e) {
218 Log.e(TAG, "Failed to get the list of docs.", e);
219 return false;
220 }
221
Garfield, Tan71ac8a02016-07-06 17:24:20 -0700222 // Check if user has canceled this task.
Garfield, Tanf46958b2016-06-17 15:32:28 -0700223 if (isCanceled()) {
224 return false;
225 }
Steve McKay14e827a2016-01-06 18:32:13 -0800226
Tomasz Mikolajewski0fa97e82016-02-18 16:45:44 +0900227 try {
228 mBatchSize = calculateSize(mSrcs);
229 } catch (ResourceException e) {
Steve McKay56d950c2016-06-14 15:56:50 -0700230 Log.w(TAG, "Failed to calculate total size. Copying without progress.", e);
Tomasz Mikolajewski0fa97e82016-02-18 16:45:44 +0900231 mBatchSize = -1;
232 }
Steve McKay14e827a2016-01-06 18:32:13 -0800233
Garfield, Tan71ac8a02016-07-06 17:24:20 -0700234 // Check if user has canceled this task. We should check it again here as user cancels
235 // tasks in main thread, but this is running in a worker thread. calculateSize() may
236 // take a long time during which user can cancel this task, and we don't want to waste
237 // resources doing useless large chunk of work.
238 if (isCanceled()) {
239 return false;
240 }
241
242 return checkSpace();
Garfield, Tanf46958b2016-06-17 15:32:28 -0700243 }
244
245 @Override
246 void start() {
247 mStartTime = elapsedRealtime();
Steve McKay14e827a2016-01-06 18:32:13 -0800248 DocumentInfo srcInfo;
Ben Kwad5b2af12016-01-28 16:39:57 -0800249 DocumentInfo dstInfo = stack.peek();
Steve McKay35645432016-01-20 15:09:35 -0800250 for (int i = 0; i < mSrcs.size() && !isCanceled(); ++i) {
251 srcInfo = mSrcs.get(i);
Steve McKay14e827a2016-01-06 18:32:13 -0800252
Steve McKay14e827a2016-01-06 18:32:13 -0800253 if (DEBUG) Log.d(TAG,
Steve McKayecbf3c52016-01-13 17:17:39 -0800254 "Copying " + srcInfo.displayName + " (" + srcInfo.derivedUri + ")"
255 + " to " + dstInfo.displayName + " (" + dstInfo.derivedUri + ")");
Steve McKay14e827a2016-01-06 18:32:13 -0800256
Tomasz Mikolajewski0fa97e82016-02-18 16:45:44 +0900257 try {
Steve McKay56d950c2016-06-14 15:56:50 -0700258 if (dstInfo.equals(srcInfo) || isDescendentOf(srcInfo, dstInfo)) {
259 Log.e(TAG, "Skipping recursive copy of " + srcInfo.derivedUri);
260 onFileFailed(srcInfo);
261 } else {
262 processDocument(srcInfo, null, dstInfo);
263 }
Tomasz Mikolajewski0fa97e82016-02-18 16:45:44 +0900264 } catch (ResourceException e) {
Steve McKay56d950c2016-06-14 15:56:50 -0700265 Log.e(TAG, "Failed to copy " + srcInfo.derivedUri, e);
Tomasz Mikolajewski0fa97e82016-02-18 16:45:44 +0900266 onFileFailed(srcInfo);
267 }
Steve McKay14e827a2016-01-06 18:32:13 -0800268 }
Ben Kwad5b2af12016-01-28 16:39:57 -0800269 Metrics.logFileOperation(service, operationType, mSrcs, dstInfo);
Steve McKay14e827a2016-01-06 18:32:13 -0800270 }
271
Garfield, Tanf46958b2016-06-17 15:32:28 -0700272 private void buildDocumentList() throws ResourceException {
273 try {
274 final ContentResolver resolver = appContext.getContentResolver();
Garfield, Tan05df8ec2016-06-28 17:17:38 -0700275 final Iterable<Uri> uris = srcs.getDocs(appContext);
Garfield, Tanf46958b2016-06-17 15:32:28 -0700276 for (Uri uri : uris) {
277 DocumentInfo doc = DocumentInfo.fromUri(resolver, uri);
278 if (canCopy(doc, stack.root)) {
279 mSrcs.add(doc);
280 } else {
281 onFileFailed(doc);
282 }
283
284 if (isCanceled()) {
285 return;
286 }
287 }
288 } catch(IOException e) {
Garfield, Tan05df8ec2016-06-28 17:17:38 -0700289 failedFileCount += srcs.getItemCount();
Garfield, Tanf46958b2016-06-17 15:32:28 -0700290 throw new ResourceException("Failed to open the list of docs to copy.", e);
291 }
292 }
293
294 private static boolean canCopy(DocumentInfo doc, RootInfo root) {
295 // Can't copy folders to downloads, because we don't show folders there.
296 return !root.isDownloads() || !doc.isDirectory();
297 }
298
Garfield, Tan71ac8a02016-07-06 17:24:20 -0700299 /**
300 * Checks whether the destination folder has enough space to take all source files.
301 * @return true if the root has enough space or doesn't provide free space info; otherwise false
302 */
303 boolean checkSpace() {
304 return checkSpace(mBatchSize);
305 }
306
307 /**
308 * Checks whether the destination folder has enough space to take files of batchSize
309 * @param batchSize the total size of files
310 * @return true if the root has enough space or doesn't provide free space info; otherwise false
311 */
312 final boolean checkSpace(long batchSize) {
313 // Default to be true because if batchSize or available space is invalid, we still let the
314 // copy start anyway.
315 boolean result = true;
316 if (batchSize >= 0) {
317 RootsCache cache = DocumentsApplication.getRootsCache(appContext);
318
319 // Query root info here instead of using stack.root because the number there may be
320 // stale.
321 RootInfo root = cache.getRootOneshot(stack.root.authority, stack.root.rootId, true);
322 if (root.availableBytes >= 0) {
323 result = (batchSize <= root.availableBytes);
324 } else {
325 Log.w(TAG, root.toString() + " doesn't provide available bytes.");
326 }
327 }
328
329 if (!result) {
330 failedFileCount += mSrcs.size();
331 failedFiles.addAll(mSrcs);
332 }
333
334 return result;
335 }
336
Tomasz Mikolajewski748ea8c2016-01-22 16:22:51 +0900337 @Override
338 boolean hasWarnings() {
339 return !convertedFiles.isEmpty();
340 }
341
Steve McKay14e827a2016-01-06 18:32:13 -0800342 /**
343 * Logs progress on the current copy operation. Displays/Updates the progress notification.
344 *
345 * @param bytesCopied
346 */
347 private void makeCopyProgress(long bytesCopied) {
348 onBytesCopied(bytesCopied);
Steve McKay14e827a2016-01-06 18:32:13 -0800349 }
350
351 /**
352 * Copies a the given document to the given location.
353 *
Steve McKay35645432016-01-20 15:09:35 -0800354 * @param src DocumentInfos for the documents to copy.
Tomasz Mikolajewskib8436af2016-01-25 16:20:15 +0900355 * @param srcParent DocumentInfo for the parent of the document to process.
Steve McKay14e827a2016-01-06 18:32:13 -0800356 * @param dstDirInfo The destination directory.
Tomasz Mikolajewski0fa97e82016-02-18 16:45:44 +0900357 * @throws ResourceException
Tomasz Mikolajewskib8436af2016-01-25 16:20:15 +0900358 *
359 * TODO: Stop passing srcParent, as it's not used for copy, but for move only.
Steve McKay14e827a2016-01-06 18:32:13 -0800360 */
Tomasz Mikolajewski0fa97e82016-02-18 16:45:44 +0900361 void processDocument(DocumentInfo src, DocumentInfo srcParent,
362 DocumentInfo dstDirInfo) throws ResourceException {
Steve McKay14e827a2016-01-06 18:32:13 -0800363
364 // TODO: When optimized copy kicks in, we'll not making any progress updates.
365 // For now. Local storage isn't using optimized copy.
366
Tomasz Mikolajewski67048082016-01-21 10:00:33 +0900367 // When copying within the same provider, try to use optimized copying.
Steve McKay14e827a2016-01-06 18:32:13 -0800368 // If not supported, then fallback to byte-by-byte copy/move.
Steve McKay35645432016-01-20 15:09:35 -0800369 if (src.authority.equals(dstDirInfo.authority)) {
370 if ((src.flags & Document.FLAG_SUPPORTS_COPY) != 0) {
Tomasz Mikolajewski0fa97e82016-02-18 16:45:44 +0900371 try {
372 if (DocumentsContract.copyDocument(getClient(src), src.derivedUri,
Tomasz Mikolajewski1008a112016-03-02 17:41:40 +0900373 dstDirInfo.derivedUri) != null) {
374 return;
Tomasz Mikolajewski0fa97e82016-02-18 16:45:44 +0900375 }
Tomasz Mikolajewski0fa97e82016-02-18 16:45:44 +0900376 } catch (RemoteException | RuntimeException e) {
Tomasz Mikolajewski1008a112016-03-02 17:41:40 +0900377 Log.e(TAG, "Provider side copy failed for: " + src.derivedUri
Steve McKay56d950c2016-06-14 15:56:50 -0700378 + " due to an exception.", e);
Steve McKay14e827a2016-01-06 18:32:13 -0800379 }
Garfield, Tan4a7aba22016-06-09 12:04:22 -0700380
Tomasz Mikolajewski1008a112016-03-02 17:41:40 +0900381 // If optimized copy fails, then fallback to byte-by-byte copy.
382 if (DEBUG) Log.d(TAG, "Fallback to byte-by-byte copy for: " + src.derivedUri);
Steve McKay14e827a2016-01-06 18:32:13 -0800383 }
384 }
385
386 // If we couldn't do an optimized copy...we fall back to vanilla byte copy.
Tomasz Mikolajewski0fa97e82016-02-18 16:45:44 +0900387 byteCopyDocument(src, dstDirInfo);
Steve McKay14e827a2016-01-06 18:32:13 -0800388 }
389
Tomasz Mikolajewski0fa97e82016-02-18 16:45:44 +0900390 void byteCopyDocument(DocumentInfo src, DocumentInfo dest) throws ResourceException {
Steve McKay14e827a2016-01-06 18:32:13 -0800391 final String dstMimeType;
392 final String dstDisplayName;
393
Steve McKay35645432016-01-20 15:09:35 -0800394 if (DEBUG) Log.d(TAG, "Doing byte copy of document: " + src);
Steve McKay14e827a2016-01-06 18:32:13 -0800395 // If the file is virtual, but can be converted to another format, then try to copy it
396 // as such format. Also, append an extension for the target mime type (if known).
Steve McKay35645432016-01-20 15:09:35 -0800397 if (src.isVirtualDocument()) {
Tomasz Mikolajewski0fa97e82016-02-18 16:45:44 +0900398 String[] streamTypes = null;
399 try {
400 streamTypes = getContentResolver().getStreamTypes(src.derivedUri, "*/*");
401 } catch (RuntimeException e) {
402 throw new ResourceException(
403 "Failed to obtain streamable types for %s due to an exception.",
404 src.derivedUri, e);
405 }
Steve McKay14e827a2016-01-06 18:32:13 -0800406 if (streamTypes != null && streamTypes.length > 0) {
407 dstMimeType = streamTypes[0];
408 final String extension = MimeTypeMap.getSingleton().
409 getExtensionFromMimeType(dstMimeType);
Steve McKay35645432016-01-20 15:09:35 -0800410 dstDisplayName = src.displayName +
411 (extension != null ? "." + extension : src.displayName);
Steve McKay14e827a2016-01-06 18:32:13 -0800412 } else {
Tomasz Mikolajewski0fa97e82016-02-18 16:45:44 +0900413 throw new ResourceException("Cannot copy virtual file %s. No streamable formats "
414 + "available.", src.derivedUri);
Steve McKay14e827a2016-01-06 18:32:13 -0800415 }
416 } else {
Steve McKay35645432016-01-20 15:09:35 -0800417 dstMimeType = src.mimeType;
418 dstDisplayName = src.displayName;
Steve McKay14e827a2016-01-06 18:32:13 -0800419 }
420
421 // Create the target document (either a file or a directory), then copy recursively the
422 // contents (bytes or children).
Tomasz Mikolajewski0fa97e82016-02-18 16:45:44 +0900423 Uri dstUri = null;
424 try {
425 dstUri = DocumentsContract.createDocument(
426 getClient(dest), dest.derivedUri, dstMimeType, dstDisplayName);
427 } catch (RemoteException | RuntimeException e) {
428 throw new ResourceException(
429 "Couldn't create destination document " + dstDisplayName + " in directory %s "
430 + "due to an exception.", dest.derivedUri, e);
431 }
Steve McKay14e827a2016-01-06 18:32:13 -0800432 if (dstUri == null) {
433 // If this is a directory, the entire subdir will not be copied over.
Tomasz Mikolajewski0fa97e82016-02-18 16:45:44 +0900434 throw new ResourceException(
435 "Couldn't create destination document " + dstDisplayName + " in directory %s.",
436 dest.derivedUri);
Steve McKay14e827a2016-01-06 18:32:13 -0800437 }
438
439 DocumentInfo dstInfo = null;
440 try {
441 dstInfo = DocumentInfo.fromUri(getContentResolver(), dstUri);
Tomasz Mikolajewski0fa97e82016-02-18 16:45:44 +0900442 } catch (FileNotFoundException | RuntimeException e) {
443 throw new ResourceException("Could not load DocumentInfo for newly created file %s.",
444 dstUri);
Steve McKay14e827a2016-01-06 18:32:13 -0800445 }
446
Steve McKay35645432016-01-20 15:09:35 -0800447 if (Document.MIME_TYPE_DIR.equals(src.mimeType)) {
Tomasz Mikolajewski0fa97e82016-02-18 16:45:44 +0900448 copyDirectoryHelper(src, dstInfo);
Steve McKay14e827a2016-01-06 18:32:13 -0800449 } else {
Tomasz Mikolajewskidb875432016-02-23 15:12:54 +0900450 copyFileHelper(src, dstInfo, dest, dstMimeType);
Steve McKay14e827a2016-01-06 18:32:13 -0800451 }
Steve McKay14e827a2016-01-06 18:32:13 -0800452 }
453
454 /**
455 * Handles recursion into a directory and copying its contents. Note that in linux terms, this
456 * does the equivalent of "cp src/* dst", not "cp -r src dst".
457 *
Steve McKay35645432016-01-20 15:09:35 -0800458 * @param srcDir Info of the directory to copy from. The routine will copy the directory's
Steve McKay14e827a2016-01-06 18:32:13 -0800459 * contents, not the directory itself.
Steve McKay35645432016-01-20 15:09:35 -0800460 * @param destDir Info of the directory to copy to. Must be created beforehand.
Tomasz Mikolajewski0fa97e82016-02-18 16:45:44 +0900461 * @throws ResourceException
Steve McKay14e827a2016-01-06 18:32:13 -0800462 */
Tomasz Mikolajewski0fa97e82016-02-18 16:45:44 +0900463 private void copyDirectoryHelper(DocumentInfo srcDir, DocumentInfo destDir)
464 throws ResourceException {
Steve McKay14e827a2016-01-06 18:32:13 -0800465 // Recurse into directories. Copy children into the new subdirectory.
466 final String queryColumns[] = new String[] {
467 Document.COLUMN_DISPLAY_NAME,
468 Document.COLUMN_DOCUMENT_ID,
469 Document.COLUMN_MIME_TYPE,
470 Document.COLUMN_SIZE,
471 Document.COLUMN_FLAGS
472 };
473 Cursor cursor = null;
474 boolean success = true;
Tomasz Mikolajewski0fa97e82016-02-18 16:45:44 +0900475 // Iterate over srcs in the directory; copy to the destination directory.
476 final Uri queryUri = buildChildDocumentsUri(srcDir.authority, srcDir.documentId);
Steve McKay14e827a2016-01-06 18:32:13 -0800477 try {
Tomasz Mikolajewski0fa97e82016-02-18 16:45:44 +0900478 try {
479 cursor = getClient(srcDir).query(queryUri, queryColumns, null, null, null);
480 } catch (RemoteException | RuntimeException e) {
481 throw new ResourceException("Failed to query children of %s due to an exception.",
482 srcDir.derivedUri, e);
Steve McKay14e827a2016-01-06 18:32:13 -0800483 }
Tomasz Mikolajewski0fa97e82016-02-18 16:45:44 +0900484
485 DocumentInfo src;
486 while (cursor.moveToNext() && !isCanceled()) {
487 try {
488 src = DocumentInfo.fromCursor(cursor, srcDir.authority);
489 processDocument(src, srcDir, destDir);
490 } catch (RuntimeException e) {
Garfield, Tan4a7aba22016-06-09 12:04:22 -0700491 Log.e(TAG, String.format(
492 "Failed to recursively process a file %s due to an exception.",
493 srcDir.derivedUri.toString()), e);
Tomasz Mikolajewski0fa97e82016-02-18 16:45:44 +0900494 success = false;
495 }
496 }
497 } catch (RuntimeException e) {
Garfield, Tan4a7aba22016-06-09 12:04:22 -0700498 Log.e(TAG, String.format(
499 "Failed to copy a file %s to %s. ",
500 srcDir.derivedUri.toString(), destDir.derivedUri.toString()), e);
Tomasz Mikolajewski0fa97e82016-02-18 16:45:44 +0900501 success = false;
Steve McKay14e827a2016-01-06 18:32:13 -0800502 } finally {
503 IoUtils.closeQuietly(cursor);
504 }
505
Tomasz Mikolajewski0fa97e82016-02-18 16:45:44 +0900506 if (!success) {
507 throw new RuntimeException("Some files failed to copy during a recursive "
508 + "directory copy.");
509 }
Steve McKay14e827a2016-01-06 18:32:13 -0800510 }
511
512 /**
513 * Handles copying a single file.
514 *
Tomasz Mikolajewskidb875432016-02-23 15:12:54 +0900515 * @param src Info of the file to copy from.
516 * @param dest Info of the *file* to copy to. Must be created beforehand.
517 * @param destParent Info of the parent of the destination.
Steve McKay14e827a2016-01-06 18:32:13 -0800518 * @param mimeType Mime type for the target. Can be different than source for virtual files.
Tomasz Mikolajewski0fa97e82016-02-18 16:45:44 +0900519 * @throws ResourceException
Steve McKay14e827a2016-01-06 18:32:13 -0800520 */
Tomasz Mikolajewskidb875432016-02-23 15:12:54 +0900521 private void copyFileHelper(DocumentInfo src, DocumentInfo dest, DocumentInfo destParent,
522 String mimeType) throws ResourceException {
Steve McKay14e827a2016-01-06 18:32:13 -0800523 CancellationSignal canceller = new CancellationSignal();
Tomasz Mikolajewski0fa97e82016-02-18 16:45:44 +0900524 AssetFileDescriptor srcFileAsAsset = null;
Steve McKay14e827a2016-01-06 18:32:13 -0800525 ParcelFileDescriptor srcFile = null;
526 ParcelFileDescriptor dstFile = null;
Steve McKay35645432016-01-20 15:09:35 -0800527 InputStream in = null;
Daichi Hironof4e7fa82016-03-28 16:07:45 +0900528 ParcelFileDescriptor.AutoCloseOutputStream out = null;
Tomasz Mikolajewski0fa97e82016-02-18 16:45:44 +0900529 boolean success = false;
Steve McKay14e827a2016-01-06 18:32:13 -0800530
Steve McKay14e827a2016-01-06 18:32:13 -0800531 try {
532 // If the file is virtual, but can be converted to another format, then try to copy it
533 // as such format.
Steve McKay35645432016-01-20 15:09:35 -0800534 if (src.isVirtualDocument()) {
Tomasz Mikolajewski0fa97e82016-02-18 16:45:44 +0900535 try {
536 srcFileAsAsset = getClient(src).openTypedAssetFileDescriptor(
Steve McKay35645432016-01-20 15:09:35 -0800537 src.derivedUri, mimeType, null, canceller);
Tomasz Mikolajewski0fa97e82016-02-18 16:45:44 +0900538 } catch (FileNotFoundException | RemoteException | RuntimeException e) {
539 throw new ResourceException("Failed to open a file as asset for %s due to an "
540 + "exception.", src.derivedUri, e);
541 }
Steve McKay14e827a2016-01-06 18:32:13 -0800542 srcFile = srcFileAsAsset.getParcelFileDescriptor();
Tomasz Mikolajewski0fa97e82016-02-18 16:45:44 +0900543 try {
544 in = new AssetFileDescriptor.AutoCloseInputStream(srcFileAsAsset);
545 } catch (IOException e) {
546 throw new ResourceException("Failed to open a file input stream for %s due "
547 + "an exception.", src.derivedUri, e);
548 }
Steve McKay14e827a2016-01-06 18:32:13 -0800549 } else {
Tomasz Mikolajewski0fa97e82016-02-18 16:45:44 +0900550 try {
551 srcFile = getClient(src).openFile(src.derivedUri, "r", canceller);
552 } catch (FileNotFoundException | RemoteException | RuntimeException e) {
553 throw new ResourceException(
554 "Failed to open a file for %s due to an exception.", src.derivedUri, e);
555 }
Steve McKay35645432016-01-20 15:09:35 -0800556 in = new ParcelFileDescriptor.AutoCloseInputStream(srcFile);
Steve McKay14e827a2016-01-06 18:32:13 -0800557 }
558
Tomasz Mikolajewski0fa97e82016-02-18 16:45:44 +0900559 try {
560 dstFile = getClient(dest).openFile(dest.derivedUri, "w", canceller);
561 } catch (FileNotFoundException | RemoteException | RuntimeException e) {
562 throw new ResourceException("Failed to open the destination file %s for writing "
563 + "due to an exception.", dest.derivedUri, e);
564 }
Steve McKay35645432016-01-20 15:09:35 -0800565 out = new ParcelFileDescriptor.AutoCloseOutputStream(dstFile);
Steve McKay14e827a2016-01-06 18:32:13 -0800566
Steve McKayecbf3c52016-01-13 17:17:39 -0800567 byte[] buffer = new byte[32 * 1024];
Steve McKay14e827a2016-01-06 18:32:13 -0800568 int len;
Tomasz Mikolajewski0fa97e82016-02-18 16:45:44 +0900569 try {
570 while ((len = in.read(buffer)) != -1) {
571 if (isCanceled()) {
Steve McKay7a3b8112016-02-23 10:06:50 -0800572 if (DEBUG) Log.d(TAG, "Canceled copy mid-copy of: " + src.derivedUri);
573 return;
Tomasz Mikolajewski0fa97e82016-02-18 16:45:44 +0900574 }
575 out.write(buffer, 0, len);
576 makeCopyProgress(len);
Steve McKay14e827a2016-01-06 18:32:13 -0800577 }
Tomasz Mikolajewski0fa97e82016-02-18 16:45:44 +0900578
Daichi Hironof4e7fa82016-03-28 16:07:45 +0900579 // Need to invoke IoUtils.close explicitly to avoid from ignoring errors at flush.
580 IoUtils.close(dstFile.getFileDescriptor());
Tomasz Mikolajewski0fa97e82016-02-18 16:45:44 +0900581 srcFile.checkError();
582 } catch (IOException e) {
583 throw new ResourceException(
584 "Failed to copy bytes from %s to %s due to an IO exception.",
585 src.derivedUri, dest.derivedUri, e);
Steve McKay14e827a2016-01-06 18:32:13 -0800586 }
587
Tomasz Mikolajewski0fa97e82016-02-18 16:45:44 +0900588 if (src.isVirtualDocument()) {
589 convertedFiles.add(src);
Steve McKay14e827a2016-01-06 18:32:13 -0800590 }
Tomasz Mikolajewski0fa97e82016-02-18 16:45:44 +0900591
592 success = true;
Steve McKay14e827a2016-01-06 18:32:13 -0800593 } finally {
Tomasz Mikolajewski0fa97e82016-02-18 16:45:44 +0900594 if (!success) {
595 if (dstFile != null) {
596 try {
597 dstFile.closeWithError("Error copying bytes.");
598 } catch (IOException closeError) {
599 Log.w(TAG, "Error closing destination.", closeError);
600 }
601 }
602
603 if (DEBUG) Log.d(TAG, "Cleaning up failed operation leftovers.");
604 canceller.cancel();
605 try {
Tomasz Mikolajewskidb875432016-02-23 15:12:54 +0900606 deleteDocument(dest, destParent);
607 } catch (ResourceException e) {
Tomasz Mikolajewski0fa97e82016-02-18 16:45:44 +0900608 Log.w(TAG, "Failed to cleanup after copy error: " + src.derivedUri, e);
609 }
610 }
611
Steve McKay14e827a2016-01-06 18:32:13 -0800612 // This also ensures the file descriptors are closed.
Steve McKay35645432016-01-20 15:09:35 -0800613 IoUtils.closeQuietly(in);
614 IoUtils.closeQuietly(out);
Steve McKay14e827a2016-01-06 18:32:13 -0800615 }
Steve McKay14e827a2016-01-06 18:32:13 -0800616 }
617
618 /**
619 * Calculates the cumulative size of all the documents in the list. Directories are recursed
620 * into and totaled up.
621 *
622 * @param srcs
623 * @return Size in bytes.
Tomasz Mikolajewski0fa97e82016-02-18 16:45:44 +0900624 * @throws ResourceException
Steve McKay14e827a2016-01-06 18:32:13 -0800625 */
Tomasz Mikolajewski0fa97e82016-02-18 16:45:44 +0900626 private long calculateSize(List<DocumentInfo> srcs) throws ResourceException {
Steve McKay14e827a2016-01-06 18:32:13 -0800627 long result = 0;
628
629 for (DocumentInfo src : srcs) {
630 if (src.isDirectory()) {
631 // Directories need to be recursed into.
Tomasz Mikolajewski0fa97e82016-02-18 16:45:44 +0900632 try {
633 result += calculateFileSizesRecursively(getClient(src), src.derivedUri);
634 } catch (RemoteException e) {
635 throw new ResourceException("Failed to obtain the client for %s.",
Garfield, Tan71ac8a02016-07-06 17:24:20 -0700636 src.derivedUri, e);
Tomasz Mikolajewski0fa97e82016-02-18 16:45:44 +0900637 }
Steve McKay14e827a2016-01-06 18:32:13 -0800638 } else {
639 result += src.size;
640 }
Garfield, Tanf46958b2016-06-17 15:32:28 -0700641
642 if (isCanceled()) {
643 return result;
644 }
Steve McKay14e827a2016-01-06 18:32:13 -0800645 }
646 return result;
647 }
648
649 /**
650 * Calculates (recursively) the cumulative size of all the files under the given directory.
651 *
Tomasz Mikolajewski0fa97e82016-02-18 16:45:44 +0900652 * @throws ResourceException
Steve McKay14e827a2016-01-06 18:32:13 -0800653 */
Garfield, Tan71ac8a02016-07-06 17:24:20 -0700654 long calculateFileSizesRecursively(
Tomasz Mikolajewski0fa97e82016-02-18 16:45:44 +0900655 ContentProviderClient client, Uri uri) throws ResourceException {
Steve McKay14e827a2016-01-06 18:32:13 -0800656 final String authority = uri.getAuthority();
Steve McKayecbf3c52016-01-13 17:17:39 -0800657 final Uri queryUri = buildChildDocumentsUri(authority, getDocumentId(uri));
Steve McKay14e827a2016-01-06 18:32:13 -0800658 final String queryColumns[] = new String[] {
659 Document.COLUMN_DOCUMENT_ID,
660 Document.COLUMN_MIME_TYPE,
661 Document.COLUMN_SIZE
662 };
663
664 long result = 0;
665 Cursor cursor = null;
666 try {
667 cursor = client.query(queryUri, queryColumns, null, null, null);
Garfield, Tanf46958b2016-06-17 15:32:28 -0700668 while (cursor.moveToNext() && !isCanceled()) {
Steve McKay14e827a2016-01-06 18:32:13 -0800669 if (Document.MIME_TYPE_DIR.equals(
670 getCursorString(cursor, Document.COLUMN_MIME_TYPE))) {
671 // Recurse into directories.
Steve McKayecbf3c52016-01-13 17:17:39 -0800672 final Uri dirUri = buildDocumentUri(authority,
Steve McKay14e827a2016-01-06 18:32:13 -0800673 getCursorString(cursor, Document.COLUMN_DOCUMENT_ID));
674 result += calculateFileSizesRecursively(client, dirUri);
675 } else {
676 // This may return -1 if the size isn't defined. Ignore those cases.
677 long size = getCursorLong(cursor, Document.COLUMN_SIZE);
678 result += size > 0 ? size : 0;
679 }
680 }
Tomasz Mikolajewski0fa97e82016-02-18 16:45:44 +0900681 } catch (RemoteException | RuntimeException e) {
682 throw new ResourceException(
683 "Failed to calculate size for %s due to an exception.", uri, e);
Steve McKay14e827a2016-01-06 18:32:13 -0800684 } finally {
685 IoUtils.closeQuietly(cursor);
686 }
687
688 return result;
689 }
690
Steve McKay14e827a2016-01-06 18:32:13 -0800691 /**
692 * Returns true if {@code doc} is a descendant of {@code parentDoc}.
Tomasz Mikolajewski0fa97e82016-02-18 16:45:44 +0900693 * @throws ResourceException
Steve McKay14e827a2016-01-06 18:32:13 -0800694 */
Steve McKay35645432016-01-20 15:09:35 -0800695 boolean isDescendentOf(DocumentInfo doc, DocumentInfo parent)
Tomasz Mikolajewski0fa97e82016-02-18 16:45:44 +0900696 throws ResourceException {
Steve McKay35645432016-01-20 15:09:35 -0800697 if (parent.isDirectory() && doc.authority.equals(parent.authority)) {
Tomasz Mikolajewski0fa97e82016-02-18 16:45:44 +0900698 try {
699 return isChildDocument(getClient(doc), doc.derivedUri, parent.derivedUri);
700 } catch (RemoteException | RuntimeException e) {
701 throw new ResourceException(
702 "Failed to check if %s is a child of %s due to an exception.",
703 doc.derivedUri, parent.derivedUri, e);
704 }
Steve McKay14e827a2016-01-06 18:32:13 -0800705 }
706 return false;
707 }
Steve McKayecbf3c52016-01-13 17:17:39 -0800708
Steve McKay35645432016-01-20 15:09:35 -0800709 @Override
710 public String toString() {
711 return new StringBuilder()
712 .append("CopyJob")
713 .append("{")
714 .append("id=" + id)
Garfield, Tan05df8ec2016-06-28 17:17:38 -0700715 .append(", docs=" + srcs)
Steve McKay35645432016-01-20 15:09:35 -0800716 .append(", destination=" + stack)
717 .append("}")
718 .toString();
719 }
Steve McKayecbf3c52016-01-13 17:17:39 -0800720}