blob: dc39235b35c516e3787c18caba6fa53b1b0466ad [file] [log] [blame]
Steve McKay14e827a2016-01-06 18:32:13 -08001/*
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
17package com.android.documentsui.services;
18
Steve McKayecbf3c52016-01-13 17:17:39 -080019import static com.android.documentsui.services.FileOperationService.OPERATION_MOVE;
20
Steve McKay14e827a2016-01-06 18:32:13 -080021import android.app.Notification;
22import android.app.Notification.Builder;
23import android.content.Context;
24import android.os.RemoteException;
25import android.provider.DocumentsContract;
26import android.provider.DocumentsContract.Document;
Steve McKay14e827a2016-01-06 18:32:13 -080027
28import com.android.documentsui.R;
29import com.android.documentsui.model.DocumentInfo;
30import com.android.documentsui.model.DocumentStack;
31
32import java.util.List;
33
Tomasz Mikolajewskib8436af2016-01-25 16:20:15 +090034// TODO: Stop extending CopyJob.
Steve McKay14e827a2016-01-06 18:32:13 -080035final class MoveJob extends CopyJob {
36
Tomasz Mikolajewskib8436af2016-01-25 16:20:15 +090037 final DocumentInfo mSrcParent;
Steve McKay14e827a2016-01-06 18:32:13 -080038
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 Mikolajewskib8436af2016-01-25 16:20:15 +090047 * @param srcParent Parent of all source files.
Steve McKay14e827a2016-01-06 18:32:13 -080048 */
Steve McKayecbf3c52016-01-13 17:17:39 -080049 MoveJob(Context service, Context appContext, Listener listener,
Tomasz Mikolajewskib8436af2016-01-25 16:20:15 +090050 String id, DocumentStack destination, List<DocumentInfo> srcs, DocumentInfo srcParent) {
Steve McKayecbf3c52016-01-13 17:17:39 -080051 super(service, appContext, listener, OPERATION_MOVE, id, destination, srcs);
Tomasz Mikolajewskib8436af2016-01-25 16:20:15 +090052 this.mSrcParent = srcParent;
Steve McKay14e827a2016-01-06 18:32:13 -080053 }
54
55 @Override
56 Builder createProgressBuilder() {
57 return super.createProgressBuilder(
Steve McKayecbf3c52016-01-13 17:17:39 -080058 service.getString(R.string.move_notification_title),
Steve McKay14e827a2016-01-06 18:32:13 -080059 R.drawable.ic_menu_copy,
Steve McKayecbf3c52016-01-13 17:17:39 -080060 service.getString(android.R.string.cancel),
Steve McKay14e827a2016-01-06 18:32:13 -080061 R.drawable.ic_cab_cancel);
62 }
63
64 @Override
65 public Notification getSetupNotification() {
Steve McKayecbf3c52016-01-13 17:17:39 -080066 return getSetupNotification(service.getString(R.string.move_preparing));
Steve McKay14e827a2016-01-06 18:32:13 -080067 }
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 Mikolajewski0fa97e82016-02-18 16:45:44 +090080 void processDocument(DocumentInfo src, DocumentInfo srcParent, DocumentInfo dest)
81 throws ResourceException {
Steve McKay14e827a2016-01-06 18:32:13 -080082
Tomasz Mikolajewski67048082016-01-21 10:00:33 +090083 // TODO: When optimized move kicks in, we're not making any progress updates. FIX IT!
Steve McKay14e827a2016-01-06 18:32:13 -080084
Tomasz Mikolajewski67048082016-01-21 10:00:33 +090085 // When moving within the same provider, try to use optimized moving.
Steve McKay14e827a2016-01-06 18:32:13 -080086 // If not supported, then fallback to byte-by-byte copy/move.
Steve McKay35645432016-01-20 15:09:35 -080087 if (src.authority.equals(dest.authority)) {
88 if ((src.flags & Document.FLAG_SUPPORTS_MOVE) != 0) {
Tomasz Mikolajewski0fa97e82016-02-18 16:45:44 +090089 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 McKay14e827a2016-01-06 18:32:13 -0800100 }
Tomasz Mikolajewski0fa97e82016-02-18 16:45:44 +0900101 return;
Steve McKay14e827a2016-01-06 18:32:13 -0800102 }
103 }
104
Tomasz Mikolajewski87156dc2016-01-21 12:49:24 +0900105 // 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 Mikolajewski0fa97e82016-02-18 16:45:44 +0900109 throw new ResourceException("Cannot move virtual file %s byte by byte.",
110 src.derivedUri);
Tomasz Mikolajewski87156dc2016-01-21 12:49:24 +0900111 }
112
Steve McKay14e827a2016-01-06 18:32:13 -0800113 // If we couldn't do an optimized copy...we fall back to vanilla byte copy.
Tomasz Mikolajewski0fa97e82016-02-18 16:45:44 +0900114 byteCopyDocument(src, dest);
Tomasz Mikolajewskidb875432016-02-23 15:12:54 +0900115
116 // Remove the source document.
117 deleteDocument(src, srcParent);
Steve McKayecbf3c52016-01-13 17:17:39 -0800118 }
119
Steve McKay35645432016-01-20 15:09:35 -0800120 @Override
121 public String toString() {
122 return new StringBuilder()
123 .append("MoveJob")
124 .append("{")
125 .append("id=" + id)
Tomasz Mikolajewskib8436af2016-01-25 16:20:15 +0900126 .append(", srcs=" + mSrcs)
127 .append(", srcParent=" + mSrcParent)
Steve McKay35645432016-01-20 15:09:35 -0800128 .append(", destination=" + stack)
129 .append("}")
130 .toString();
Steve McKay14e827a2016-01-06 18:32:13 -0800131 }
132}