Steve McKay | 14e827a | 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 | |
| 19 | import static android.os.SystemClock.elapsedRealtime; |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 20 | import static android.provider.DocumentsContract.buildChildDocumentsUri; |
| 21 | import static android.provider.DocumentsContract.buildDocumentUri; |
| 22 | import static android.provider.DocumentsContract.getDocumentId; |
| 23 | import static android.provider.DocumentsContract.isChildDocument; |
Tomasz Mikolajewski | 748ea8c | 2016-01-22 16:22:51 +0900 | [diff] [blame] | 24 | import static com.android.documentsui.OperationDialogFragment.DIALOG_TYPE_CONVERTED; |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 25 | import static com.android.documentsui.Shared.DEBUG; |
| 26 | import static com.android.documentsui.model.DocumentInfo.getCursorLong; |
| 27 | import static com.android.documentsui.model.DocumentInfo.getCursorString; |
Tomasz Mikolajewski | 748ea8c | 2016-01-22 16:22:51 +0900 | [diff] [blame] | 28 | import static com.android.documentsui.services.FileOperationService.EXTRA_DIALOG_TYPE; |
| 29 | import static com.android.documentsui.services.FileOperationService.EXTRA_OPERATION; |
| 30 | import static com.android.documentsui.services.FileOperationService.EXTRA_SRC_LIST; |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 31 | import static com.android.documentsui.services.FileOperationService.OPERATION_COPY; |
| 32 | import static com.google.common.base.Preconditions.checkArgument; |
| 33 | |
| 34 | import android.annotation.StringRes; |
| 35 | import android.app.Notification; |
| 36 | import android.app.Notification.Builder; |
Tomasz Mikolajewski | 748ea8c | 2016-01-22 16:22:51 +0900 | [diff] [blame] | 37 | import android.app.PendingIntent; |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 38 | import android.content.ContentProviderClient; |
| 39 | import android.content.Context; |
Tomasz Mikolajewski | 748ea8c | 2016-01-22 16:22:51 +0900 | [diff] [blame] | 40 | import android.content.Intent; |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 41 | import android.content.res.AssetFileDescriptor; |
| 42 | import android.database.Cursor; |
| 43 | import android.net.Uri; |
| 44 | import android.os.CancellationSignal; |
| 45 | import android.os.ParcelFileDescriptor; |
| 46 | import android.os.RemoteException; |
| 47 | import android.provider.DocumentsContract; |
| 48 | import android.provider.DocumentsContract.Document; |
| 49 | import android.text.format.DateUtils; |
| 50 | import android.util.Log; |
| 51 | import android.webkit.MimeTypeMap; |
| 52 | |
Ben Kwa | d5b2af1 | 2016-01-28 16:39:57 -0800 | [diff] [blame] | 53 | import com.android.documentsui.Metrics; |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 54 | import com.android.documentsui.R; |
| 55 | import com.android.documentsui.model.DocumentInfo; |
| 56 | import com.android.documentsui.model.DocumentStack; |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 57 | import com.android.documentsui.services.FileOperationService.OpType; |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 58 | |
| 59 | import libcore.io.IoUtils; |
| 60 | |
| 61 | import java.io.FileNotFoundException; |
| 62 | import java.io.IOException; |
| 63 | import java.io.InputStream; |
| 64 | import java.io.OutputStream; |
| 65 | import java.text.NumberFormat; |
Tomasz Mikolajewski | 748ea8c | 2016-01-22 16:22:51 +0900 | [diff] [blame] | 66 | import java.util.ArrayList; |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 67 | import java.util.List; |
| 68 | |
| 69 | class CopyJob extends Job { |
| 70 | private static final String TAG = "CopyJob"; |
| 71 | private static final int PROGRESS_INTERVAL_MILLIS = 1000; |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 72 | final List<DocumentInfo> mSrcs; |
Tomasz Mikolajewski | 748ea8c | 2016-01-22 16:22:51 +0900 | [diff] [blame] | 73 | final ArrayList<DocumentInfo> convertedFiles = new ArrayList<>(); |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 74 | |
| 75 | private long mStartTime = -1; |
| 76 | private long mBatchSize; |
| 77 | private long mBytesCopied; |
| 78 | private long mLastNotificationTime; |
| 79 | // Speed estimation |
| 80 | private long mBytesCopiedSample; |
| 81 | private long mSampleTime; |
| 82 | private long mSpeed; |
| 83 | private long mRemainingTime; |
| 84 | |
| 85 | /** |
| 86 | * Copies files to a destination identified by {@code destination}. |
| 87 | * @see @link {@link Job} constructor for most param descriptions. |
| 88 | * |
| 89 | * @param srcs List of files to be copied. |
| 90 | */ |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 91 | CopyJob(Context service, Context appContext, Listener listener, |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 92 | String id, DocumentStack stack, List<DocumentInfo> srcs) { |
| 93 | super(service, appContext, listener, OPERATION_COPY, id, stack); |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 94 | |
| 95 | checkArgument(!srcs.isEmpty()); |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 96 | this.mSrcs = srcs; |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | /** |
| 100 | * @see @link {@link Job} constructor for most param descriptions. |
| 101 | * |
| 102 | * @param srcs List of files to be copied. |
| 103 | */ |
| 104 | CopyJob(Context service, Context appContext, Listener listener, |
| 105 | @OpType int opType, String id, DocumentStack destination, List<DocumentInfo> srcs) { |
| 106 | super(service, appContext, listener, opType, id, destination); |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 107 | |
| 108 | checkArgument(!srcs.isEmpty()); |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 109 | this.mSrcs = srcs; |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | @Override |
| 113 | Builder createProgressBuilder() { |
| 114 | return super.createProgressBuilder( |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 115 | service.getString(R.string.copy_notification_title), |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 116 | R.drawable.ic_menu_copy, |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 117 | service.getString(android.R.string.cancel), |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 118 | R.drawable.ic_cab_cancel); |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public Notification getSetupNotification() { |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 123 | return getSetupNotification(service.getString(R.string.copy_preparing)); |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | public boolean shouldUpdateProgress() { |
| 127 | // Wait a while between updates :) |
| 128 | return elapsedRealtime() - mLastNotificationTime > PROGRESS_INTERVAL_MILLIS; |
| 129 | } |
| 130 | |
| 131 | Notification getProgressNotification(@StringRes int msgId) { |
| 132 | double completed = (double) this.mBytesCopied / mBatchSize; |
| 133 | mProgressBuilder.setProgress(100, (int) (completed * 100), false); |
| 134 | mProgressBuilder.setContentInfo( |
| 135 | NumberFormat.getPercentInstance().format(completed)); |
| 136 | if (mRemainingTime > 0) { |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 137 | mProgressBuilder.setContentText(service.getString(msgId, |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 138 | DateUtils.formatDuration(mRemainingTime))); |
| 139 | } else { |
| 140 | mProgressBuilder.setContentText(null); |
| 141 | } |
| 142 | |
| 143 | // Remember when we last returned progress so we can provide an answer |
| 144 | // in shouldUpdateProgress. |
| 145 | mLastNotificationTime = elapsedRealtime(); |
| 146 | return mProgressBuilder.build(); |
| 147 | } |
| 148 | |
| 149 | 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 | */ |
| 160 | void updateRemainingTimeEstimate() { |
| 161 | long elapsedTime = elapsedRealtime() - mStartTime; |
| 162 | |
| 163 | final long sampleDuration = elapsedTime - mSampleTime; |
| 164 | final long sampleSpeed = ((mBytesCopied - mBytesCopiedSample) * 1000) / sampleDuration; |
| 165 | if (mSpeed == 0) { |
| 166 | mSpeed = sampleSpeed; |
| 167 | } else { |
| 168 | mSpeed = ((3 * mSpeed) + sampleSpeed) / 4; |
| 169 | } |
| 170 | |
| 171 | if (mSampleTime > 0 && mSpeed > 0) { |
| 172 | mRemainingTime = ((mBatchSize - mBytesCopied) * 1000) / mSpeed; |
| 173 | } else { |
| 174 | mRemainingTime = 0; |
| 175 | } |
| 176 | |
| 177 | mSampleTime = elapsedTime; |
| 178 | mBytesCopiedSample = mBytesCopied; |
| 179 | } |
| 180 | |
| 181 | @Override |
| 182 | Notification getFailureNotification() { |
| 183 | return getFailureNotification( |
| 184 | R.plurals.copy_error_notification_title, R.drawable.ic_menu_copy); |
| 185 | } |
| 186 | |
| 187 | @Override |
Tomasz Mikolajewski | 748ea8c | 2016-01-22 16:22:51 +0900 | [diff] [blame] | 188 | Notification getWarningNotification() { |
Tomasz Mikolajewski | cd27015 | 2016-02-01 12:01:14 +0900 | [diff] [blame] | 189 | final Intent navigateIntent = buildNavigateIntent(INTENT_TAG_WARNING); |
Tomasz Mikolajewski | 748ea8c | 2016-01-22 16:22:51 +0900 | [diff] [blame] | 190 | navigateIntent.putExtra(EXTRA_DIALOG_TYPE, DIALOG_TYPE_CONVERTED); |
| 191 | navigateIntent.putExtra(EXTRA_OPERATION, operationType); |
| 192 | |
| 193 | navigateIntent.putParcelableArrayListExtra(EXTRA_SRC_LIST, convertedFiles); |
| 194 | |
| 195 | // TODO: Consider adding a dialog on tapping the notification with a list of |
| 196 | // converted files. |
| 197 | final Notification.Builder warningBuilder = new Notification.Builder(service) |
| 198 | .setContentTitle(service.getResources().getString( |
| 199 | R.string.notification_copy_files_converted_title)) |
| 200 | .setContentText(service.getString( |
| 201 | R.string.notification_touch_for_details)) |
| 202 | .setContentIntent(PendingIntent.getActivity(appContext, 0, navigateIntent, |
| 203 | PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT)) |
| 204 | .setCategory(Notification.CATEGORY_ERROR) |
| 205 | .setSmallIcon(R.drawable.ic_menu_copy) |
| 206 | .setAutoCancel(true); |
| 207 | return warningBuilder.build(); |
| 208 | } |
| 209 | |
| 210 | @Override |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 211 | void start() throws RemoteException { |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 212 | mStartTime = elapsedRealtime(); |
| 213 | |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 214 | // client |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 215 | mBatchSize = calculateSize(mSrcs); |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 216 | |
| 217 | DocumentInfo srcInfo; |
Ben Kwa | d5b2af1 | 2016-01-28 16:39:57 -0800 | [diff] [blame] | 218 | DocumentInfo dstInfo = stack.peek(); |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 219 | for (int i = 0; i < mSrcs.size() && !isCanceled(); ++i) { |
| 220 | srcInfo = mSrcs.get(i); |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 221 | |
| 222 | // Guard unsupported recursive operation. |
| 223 | if (dstInfo.equals(srcInfo) || isDescendentOf(srcInfo, dstInfo)) { |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 224 | onFileFailed(srcInfo, |
| 225 | "Skipping recursive operation on directory " + dstInfo.derivedUri + "."); |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 226 | continue; |
| 227 | } |
| 228 | |
| 229 | if (DEBUG) Log.d(TAG, |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 230 | "Copying " + srcInfo.displayName + " (" + srcInfo.derivedUri + ")" |
| 231 | + " to " + dstInfo.displayName + " (" + dstInfo.derivedUri + ")"); |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 232 | |
Tomasz Mikolajewski | b8436af | 2016-01-25 16:20:15 +0900 | [diff] [blame] | 233 | processDocument(srcInfo, null, dstInfo); |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 234 | } |
Ben Kwa | d5b2af1 | 2016-01-28 16:39:57 -0800 | [diff] [blame] | 235 | Metrics.logFileOperation(service, operationType, mSrcs, dstInfo); |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 236 | } |
| 237 | |
Tomasz Mikolajewski | 748ea8c | 2016-01-22 16:22:51 +0900 | [diff] [blame] | 238 | @Override |
| 239 | boolean hasWarnings() { |
| 240 | return !convertedFiles.isEmpty(); |
| 241 | } |
| 242 | |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 243 | /** |
| 244 | * Logs progress on the current copy operation. Displays/Updates the progress notification. |
| 245 | * |
| 246 | * @param bytesCopied |
| 247 | */ |
| 248 | private void makeCopyProgress(long bytesCopied) { |
| 249 | onBytesCopied(bytesCopied); |
| 250 | if (shouldUpdateProgress()) { |
| 251 | updateRemainingTimeEstimate(); |
| 252 | listener.onProgress(this); |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Copies a the given document to the given location. |
| 258 | * |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 259 | * @param src DocumentInfos for the documents to copy. |
Tomasz Mikolajewski | b8436af | 2016-01-25 16:20:15 +0900 | [diff] [blame] | 260 | * @param srcParent DocumentInfo for the parent of the document to process. |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 261 | * @param dstDirInfo The destination directory. |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 262 | * @return True on success, false on failure. |
| 263 | * @throws RemoteException |
Tomasz Mikolajewski | b8436af | 2016-01-25 16:20:15 +0900 | [diff] [blame] | 264 | * |
| 265 | * TODO: Stop passing srcParent, as it's not used for copy, but for move only. |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 266 | */ |
Tomasz Mikolajewski | b8436af | 2016-01-25 16:20:15 +0900 | [diff] [blame] | 267 | boolean processDocument(DocumentInfo src, DocumentInfo srcParent, |
| 268 | DocumentInfo dstDirInfo) throws RemoteException { |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 269 | |
| 270 | // TODO: When optimized copy kicks in, we'll not making any progress updates. |
| 271 | // For now. Local storage isn't using optimized copy. |
| 272 | |
Tomasz Mikolajewski | 6704808 | 2016-01-21 10:00:33 +0900 | [diff] [blame] | 273 | // When copying within the same provider, try to use optimized copying. |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 274 | // If not supported, then fallback to byte-by-byte copy/move. |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 275 | if (src.authority.equals(dstDirInfo.authority)) { |
| 276 | if ((src.flags & Document.FLAG_SUPPORTS_COPY) != 0) { |
| 277 | if (DocumentsContract.copyDocument(getClient(src), src.derivedUri, |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 278 | dstDirInfo.derivedUri) == null) { |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 279 | onFileFailed(src, |
| 280 | "Provider side copy failed for documents: " + src.derivedUri + "."); |
Tomasz Mikolajewski | 6704808 | 2016-01-21 10:00:33 +0900 | [diff] [blame] | 281 | return false; |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 282 | } |
Tomasz Mikolajewski | 6704808 | 2016-01-21 10:00:33 +0900 | [diff] [blame] | 283 | return true; |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 284 | } |
| 285 | } |
| 286 | |
| 287 | // If we couldn't do an optimized copy...we fall back to vanilla byte copy. |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 288 | return byteCopyDocument(src, dstDirInfo); |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 289 | } |
| 290 | |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 291 | boolean byteCopyDocument(DocumentInfo src, DocumentInfo dest) |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 292 | throws RemoteException { |
| 293 | final String dstMimeType; |
| 294 | final String dstDisplayName; |
| 295 | |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 296 | if (DEBUG) Log.d(TAG, "Doing byte copy of document: " + src); |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 297 | // If the file is virtual, but can be converted to another format, then try to copy it |
| 298 | // as such format. Also, append an extension for the target mime type (if known). |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 299 | if (src.isVirtualDocument()) { |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 300 | final String[] streamTypes = getContentResolver().getStreamTypes( |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 301 | src.derivedUri, "*/*"); |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 302 | if (streamTypes != null && streamTypes.length > 0) { |
| 303 | dstMimeType = streamTypes[0]; |
| 304 | final String extension = MimeTypeMap.getSingleton(). |
| 305 | getExtensionFromMimeType(dstMimeType); |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 306 | dstDisplayName = src.displayName + |
| 307 | (extension != null ? "." + extension : src.displayName); |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 308 | } else { |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 309 | onFileFailed(src, "Cannot copy virtual file. No streamable formats available."); |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 310 | return false; |
| 311 | } |
| 312 | } else { |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 313 | dstMimeType = src.mimeType; |
| 314 | dstDisplayName = src.displayName; |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | // Create the target document (either a file or a directory), then copy recursively the |
| 318 | // contents (bytes or children). |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 319 | final Uri dstUri = DocumentsContract.createDocument( |
| 320 | getClient(dest), dest.derivedUri, dstMimeType, dstDisplayName); |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 321 | if (dstUri == null) { |
| 322 | // If this is a directory, the entire subdir will not be copied over. |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 323 | onFileFailed(src, |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 324 | "Couldn't create destination document " + dstDisplayName |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 325 | + " in directory " + dest.displayName + "."); |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 326 | return false; |
| 327 | } |
| 328 | |
| 329 | DocumentInfo dstInfo = null; |
| 330 | try { |
| 331 | dstInfo = DocumentInfo.fromUri(getContentResolver(), dstUri); |
| 332 | } catch (FileNotFoundException e) { |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 333 | onFileFailed(src, |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 334 | "Could not load DocumentInfo for newly created file: " + dstUri + "."); |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 335 | return false; |
| 336 | } |
| 337 | |
| 338 | final boolean success; |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 339 | if (Document.MIME_TYPE_DIR.equals(src.mimeType)) { |
| 340 | success = copyDirectoryHelper(src, dstInfo); |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 341 | } else { |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 342 | success = copyFileHelper(src, dstInfo, dstMimeType); |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | return success; |
| 346 | } |
| 347 | |
| 348 | /** |
| 349 | * Handles recursion into a directory and copying its contents. Note that in linux terms, this |
| 350 | * does the equivalent of "cp src/* dst", not "cp -r src dst". |
| 351 | * |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 352 | * @param srcDir Info of the directory to copy from. The routine will copy the directory's |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 353 | * contents, not the directory itself. |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 354 | * @param destDir Info of the directory to copy to. Must be created beforehand. |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 355 | * @return True on success, false if some of the children failed to copy. |
| 356 | * @throws RemoteException |
| 357 | */ |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 358 | private boolean copyDirectoryHelper(DocumentInfo srcDir, DocumentInfo destDir) |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 359 | throws RemoteException { |
| 360 | // Recurse into directories. Copy children into the new subdirectory. |
| 361 | final String queryColumns[] = new String[] { |
| 362 | Document.COLUMN_DISPLAY_NAME, |
| 363 | Document.COLUMN_DOCUMENT_ID, |
| 364 | Document.COLUMN_MIME_TYPE, |
| 365 | Document.COLUMN_SIZE, |
| 366 | Document.COLUMN_FLAGS |
| 367 | }; |
| 368 | Cursor cursor = null; |
| 369 | boolean success = true; |
| 370 | try { |
| 371 | // Iterate over srcs in the directory; copy to the destination directory. |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 372 | final Uri queryUri = buildChildDocumentsUri(srcDir.authority, srcDir.documentId); |
| 373 | cursor = getClient(srcDir).query(queryUri, queryColumns, null, null, null); |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 374 | while (cursor.moveToNext() && !isCanceled()) { |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 375 | DocumentInfo src = DocumentInfo.fromCursor(cursor, srcDir.authority); |
Tomasz Mikolajewski | b8436af | 2016-01-25 16:20:15 +0900 | [diff] [blame] | 376 | success &= processDocument(src, srcDir, destDir); |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 377 | } |
| 378 | } finally { |
| 379 | IoUtils.closeQuietly(cursor); |
| 380 | } |
| 381 | |
| 382 | return success; |
| 383 | } |
| 384 | |
| 385 | /** |
| 386 | * Handles copying a single file. |
| 387 | * |
| 388 | * @param srcUriInfo Info of the file to copy from. |
| 389 | * @param dstUriInfo Info of the *file* to copy to. Must be created beforehand. |
| 390 | * @param mimeType Mime type for the target. Can be different than source for virtual files. |
| 391 | * @return True on success, false on error. |
| 392 | * @throws RemoteException |
| 393 | */ |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 394 | private boolean copyFileHelper(DocumentInfo src, DocumentInfo dest, String mimeType) |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 395 | throws RemoteException { |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 396 | CancellationSignal canceller = new CancellationSignal(); |
| 397 | ParcelFileDescriptor srcFile = null; |
| 398 | ParcelFileDescriptor dstFile = null; |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 399 | InputStream in = null; |
| 400 | OutputStream out = null; |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 401 | |
| 402 | boolean success = true; |
| 403 | try { |
| 404 | // If the file is virtual, but can be converted to another format, then try to copy it |
| 405 | // as such format. |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 406 | if (src.isVirtualDocument()) { |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 407 | final AssetFileDescriptor srcFileAsAsset = |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 408 | getClient(src).openTypedAssetFileDescriptor( |
| 409 | src.derivedUri, mimeType, null, canceller); |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 410 | srcFile = srcFileAsAsset.getParcelFileDescriptor(); |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 411 | in = new AssetFileDescriptor.AutoCloseInputStream(srcFileAsAsset); |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 412 | } else { |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 413 | srcFile = getClient(src).openFile(src.derivedUri, "r", canceller); |
| 414 | in = new ParcelFileDescriptor.AutoCloseInputStream(srcFile); |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 415 | } |
| 416 | |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 417 | dstFile = getClient(dest).openFile(dest.derivedUri, "w", canceller); |
| 418 | out = new ParcelFileDescriptor.AutoCloseOutputStream(dstFile); |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 419 | |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 420 | byte[] buffer = new byte[32 * 1024]; |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 421 | int len; |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 422 | while ((len = in.read(buffer)) != -1) { |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 423 | if (isCanceled()) { |
| 424 | if (DEBUG) Log.d(TAG, "Canceled copy mid-copy. Id:" + id); |
| 425 | success = false; |
| 426 | break; |
| 427 | } |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 428 | out.write(buffer, 0, len); |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 429 | makeCopyProgress(len); |
| 430 | } |
| 431 | |
| 432 | srcFile.checkError(); |
| 433 | } catch (IOException e) { |
| 434 | success = false; |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 435 | onFileFailed(src, "Exception thrown while copying from " |
| 436 | + src.derivedUri + " to " + dest.derivedUri + "."); |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 437 | |
| 438 | if (dstFile != null) { |
| 439 | try { |
| 440 | dstFile.closeWithError(e.getMessage()); |
| 441 | } catch (IOException closeError) { |
| 442 | Log.e(TAG, "Error closing destination", closeError); |
| 443 | } |
| 444 | } |
| 445 | } finally { |
| 446 | // This also ensures the file descriptors are closed. |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 447 | IoUtils.closeQuietly(in); |
| 448 | IoUtils.closeQuietly(out); |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | if (!success) { |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 452 | if (DEBUG) Log.d(TAG, "Cleaning up failed operation leftovers."); |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 453 | canceller.cancel(); |
| 454 | try { |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 455 | DocumentsContract.deleteDocument(getClient(dest), dest.derivedUri); |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 456 | } catch (RemoteException e) { |
| 457 | // RemoteExceptions usually signal that the connection is dead, so there's no |
| 458 | // point attempting to continue. Propagate the exception up so the copy job is |
| 459 | // cancelled. |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 460 | Log.w(TAG, "Failed to cleanup after copy error: " + src.derivedUri, e); |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 461 | throw e; |
| 462 | } |
| 463 | } |
| 464 | |
Tomasz Mikolajewski | 748ea8c | 2016-01-22 16:22:51 +0900 | [diff] [blame] | 465 | if (src.isVirtualDocument() && success) { |
| 466 | convertedFiles.add(src); |
| 467 | } |
| 468 | |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 469 | return success; |
| 470 | } |
| 471 | |
| 472 | /** |
| 473 | * Calculates the cumulative size of all the documents in the list. Directories are recursed |
| 474 | * into and totaled up. |
| 475 | * |
| 476 | * @param srcs |
| 477 | * @return Size in bytes. |
| 478 | * @throws RemoteException |
| 479 | */ |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 480 | private long calculateSize(List<DocumentInfo> srcs) |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 481 | throws RemoteException { |
| 482 | long result = 0; |
| 483 | |
| 484 | for (DocumentInfo src : srcs) { |
| 485 | if (src.isDirectory()) { |
| 486 | // Directories need to be recursed into. |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 487 | result += calculateFileSizesRecursively(getClient(src), src.derivedUri); |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 488 | } else { |
| 489 | result += src.size; |
| 490 | } |
| 491 | } |
| 492 | return result; |
| 493 | } |
| 494 | |
| 495 | /** |
| 496 | * Calculates (recursively) the cumulative size of all the files under the given directory. |
| 497 | * |
| 498 | * @throws RemoteException |
| 499 | */ |
| 500 | private static long calculateFileSizesRecursively( |
| 501 | ContentProviderClient client, Uri uri) throws RemoteException { |
| 502 | final String authority = uri.getAuthority(); |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 503 | final Uri queryUri = buildChildDocumentsUri(authority, getDocumentId(uri)); |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 504 | final String queryColumns[] = new String[] { |
| 505 | Document.COLUMN_DOCUMENT_ID, |
| 506 | Document.COLUMN_MIME_TYPE, |
| 507 | Document.COLUMN_SIZE |
| 508 | }; |
| 509 | |
| 510 | long result = 0; |
| 511 | Cursor cursor = null; |
| 512 | try { |
| 513 | cursor = client.query(queryUri, queryColumns, null, null, null); |
| 514 | while (cursor.moveToNext()) { |
| 515 | if (Document.MIME_TYPE_DIR.equals( |
| 516 | getCursorString(cursor, Document.COLUMN_MIME_TYPE))) { |
| 517 | // Recurse into directories. |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 518 | final Uri dirUri = buildDocumentUri(authority, |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 519 | getCursorString(cursor, Document.COLUMN_DOCUMENT_ID)); |
| 520 | result += calculateFileSizesRecursively(client, dirUri); |
| 521 | } else { |
| 522 | // This may return -1 if the size isn't defined. Ignore those cases. |
| 523 | long size = getCursorLong(cursor, Document.COLUMN_SIZE); |
| 524 | result += size > 0 ? size : 0; |
| 525 | } |
| 526 | } |
| 527 | } finally { |
| 528 | IoUtils.closeQuietly(cursor); |
| 529 | } |
| 530 | |
| 531 | return result; |
| 532 | } |
| 533 | |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 534 | /** |
| 535 | * Returns true if {@code doc} is a descendant of {@code parentDoc}. |
| 536 | * @throws RemoteException |
| 537 | */ |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 538 | boolean isDescendentOf(DocumentInfo doc, DocumentInfo parent) |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 539 | throws RemoteException { |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 540 | if (parent.isDirectory() && doc.authority.equals(parent.authority)) { |
| 541 | return isChildDocument(getClient(doc), doc.derivedUri, parent.derivedUri); |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 542 | } |
| 543 | return false; |
| 544 | } |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 545 | |
| 546 | private void onFileFailed(DocumentInfo file, String msg) { |
| 547 | Log.w(TAG, msg); |
| 548 | onFileFailed(file); |
| 549 | } |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 550 | |
| 551 | @Override |
| 552 | public String toString() { |
| 553 | return new StringBuilder() |
| 554 | .append("CopyJob") |
| 555 | .append("{") |
| 556 | .append("id=" + id) |
Tomasz Mikolajewski | b8436af | 2016-01-25 16:20:15 +0900 | [diff] [blame] | 557 | .append(", srcs=" + mSrcs) |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 558 | .append(", destination=" + stack) |
| 559 | .append("}") |
| 560 | .toString(); |
| 561 | } |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 562 | } |