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.content.ContentResolver.wrap;
20 
21 import static com.android.documentsui.base.SharedMinimal.DEBUG;
22 import static com.android.documentsui.services.FileOperationService.OPERATION_MOVE;
23 
24 import android.app.Notification;
25 import android.app.Notification.Builder;
26 import android.content.Context;
27 import android.net.Uri;
28 import android.os.DeadObjectException;
29 import android.os.Messenger;
30 import android.os.RemoteException;
31 import android.provider.DocumentsContract;
32 import android.provider.DocumentsContract.Document;
33 import android.util.Log;
34 
35 import com.android.documentsui.MetricConsts;
36 import com.android.documentsui.Metrics;
37 import com.android.documentsui.R;
38 import com.android.documentsui.base.DocumentInfo;
39 import com.android.documentsui.base.DocumentStack;
40 import com.android.documentsui.base.Features;
41 import com.android.documentsui.base.UserId;
42 import com.android.documentsui.clipping.UrisSupplier;
43 
44 import java.io.FileNotFoundException;
45 
46 import javax.annotation.Nullable;
47 
48 // TODO: Stop extending CopyJob.
49 final class MoveJob extends CopyJob {
50 
51     private static final String TAG = "MoveJob";
52 
53     private final @Nullable Uri mSrcParentUri;
54 
55     // mSrcParent may be populated during setup.
56     private @Nullable DocumentInfo mSrcParent;
57 
58     /**
59      * Moves files to a destination identified by {@code destination}.
60      * Performs most work by delegating to CopyJob, then deleting
61      * a file after it has been copied.
62      *
63      * @see @link {@link Job} constructor for most param descriptions.
64      */
MoveJob(Context service, Listener listener, String id, DocumentStack destination, UrisSupplier srcs, @Nullable Uri srcParent, Messenger messenger, Features features)65     MoveJob(Context service, Listener listener, String id, DocumentStack destination,
66             UrisSupplier srcs, @Nullable Uri srcParent, Messenger messenger, Features features) {
67         super(service, listener, id, OPERATION_MOVE, destination, srcs, messenger, features);
68         mSrcParentUri = srcParent;
69     }
70 
71     @Override
createProgressBuilder()72     Builder createProgressBuilder() {
73         return super.createProgressBuilder(
74                 service.getString(R.string.move_notification_title),
75                 R.drawable.ic_menu_copy,
76                 service.getString(android.R.string.cancel),
77                 R.drawable.ic_cab_cancel);
78     }
79 
80     @Override
getSetupNotification()81     public Notification getSetupNotification() {
82         return getSetupNotification(service.getString(R.string.move_preparing));
83     }
84 
85     @Override
getProgressNotification()86     public Notification getProgressNotification() {
87         return getProgressNotification(R.string.copy_remaining);
88     }
89 
90     @Override
getFailureNotification()91     Notification getFailureNotification() {
92         return getFailureNotification(
93                 R.plurals.move_error_notification_title, R.drawable.ic_menu_copy);
94     }
95 
96     @Override
setUp()97     public boolean setUp() {
98         if (mSrcParentUri != null) {
99             try {
100                 mSrcParent = DocumentInfo.fromUri(appContext.getContentResolver(), mSrcParentUri,
101                         UserId.DEFAULT_USER);
102             } catch (FileNotFoundException e) {
103                 Log.e(TAG, "Failed to create srcParent.", e);
104                 failureCount = mResourceUris.getItemCount();
105                 return false;
106             }
107         }
108 
109         return super.setUp();
110     }
111 
112     /**
113      * {@inheritDoc}
114      *
115      * Only check space for moves across authorities. For now we don't know if the doc in
116      * {@link #mSrcs} is in the same root of destination, and if it's optimized move in the same
117      * root it should succeed regardless of free space, but it's for sure a failure if there is no
118      * enough free space if docs are moved from another authority.
119      */
120     @Override
checkSpace()121     boolean checkSpace() {
122         long size = 0;
123         for (DocumentInfo src : mResolvedDocs) {
124             if (!src.authority.equals(stack.getRoot().authority)) {
125                 if (src.isDirectory()) {
126                     try {
127                         size += calculateFileSizesRecursively(getClient(src), src.derivedUri);
128                     } catch (RemoteException|ResourceException e) {
129                         Log.w(TAG, "Failed to obtain client for %s" + src.derivedUri + ".", e);
130 
131                         // Failed to calculate size, but move may still succeed.
132                         return true;
133                     }
134                 } else {
135                     size += src.size;
136                 }
137             }
138         }
139 
140         return verifySpaceAvailable(size);
141     }
142 
processDocument(DocumentInfo src, DocumentInfo srcParent, DocumentInfo dest)143     void processDocument(DocumentInfo src, DocumentInfo srcParent, DocumentInfo dest)
144             throws ResourceException {
145         // When moving within the same provider, try to use optimized moving.
146         // If not supported, then fallback to byte-by-byte copy/move.
147         if (src.authority.equals(dest.authority) && (srcParent != null || mSrcParent != null)) {
148             if ((src.flags & Document.FLAG_SUPPORTS_MOVE) != 0) {
149                 try {
150                     if (DocumentsContract.moveDocument(wrap(getClient(src)), src.derivedUri,
151                             srcParent != null ? srcParent.derivedUri : mSrcParent.derivedUri,
152                             dest.derivedUri) != null) {
153                         Metrics.logFileOperated(operationType, MetricConsts.OPMODE_PROVIDER);
154                         makeOptimizedCopyProgress(src);
155                         return;
156                     }
157                 } catch (FileNotFoundException | RemoteException | RuntimeException e) {
158                     if (e instanceof DeadObjectException) {
159                         releaseClient(src);
160                     }
161                     Metrics.logFileOperationFailure(
162                             appContext, MetricConsts.SUBFILEOP_QUICK_MOVE, src.derivedUri);
163                     Log.e(TAG, "Provider side move failed for: " + src.derivedUri
164                             + " due to an exception: ", e);
165                 }
166                 // If optimized move fails, then fallback to byte-by-byte copy.
167                 if (DEBUG) {
168                     Log.d(TAG, "Fallback to byte-by-byte move for: " + src.derivedUri);
169                 }
170             }
171         }
172 
173         // Moving virtual files by bytes is not supported. This is because, it would involve
174         // conversion, and the source file should not be deleted in such case (as it's a different
175         // file).
176         if (src.isVirtual()) {
177             throw new ResourceException("Cannot move virtual file %s byte by byte.",
178                     src.derivedUri);
179         }
180 
181         // If we couldn't do an optimized copy...we fall back to vanilla byte copy.
182         byteCopyDocument(src, dest);
183 
184         // Remove the source document.
185         if(!isCanceled()) {
186             deleteDocument(src, srcParent);
187         }
188     }
189 
190     @Override
toString()191     public String toString() {
192         return new StringBuilder()
193                 .append("MoveJob")
194                 .append("{")
195                 .append("id=" + id)
196                 .append(", uris=" + mResourceUris)
197                 .append(", docs=" + mResolvedDocs)
198                 .append(", srcParent=" + mSrcParent)
199                 .append(", destination=" + stack)
200                 .append("}")
201                 .toString();
202     }
203 }
204