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; |
Garfield, Tan | 4a7aba2 | 2016-06-09 12:04:22 -0700 | [diff] [blame] | 24 | |
Tomasz Mikolajewski | 748ea8c | 2016-01-22 16:22:51 +0900 | [diff] [blame] | 25 | import static com.android.documentsui.OperationDialogFragment.DIALOG_TYPE_CONVERTED; |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 26 | import static com.android.documentsui.Shared.DEBUG; |
| 27 | import static com.android.documentsui.model.DocumentInfo.getCursorLong; |
| 28 | import static com.android.documentsui.model.DocumentInfo.getCursorString; |
Tomasz Mikolajewski | 748ea8c | 2016-01-22 16:22:51 +0900 | [diff] [blame] | 29 | import static com.android.documentsui.services.FileOperationService.EXTRA_DIALOG_TYPE; |
Garfield, Tan | 05df8ec | 2016-06-28 17:17:38 -0700 | [diff] [blame] | 30 | import static com.android.documentsui.services.FileOperationService.EXTRA_OPERATION_TYPE; |
Tomasz Mikolajewski | 748ea8c | 2016-01-22 16:22:51 +0900 | [diff] [blame] | 31 | import static com.android.documentsui.services.FileOperationService.EXTRA_SRC_LIST; |
Garfield, Tan | 05df8ec | 2016-06-28 17:17:38 -0700 | [diff] [blame] | 32 | import static com.android.documentsui.services.FileOperationService.OPERATION_COPY; |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 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; |
Garfield, Tan | f46958b | 2016-06-17 15:32:28 -0700 | [diff] [blame] | 39 | import android.content.ContentResolver; |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 40 | import android.content.Context; |
Tomasz Mikolajewski | 748ea8c | 2016-01-22 16:22:51 +0900 | [diff] [blame] | 41 | import android.content.Intent; |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 42 | import android.content.res.AssetFileDescriptor; |
| 43 | import android.database.Cursor; |
| 44 | import android.net.Uri; |
| 45 | import android.os.CancellationSignal; |
| 46 | import android.os.ParcelFileDescriptor; |
| 47 | import android.os.RemoteException; |
| 48 | import android.provider.DocumentsContract; |
| 49 | import android.provider.DocumentsContract.Document; |
| 50 | import android.text.format.DateUtils; |
| 51 | import android.util.Log; |
| 52 | import android.webkit.MimeTypeMap; |
| 53 | |
Garfield, Tan | 71ac8a0 | 2016-07-06 17:24:20 -0700 | [diff] [blame^] | 54 | import com.android.documentsui.DocumentsApplication; |
Ben Kwa | d5b2af1 | 2016-01-28 16:39:57 -0800 | [diff] [blame] | 55 | import com.android.documentsui.Metrics; |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 56 | import com.android.documentsui.R; |
Garfield, Tan | 71ac8a0 | 2016-07-06 17:24:20 -0700 | [diff] [blame^] | 57 | import com.android.documentsui.RootsCache; |
| 58 | import com.android.documentsui.UrisSupplier; |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 59 | import com.android.documentsui.model.DocumentInfo; |
| 60 | import com.android.documentsui.model.DocumentStack; |
Garfield, Tan | f46958b | 2016-06-17 15:32:28 -0700 | [diff] [blame] | 61 | import com.android.documentsui.model.RootInfo; |
Garfield, Tan | 05df8ec | 2016-06-28 17:17:38 -0700 | [diff] [blame] | 62 | import com.android.documentsui.services.FileOperationService.OpType; |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 63 | |
| 64 | import libcore.io.IoUtils; |
| 65 | |
| 66 | import java.io.FileNotFoundException; |
| 67 | import java.io.IOException; |
| 68 | import java.io.InputStream; |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 69 | import java.text.NumberFormat; |
Tomasz Mikolajewski | 748ea8c | 2016-01-22 16:22:51 +0900 | [diff] [blame] | 70 | import java.util.ArrayList; |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 71 | import java.util.List; |
| 72 | |
| 73 | class CopyJob extends Job { |
Steve McKay | 7a3b811 | 2016-02-23 10:06:50 -0800 | [diff] [blame] | 74 | |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 75 | private static final String TAG = "CopyJob"; |
Steve McKay | 7a3b811 | 2016-02-23 10:06:50 -0800 | [diff] [blame] | 76 | |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 77 | final List<DocumentInfo> mSrcs; |
Tomasz Mikolajewski | 748ea8c | 2016-01-22 16:22:51 +0900 | [diff] [blame] | 78 | final ArrayList<DocumentInfo> convertedFiles = new ArrayList<>(); |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 79 | |
| 80 | private long mStartTime = -1; |
Steve McKay | 7a3b811 | 2016-02-23 10:06:50 -0800 | [diff] [blame] | 81 | |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 82 | private long mBatchSize; |
Garfield, Tan | 4a7aba2 | 2016-06-09 12:04:22 -0700 | [diff] [blame] | 83 | private volatile long mBytesCopied; |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 84 | // Speed estimation |
| 85 | private long mBytesCopiedSample; |
| 86 | private long mSampleTime; |
| 87 | private long mSpeed; |
| 88 | private long mRemainingTime; |
| 89 | |
| 90 | /** |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 91 | * @see @link {@link Job} constructor for most param descriptions. |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 92 | */ |
Garfield, Tan | 05df8ec | 2016-06-28 17:17:38 -0700 | [diff] [blame] | 93 | CopyJob(Context service, Listener listener, String id, DocumentStack destination, |
| 94 | UrisSupplier srcs) { |
| 95 | this(service, listener, id, OPERATION_COPY, destination, srcs); |
| 96 | } |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 97 | |
Garfield, Tan | 05df8ec | 2016-06-28 17:17:38 -0700 | [diff] [blame] | 98 | 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 McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 103 | |
Garfield, Tan | f46958b | 2016-06-17 15:32:28 -0700 | [diff] [blame] | 104 | // delay the initialization of it to setUp() because it may be IO extensive. |
Garfield, Tan | 05df8ec | 2016-06-28 17:17:38 -0700 | [diff] [blame] | 105 | mSrcs = new ArrayList<>(srcs.getItemCount()); |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | @Override |
| 109 | Builder createProgressBuilder() { |
| 110 | return super.createProgressBuilder( |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 111 | service.getString(R.string.copy_notification_title), |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 112 | R.drawable.ic_menu_copy, |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 113 | service.getString(android.R.string.cancel), |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 114 | R.drawable.ic_cab_cancel); |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public Notification getSetupNotification() { |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 119 | return getSetupNotification(service.getString(R.string.copy_preparing)); |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 120 | } |
| 121 | |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 122 | Notification getProgressNotification(@StringRes int msgId) { |
Garfield, Tan | 4a7aba2 | 2016-06-09 12:04:22 -0700 | [diff] [blame] | 123 | updateRemainingTimeEstimate(); |
| 124 | |
Tomasz Mikolajewski | 0fa97e8 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 125 | if (mBatchSize >= 0) { |
| 126 | double completed = (double) this.mBytesCopied / mBatchSize; |
| 127 | mProgressBuilder.setProgress(100, (int) (completed * 100), false); |
Garfield, Tan | 4a7aba2 | 2016-06-09 12:04:22 -0700 | [diff] [blame] | 128 | mProgressBuilder.setSubText( |
Tomasz Mikolajewski | 0fa97e8 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 129 | 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 McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 138 | if (mRemainingTime > 0) { |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 139 | mProgressBuilder.setContentText(service.getString(msgId, |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 140 | DateUtils.formatDuration(mRemainingTime))); |
| 141 | } else { |
| 142 | mProgressBuilder.setContentText(null); |
| 143 | } |
| 144 | |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 145 | return mProgressBuilder.build(); |
| 146 | } |
| 147 | |
Garfield, Tan | 4a7aba2 | 2016-06-09 12:04:22 -0700 | [diff] [blame] | 148 | @Override |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 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 | */ |
Garfield, Tan | 4a7aba2 | 2016-06-09 12:04:22 -0700 | [diff] [blame] | 160 | private void updateRemainingTimeEstimate() { |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 161 | long elapsedTime = elapsedRealtime() - mStartTime; |
| 162 | |
Garfield, Tan | 4a7aba2 | 2016-06-09 12:04:22 -0700 | [diff] [blame] | 163 | // 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, Tan | f46958b | 2016-06-17 15:32:28 -0700 | [diff] [blame] | 166 | final long sampleDuration = Math.max(elapsedTime - mSampleTime, 1L); // avoid dividing 0 |
Garfield, Tan | 4a7aba2 | 2016-06-09 12:04:22 -0700 | [diff] [blame] | 167 | final long sampleSpeed = ((bytesCopied - mBytesCopiedSample) * 1000) / sampleDuration; |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 168 | if (mSpeed == 0) { |
| 169 | mSpeed = sampleSpeed; |
| 170 | } else { |
| 171 | mSpeed = ((3 * mSpeed) + sampleSpeed) / 4; |
| 172 | } |
| 173 | |
| 174 | if (mSampleTime > 0 && mSpeed > 0) { |
Garfield, Tan | 4a7aba2 | 2016-06-09 12:04:22 -0700 | [diff] [blame] | 175 | mRemainingTime = ((mBatchSize - bytesCopied) * 1000) / mSpeed; |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 176 | } else { |
| 177 | mRemainingTime = 0; |
| 178 | } |
| 179 | |
| 180 | mSampleTime = elapsedTime; |
Garfield, Tan | 4a7aba2 | 2016-06-09 12:04:22 -0700 | [diff] [blame] | 181 | mBytesCopiedSample = bytesCopied; |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 182 | } |
| 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 Mikolajewski | 748ea8c | 2016-01-22 16:22:51 +0900 | [diff] [blame] | 191 | Notification getWarningNotification() { |
Tomasz Mikolajewski | cd27015 | 2016-02-01 12:01:14 +0900 | [diff] [blame] | 192 | final Intent navigateIntent = buildNavigateIntent(INTENT_TAG_WARNING); |
Tomasz Mikolajewski | 748ea8c | 2016-01-22 16:22:51 +0900 | [diff] [blame] | 193 | navigateIntent.putExtra(EXTRA_DIALOG_TYPE, DIALOG_TYPE_CONVERTED); |
Garfield, Tan | 05df8ec | 2016-06-28 17:17:38 -0700 | [diff] [blame] | 194 | navigateIntent.putExtra(EXTRA_OPERATION_TYPE, operationType); |
Tomasz Mikolajewski | 748ea8c | 2016-01-22 16:22:51 +0900 | [diff] [blame] | 195 | |
| 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, Tan | f46958b | 2016-06-17 15:32:28 -0700 | [diff] [blame] | 214 | boolean setUp() { |
Garfield, Tan | f46958b | 2016-06-17 15:32:28 -0700 | [diff] [blame] | 215 | 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, Tan | 71ac8a0 | 2016-07-06 17:24:20 -0700 | [diff] [blame^] | 222 | // Check if user has canceled this task. |
Garfield, Tan | f46958b | 2016-06-17 15:32:28 -0700 | [diff] [blame] | 223 | if (isCanceled()) { |
| 224 | return false; |
| 225 | } |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 226 | |
Tomasz Mikolajewski | 0fa97e8 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 227 | try { |
| 228 | mBatchSize = calculateSize(mSrcs); |
| 229 | } catch (ResourceException e) { |
Steve McKay | 56d950c | 2016-06-14 15:56:50 -0700 | [diff] [blame] | 230 | Log.w(TAG, "Failed to calculate total size. Copying without progress.", e); |
Tomasz Mikolajewski | 0fa97e8 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 231 | mBatchSize = -1; |
| 232 | } |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 233 | |
Garfield, Tan | 71ac8a0 | 2016-07-06 17:24:20 -0700 | [diff] [blame^] | 234 | // 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, Tan | f46958b | 2016-06-17 15:32:28 -0700 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | @Override |
| 246 | void start() { |
| 247 | mStartTime = elapsedRealtime(); |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 248 | DocumentInfo srcInfo; |
Ben Kwa | d5b2af1 | 2016-01-28 16:39:57 -0800 | [diff] [blame] | 249 | DocumentInfo dstInfo = stack.peek(); |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 250 | for (int i = 0; i < mSrcs.size() && !isCanceled(); ++i) { |
| 251 | srcInfo = mSrcs.get(i); |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 252 | |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 253 | if (DEBUG) Log.d(TAG, |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 254 | "Copying " + srcInfo.displayName + " (" + srcInfo.derivedUri + ")" |
| 255 | + " to " + dstInfo.displayName + " (" + dstInfo.derivedUri + ")"); |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 256 | |
Tomasz Mikolajewski | 0fa97e8 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 257 | try { |
Steve McKay | 56d950c | 2016-06-14 15:56:50 -0700 | [diff] [blame] | 258 | 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 Mikolajewski | 0fa97e8 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 264 | } catch (ResourceException e) { |
Steve McKay | 56d950c | 2016-06-14 15:56:50 -0700 | [diff] [blame] | 265 | Log.e(TAG, "Failed to copy " + srcInfo.derivedUri, e); |
Tomasz Mikolajewski | 0fa97e8 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 266 | onFileFailed(srcInfo); |
| 267 | } |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 268 | } |
Ben Kwa | d5b2af1 | 2016-01-28 16:39:57 -0800 | [diff] [blame] | 269 | Metrics.logFileOperation(service, operationType, mSrcs, dstInfo); |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 270 | } |
| 271 | |
Garfield, Tan | f46958b | 2016-06-17 15:32:28 -0700 | [diff] [blame] | 272 | private void buildDocumentList() throws ResourceException { |
| 273 | try { |
| 274 | final ContentResolver resolver = appContext.getContentResolver(); |
Garfield, Tan | 05df8ec | 2016-06-28 17:17:38 -0700 | [diff] [blame] | 275 | final Iterable<Uri> uris = srcs.getDocs(appContext); |
Garfield, Tan | f46958b | 2016-06-17 15:32:28 -0700 | [diff] [blame] | 276 | 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, Tan | 05df8ec | 2016-06-28 17:17:38 -0700 | [diff] [blame] | 289 | failedFileCount += srcs.getItemCount(); |
Garfield, Tan | f46958b | 2016-06-17 15:32:28 -0700 | [diff] [blame] | 290 | 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, Tan | 71ac8a0 | 2016-07-06 17:24:20 -0700 | [diff] [blame^] | 299 | /** |
| 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 Mikolajewski | 748ea8c | 2016-01-22 16:22:51 +0900 | [diff] [blame] | 337 | @Override |
| 338 | boolean hasWarnings() { |
| 339 | return !convertedFiles.isEmpty(); |
| 340 | } |
| 341 | |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 342 | /** |
| 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 McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | /** |
| 352 | * Copies a the given document to the given location. |
| 353 | * |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 354 | * @param src DocumentInfos for the documents to copy. |
Tomasz Mikolajewski | b8436af | 2016-01-25 16:20:15 +0900 | [diff] [blame] | 355 | * @param srcParent DocumentInfo for the parent of the document to process. |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 356 | * @param dstDirInfo The destination directory. |
Tomasz Mikolajewski | 0fa97e8 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 357 | * @throws ResourceException |
Tomasz Mikolajewski | b8436af | 2016-01-25 16:20:15 +0900 | [diff] [blame] | 358 | * |
| 359 | * 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] | 360 | */ |
Tomasz Mikolajewski | 0fa97e8 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 361 | void processDocument(DocumentInfo src, DocumentInfo srcParent, |
| 362 | DocumentInfo dstDirInfo) throws ResourceException { |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 363 | |
| 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 Mikolajewski | 6704808 | 2016-01-21 10:00:33 +0900 | [diff] [blame] | 367 | // When copying within the same provider, try to use optimized copying. |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 368 | // If not supported, then fallback to byte-by-byte copy/move. |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 369 | if (src.authority.equals(dstDirInfo.authority)) { |
| 370 | if ((src.flags & Document.FLAG_SUPPORTS_COPY) != 0) { |
Tomasz Mikolajewski | 0fa97e8 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 371 | try { |
| 372 | if (DocumentsContract.copyDocument(getClient(src), src.derivedUri, |
Tomasz Mikolajewski | 1008a11 | 2016-03-02 17:41:40 +0900 | [diff] [blame] | 373 | dstDirInfo.derivedUri) != null) { |
| 374 | return; |
Tomasz Mikolajewski | 0fa97e8 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 375 | } |
Tomasz Mikolajewski | 0fa97e8 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 376 | } catch (RemoteException | RuntimeException e) { |
Tomasz Mikolajewski | 1008a11 | 2016-03-02 17:41:40 +0900 | [diff] [blame] | 377 | Log.e(TAG, "Provider side copy failed for: " + src.derivedUri |
Steve McKay | 56d950c | 2016-06-14 15:56:50 -0700 | [diff] [blame] | 378 | + " due to an exception.", e); |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 379 | } |
Garfield, Tan | 4a7aba2 | 2016-06-09 12:04:22 -0700 | [diff] [blame] | 380 | |
Tomasz Mikolajewski | 1008a11 | 2016-03-02 17:41:40 +0900 | [diff] [blame] | 381 | // 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 McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 383 | } |
| 384 | } |
| 385 | |
| 386 | // If we couldn't do an optimized copy...we fall back to vanilla byte copy. |
Tomasz Mikolajewski | 0fa97e8 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 387 | byteCopyDocument(src, dstDirInfo); |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 388 | } |
| 389 | |
Tomasz Mikolajewski | 0fa97e8 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 390 | void byteCopyDocument(DocumentInfo src, DocumentInfo dest) throws ResourceException { |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 391 | final String dstMimeType; |
| 392 | final String dstDisplayName; |
| 393 | |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 394 | if (DEBUG) Log.d(TAG, "Doing byte copy of document: " + src); |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 395 | // 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 McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 397 | if (src.isVirtualDocument()) { |
Tomasz Mikolajewski | 0fa97e8 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 398 | 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 McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 406 | if (streamTypes != null && streamTypes.length > 0) { |
| 407 | dstMimeType = streamTypes[0]; |
| 408 | final String extension = MimeTypeMap.getSingleton(). |
| 409 | getExtensionFromMimeType(dstMimeType); |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 410 | dstDisplayName = src.displayName + |
| 411 | (extension != null ? "." + extension : src.displayName); |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 412 | } else { |
Tomasz Mikolajewski | 0fa97e8 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 413 | throw new ResourceException("Cannot copy virtual file %s. No streamable formats " |
| 414 | + "available.", src.derivedUri); |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 415 | } |
| 416 | } else { |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 417 | dstMimeType = src.mimeType; |
| 418 | dstDisplayName = src.displayName; |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | // Create the target document (either a file or a directory), then copy recursively the |
| 422 | // contents (bytes or children). |
Tomasz Mikolajewski | 0fa97e8 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 423 | 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 McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 432 | if (dstUri == null) { |
| 433 | // If this is a directory, the entire subdir will not be copied over. |
Tomasz Mikolajewski | 0fa97e8 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 434 | throw new ResourceException( |
| 435 | "Couldn't create destination document " + dstDisplayName + " in directory %s.", |
| 436 | dest.derivedUri); |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | DocumentInfo dstInfo = null; |
| 440 | try { |
| 441 | dstInfo = DocumentInfo.fromUri(getContentResolver(), dstUri); |
Tomasz Mikolajewski | 0fa97e8 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 442 | } catch (FileNotFoundException | RuntimeException e) { |
| 443 | throw new ResourceException("Could not load DocumentInfo for newly created file %s.", |
| 444 | dstUri); |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 445 | } |
| 446 | |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 447 | if (Document.MIME_TYPE_DIR.equals(src.mimeType)) { |
Tomasz Mikolajewski | 0fa97e8 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 448 | copyDirectoryHelper(src, dstInfo); |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 449 | } else { |
Tomasz Mikolajewski | db87543 | 2016-02-23 15:12:54 +0900 | [diff] [blame] | 450 | copyFileHelper(src, dstInfo, dest, dstMimeType); |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 451 | } |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 452 | } |
| 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 McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 458 | * @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] | 459 | * contents, not the directory itself. |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 460 | * @param destDir Info of the directory to copy to. Must be created beforehand. |
Tomasz Mikolajewski | 0fa97e8 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 461 | * @throws ResourceException |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 462 | */ |
Tomasz Mikolajewski | 0fa97e8 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 463 | private void copyDirectoryHelper(DocumentInfo srcDir, DocumentInfo destDir) |
| 464 | throws ResourceException { |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 465 | // 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 Mikolajewski | 0fa97e8 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 475 | // Iterate over srcs in the directory; copy to the destination directory. |
| 476 | final Uri queryUri = buildChildDocumentsUri(srcDir.authority, srcDir.documentId); |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 477 | try { |
Tomasz Mikolajewski | 0fa97e8 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 478 | 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 McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 483 | } |
Tomasz Mikolajewski | 0fa97e8 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 484 | |
| 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, Tan | 4a7aba2 | 2016-06-09 12:04:22 -0700 | [diff] [blame] | 491 | Log.e(TAG, String.format( |
| 492 | "Failed to recursively process a file %s due to an exception.", |
| 493 | srcDir.derivedUri.toString()), e); |
Tomasz Mikolajewski | 0fa97e8 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 494 | success = false; |
| 495 | } |
| 496 | } |
| 497 | } catch (RuntimeException e) { |
Garfield, Tan | 4a7aba2 | 2016-06-09 12:04:22 -0700 | [diff] [blame] | 498 | Log.e(TAG, String.format( |
| 499 | "Failed to copy a file %s to %s. ", |
| 500 | srcDir.derivedUri.toString(), destDir.derivedUri.toString()), e); |
Tomasz Mikolajewski | 0fa97e8 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 501 | success = false; |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 502 | } finally { |
| 503 | IoUtils.closeQuietly(cursor); |
| 504 | } |
| 505 | |
Tomasz Mikolajewski | 0fa97e8 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 506 | if (!success) { |
| 507 | throw new RuntimeException("Some files failed to copy during a recursive " |
| 508 | + "directory copy."); |
| 509 | } |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | /** |
| 513 | * Handles copying a single file. |
| 514 | * |
Tomasz Mikolajewski | db87543 | 2016-02-23 15:12:54 +0900 | [diff] [blame] | 515 | * @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 McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 518 | * @param mimeType Mime type for the target. Can be different than source for virtual files. |
Tomasz Mikolajewski | 0fa97e8 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 519 | * @throws ResourceException |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 520 | */ |
Tomasz Mikolajewski | db87543 | 2016-02-23 15:12:54 +0900 | [diff] [blame] | 521 | private void copyFileHelper(DocumentInfo src, DocumentInfo dest, DocumentInfo destParent, |
| 522 | String mimeType) throws ResourceException { |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 523 | CancellationSignal canceller = new CancellationSignal(); |
Tomasz Mikolajewski | 0fa97e8 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 524 | AssetFileDescriptor srcFileAsAsset = null; |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 525 | ParcelFileDescriptor srcFile = null; |
| 526 | ParcelFileDescriptor dstFile = null; |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 527 | InputStream in = null; |
Daichi Hirono | f4e7fa8 | 2016-03-28 16:07:45 +0900 | [diff] [blame] | 528 | ParcelFileDescriptor.AutoCloseOutputStream out = null; |
Tomasz Mikolajewski | 0fa97e8 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 529 | boolean success = false; |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 530 | |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 531 | try { |
| 532 | // If the file is virtual, but can be converted to another format, then try to copy it |
| 533 | // as such format. |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 534 | if (src.isVirtualDocument()) { |
Tomasz Mikolajewski | 0fa97e8 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 535 | try { |
| 536 | srcFileAsAsset = getClient(src).openTypedAssetFileDescriptor( |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 537 | src.derivedUri, mimeType, null, canceller); |
Tomasz Mikolajewski | 0fa97e8 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 538 | } 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 McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 542 | srcFile = srcFileAsAsset.getParcelFileDescriptor(); |
Tomasz Mikolajewski | 0fa97e8 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 543 | 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 McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 549 | } else { |
Tomasz Mikolajewski | 0fa97e8 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 550 | 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 McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 556 | in = new ParcelFileDescriptor.AutoCloseInputStream(srcFile); |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 557 | } |
| 558 | |
Tomasz Mikolajewski | 0fa97e8 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 559 | 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 McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 565 | out = new ParcelFileDescriptor.AutoCloseOutputStream(dstFile); |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 566 | |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 567 | byte[] buffer = new byte[32 * 1024]; |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 568 | int len; |
Tomasz Mikolajewski | 0fa97e8 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 569 | try { |
| 570 | while ((len = in.read(buffer)) != -1) { |
| 571 | if (isCanceled()) { |
Steve McKay | 7a3b811 | 2016-02-23 10:06:50 -0800 | [diff] [blame] | 572 | if (DEBUG) Log.d(TAG, "Canceled copy mid-copy of: " + src.derivedUri); |
| 573 | return; |
Tomasz Mikolajewski | 0fa97e8 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 574 | } |
| 575 | out.write(buffer, 0, len); |
| 576 | makeCopyProgress(len); |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 577 | } |
Tomasz Mikolajewski | 0fa97e8 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 578 | |
Daichi Hirono | f4e7fa8 | 2016-03-28 16:07:45 +0900 | [diff] [blame] | 579 | // Need to invoke IoUtils.close explicitly to avoid from ignoring errors at flush. |
| 580 | IoUtils.close(dstFile.getFileDescriptor()); |
Tomasz Mikolajewski | 0fa97e8 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 581 | 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 McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 586 | } |
| 587 | |
Tomasz Mikolajewski | 0fa97e8 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 588 | if (src.isVirtualDocument()) { |
| 589 | convertedFiles.add(src); |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 590 | } |
Tomasz Mikolajewski | 0fa97e8 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 591 | |
| 592 | success = true; |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 593 | } finally { |
Tomasz Mikolajewski | 0fa97e8 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 594 | 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 Mikolajewski | db87543 | 2016-02-23 15:12:54 +0900 | [diff] [blame] | 606 | deleteDocument(dest, destParent); |
| 607 | } catch (ResourceException e) { |
Tomasz Mikolajewski | 0fa97e8 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 608 | Log.w(TAG, "Failed to cleanup after copy error: " + src.derivedUri, e); |
| 609 | } |
| 610 | } |
| 611 | |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 612 | // This also ensures the file descriptors are closed. |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 613 | IoUtils.closeQuietly(in); |
| 614 | IoUtils.closeQuietly(out); |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 615 | } |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 616 | } |
| 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 Mikolajewski | 0fa97e8 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 624 | * @throws ResourceException |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 625 | */ |
Tomasz Mikolajewski | 0fa97e8 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 626 | private long calculateSize(List<DocumentInfo> srcs) throws ResourceException { |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 627 | long result = 0; |
| 628 | |
| 629 | for (DocumentInfo src : srcs) { |
| 630 | if (src.isDirectory()) { |
| 631 | // Directories need to be recursed into. |
Tomasz Mikolajewski | 0fa97e8 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 632 | try { |
| 633 | result += calculateFileSizesRecursively(getClient(src), src.derivedUri); |
| 634 | } catch (RemoteException e) { |
| 635 | throw new ResourceException("Failed to obtain the client for %s.", |
Garfield, Tan | 71ac8a0 | 2016-07-06 17:24:20 -0700 | [diff] [blame^] | 636 | src.derivedUri, e); |
Tomasz Mikolajewski | 0fa97e8 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 637 | } |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 638 | } else { |
| 639 | result += src.size; |
| 640 | } |
Garfield, Tan | f46958b | 2016-06-17 15:32:28 -0700 | [diff] [blame] | 641 | |
| 642 | if (isCanceled()) { |
| 643 | return result; |
| 644 | } |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 645 | } |
| 646 | return result; |
| 647 | } |
| 648 | |
| 649 | /** |
| 650 | * Calculates (recursively) the cumulative size of all the files under the given directory. |
| 651 | * |
Tomasz Mikolajewski | 0fa97e8 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 652 | * @throws ResourceException |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 653 | */ |
Garfield, Tan | 71ac8a0 | 2016-07-06 17:24:20 -0700 | [diff] [blame^] | 654 | long calculateFileSizesRecursively( |
Tomasz Mikolajewski | 0fa97e8 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 655 | ContentProviderClient client, Uri uri) throws ResourceException { |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 656 | final String authority = uri.getAuthority(); |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 657 | final Uri queryUri = buildChildDocumentsUri(authority, getDocumentId(uri)); |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 658 | 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, Tan | f46958b | 2016-06-17 15:32:28 -0700 | [diff] [blame] | 668 | while (cursor.moveToNext() && !isCanceled()) { |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 669 | if (Document.MIME_TYPE_DIR.equals( |
| 670 | getCursorString(cursor, Document.COLUMN_MIME_TYPE))) { |
| 671 | // Recurse into directories. |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 672 | final Uri dirUri = buildDocumentUri(authority, |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 673 | 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 Mikolajewski | 0fa97e8 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 681 | } catch (RemoteException | RuntimeException e) { |
| 682 | throw new ResourceException( |
| 683 | "Failed to calculate size for %s due to an exception.", uri, e); |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 684 | } finally { |
| 685 | IoUtils.closeQuietly(cursor); |
| 686 | } |
| 687 | |
| 688 | return result; |
| 689 | } |
| 690 | |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 691 | /** |
| 692 | * Returns true if {@code doc} is a descendant of {@code parentDoc}. |
Tomasz Mikolajewski | 0fa97e8 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 693 | * @throws ResourceException |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 694 | */ |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 695 | boolean isDescendentOf(DocumentInfo doc, DocumentInfo parent) |
Tomasz Mikolajewski | 0fa97e8 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 696 | throws ResourceException { |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 697 | if (parent.isDirectory() && doc.authority.equals(parent.authority)) { |
Tomasz Mikolajewski | 0fa97e8 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 698 | 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 McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 705 | } |
| 706 | return false; |
| 707 | } |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 708 | |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 709 | @Override |
| 710 | public String toString() { |
| 711 | return new StringBuilder() |
| 712 | .append("CopyJob") |
| 713 | .append("{") |
| 714 | .append("id=" + id) |
Garfield, Tan | 05df8ec | 2016-06-28 17:17:38 -0700 | [diff] [blame] | 715 | .append(", docs=" + srcs) |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 716 | .append(", destination=" + stack) |
| 717 | .append("}") |
| 718 | .toString(); |
| 719 | } |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 720 | } |