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