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