blob: 79440100689a49cb5121f404d01544f0c85513aa [file] [log] [blame]
Steve McKayc83baa02016-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 McKaybbeba522016-01-13 17:17:39 -080019import static com.android.documentsui.services.FileOperationService.OPERATION_MOVE;
20
Steve McKayc83baa02016-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;
27import android.util.Log;
28
29import com.android.documentsui.R;
30import com.android.documentsui.model.DocumentInfo;
31import com.android.documentsui.model.DocumentStack;
32
33import java.util.List;
34
35final class MoveJob extends CopyJob {
36
37 private static final String TAG = "MoveJob";
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.
47 */
Steve McKaybbeba522016-01-13 17:17:39 -080048 MoveJob(Context service, Context appContext, Listener listener,
Steve McKayc83baa02016-01-06 18:32:13 -080049 String id, DocumentStack destination, List<DocumentInfo> srcs) {
Steve McKaybbeba522016-01-13 17:17:39 -080050 super(service, appContext, listener, OPERATION_MOVE, id, destination, srcs);
Steve McKayc83baa02016-01-06 18:32:13 -080051 }
52
53 @Override
54 Builder createProgressBuilder() {
55 return super.createProgressBuilder(
Steve McKaybbeba522016-01-13 17:17:39 -080056 service.getString(R.string.move_notification_title),
Steve McKayc83baa02016-01-06 18:32:13 -080057 R.drawable.ic_menu_copy,
Steve McKaybbeba522016-01-13 17:17:39 -080058 service.getString(android.R.string.cancel),
Steve McKayc83baa02016-01-06 18:32:13 -080059 R.drawable.ic_cab_cancel);
60 }
61
62 @Override
63 public Notification getSetupNotification() {
Steve McKaybbeba522016-01-13 17:17:39 -080064 return getSetupNotification(service.getString(R.string.move_preparing));
Steve McKayc83baa02016-01-06 18:32:13 -080065 }
66
67 @Override
68 public Notification getProgressNotification() {
69 return getProgressNotification(R.string.copy_preparing);
70 }
71
72 @Override
73 Notification getFailureNotification() {
74 return getFailureNotification(
75 R.plurals.move_error_notification_title, R.drawable.ic_menu_copy);
76 }
77
Steve McKayc83baa02016-01-06 18:32:13 -080078 @Override
79 boolean processDocument(DocumentInfo srcInfo, DocumentInfo dstDirInfo) throws RemoteException {
80
81 // TODO: When optimized copy kicks in, we're not making any progress updates. FIX IT!
82
83 // When copying within the same provider, try to use optimized copying and moving.
84 // If not supported, then fallback to byte-by-byte copy/move.
85 if (srcInfo.authority.equals(dstDirInfo.authority)) {
86 if ((srcInfo.flags & Document.FLAG_SUPPORTS_MOVE) != 0) {
87 if (DocumentsContract.moveDocument(srcClient, srcInfo.derivedUri,
88 dstDirInfo.derivedUri) == null) {
89 onFileFailed(srcInfo);
90 }
91 return false;
92 }
93 }
94
95 // If we couldn't do an optimized copy...we fall back to vanilla byte copy.
Steve McKaybbeba522016-01-13 17:17:39 -080096 boolean copied = byteCopyDocument(srcInfo, dstDirInfo);
Steve McKayc83baa02016-01-06 18:32:13 -080097
Steve McKaybbeba522016-01-13 17:17:39 -080098 return copied && !isCanceled() && deleteSrcDocument(srcInfo);
99 }
100
101 private boolean deleteSrcDocument(DocumentInfo srcInfo) {
102 // This is racey. We should make sure that we never delete a directory after
103 // it changed, so we don't remove a file which had not been copied earlier
104 // to the target location.
105 try {
106 DocumentsContract.deleteDocument(srcClient, srcInfo.derivedUri);
107 } catch (RemoteException e) {
108 Log.w(TAG, "Failed to delete source after copy: " + srcInfo.derivedUri, e);
109 return false;
Steve McKayc83baa02016-01-06 18:32:13 -0800110 }
111
Steve McKaybbeba522016-01-13 17:17:39 -0800112 return true; // victory dance!
Steve McKayc83baa02016-01-06 18:32:13 -0800113 }
114}