Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.documentsui.services; |
| 18 | |
| 19 | import static android.os.SystemClock.elapsedRealtime; |
Steve McKay | bbeba52 | 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 | dd2b31c | 2016-01-22 16:22:51 +0900 | [diff] [blame] | 24 | import static com.android.documentsui.OperationDialogFragment.DIALOG_TYPE_CONVERTED; |
Steve McKay | d080506 | 2016-09-15 14:30:38 -0700 | [diff] [blame] | 25 | import static com.android.documentsui.base.DocumentInfo.getCursorLong; |
| 26 | import static com.android.documentsui.base.DocumentInfo.getCursorString; |
Steve McKay | d9caa6a | 2016-09-15 16:36:45 -0700 | [diff] [blame] | 27 | import static com.android.documentsui.base.Shared.DEBUG; |
Tomasz Mikolajewski | dd2b31c | 2016-01-22 16:22:51 +0900 | [diff] [blame] | 28 | import static com.android.documentsui.services.FileOperationService.EXTRA_DIALOG_TYPE; |
Garfield, Tan | 4833477 | 2016-06-28 17:17:38 -0700 | [diff] [blame] | 29 | import static com.android.documentsui.services.FileOperationService.EXTRA_OPERATION_TYPE; |
Tomasz Mikolajewski | dd2b31c | 2016-01-22 16:22:51 +0900 | [diff] [blame] | 30 | import static com.android.documentsui.services.FileOperationService.EXTRA_SRC_LIST; |
Garfield, Tan | 4833477 | 2016-06-28 17:17:38 -0700 | [diff] [blame] | 31 | import static com.android.documentsui.services.FileOperationService.OPERATION_COPY; |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 32 | |
| 33 | import android.annotation.StringRes; |
| 34 | import android.app.Notification; |
| 35 | import android.app.Notification.Builder; |
Tomasz Mikolajewski | dd2b31c | 2016-01-22 16:22:51 +0900 | [diff] [blame] | 36 | import android.app.PendingIntent; |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 37 | import android.content.ContentProviderClient; |
Garfield, Tan | edce554 | 2016-06-17 15:32:28 -0700 | [diff] [blame] | 38 | import android.content.ContentResolver; |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 39 | import android.content.Context; |
Tomasz Mikolajewski | dd2b31c | 2016-01-22 16:22:51 +0900 | [diff] [blame] | 40 | import android.content.Intent; |
Steve McKay | c83baa0 | 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; |
Daichi Hirono | 0f3b4bd | 2016-11-16 15:42:42 +0900 | [diff] [blame^] | 49 | import android.system.ErrnoException; |
| 50 | import android.system.Os; |
| 51 | import android.system.OsConstants; |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 52 | import android.text.format.DateUtils; |
| 53 | import android.util.Log; |
| 54 | import android.webkit.MimeTypeMap; |
| 55 | |
Garfield, Tan | c20c6f6 | 2016-07-06 17:24:20 -0700 | [diff] [blame] | 56 | import com.android.documentsui.DocumentsApplication; |
Ben Kwa | faa2720 | 2016-01-28 16:39:57 -0800 | [diff] [blame] | 57 | import com.android.documentsui.Metrics; |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 58 | import com.android.documentsui.R; |
Steve McKay | d080506 | 2016-09-15 14:30:38 -0700 | [diff] [blame] | 59 | import com.android.documentsui.base.DocumentInfo; |
| 60 | import com.android.documentsui.base.DocumentStack; |
| 61 | import com.android.documentsui.base.RootInfo; |
Garfield, Tan | 9666ce6 | 2016-07-12 11:02:09 -0700 | [diff] [blame] | 62 | import com.android.documentsui.clipping.UrisSupplier; |
Steve McKay | d9caa6a | 2016-09-15 16:36:45 -0700 | [diff] [blame] | 63 | import com.android.documentsui.roots.RootsCache; |
Garfield, Tan | 4833477 | 2016-06-28 17:17:38 -0700 | [diff] [blame] | 64 | import com.android.documentsui.services.FileOperationService.OpType; |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 65 | |
| 66 | import libcore.io.IoUtils; |
| 67 | |
| 68 | import java.io.FileNotFoundException; |
| 69 | import java.io.IOException; |
| 70 | import java.io.InputStream; |
Daichi Hirono | 0f3b4bd | 2016-11-16 15:42:42 +0900 | [diff] [blame^] | 71 | import java.io.SyncFailedException; |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 72 | import java.text.NumberFormat; |
Tomasz Mikolajewski | dd2b31c | 2016-01-22 16:22:51 +0900 | [diff] [blame] | 73 | import java.util.ArrayList; |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 74 | import java.util.List; |
| 75 | |
| 76 | class CopyJob extends Job { |
Steve McKay | 003097d | 2016-02-23 10:06:50 -0800 | [diff] [blame] | 77 | |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 78 | private static final String TAG = "CopyJob"; |
Steve McKay | 003097d | 2016-02-23 10:06:50 -0800 | [diff] [blame] | 79 | |
Steve McKay | 97b4be4 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 80 | final List<DocumentInfo> mSrcs; |
Tomasz Mikolajewski | dd2b31c | 2016-01-22 16:22:51 +0900 | [diff] [blame] | 81 | final ArrayList<DocumentInfo> convertedFiles = new ArrayList<>(); |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 82 | |
| 83 | private long mStartTime = -1; |
Steve McKay | 003097d | 2016-02-23 10:06:50 -0800 | [diff] [blame] | 84 | |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 85 | private long mBatchSize; |
Garfield, Tan | 48ef36f | 2016-06-09 12:04:22 -0700 | [diff] [blame] | 86 | private volatile long mBytesCopied; |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 87 | // Speed estimation |
| 88 | private long mBytesCopiedSample; |
| 89 | private long mSampleTime; |
| 90 | private long mSpeed; |
| 91 | private long mRemainingTime; |
| 92 | |
| 93 | /** |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 94 | * @see @link {@link Job} constructor for most param descriptions. |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 95 | */ |
Garfield, Tan | 4833477 | 2016-06-28 17:17:38 -0700 | [diff] [blame] | 96 | CopyJob(Context service, Listener listener, String id, DocumentStack destination, |
| 97 | UrisSupplier srcs) { |
| 98 | this(service, listener, id, OPERATION_COPY, destination, srcs); |
| 99 | } |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 100 | |
Garfield, Tan | 4833477 | 2016-06-28 17:17:38 -0700 | [diff] [blame] | 101 | CopyJob(Context service, Listener listener, String id, @OpType int opType, |
| 102 | DocumentStack destination, UrisSupplier srcs) { |
| 103 | super(service, listener, id, opType, destination, srcs); |
| 104 | |
| 105 | assert(srcs.getItemCount() > 0); |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 106 | |
Garfield, Tan | edce554 | 2016-06-17 15:32:28 -0700 | [diff] [blame] | 107 | // delay the initialization of it to setUp() because it may be IO extensive. |
Garfield, Tan | 4833477 | 2016-06-28 17:17:38 -0700 | [diff] [blame] | 108 | mSrcs = new ArrayList<>(srcs.getItemCount()); |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | @Override |
| 112 | Builder createProgressBuilder() { |
| 113 | return super.createProgressBuilder( |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 114 | service.getString(R.string.copy_notification_title), |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 115 | R.drawable.ic_menu_copy, |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 116 | service.getString(android.R.string.cancel), |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 117 | R.drawable.ic_cab_cancel); |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public Notification getSetupNotification() { |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 122 | return getSetupNotification(service.getString(R.string.copy_preparing)); |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 123 | } |
| 124 | |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 125 | Notification getProgressNotification(@StringRes int msgId) { |
Garfield, Tan | 48ef36f | 2016-06-09 12:04:22 -0700 | [diff] [blame] | 126 | updateRemainingTimeEstimate(); |
| 127 | |
Tomasz Mikolajewski | c515150 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 128 | if (mBatchSize >= 0) { |
| 129 | double completed = (double) this.mBytesCopied / mBatchSize; |
| 130 | mProgressBuilder.setProgress(100, (int) (completed * 100), false); |
Garfield, Tan | 48ef36f | 2016-06-09 12:04:22 -0700 | [diff] [blame] | 131 | mProgressBuilder.setSubText( |
Tomasz Mikolajewski | c515150 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 132 | NumberFormat.getPercentInstance().format(completed)); |
| 133 | } else { |
| 134 | // If the total file size failed to compute on some files, then show |
| 135 | // an indeterminate spinner. CopyJob would most likely fail on those |
| 136 | // files while copying, but would continue with another files. |
| 137 | // Also, if the total size is 0 bytes, show an indeterminate spinner. |
| 138 | mProgressBuilder.setProgress(0, 0, true); |
| 139 | } |
| 140 | |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 141 | if (mRemainingTime > 0) { |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 142 | mProgressBuilder.setContentText(service.getString(msgId, |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 143 | DateUtils.formatDuration(mRemainingTime))); |
| 144 | } else { |
| 145 | mProgressBuilder.setContentText(null); |
| 146 | } |
| 147 | |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 148 | return mProgressBuilder.build(); |
| 149 | } |
| 150 | |
Garfield, Tan | 48ef36f | 2016-06-09 12:04:22 -0700 | [diff] [blame] | 151 | @Override |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 152 | public Notification getProgressNotification() { |
| 153 | return getProgressNotification(R.string.copy_remaining); |
| 154 | } |
| 155 | |
| 156 | void onBytesCopied(long numBytes) { |
| 157 | this.mBytesCopied += numBytes; |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Generates an estimate of the remaining time in the copy. |
| 162 | */ |
Garfield, Tan | 48ef36f | 2016-06-09 12:04:22 -0700 | [diff] [blame] | 163 | private void updateRemainingTimeEstimate() { |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 164 | long elapsedTime = elapsedRealtime() - mStartTime; |
| 165 | |
Garfield, Tan | 48ef36f | 2016-06-09 12:04:22 -0700 | [diff] [blame] | 166 | // mBytesCopied is modified in worker thread, but this method is called in monitor thread, |
| 167 | // so take a snapshot of mBytesCopied to make sure the updated estimate is consistent. |
| 168 | final long bytesCopied = mBytesCopied; |
Garfield, Tan | edce554 | 2016-06-17 15:32:28 -0700 | [diff] [blame] | 169 | final long sampleDuration = Math.max(elapsedTime - mSampleTime, 1L); // avoid dividing 0 |
Garfield, Tan | 48ef36f | 2016-06-09 12:04:22 -0700 | [diff] [blame] | 170 | final long sampleSpeed = ((bytesCopied - mBytesCopiedSample) * 1000) / sampleDuration; |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 171 | if (mSpeed == 0) { |
| 172 | mSpeed = sampleSpeed; |
| 173 | } else { |
| 174 | mSpeed = ((3 * mSpeed) + sampleSpeed) / 4; |
| 175 | } |
| 176 | |
| 177 | if (mSampleTime > 0 && mSpeed > 0) { |
Garfield, Tan | 48ef36f | 2016-06-09 12:04:22 -0700 | [diff] [blame] | 178 | mRemainingTime = ((mBatchSize - bytesCopied) * 1000) / mSpeed; |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 179 | } else { |
| 180 | mRemainingTime = 0; |
| 181 | } |
| 182 | |
| 183 | mSampleTime = elapsedTime; |
Garfield, Tan | 48ef36f | 2016-06-09 12:04:22 -0700 | [diff] [blame] | 184 | mBytesCopiedSample = bytesCopied; |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | @Override |
| 188 | Notification getFailureNotification() { |
| 189 | return getFailureNotification( |
| 190 | R.plurals.copy_error_notification_title, R.drawable.ic_menu_copy); |
| 191 | } |
| 192 | |
| 193 | @Override |
Tomasz Mikolajewski | dd2b31c | 2016-01-22 16:22:51 +0900 | [diff] [blame] | 194 | Notification getWarningNotification() { |
Tomasz Mikolajewski | 63e2aae | 2016-02-01 12:01:14 +0900 | [diff] [blame] | 195 | final Intent navigateIntent = buildNavigateIntent(INTENT_TAG_WARNING); |
Tomasz Mikolajewski | dd2b31c | 2016-01-22 16:22:51 +0900 | [diff] [blame] | 196 | navigateIntent.putExtra(EXTRA_DIALOG_TYPE, DIALOG_TYPE_CONVERTED); |
Garfield, Tan | 4833477 | 2016-06-28 17:17:38 -0700 | [diff] [blame] | 197 | navigateIntent.putExtra(EXTRA_OPERATION_TYPE, operationType); |
Tomasz Mikolajewski | dd2b31c | 2016-01-22 16:22:51 +0900 | [diff] [blame] | 198 | |
| 199 | navigateIntent.putParcelableArrayListExtra(EXTRA_SRC_LIST, convertedFiles); |
| 200 | |
| 201 | // TODO: Consider adding a dialog on tapping the notification with a list of |
| 202 | // converted files. |
| 203 | final Notification.Builder warningBuilder = new Notification.Builder(service) |
| 204 | .setContentTitle(service.getResources().getString( |
| 205 | R.string.notification_copy_files_converted_title)) |
| 206 | .setContentText(service.getString( |
| 207 | R.string.notification_touch_for_details)) |
| 208 | .setContentIntent(PendingIntent.getActivity(appContext, 0, navigateIntent, |
| 209 | PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT)) |
| 210 | .setCategory(Notification.CATEGORY_ERROR) |
| 211 | .setSmallIcon(R.drawable.ic_menu_copy) |
| 212 | .setAutoCancel(true); |
| 213 | return warningBuilder.build(); |
| 214 | } |
| 215 | |
| 216 | @Override |
Garfield, Tan | edce554 | 2016-06-17 15:32:28 -0700 | [diff] [blame] | 217 | boolean setUp() { |
Garfield, Tan | edce554 | 2016-06-17 15:32:28 -0700 | [diff] [blame] | 218 | try { |
| 219 | buildDocumentList(); |
| 220 | } catch (ResourceException e) { |
| 221 | Log.e(TAG, "Failed to get the list of docs.", e); |
| 222 | return false; |
| 223 | } |
| 224 | |
Garfield, Tan | c20c6f6 | 2016-07-06 17:24:20 -0700 | [diff] [blame] | 225 | // Check if user has canceled this task. |
Garfield, Tan | edce554 | 2016-06-17 15:32:28 -0700 | [diff] [blame] | 226 | if (isCanceled()) { |
| 227 | return false; |
| 228 | } |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 229 | |
Tomasz Mikolajewski | c515150 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 230 | try { |
| 231 | mBatchSize = calculateSize(mSrcs); |
| 232 | } catch (ResourceException e) { |
Steve McKay | b1b08b2 | 2016-06-14 15:56:50 -0700 | [diff] [blame] | 233 | Log.w(TAG, "Failed to calculate total size. Copying without progress.", e); |
Tomasz Mikolajewski | c515150 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 234 | mBatchSize = -1; |
| 235 | } |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 236 | |
Garfield, Tan | c20c6f6 | 2016-07-06 17:24:20 -0700 | [diff] [blame] | 237 | // Check if user has canceled this task. We should check it again here as user cancels |
| 238 | // tasks in main thread, but this is running in a worker thread. calculateSize() may |
| 239 | // take a long time during which user can cancel this task, and we don't want to waste |
| 240 | // resources doing useless large chunk of work. |
| 241 | if (isCanceled()) { |
| 242 | return false; |
| 243 | } |
| 244 | |
| 245 | return checkSpace(); |
Garfield, Tan | edce554 | 2016-06-17 15:32:28 -0700 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | @Override |
| 249 | void start() { |
| 250 | mStartTime = elapsedRealtime(); |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 251 | DocumentInfo srcInfo; |
Ben Kwa | faa2720 | 2016-01-28 16:39:57 -0800 | [diff] [blame] | 252 | DocumentInfo dstInfo = stack.peek(); |
Steve McKay | 97b4be4 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 253 | for (int i = 0; i < mSrcs.size() && !isCanceled(); ++i) { |
| 254 | srcInfo = mSrcs.get(i); |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 255 | |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 256 | if (DEBUG) Log.d(TAG, |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 257 | "Copying " + srcInfo.displayName + " (" + srcInfo.derivedUri + ")" |
| 258 | + " to " + dstInfo.displayName + " (" + dstInfo.derivedUri + ")"); |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 259 | |
Tomasz Mikolajewski | c515150 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 260 | try { |
Steve McKay | b1b08b2 | 2016-06-14 15:56:50 -0700 | [diff] [blame] | 261 | if (dstInfo.equals(srcInfo) || isDescendentOf(srcInfo, dstInfo)) { |
| 262 | Log.e(TAG, "Skipping recursive copy of " + srcInfo.derivedUri); |
| 263 | onFileFailed(srcInfo); |
| 264 | } else { |
| 265 | processDocument(srcInfo, null, dstInfo); |
| 266 | } |
Tomasz Mikolajewski | c515150 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 267 | } catch (ResourceException e) { |
Steve McKay | b1b08b2 | 2016-06-14 15:56:50 -0700 | [diff] [blame] | 268 | Log.e(TAG, "Failed to copy " + srcInfo.derivedUri, e); |
Tomasz Mikolajewski | c515150 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 269 | onFileFailed(srcInfo); |
| 270 | } |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 271 | } |
Ben Kwa | faa2720 | 2016-01-28 16:39:57 -0800 | [diff] [blame] | 272 | Metrics.logFileOperation(service, operationType, mSrcs, dstInfo); |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 273 | } |
| 274 | |
Garfield, Tan | edce554 | 2016-06-17 15:32:28 -0700 | [diff] [blame] | 275 | private void buildDocumentList() throws ResourceException { |
| 276 | try { |
| 277 | final ContentResolver resolver = appContext.getContentResolver(); |
Garfield, Tan | b7e5f6b | 2016-06-30 18:27:47 -0700 | [diff] [blame] | 278 | final Iterable<Uri> uris = srcs.getUris(appContext); |
Garfield, Tan | 0ead784 | 2016-07-21 11:10:44 -0700 | [diff] [blame] | 279 | |
| 280 | int docProcessed = 0; |
Garfield, Tan | edce554 | 2016-06-17 15:32:28 -0700 | [diff] [blame] | 281 | for (Uri uri : uris) { |
| 282 | DocumentInfo doc = DocumentInfo.fromUri(resolver, uri); |
Garfield Tan | 2a83742 | 2016-10-19 11:50:45 -0700 | [diff] [blame] | 283 | if (canCopy(doc, stack.getRoot())) { |
Garfield, Tan | edce554 | 2016-06-17 15:32:28 -0700 | [diff] [blame] | 284 | mSrcs.add(doc); |
| 285 | } else { |
| 286 | onFileFailed(doc); |
| 287 | } |
Garfield, Tan | 0ead784 | 2016-07-21 11:10:44 -0700 | [diff] [blame] | 288 | ++docProcessed; |
Garfield, Tan | edce554 | 2016-06-17 15:32:28 -0700 | [diff] [blame] | 289 | |
| 290 | if (isCanceled()) { |
| 291 | return; |
| 292 | } |
| 293 | } |
Garfield, Tan | 0ead784 | 2016-07-21 11:10:44 -0700 | [diff] [blame] | 294 | |
| 295 | // If docProcessed is different than the count claimed by UrisSupplier, add the number |
| 296 | // to failedFileCount. |
| 297 | failedFileCount += (srcs.getItemCount() - docProcessed); |
Garfield, Tan | edce554 | 2016-06-17 15:32:28 -0700 | [diff] [blame] | 298 | } catch(IOException e) { |
Garfield, Tan | 4833477 | 2016-06-28 17:17:38 -0700 | [diff] [blame] | 299 | failedFileCount += srcs.getItemCount(); |
Garfield, Tan | edce554 | 2016-06-17 15:32:28 -0700 | [diff] [blame] | 300 | throw new ResourceException("Failed to open the list of docs to copy.", e); |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | private static boolean canCopy(DocumentInfo doc, RootInfo root) { |
| 305 | // Can't copy folders to downloads, because we don't show folders there. |
| 306 | return !root.isDownloads() || !doc.isDirectory(); |
| 307 | } |
| 308 | |
Garfield, Tan | c20c6f6 | 2016-07-06 17:24:20 -0700 | [diff] [blame] | 309 | /** |
| 310 | * Checks whether the destination folder has enough space to take all source files. |
| 311 | * @return true if the root has enough space or doesn't provide free space info; otherwise false |
| 312 | */ |
| 313 | boolean checkSpace() { |
| 314 | return checkSpace(mBatchSize); |
| 315 | } |
| 316 | |
| 317 | /** |
| 318 | * Checks whether the destination folder has enough space to take files of batchSize |
| 319 | * @param batchSize the total size of files |
| 320 | * @return true if the root has enough space or doesn't provide free space info; otherwise false |
| 321 | */ |
| 322 | final boolean checkSpace(long batchSize) { |
| 323 | // Default to be true because if batchSize or available space is invalid, we still let the |
| 324 | // copy start anyway. |
| 325 | boolean result = true; |
| 326 | if (batchSize >= 0) { |
| 327 | RootsCache cache = DocumentsApplication.getRootsCache(appContext); |
| 328 | |
Garfield Tan | 2a83742 | 2016-10-19 11:50:45 -0700 | [diff] [blame] | 329 | RootInfo root = stack.getRoot(); |
Garfield, Tan | c20c6f6 | 2016-07-06 17:24:20 -0700 | [diff] [blame] | 330 | // Query root info here instead of using stack.root because the number there may be |
| 331 | // stale. |
Garfield Tan | 2a83742 | 2016-10-19 11:50:45 -0700 | [diff] [blame] | 332 | root = cache.getRootOneshot(root.authority, root.rootId, true); |
Garfield, Tan | c20c6f6 | 2016-07-06 17:24:20 -0700 | [diff] [blame] | 333 | if (root.availableBytes >= 0) { |
| 334 | result = (batchSize <= root.availableBytes); |
| 335 | } else { |
| 336 | Log.w(TAG, root.toString() + " doesn't provide available bytes."); |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | if (!result) { |
| 341 | failedFileCount += mSrcs.size(); |
| 342 | failedFiles.addAll(mSrcs); |
| 343 | } |
| 344 | |
| 345 | return result; |
| 346 | } |
| 347 | |
Tomasz Mikolajewski | dd2b31c | 2016-01-22 16:22:51 +0900 | [diff] [blame] | 348 | @Override |
| 349 | boolean hasWarnings() { |
| 350 | return !convertedFiles.isEmpty(); |
| 351 | } |
| 352 | |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 353 | /** |
| 354 | * Logs progress on the current copy operation. Displays/Updates the progress notification. |
| 355 | * |
| 356 | * @param bytesCopied |
| 357 | */ |
| 358 | private void makeCopyProgress(long bytesCopied) { |
| 359 | onBytesCopied(bytesCopied); |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | /** |
| 363 | * Copies a the given document to the given location. |
| 364 | * |
Steve McKay | 97b4be4 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 365 | * @param src DocumentInfos for the documents to copy. |
Tomasz Mikolajewski | e009441 | 2016-01-25 16:20:15 +0900 | [diff] [blame] | 366 | * @param srcParent DocumentInfo for the parent of the document to process. |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 367 | * @param dstDirInfo The destination directory. |
Tomasz Mikolajewski | c515150 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 368 | * @throws ResourceException |
Tomasz Mikolajewski | e009441 | 2016-01-25 16:20:15 +0900 | [diff] [blame] | 369 | * |
| 370 | * TODO: Stop passing srcParent, as it's not used for copy, but for move only. |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 371 | */ |
Tomasz Mikolajewski | c515150 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 372 | void processDocument(DocumentInfo src, DocumentInfo srcParent, |
| 373 | DocumentInfo dstDirInfo) throws ResourceException { |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 374 | |
| 375 | // TODO: When optimized copy kicks in, we'll not making any progress updates. |
| 376 | // For now. Local storage isn't using optimized copy. |
| 377 | |
Tomasz Mikolajewski | 4aa8967 | 2016-01-21 10:00:33 +0900 | [diff] [blame] | 378 | // When copying within the same provider, try to use optimized copying. |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 379 | // If not supported, then fallback to byte-by-byte copy/move. |
Steve McKay | 97b4be4 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 380 | if (src.authority.equals(dstDirInfo.authority)) { |
| 381 | if ((src.flags & Document.FLAG_SUPPORTS_COPY) != 0) { |
Tomasz Mikolajewski | c515150 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 382 | try { |
| 383 | if (DocumentsContract.copyDocument(getClient(src), src.derivedUri, |
Tomasz Mikolajewski | fe7f536 | 2016-03-02 17:41:40 +0900 | [diff] [blame] | 384 | dstDirInfo.derivedUri) != null) { |
| 385 | return; |
Tomasz Mikolajewski | c515150 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 386 | } |
Tomasz Mikolajewski | c515150 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 387 | } catch (RemoteException | RuntimeException e) { |
Tomasz Mikolajewski | fe7f536 | 2016-03-02 17:41:40 +0900 | [diff] [blame] | 388 | Log.e(TAG, "Provider side copy failed for: " + src.derivedUri |
Steve McKay | b1b08b2 | 2016-06-14 15:56:50 -0700 | [diff] [blame] | 389 | + " due to an exception.", e); |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 390 | } |
Garfield, Tan | 48ef36f | 2016-06-09 12:04:22 -0700 | [diff] [blame] | 391 | |
Tomasz Mikolajewski | fe7f536 | 2016-03-02 17:41:40 +0900 | [diff] [blame] | 392 | // If optimized copy fails, then fallback to byte-by-byte copy. |
| 393 | if (DEBUG) Log.d(TAG, "Fallback to byte-by-byte copy for: " + src.derivedUri); |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 394 | } |
| 395 | } |
| 396 | |
| 397 | // If we couldn't do an optimized copy...we fall back to vanilla byte copy. |
Tomasz Mikolajewski | c515150 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 398 | byteCopyDocument(src, dstDirInfo); |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 399 | } |
| 400 | |
Tomasz Mikolajewski | c515150 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 401 | void byteCopyDocument(DocumentInfo src, DocumentInfo dest) throws ResourceException { |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 402 | final String dstMimeType; |
| 403 | final String dstDisplayName; |
| 404 | |
Steve McKay | 97b4be4 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 405 | if (DEBUG) Log.d(TAG, "Doing byte copy of document: " + src); |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 406 | // If the file is virtual, but can be converted to another format, then try to copy it |
| 407 | // as such format. Also, append an extension for the target mime type (if known). |
Steve McKay | 358c3ec | 2016-10-21 09:16:57 -0700 | [diff] [blame] | 408 | if (src.isVirtual()) { |
Tomasz Mikolajewski | c515150 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 409 | String[] streamTypes = null; |
| 410 | try { |
| 411 | streamTypes = getContentResolver().getStreamTypes(src.derivedUri, "*/*"); |
| 412 | } catch (RuntimeException e) { |
| 413 | throw new ResourceException( |
| 414 | "Failed to obtain streamable types for %s due to an exception.", |
| 415 | src.derivedUri, e); |
| 416 | } |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 417 | if (streamTypes != null && streamTypes.length > 0) { |
| 418 | dstMimeType = streamTypes[0]; |
| 419 | final String extension = MimeTypeMap.getSingleton(). |
| 420 | getExtensionFromMimeType(dstMimeType); |
Steve McKay | 97b4be4 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 421 | dstDisplayName = src.displayName + |
| 422 | (extension != null ? "." + extension : src.displayName); |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 423 | } else { |
Tomasz Mikolajewski | c515150 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 424 | throw new ResourceException("Cannot copy virtual file %s. No streamable formats " |
| 425 | + "available.", src.derivedUri); |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 426 | } |
| 427 | } else { |
Steve McKay | 97b4be4 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 428 | dstMimeType = src.mimeType; |
| 429 | dstDisplayName = src.displayName; |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | // Create the target document (either a file or a directory), then copy recursively the |
| 433 | // contents (bytes or children). |
Tomasz Mikolajewski | c515150 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 434 | Uri dstUri = null; |
| 435 | try { |
| 436 | dstUri = DocumentsContract.createDocument( |
| 437 | getClient(dest), dest.derivedUri, dstMimeType, dstDisplayName); |
| 438 | } catch (RemoteException | RuntimeException e) { |
| 439 | throw new ResourceException( |
| 440 | "Couldn't create destination document " + dstDisplayName + " in directory %s " |
| 441 | + "due to an exception.", dest.derivedUri, e); |
| 442 | } |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 443 | if (dstUri == null) { |
| 444 | // If this is a directory, the entire subdir will not be copied over. |
Tomasz Mikolajewski | c515150 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 445 | throw new ResourceException( |
| 446 | "Couldn't create destination document " + dstDisplayName + " in directory %s.", |
| 447 | dest.derivedUri); |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | DocumentInfo dstInfo = null; |
| 451 | try { |
| 452 | dstInfo = DocumentInfo.fromUri(getContentResolver(), dstUri); |
Tomasz Mikolajewski | c515150 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 453 | } catch (FileNotFoundException | RuntimeException e) { |
| 454 | throw new ResourceException("Could not load DocumentInfo for newly created file %s.", |
| 455 | dstUri); |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 456 | } |
| 457 | |
Steve McKay | 97b4be4 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 458 | if (Document.MIME_TYPE_DIR.equals(src.mimeType)) { |
Tomasz Mikolajewski | c515150 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 459 | copyDirectoryHelper(src, dstInfo); |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 460 | } else { |
Tomasz Mikolajewski | 8fe5498 | 2016-02-23 15:12:54 +0900 | [diff] [blame] | 461 | copyFileHelper(src, dstInfo, dest, dstMimeType); |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 462 | } |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | /** |
| 466 | * Handles recursion into a directory and copying its contents. Note that in linux terms, this |
| 467 | * does the equivalent of "cp src/* dst", not "cp -r src dst". |
| 468 | * |
Steve McKay | 97b4be4 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 469 | * @param srcDir Info of the directory to copy from. The routine will copy the directory's |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 470 | * contents, not the directory itself. |
Steve McKay | 97b4be4 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 471 | * @param destDir Info of the directory to copy to. Must be created beforehand. |
Tomasz Mikolajewski | c515150 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 472 | * @throws ResourceException |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 473 | */ |
Tomasz Mikolajewski | c515150 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 474 | private void copyDirectoryHelper(DocumentInfo srcDir, DocumentInfo destDir) |
| 475 | throws ResourceException { |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 476 | // Recurse into directories. Copy children into the new subdirectory. |
| 477 | final String queryColumns[] = new String[] { |
| 478 | Document.COLUMN_DISPLAY_NAME, |
| 479 | Document.COLUMN_DOCUMENT_ID, |
| 480 | Document.COLUMN_MIME_TYPE, |
| 481 | Document.COLUMN_SIZE, |
| 482 | Document.COLUMN_FLAGS |
| 483 | }; |
| 484 | Cursor cursor = null; |
| 485 | boolean success = true; |
Tomasz Mikolajewski | c515150 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 486 | // Iterate over srcs in the directory; copy to the destination directory. |
| 487 | final Uri queryUri = buildChildDocumentsUri(srcDir.authority, srcDir.documentId); |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 488 | try { |
Tomasz Mikolajewski | c515150 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 489 | try { |
| 490 | cursor = getClient(srcDir).query(queryUri, queryColumns, null, null, null); |
| 491 | } catch (RemoteException | RuntimeException e) { |
| 492 | throw new ResourceException("Failed to query children of %s due to an exception.", |
| 493 | srcDir.derivedUri, e); |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 494 | } |
Tomasz Mikolajewski | c515150 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 495 | |
| 496 | DocumentInfo src; |
| 497 | while (cursor.moveToNext() && !isCanceled()) { |
| 498 | try { |
| 499 | src = DocumentInfo.fromCursor(cursor, srcDir.authority); |
| 500 | processDocument(src, srcDir, destDir); |
| 501 | } catch (RuntimeException e) { |
Garfield, Tan | 48ef36f | 2016-06-09 12:04:22 -0700 | [diff] [blame] | 502 | Log.e(TAG, String.format( |
| 503 | "Failed to recursively process a file %s due to an exception.", |
| 504 | srcDir.derivedUri.toString()), e); |
Tomasz Mikolajewski | c515150 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 505 | success = false; |
| 506 | } |
| 507 | } |
| 508 | } catch (RuntimeException e) { |
Garfield, Tan | 48ef36f | 2016-06-09 12:04:22 -0700 | [diff] [blame] | 509 | Log.e(TAG, String.format( |
| 510 | "Failed to copy a file %s to %s. ", |
| 511 | srcDir.derivedUri.toString(), destDir.derivedUri.toString()), e); |
Tomasz Mikolajewski | c515150 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 512 | success = false; |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 513 | } finally { |
| 514 | IoUtils.closeQuietly(cursor); |
| 515 | } |
| 516 | |
Tomasz Mikolajewski | c515150 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 517 | if (!success) { |
| 518 | throw new RuntimeException("Some files failed to copy during a recursive " |
| 519 | + "directory copy."); |
| 520 | } |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | /** |
| 524 | * Handles copying a single file. |
| 525 | * |
Tomasz Mikolajewski | 8fe5498 | 2016-02-23 15:12:54 +0900 | [diff] [blame] | 526 | * @param src Info of the file to copy from. |
| 527 | * @param dest Info of the *file* to copy to. Must be created beforehand. |
| 528 | * @param destParent Info of the parent of the destination. |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 529 | * @param mimeType Mime type for the target. Can be different than source for virtual files. |
Tomasz Mikolajewski | c515150 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 530 | * @throws ResourceException |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 531 | */ |
Tomasz Mikolajewski | 8fe5498 | 2016-02-23 15:12:54 +0900 | [diff] [blame] | 532 | private void copyFileHelper(DocumentInfo src, DocumentInfo dest, DocumentInfo destParent, |
| 533 | String mimeType) throws ResourceException { |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 534 | CancellationSignal canceller = new CancellationSignal(); |
Tomasz Mikolajewski | c515150 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 535 | AssetFileDescriptor srcFileAsAsset = null; |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 536 | ParcelFileDescriptor srcFile = null; |
| 537 | ParcelFileDescriptor dstFile = null; |
Steve McKay | 97b4be4 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 538 | InputStream in = null; |
Daichi Hirono | 7551240 | 2016-03-28 16:07:45 +0900 | [diff] [blame] | 539 | ParcelFileDescriptor.AutoCloseOutputStream out = null; |
Tomasz Mikolajewski | c515150 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 540 | boolean success = false; |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 541 | |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 542 | try { |
| 543 | // If the file is virtual, but can be converted to another format, then try to copy it |
| 544 | // as such format. |
Steve McKay | 358c3ec | 2016-10-21 09:16:57 -0700 | [diff] [blame] | 545 | if (src.isVirtual()) { |
Tomasz Mikolajewski | c515150 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 546 | try { |
| 547 | srcFileAsAsset = getClient(src).openTypedAssetFileDescriptor( |
Steve McKay | 97b4be4 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 548 | src.derivedUri, mimeType, null, canceller); |
Tomasz Mikolajewski | c515150 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 549 | } catch (FileNotFoundException | RemoteException | RuntimeException e) { |
| 550 | throw new ResourceException("Failed to open a file as asset for %s due to an " |
| 551 | + "exception.", src.derivedUri, e); |
| 552 | } |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 553 | srcFile = srcFileAsAsset.getParcelFileDescriptor(); |
Tomasz Mikolajewski | c515150 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 554 | try { |
| 555 | in = new AssetFileDescriptor.AutoCloseInputStream(srcFileAsAsset); |
| 556 | } catch (IOException e) { |
| 557 | throw new ResourceException("Failed to open a file input stream for %s due " |
| 558 | + "an exception.", src.derivedUri, e); |
| 559 | } |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 560 | } else { |
Tomasz Mikolajewski | c515150 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 561 | try { |
| 562 | srcFile = getClient(src).openFile(src.derivedUri, "r", canceller); |
| 563 | } catch (FileNotFoundException | RemoteException | RuntimeException e) { |
| 564 | throw new ResourceException( |
| 565 | "Failed to open a file for %s due to an exception.", src.derivedUri, e); |
| 566 | } |
Steve McKay | 97b4be4 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 567 | in = new ParcelFileDescriptor.AutoCloseInputStream(srcFile); |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 568 | } |
| 569 | |
Tomasz Mikolajewski | c515150 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 570 | try { |
| 571 | dstFile = getClient(dest).openFile(dest.derivedUri, "w", canceller); |
| 572 | } catch (FileNotFoundException | RemoteException | RuntimeException e) { |
| 573 | throw new ResourceException("Failed to open the destination file %s for writing " |
| 574 | + "due to an exception.", dest.derivedUri, e); |
| 575 | } |
Steve McKay | 97b4be4 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 576 | out = new ParcelFileDescriptor.AutoCloseOutputStream(dstFile); |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 577 | |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 578 | byte[] buffer = new byte[32 * 1024]; |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 579 | int len; |
Tomasz Mikolajewski | c515150 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 580 | try { |
| 581 | while ((len = in.read(buffer)) != -1) { |
| 582 | if (isCanceled()) { |
Steve McKay | 003097d | 2016-02-23 10:06:50 -0800 | [diff] [blame] | 583 | if (DEBUG) Log.d(TAG, "Canceled copy mid-copy of: " + src.derivedUri); |
| 584 | return; |
Tomasz Mikolajewski | c515150 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 585 | } |
| 586 | out.write(buffer, 0, len); |
| 587 | makeCopyProgress(len); |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 588 | } |
Tomasz Mikolajewski | c515150 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 589 | |
Daichi Hirono | 0f3b4bd | 2016-11-16 15:42:42 +0900 | [diff] [blame^] | 590 | // Need to invoke Os#fsync to ensure the file is written to the storage device. |
| 591 | try { |
| 592 | Os.fsync(dstFile.getFileDescriptor()); |
| 593 | } catch (ErrnoException error) { |
| 594 | // fsync will fail with fd of pipes and return EROFS or EINVAL. |
| 595 | if (error.errno != OsConstants.EROFS && error.errno != OsConstants.EINVAL) { |
| 596 | throw new SyncFailedException( |
| 597 | "Failed to sync bytes after copying a file."); |
| 598 | } |
| 599 | } |
| 600 | |
Daichi Hirono | 7551240 | 2016-03-28 16:07:45 +0900 | [diff] [blame] | 601 | // Need to invoke IoUtils.close explicitly to avoid from ignoring errors at flush. |
| 602 | IoUtils.close(dstFile.getFileDescriptor()); |
Tomasz Mikolajewski | c515150 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 603 | srcFile.checkError(); |
| 604 | } catch (IOException e) { |
| 605 | throw new ResourceException( |
| 606 | "Failed to copy bytes from %s to %s due to an IO exception.", |
| 607 | src.derivedUri, dest.derivedUri, e); |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 608 | } |
| 609 | |
Steve McKay | 358c3ec | 2016-10-21 09:16:57 -0700 | [diff] [blame] | 610 | if (src.isVirtual()) { |
Tomasz Mikolajewski | c515150 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 611 | convertedFiles.add(src); |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 612 | } |
Tomasz Mikolajewski | c515150 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 613 | |
| 614 | success = true; |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 615 | } finally { |
Tomasz Mikolajewski | c515150 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 616 | if (!success) { |
| 617 | if (dstFile != null) { |
| 618 | try { |
| 619 | dstFile.closeWithError("Error copying bytes."); |
| 620 | } catch (IOException closeError) { |
| 621 | Log.w(TAG, "Error closing destination.", closeError); |
| 622 | } |
| 623 | } |
| 624 | |
| 625 | if (DEBUG) Log.d(TAG, "Cleaning up failed operation leftovers."); |
| 626 | canceller.cancel(); |
| 627 | try { |
Tomasz Mikolajewski | 8fe5498 | 2016-02-23 15:12:54 +0900 | [diff] [blame] | 628 | deleteDocument(dest, destParent); |
| 629 | } catch (ResourceException e) { |
Tomasz Mikolajewski | c515150 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 630 | Log.w(TAG, "Failed to cleanup after copy error: " + src.derivedUri, e); |
| 631 | } |
| 632 | } |
| 633 | |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 634 | // This also ensures the file descriptors are closed. |
Steve McKay | 97b4be4 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 635 | IoUtils.closeQuietly(in); |
| 636 | IoUtils.closeQuietly(out); |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 637 | } |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | /** |
| 641 | * Calculates the cumulative size of all the documents in the list. Directories are recursed |
| 642 | * into and totaled up. |
| 643 | * |
| 644 | * @param srcs |
| 645 | * @return Size in bytes. |
Tomasz Mikolajewski | c515150 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 646 | * @throws ResourceException |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 647 | */ |
Tomasz Mikolajewski | c515150 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 648 | private long calculateSize(List<DocumentInfo> srcs) throws ResourceException { |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 649 | long result = 0; |
| 650 | |
| 651 | for (DocumentInfo src : srcs) { |
| 652 | if (src.isDirectory()) { |
| 653 | // Directories need to be recursed into. |
Tomasz Mikolajewski | c515150 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 654 | try { |
| 655 | result += calculateFileSizesRecursively(getClient(src), src.derivedUri); |
| 656 | } catch (RemoteException e) { |
| 657 | throw new ResourceException("Failed to obtain the client for %s.", |
Garfield, Tan | c20c6f6 | 2016-07-06 17:24:20 -0700 | [diff] [blame] | 658 | src.derivedUri, e); |
Tomasz Mikolajewski | c515150 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 659 | } |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 660 | } else { |
| 661 | result += src.size; |
| 662 | } |
Garfield, Tan | edce554 | 2016-06-17 15:32:28 -0700 | [diff] [blame] | 663 | |
| 664 | if (isCanceled()) { |
| 665 | return result; |
| 666 | } |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 667 | } |
| 668 | return result; |
| 669 | } |
| 670 | |
| 671 | /** |
| 672 | * Calculates (recursively) the cumulative size of all the files under the given directory. |
| 673 | * |
Tomasz Mikolajewski | c515150 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 674 | * @throws ResourceException |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 675 | */ |
Garfield, Tan | c20c6f6 | 2016-07-06 17:24:20 -0700 | [diff] [blame] | 676 | long calculateFileSizesRecursively( |
Tomasz Mikolajewski | c515150 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 677 | ContentProviderClient client, Uri uri) throws ResourceException { |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 678 | final String authority = uri.getAuthority(); |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 679 | final Uri queryUri = buildChildDocumentsUri(authority, getDocumentId(uri)); |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 680 | final String queryColumns[] = new String[] { |
| 681 | Document.COLUMN_DOCUMENT_ID, |
| 682 | Document.COLUMN_MIME_TYPE, |
| 683 | Document.COLUMN_SIZE |
| 684 | }; |
| 685 | |
| 686 | long result = 0; |
| 687 | Cursor cursor = null; |
| 688 | try { |
| 689 | cursor = client.query(queryUri, queryColumns, null, null, null); |
Garfield, Tan | edce554 | 2016-06-17 15:32:28 -0700 | [diff] [blame] | 690 | while (cursor.moveToNext() && !isCanceled()) { |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 691 | if (Document.MIME_TYPE_DIR.equals( |
| 692 | getCursorString(cursor, Document.COLUMN_MIME_TYPE))) { |
| 693 | // Recurse into directories. |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 694 | final Uri dirUri = buildDocumentUri(authority, |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 695 | getCursorString(cursor, Document.COLUMN_DOCUMENT_ID)); |
| 696 | result += calculateFileSizesRecursively(client, dirUri); |
| 697 | } else { |
| 698 | // This may return -1 if the size isn't defined. Ignore those cases. |
| 699 | long size = getCursorLong(cursor, Document.COLUMN_SIZE); |
| 700 | result += size > 0 ? size : 0; |
| 701 | } |
| 702 | } |
Tomasz Mikolajewski | c515150 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 703 | } catch (RemoteException | RuntimeException e) { |
| 704 | throw new ResourceException( |
| 705 | "Failed to calculate size for %s due to an exception.", uri, e); |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 706 | } finally { |
| 707 | IoUtils.closeQuietly(cursor); |
| 708 | } |
| 709 | |
| 710 | return result; |
| 711 | } |
| 712 | |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 713 | /** |
| 714 | * Returns true if {@code doc} is a descendant of {@code parentDoc}. |
Tomasz Mikolajewski | c515150 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 715 | * @throws ResourceException |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 716 | */ |
Steve McKay | 97b4be4 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 717 | boolean isDescendentOf(DocumentInfo doc, DocumentInfo parent) |
Tomasz Mikolajewski | c515150 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 718 | throws ResourceException { |
Steve McKay | 97b4be4 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 719 | if (parent.isDirectory() && doc.authority.equals(parent.authority)) { |
Tomasz Mikolajewski | c515150 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 720 | try { |
| 721 | return isChildDocument(getClient(doc), doc.derivedUri, parent.derivedUri); |
| 722 | } catch (RemoteException | RuntimeException e) { |
| 723 | throw new ResourceException( |
| 724 | "Failed to check if %s is a child of %s due to an exception.", |
| 725 | doc.derivedUri, parent.derivedUri, e); |
| 726 | } |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 727 | } |
| 728 | return false; |
| 729 | } |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 730 | |
Steve McKay | 97b4be4 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 731 | @Override |
| 732 | public String toString() { |
| 733 | return new StringBuilder() |
| 734 | .append("CopyJob") |
| 735 | .append("{") |
| 736 | .append("id=" + id) |
Garfield, Tan | 4833477 | 2016-06-28 17:17:38 -0700 | [diff] [blame] | 737 | .append(", docs=" + srcs) |
Steve McKay | 97b4be4 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 738 | .append(", destination=" + stack) |
| 739 | .append("}") |
| 740 | .toString(); |
| 741 | } |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 742 | } |