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 | |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame^] | 19 | import static com.android.documentsui.services.FileOperationService.OPERATION_MOVE; |
| 20 | |
Steve McKay | c83baa0 | 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; |
| 27 | import android.util.Log; |
| 28 | |
| 29 | import com.android.documentsui.R; |
| 30 | import com.android.documentsui.model.DocumentInfo; |
| 31 | import com.android.documentsui.model.DocumentStack; |
| 32 | |
| 33 | import java.util.List; |
| 34 | |
| 35 | final 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 McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame^] | 48 | MoveJob(Context service, Context appContext, Listener listener, |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 49 | String id, DocumentStack destination, List<DocumentInfo> srcs) { |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame^] | 50 | super(service, appContext, listener, OPERATION_MOVE, id, destination, srcs); |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | @Override |
| 54 | Builder createProgressBuilder() { |
| 55 | return super.createProgressBuilder( |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame^] | 56 | service.getString(R.string.move_notification_title), |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 57 | R.drawable.ic_menu_copy, |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame^] | 58 | service.getString(android.R.string.cancel), |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 59 | R.drawable.ic_cab_cancel); |
| 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public Notification getSetupNotification() { |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame^] | 64 | return getSetupNotification(service.getString(R.string.move_preparing)); |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 65 | } |
| 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 McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 78 | @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 McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame^] | 96 | boolean copied = byteCopyDocument(srcInfo, dstDirInfo); |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 97 | |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame^] | 98 | 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 McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 110 | } |
| 111 | |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame^] | 112 | return true; // victory dance! |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 113 | } |
| 114 | } |