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 | |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 19 | import static com.android.documentsui.services.FileOperationService.OPERATION_MOVE; |
| 20 | |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 21 | import android.app.Notification; |
| 22 | import android.app.Notification.Builder; |
| 23 | import android.content.Context; |
| 24 | import android.os.RemoteException; |
| 25 | import android.provider.DocumentsContract; |
| 26 | import android.provider.DocumentsContract.Document; |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 27 | |
| 28 | import com.android.documentsui.R; |
| 29 | import com.android.documentsui.model.DocumentInfo; |
| 30 | import com.android.documentsui.model.DocumentStack; |
| 31 | |
| 32 | import java.util.List; |
| 33 | |
Tomasz Mikolajewski | b8436af | 2016-01-25 16:20:15 +0900 | [diff] [blame] | 34 | // TODO: Stop extending CopyJob. |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 35 | final class MoveJob extends CopyJob { |
| 36 | |
Tomasz Mikolajewski | b8436af | 2016-01-25 16:20:15 +0900 | [diff] [blame] | 37 | final DocumentInfo mSrcParent; |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 38 | |
| 39 | /** |
| 40 | * Moves files to a destination identified by {@code destination}. |
| 41 | * Performs most work by delegating to CopyJob, then deleting |
| 42 | * a file after it has been copied. |
| 43 | * |
| 44 | * @see @link {@link Job} constructor for most param descriptions. |
| 45 | * |
| 46 | * @param srcs List of files to be moved. |
Tomasz Mikolajewski | b8436af | 2016-01-25 16:20:15 +0900 | [diff] [blame] | 47 | * @param srcParent Parent of all source files. |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 48 | */ |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 49 | MoveJob(Context service, Context appContext, Listener listener, |
Tomasz Mikolajewski | b8436af | 2016-01-25 16:20:15 +0900 | [diff] [blame] | 50 | String id, DocumentStack destination, List<DocumentInfo> srcs, DocumentInfo srcParent) { |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 51 | super(service, appContext, listener, OPERATION_MOVE, id, destination, srcs); |
Tomasz Mikolajewski | b8436af | 2016-01-25 16:20:15 +0900 | [diff] [blame] | 52 | this.mSrcParent = srcParent; |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | @Override |
| 56 | Builder createProgressBuilder() { |
| 57 | return super.createProgressBuilder( |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 58 | service.getString(R.string.move_notification_title), |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 59 | R.drawable.ic_menu_copy, |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 60 | service.getString(android.R.string.cancel), |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 61 | R.drawable.ic_cab_cancel); |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public Notification getSetupNotification() { |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 66 | return getSetupNotification(service.getString(R.string.move_preparing)); |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public Notification getProgressNotification() { |
| 71 | return getProgressNotification(R.string.copy_preparing); |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | Notification getFailureNotification() { |
| 76 | return getFailureNotification( |
| 77 | R.plurals.move_error_notification_title, R.drawable.ic_menu_copy); |
| 78 | } |
| 79 | |
Tomasz Mikolajewski | 0fa97e8 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 80 | void processDocument(DocumentInfo src, DocumentInfo srcParent, DocumentInfo dest) |
| 81 | throws ResourceException { |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 82 | |
Tomasz Mikolajewski | 6704808 | 2016-01-21 10:00:33 +0900 | [diff] [blame] | 83 | // TODO: When optimized move kicks in, we're not making any progress updates. FIX IT! |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 84 | |
Tomasz Mikolajewski | 6704808 | 2016-01-21 10:00:33 +0900 | [diff] [blame] | 85 | // When moving within the same provider, try to use optimized moving. |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 86 | // If not supported, then fallback to byte-by-byte copy/move. |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 87 | if (src.authority.equals(dest.authority)) { |
| 88 | if ((src.flags & Document.FLAG_SUPPORTS_MOVE) != 0) { |
Tomasz Mikolajewski | 0fa97e8 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 89 | try { |
| 90 | if (DocumentsContract.moveDocument(getClient(src), src.derivedUri, |
| 91 | srcParent != null ? srcParent.derivedUri : mSrcParent.derivedUri, |
| 92 | dest.derivedUri) == null) { |
| 93 | throw new ResourceException("Provider side move failed for document %s.", |
| 94 | src.derivedUri); |
| 95 | } |
| 96 | } catch (RuntimeException | RemoteException e) { |
| 97 | throw new ResourceException( |
| 98 | "Provider side move failed for document %s due to an exception.", |
| 99 | src.derivedUri, e); |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 100 | } |
Tomasz Mikolajewski | 0fa97e8 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 101 | return; |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | |
Tomasz Mikolajewski | 87156dc | 2016-01-21 12:49:24 +0900 | [diff] [blame] | 105 | // Moving virtual files by bytes is not supported. This is because, it would involve |
| 106 | // conversion, and the source file should not be deleted in such case (as it's a different |
| 107 | // file). |
| 108 | if (src.isVirtualDocument()) { |
Tomasz Mikolajewski | 0fa97e8 | 2016-02-18 16:45:44 +0900 | [diff] [blame] | 109 | throw new ResourceException("Cannot move virtual file %s byte by byte.", |
| 110 | src.derivedUri); |
Tomasz Mikolajewski | 87156dc | 2016-01-21 12:49:24 +0900 | [diff] [blame] | 111 | } |
| 112 | |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 113 | // 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] | 114 | byteCopyDocument(src, dest); |
Tomasz Mikolajewski | db87543 | 2016-02-23 15:12:54 +0900 | [diff] [blame] | 115 | |
| 116 | // Remove the source document. |
| 117 | deleteDocument(src, srcParent); |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 118 | } |
| 119 | |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 120 | @Override |
| 121 | public String toString() { |
| 122 | return new StringBuilder() |
| 123 | .append("MoveJob") |
| 124 | .append("{") |
| 125 | .append("id=" + id) |
Tomasz Mikolajewski | b8436af | 2016-01-25 16:20:15 +0900 | [diff] [blame] | 126 | .append(", srcs=" + mSrcs) |
| 127 | .append(", srcParent=" + mSrcParent) |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 128 | .append(", destination=" + stack) |
| 129 | .append("}") |
| 130 | .toString(); |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 131 | } |
| 132 | } |