Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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; |
| 18 | |
Steve McKay | f8a5e08 | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 19 | import static com.android.documentsui.Shared.DEBUG; |
Ben Kwa | f527c63 | 2015-04-08 15:03:35 -0700 | [diff] [blame] | 20 | import static com.android.documentsui.model.DocumentInfo.getCursorLong; |
| 21 | import static com.android.documentsui.model.DocumentInfo.getCursorString; |
| 22 | |
Ben Kwa | c469334 | 2015-09-30 10:00:10 -0700 | [diff] [blame] | 23 | import android.app.Activity; |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 24 | import android.app.IntentService; |
| 25 | import android.app.Notification; |
| 26 | import android.app.NotificationManager; |
| 27 | import android.app.PendingIntent; |
Ben Kwa | f527c63 | 2015-04-08 15:03:35 -0700 | [diff] [blame] | 28 | import android.content.ContentProviderClient; |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 29 | import android.content.Context; |
| 30 | import android.content.Intent; |
Tomasz Mikolajewski | 9452c44 | 2015-04-14 16:32:41 +0900 | [diff] [blame] | 31 | import android.content.res.Resources; |
Ben Kwa | f527c63 | 2015-04-08 15:03:35 -0700 | [diff] [blame] | 32 | import android.database.Cursor; |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 33 | import android.net.Uri; |
Ben Kwa | f585893 | 2015-04-07 15:43:39 -0700 | [diff] [blame] | 34 | import android.os.CancellationSignal; |
Ben Kwa | f585893 | 2015-04-07 15:43:39 -0700 | [diff] [blame] | 35 | import android.os.ParcelFileDescriptor; |
Tomasz Mikolajewski | 2023fcf | 2015-04-10 10:30:33 +0900 | [diff] [blame] | 36 | import android.os.Parcelable; |
Ben Kwa | f527c63 | 2015-04-08 15:03:35 -0700 | [diff] [blame] | 37 | import android.os.RemoteException; |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 38 | import android.os.SystemClock; |
Ben Kwa | f585893 | 2015-04-07 15:43:39 -0700 | [diff] [blame] | 39 | import android.provider.DocumentsContract; |
Ben Kwa | f527c63 | 2015-04-08 15:03:35 -0700 | [diff] [blame] | 40 | import android.provider.DocumentsContract.Document; |
Ben Kwa | c469334 | 2015-09-30 10:00:10 -0700 | [diff] [blame] | 41 | import android.support.design.widget.Snackbar; |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 42 | import android.text.format.DateUtils; |
| 43 | import android.util.Log; |
Tomasz Mikolajewski | 9452c44 | 2015-04-14 16:32:41 +0900 | [diff] [blame] | 44 | import android.widget.Toast; |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 45 | |
| 46 | import com.android.documentsui.model.DocumentInfo; |
Tomasz Mikolajewski | 2023fcf | 2015-04-10 10:30:33 +0900 | [diff] [blame] | 47 | import com.android.documentsui.model.DocumentStack; |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 48 | |
| 49 | import libcore.io.IoUtils; |
| 50 | |
Tomasz Mikolajewski | 9452c44 | 2015-04-14 16:32:41 +0900 | [diff] [blame] | 51 | import java.io.FileNotFoundException; |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 52 | import java.io.IOException; |
| 53 | import java.io.InputStream; |
| 54 | import java.io.OutputStream; |
| 55 | import java.text.NumberFormat; |
| 56 | import java.util.ArrayList; |
Ben Kwa | f527c63 | 2015-04-08 15:03:35 -0700 | [diff] [blame] | 57 | import java.util.List; |
| 58 | import java.util.Objects; |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 59 | |
| 60 | public class CopyService extends IntentService { |
| 61 | public static final String TAG = "CopyService"; |
Tomasz Mikolajewski | 2023fcf | 2015-04-10 10:30:33 +0900 | [diff] [blame] | 62 | |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 63 | private static final String EXTRA_CANCEL = "com.android.documentsui.CANCEL"; |
Tomasz Mikolajewski | 2023fcf | 2015-04-10 10:30:33 +0900 | [diff] [blame] | 64 | public static final String EXTRA_SRC_LIST = "com.android.documentsui.SRC_LIST"; |
Tomasz Mikolajewski | 332d819 | 2015-04-13 19:38:43 +0900 | [diff] [blame] | 65 | public static final String EXTRA_FAILURE = "com.android.documentsui.FAILURE"; |
Ben Kwa | cb4461f | 2015-05-05 11:50:11 -0700 | [diff] [blame] | 66 | public static final String EXTRA_TRANSFER_MODE = "com.android.documentsui.TRANSFER_MODE"; |
| 67 | |
| 68 | public static final int TRANSFER_MODE_NONE = 0; |
| 69 | public static final int TRANSFER_MODE_COPY = 1; |
| 70 | public static final int TRANSFER_MODE_MOVE = 2; |
Tomasz Mikolajewski | 332d819 | 2015-04-13 19:38:43 +0900 | [diff] [blame] | 71 | |
| 72 | // TODO: Move it to a shared file when more operations are implemented. |
| 73 | public static final int FAILURE_COPY = 1; |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 74 | |
| 75 | private NotificationManager mNotificationManager; |
| 76 | private Notification.Builder mProgressBuilder; |
| 77 | |
| 78 | // Jobs are serialized but a job ID is used, to avoid mixing up cancellation requests. |
| 79 | private String mJobId; |
| 80 | private volatile boolean mIsCancelled; |
| 81 | // Parameters of the copy job. Requests to an IntentService are serialized so this code only |
| 82 | // needs to deal with one job at a time. |
Tomasz Mikolajewski | 9452c44 | 2015-04-14 16:32:41 +0900 | [diff] [blame] | 83 | private final ArrayList<DocumentInfo> mFailedFiles; |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 84 | private long mBatchSize; |
| 85 | private long mBytesCopied; |
| 86 | private long mStartTime; |
| 87 | private long mLastNotificationTime; |
| 88 | // Speed estimation |
| 89 | private long mBytesCopiedSample; |
| 90 | private long mSampleTime; |
| 91 | private long mSpeed; |
| 92 | private long mRemainingTime; |
Ben Kwa | f527c63 | 2015-04-08 15:03:35 -0700 | [diff] [blame] | 93 | // Provider clients are acquired for the duration of each copy job. Note that there is an |
| 94 | // implicit assumption that all srcs come from the same authority. |
| 95 | private ContentProviderClient mSrcClient; |
| 96 | private ContentProviderClient mDstClient; |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 97 | |
| 98 | public CopyService() { |
| 99 | super("CopyService"); |
Ben Kwa | f527c63 | 2015-04-08 15:03:35 -0700 | [diff] [blame] | 100 | |
Tomasz Mikolajewski | 9452c44 | 2015-04-14 16:32:41 +0900 | [diff] [blame] | 101 | mFailedFiles = new ArrayList<DocumentInfo>(); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Starts the service for a copy operation. |
| 106 | * |
| 107 | * @param context Context for the intent. |
| 108 | * @param srcDocs A list of src files to copy. |
| 109 | * @param dstStack The copy destination stack. |
| 110 | */ |
Ben Kwa | c469334 | 2015-09-30 10:00:10 -0700 | [diff] [blame] | 111 | public static void start(Activity activity, List<DocumentInfo> srcDocs, DocumentStack dstStack, |
Ben Kwa | cb4461f | 2015-05-05 11:50:11 -0700 | [diff] [blame] | 112 | int mode) { |
Ben Kwa | c469334 | 2015-09-30 10:00:10 -0700 | [diff] [blame] | 113 | final Resources res = activity.getResources(); |
| 114 | final Intent copyIntent = new Intent(activity, CopyService.class); |
Tomasz Mikolajewski | 9452c44 | 2015-04-14 16:32:41 +0900 | [diff] [blame] | 115 | copyIntent.putParcelableArrayListExtra( |
| 116 | EXTRA_SRC_LIST, new ArrayList<DocumentInfo>(srcDocs)); |
Steve McKay | 4d0255f | 2015-09-25 16:02:56 -0700 | [diff] [blame] | 117 | copyIntent.putExtra(Shared.EXTRA_STACK, (Parcelable) dstStack); |
Ben Kwa | cb4461f | 2015-05-05 11:50:11 -0700 | [diff] [blame] | 118 | copyIntent.putExtra(EXTRA_TRANSFER_MODE, mode); |
Tomasz Mikolajewski | 9452c44 | 2015-04-14 16:32:41 +0900 | [diff] [blame] | 119 | |
Ben Kwa | cb4461f | 2015-05-05 11:50:11 -0700 | [diff] [blame] | 120 | int toastMessage = (mode == TRANSFER_MODE_COPY) ? R.plurals.copy_begin |
| 121 | : R.plurals.move_begin; |
Ben Kwa | c469334 | 2015-09-30 10:00:10 -0700 | [diff] [blame] | 122 | Shared.makeSnackbar(activity, |
Ben Kwa | cb4461f | 2015-05-05 11:50:11 -0700 | [diff] [blame] | 123 | res.getQuantityString(toastMessage, srcDocs.size(), srcDocs.size()), |
Ben Kwa | c469334 | 2015-09-30 10:00:10 -0700 | [diff] [blame] | 124 | Snackbar.LENGTH_SHORT).show(); |
| 125 | activity.startService(copyIntent); |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public int onStartCommand(Intent intent, int flags, int startId) { |
| 130 | if (intent.hasExtra(EXTRA_CANCEL)) { |
| 131 | handleCancel(intent); |
| 132 | } |
| 133 | return super.onStartCommand(intent, flags, startId); |
| 134 | } |
| 135 | |
| 136 | @Override |
| 137 | protected void onHandleIntent(Intent intent) { |
| 138 | if (intent.hasExtra(EXTRA_CANCEL)) { |
| 139 | handleCancel(intent); |
| 140 | return; |
| 141 | } |
| 142 | |
Tomasz Mikolajewski | 2023fcf | 2015-04-10 10:30:33 +0900 | [diff] [blame] | 143 | final ArrayList<DocumentInfo> srcs = intent.getParcelableArrayListExtra(EXTRA_SRC_LIST); |
Steve McKay | 4d0255f | 2015-09-25 16:02:56 -0700 | [diff] [blame] | 144 | final DocumentStack stack = intent.getParcelableExtra(Shared.EXTRA_STACK); |
Ben Kwa | cb4461f | 2015-05-05 11:50:11 -0700 | [diff] [blame] | 145 | // Copy by default. |
| 146 | final int transferMode = intent.getIntExtra(EXTRA_TRANSFER_MODE, TRANSFER_MODE_COPY); |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 147 | |
Ben Kwa | f527c63 | 2015-04-08 15:03:35 -0700 | [diff] [blame] | 148 | try { |
| 149 | // Acquire content providers. |
| 150 | mSrcClient = DocumentsApplication.acquireUnstableProviderOrThrow(getContentResolver(), |
| 151 | srcs.get(0).authority); |
| 152 | mDstClient = DocumentsApplication.acquireUnstableProviderOrThrow(getContentResolver(), |
Tomasz Mikolajewski | 2023fcf | 2015-04-10 10:30:33 +0900 | [diff] [blame] | 153 | stack.peek().authority); |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 154 | |
Ben Kwa | cf87011 | 2015-05-28 16:18:59 -0700 | [diff] [blame] | 155 | setupCopyJob(srcs, stack, transferMode); |
Ben Kwa | f527c63 | 2015-04-08 15:03:35 -0700 | [diff] [blame] | 156 | |
| 157 | for (int i = 0; i < srcs.size() && !mIsCancelled; ++i) { |
Ben Kwa | cb4461f | 2015-05-05 11:50:11 -0700 | [diff] [blame] | 158 | copy(srcs.get(i), stack.peek(), transferMode); |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 159 | } |
Ben Kwa | f527c63 | 2015-04-08 15:03:35 -0700 | [diff] [blame] | 160 | } catch (Exception e) { |
| 161 | // Catch-all to prevent any copy errors from wedging the app. |
| 162 | Log.e(TAG, "Exceptions occurred during copying", e); |
| 163 | } finally { |
Ben Kwa | 5f88199 | 2015-08-31 15:57:19 -0700 | [diff] [blame] | 164 | if (DEBUG) Log.d(TAG, "Cleaning up after copy"); |
Ben Kwa | f527c63 | 2015-04-08 15:03:35 -0700 | [diff] [blame] | 165 | ContentProviderClient.releaseQuietly(mSrcClient); |
| 166 | ContentProviderClient.releaseQuietly(mDstClient); |
| 167 | |
| 168 | // Dismiss the ongoing copy notification when the copy is done. |
| 169 | mNotificationManager.cancel(mJobId, 0); |
| 170 | |
| 171 | if (mFailedFiles.size() > 0) { |
Ben Kwa | 5f88199 | 2015-08-31 15:57:19 -0700 | [diff] [blame] | 172 | Log.e(TAG, mFailedFiles.size() + " files failed to copy"); |
Tomasz Mikolajewski | 332d819 | 2015-04-13 19:38:43 +0900 | [diff] [blame] | 173 | final Context context = getApplicationContext(); |
Steve McKay | 0fbfc65 | 2015-08-20 16:48:49 -0700 | [diff] [blame] | 174 | final Intent navigateIntent = new Intent(context, FilesActivity.class); |
Steve McKay | 4d0255f | 2015-09-25 16:02:56 -0700 | [diff] [blame] | 175 | navigateIntent.putExtra(Shared.EXTRA_STACK, (Parcelable) stack); |
Tomasz Mikolajewski | 332d819 | 2015-04-13 19:38:43 +0900 | [diff] [blame] | 176 | navigateIntent.putExtra(EXTRA_FAILURE, FAILURE_COPY); |
Ben Kwa | 5f88199 | 2015-08-31 15:57:19 -0700 | [diff] [blame] | 177 | navigateIntent.putExtra(EXTRA_TRANSFER_MODE, transferMode); |
Tomasz Mikolajewski | 332d819 | 2015-04-13 19:38:43 +0900 | [diff] [blame] | 178 | navigateIntent.putParcelableArrayListExtra(EXTRA_SRC_LIST, mFailedFiles); |
| 179 | |
Ben Kwa | cf87011 | 2015-05-28 16:18:59 -0700 | [diff] [blame] | 180 | final int titleResourceId = (transferMode == TRANSFER_MODE_COPY ? |
| 181 | R.plurals.copy_error_notification_title : |
| 182 | R.plurals.move_error_notification_title); |
Tomasz Mikolajewski | 332d819 | 2015-04-13 19:38:43 +0900 | [diff] [blame] | 183 | final Notification.Builder errorBuilder = new Notification.Builder(this) |
Ben Kwa | cf87011 | 2015-05-28 16:18:59 -0700 | [diff] [blame] | 184 | .setContentTitle(context.getResources().getQuantityString(titleResourceId, |
| 185 | mFailedFiles.size(), mFailedFiles.size())) |
Tomasz Mikolajewski | 332d819 | 2015-04-13 19:38:43 +0900 | [diff] [blame] | 186 | .setContentText(getString(R.string.notification_touch_for_details)) |
| 187 | .setContentIntent(PendingIntent.getActivity(context, 0, navigateIntent, |
| 188 | PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT)) |
| 189 | .setCategory(Notification.CATEGORY_ERROR) |
| 190 | .setSmallIcon(R.drawable.ic_menu_copy) |
| 191 | .setAutoCancel(true); |
| 192 | mNotificationManager.notify(mJobId, 0, errorBuilder.build()); |
Ben Kwa | f527c63 | 2015-04-08 15:03:35 -0700 | [diff] [blame] | 193 | } |
Ben Kwa | 5f88199 | 2015-08-31 15:57:19 -0700 | [diff] [blame] | 194 | if (DEBUG) Log.d(TAG, "Done cleaning up"); |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 195 | } |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | @Override |
| 199 | public void onCreate() { |
| 200 | super.onCreate(); |
| 201 | mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Sets up the CopyService to start tracking and sending notifications for the given batch of |
| 206 | * files. |
| 207 | * |
| 208 | * @param srcs A list of src files to copy. |
Tomasz Mikolajewski | 2023fcf | 2015-04-10 10:30:33 +0900 | [diff] [blame] | 209 | * @param stack The copy destination stack. |
Ben Kwa | cf87011 | 2015-05-28 16:18:59 -0700 | [diff] [blame] | 210 | * @param transferMode The mode (i.e. copy, or move) |
Ben Kwa | f527c63 | 2015-04-08 15:03:35 -0700 | [diff] [blame] | 211 | * @throws RemoteException |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 212 | */ |
Ben Kwa | cf87011 | 2015-05-28 16:18:59 -0700 | [diff] [blame] | 213 | private void setupCopyJob(ArrayList<DocumentInfo> srcs, DocumentStack stack, int transferMode) |
Ben Kwa | f527c63 | 2015-04-08 15:03:35 -0700 | [diff] [blame] | 214 | throws RemoteException { |
Ben Kwa | cf87011 | 2015-05-28 16:18:59 -0700 | [diff] [blame] | 215 | final boolean copying = (transferMode == TRANSFER_MODE_COPY); |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 216 | // Create an ID for this copy job. Use the timestamp. |
| 217 | mJobId = String.valueOf(SystemClock.elapsedRealtime()); |
| 218 | // Reset the cancellation flag. |
| 219 | mIsCancelled = false; |
| 220 | |
Tomasz Mikolajewski | 2023fcf | 2015-04-10 10:30:33 +0900 | [diff] [blame] | 221 | final Context context = getApplicationContext(); |
Steve McKay | 0fbfc65 | 2015-08-20 16:48:49 -0700 | [diff] [blame] | 222 | final Intent navigateIntent = new Intent(context, FilesActivity.class); |
Steve McKay | 4d0255f | 2015-09-25 16:02:56 -0700 | [diff] [blame] | 223 | navigateIntent.putExtra(Shared.EXTRA_STACK, (Parcelable) stack); |
Tomasz Mikolajewski | 2023fcf | 2015-04-10 10:30:33 +0900 | [diff] [blame] | 224 | |
Ben Kwa | cf87011 | 2015-05-28 16:18:59 -0700 | [diff] [blame] | 225 | final String contentTitle = getString(copying ? R.string.copy_notification_title |
| 226 | : R.string.move_notification_title); |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 227 | mProgressBuilder = new Notification.Builder(this) |
Ben Kwa | cf87011 | 2015-05-28 16:18:59 -0700 | [diff] [blame] | 228 | .setContentTitle(contentTitle) |
Tomasz Mikolajewski | 2023fcf | 2015-04-10 10:30:33 +0900 | [diff] [blame] | 229 | .setContentIntent(PendingIntent.getActivity(context, 0, navigateIntent, 0)) |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 230 | .setCategory(Notification.CATEGORY_PROGRESS) |
Tomasz Mikolajewski | 332d819 | 2015-04-13 19:38:43 +0900 | [diff] [blame] | 231 | .setSmallIcon(R.drawable.ic_menu_copy) |
| 232 | .setOngoing(true); |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 233 | |
Tomasz Mikolajewski | 2023fcf | 2015-04-10 10:30:33 +0900 | [diff] [blame] | 234 | final Intent cancelIntent = new Intent(this, CopyService.class); |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 235 | cancelIntent.putExtra(EXTRA_CANCEL, mJobId); |
| 236 | mProgressBuilder.addAction(R.drawable.ic_cab_cancel, |
Daichi Hirono | 677ce6c | 2015-04-15 14:33:52 +0900 | [diff] [blame] | 237 | getString(android.R.string.cancel), PendingIntent.getService(this, 0, |
Ben Kwa | c89eb7c | 2015-04-15 12:13:32 -0700 | [diff] [blame] | 238 | cancelIntent, |
| 239 | PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_CANCEL_CURRENT)); |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 240 | |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 241 | // Send an initial progress notification. |
Ben Kwa | cf87011 | 2015-05-28 16:18:59 -0700 | [diff] [blame] | 242 | final String contentText = getString(copying ? R.string.copy_preparing |
| 243 | : R.string.move_preparing); |
Ben Kwa | f527c63 | 2015-04-08 15:03:35 -0700 | [diff] [blame] | 244 | mProgressBuilder.setProgress(0, 0, true); // Indeterminate progress while setting up. |
Ben Kwa | cf87011 | 2015-05-28 16:18:59 -0700 | [diff] [blame] | 245 | mProgressBuilder.setContentText(contentText); |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 246 | mNotificationManager.notify(mJobId, 0, mProgressBuilder.build()); |
| 247 | |
| 248 | // Reset batch parameters. |
Ben Kwa | f527c63 | 2015-04-08 15:03:35 -0700 | [diff] [blame] | 249 | mFailedFiles.clear(); |
| 250 | mBatchSize = calculateFileSizes(srcs); |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 251 | mBytesCopied = 0; |
| 252 | mStartTime = SystemClock.elapsedRealtime(); |
| 253 | mLastNotificationTime = 0; |
| 254 | mBytesCopiedSample = 0; |
| 255 | mSampleTime = 0; |
| 256 | mSpeed = 0; |
| 257 | mRemainingTime = 0; |
| 258 | |
| 259 | // TODO: Check preconditions for copy. |
| 260 | // - check that the destination has enough space and is writeable? |
| 261 | // - check MIME types? |
| 262 | } |
| 263 | |
| 264 | /** |
Ben Kwa | f527c63 | 2015-04-08 15:03:35 -0700 | [diff] [blame] | 265 | * Calculates the cumulative size of all the documents in the list. Directories are recursed |
| 266 | * into and totaled up. |
| 267 | * |
| 268 | * @param srcs |
| 269 | * @return Size in bytes. |
| 270 | * @throws RemoteException |
| 271 | */ |
| 272 | private long calculateFileSizes(List<DocumentInfo> srcs) throws RemoteException { |
| 273 | long result = 0; |
| 274 | for (DocumentInfo src : srcs) { |
| 275 | if (Document.MIME_TYPE_DIR.equals(src.mimeType)) { |
| 276 | // Directories need to be recursed into. |
| 277 | result += calculateFileSizesHelper(src.derivedUri); |
| 278 | } else { |
| 279 | result += src.size; |
| 280 | } |
| 281 | } |
| 282 | return result; |
| 283 | } |
| 284 | |
| 285 | /** |
| 286 | * Calculates (recursively) the cumulative size of all the files under the given directory. |
| 287 | * |
| 288 | * @throws RemoteException |
| 289 | */ |
| 290 | private long calculateFileSizesHelper(Uri uri) throws RemoteException { |
| 291 | final String authority = uri.getAuthority(); |
| 292 | final Uri queryUri = DocumentsContract.buildChildDocumentsUri(authority, |
| 293 | DocumentsContract.getDocumentId(uri)); |
| 294 | final String queryColumns[] = new String[] { |
| 295 | Document.COLUMN_DOCUMENT_ID, |
| 296 | Document.COLUMN_MIME_TYPE, |
| 297 | Document.COLUMN_SIZE |
| 298 | }; |
| 299 | |
| 300 | long result = 0; |
| 301 | Cursor cursor = null; |
| 302 | try { |
| 303 | cursor = mSrcClient.query(queryUri, queryColumns, null, null, null); |
| 304 | while (cursor.moveToNext()) { |
| 305 | if (Document.MIME_TYPE_DIR.equals( |
| 306 | getCursorString(cursor, Document.COLUMN_MIME_TYPE))) { |
| 307 | // Recurse into directories. |
| 308 | final Uri subdirUri = DocumentsContract.buildDocumentUri(authority, |
| 309 | getCursorString(cursor, Document.COLUMN_DOCUMENT_ID)); |
| 310 | result += calculateFileSizesHelper(subdirUri); |
| 311 | } else { |
| 312 | // This may return -1 if the size isn't defined. Ignore those cases. |
| 313 | long size = getCursorLong(cursor, Document.COLUMN_SIZE); |
| 314 | result += size > 0 ? size : 0; |
| 315 | } |
| 316 | } |
| 317 | } finally { |
| 318 | IoUtils.closeQuietly(cursor); |
| 319 | } |
| 320 | |
| 321 | return result; |
| 322 | } |
| 323 | |
| 324 | /** |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 325 | * Cancels the current copy job, if its ID matches the given ID. |
| 326 | * |
| 327 | * @param intent The cancellation intent. |
| 328 | */ |
| 329 | private void handleCancel(Intent intent) { |
| 330 | final String cancelledId = intent.getStringExtra(EXTRA_CANCEL); |
| 331 | // Do nothing if the cancelled ID doesn't match the current job ID. This prevents racey |
Ben Kwa | 6cbab7d | 2015-05-27 09:18:53 -0700 | [diff] [blame] | 332 | // cancellation requests from affecting unrelated copy jobs. However, if the current job ID |
| 333 | // is null, the service most likely crashed and was revived by the incoming cancel intent. |
| 334 | // In that case, always allow the cancellation to proceed. |
| 335 | if (Objects.equals(mJobId, cancelledId) || mJobId == null) { |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 336 | // Set the cancel flag. This causes the copy loops to exit. |
| 337 | mIsCancelled = true; |
| 338 | // Dismiss the progress notification here rather than in the copy loop. This preserves |
| 339 | // interactivity for the user in case the copy loop is stalled. |
Ben Kwa | 6cbab7d | 2015-05-27 09:18:53 -0700 | [diff] [blame] | 340 | mNotificationManager.cancel(cancelledId, 0); |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 341 | } |
| 342 | } |
| 343 | |
| 344 | /** |
| 345 | * Logs progress on the current copy operation. Displays/Updates the progress notification. |
| 346 | * |
| 347 | * @param bytesCopied |
| 348 | */ |
| 349 | private void makeProgress(long bytesCopied) { |
| 350 | mBytesCopied += bytesCopied; |
| 351 | double done = (double) mBytesCopied / mBatchSize; |
| 352 | String percent = NumberFormat.getPercentInstance().format(done); |
| 353 | |
| 354 | // Update time estimate |
| 355 | long currentTime = SystemClock.elapsedRealtime(); |
| 356 | long elapsedTime = currentTime - mStartTime; |
| 357 | |
| 358 | // Send out progress notifications once a second. |
| 359 | if (currentTime - mLastNotificationTime > 1000) { |
| 360 | updateRemainingTimeEstimate(elapsedTime); |
| 361 | mProgressBuilder.setProgress(100, (int) (done * 100), false); |
| 362 | mProgressBuilder.setContentInfo(percent); |
| 363 | if (mRemainingTime > 0) { |
| 364 | mProgressBuilder.setContentText(getString(R.string.copy_remaining, |
| 365 | DateUtils.formatDuration(mRemainingTime))); |
| 366 | } else { |
| 367 | mProgressBuilder.setContentText(null); |
| 368 | } |
| 369 | mNotificationManager.notify(mJobId, 0, mProgressBuilder.build()); |
| 370 | mLastNotificationTime = currentTime; |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | /** |
| 375 | * Generates an estimate of the remaining time in the copy. |
| 376 | * |
| 377 | * @param elapsedTime The time elapsed so far. |
| 378 | */ |
| 379 | private void updateRemainingTimeEstimate(long elapsedTime) { |
| 380 | final long sampleDuration = elapsedTime - mSampleTime; |
| 381 | final long sampleSpeed = ((mBytesCopied - mBytesCopiedSample) * 1000) / sampleDuration; |
| 382 | if (mSpeed == 0) { |
| 383 | mSpeed = sampleSpeed; |
| 384 | } else { |
| 385 | mSpeed = ((3 * mSpeed) + sampleSpeed) / 4; |
| 386 | } |
| 387 | |
| 388 | if (mSampleTime > 0 && mSpeed > 0) { |
| 389 | mRemainingTime = ((mBatchSize - mBytesCopied) * 1000) / mSpeed; |
| 390 | } else { |
| 391 | mRemainingTime = 0; |
| 392 | } |
| 393 | |
| 394 | mSampleTime = elapsedTime; |
| 395 | mBytesCopiedSample = mBytesCopied; |
| 396 | } |
| 397 | |
| 398 | /** |
Ben Kwa | f527c63 | 2015-04-08 15:03:35 -0700 | [diff] [blame] | 399 | * Copies a the given documents to the given location. |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 400 | * |
Ben Kwa | f527c63 | 2015-04-08 15:03:35 -0700 | [diff] [blame] | 401 | * @param srcInfo DocumentInfos for the documents to copy. |
Tomasz Mikolajewski | 2023fcf | 2015-04-10 10:30:33 +0900 | [diff] [blame] | 402 | * @param dstDirInfo The destination directory. |
Ben Kwa | f527c63 | 2015-04-08 15:03:35 -0700 | [diff] [blame] | 403 | * @throws RemoteException |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 404 | */ |
Ben Kwa | cb4461f | 2015-05-05 11:50:11 -0700 | [diff] [blame] | 405 | private void copy(DocumentInfo srcInfo, DocumentInfo dstDirInfo, int mode) |
| 406 | throws RemoteException { |
Ben Kwa | 5f88199 | 2015-08-31 15:57:19 -0700 | [diff] [blame] | 407 | if (DEBUG) Log.d(TAG, "Copying " + srcInfo.displayName + " (" + srcInfo.derivedUri + ")" + |
| 408 | " to " + dstDirInfo.displayName + " (" + dstDirInfo.derivedUri + ")"); |
| 409 | |
Tomasz Mikolajewski | 2023fcf | 2015-04-10 10:30:33 +0900 | [diff] [blame] | 410 | final Uri dstUri = DocumentsContract.createDocument(mDstClient, dstDirInfo.derivedUri, |
Ben Kwa | f585893 | 2015-04-07 15:43:39 -0700 | [diff] [blame] | 411 | srcInfo.mimeType, srcInfo.displayName); |
Ben Kwa | f527c63 | 2015-04-08 15:03:35 -0700 | [diff] [blame] | 412 | if (dstUri == null) { |
| 413 | // If this is a directory, the entire subdir will not be copied over. |
| 414 | Log.e(TAG, "Error while copying " + srcInfo.displayName); |
Tomasz Mikolajewski | 9452c44 | 2015-04-14 16:32:41 +0900 | [diff] [blame] | 415 | mFailedFiles.add(srcInfo); |
Ben Kwa | f527c63 | 2015-04-08 15:03:35 -0700 | [diff] [blame] | 416 | return; |
| 417 | } |
Ben Kwa | f585893 | 2015-04-07 15:43:39 -0700 | [diff] [blame] | 418 | |
Ben Kwa | f527c63 | 2015-04-08 15:03:35 -0700 | [diff] [blame] | 419 | if (Document.MIME_TYPE_DIR.equals(srcInfo.mimeType)) { |
Ben Kwa | cb4461f | 2015-05-05 11:50:11 -0700 | [diff] [blame] | 420 | copyDirectoryHelper(srcInfo.derivedUri, dstUri, mode); |
Ben Kwa | f527c63 | 2015-04-08 15:03:35 -0700 | [diff] [blame] | 421 | } else { |
Ben Kwa | cb4461f | 2015-05-05 11:50:11 -0700 | [diff] [blame] | 422 | copyFileHelper(srcInfo.derivedUri, dstUri, mode); |
Ben Kwa | f527c63 | 2015-04-08 15:03:35 -0700 | [diff] [blame] | 423 | } |
| 424 | } |
| 425 | |
| 426 | /** |
| 427 | * Handles recursion into a directory and copying its contents. Note that in linux terms, this |
| 428 | * does the equivalent of "cp src/* dst", not "cp -r src dst". |
| 429 | * |
| 430 | * @param srcDirUri URI of the directory to copy from. The routine will copy the directory's |
| 431 | * contents, not the directory itself. |
| 432 | * @param dstDirUri URI of the directory to copy to. Must be created beforehand. |
| 433 | * @throws RemoteException |
| 434 | */ |
Ben Kwa | cb4461f | 2015-05-05 11:50:11 -0700 | [diff] [blame] | 435 | private void copyDirectoryHelper(Uri srcDirUri, Uri dstDirUri, int mode) |
| 436 | throws RemoteException { |
Ben Kwa | f527c63 | 2015-04-08 15:03:35 -0700 | [diff] [blame] | 437 | // Recurse into directories. Copy children into the new subdirectory. |
| 438 | final String queryColumns[] = new String[] { |
| 439 | Document.COLUMN_DISPLAY_NAME, |
| 440 | Document.COLUMN_DOCUMENT_ID, |
| 441 | Document.COLUMN_MIME_TYPE, |
| 442 | Document.COLUMN_SIZE |
| 443 | }; |
| 444 | final Uri queryUri = DocumentsContract.buildChildDocumentsUri(srcDirUri.getAuthority(), |
| 445 | DocumentsContract.getDocumentId(srcDirUri)); |
| 446 | Cursor cursor = null; |
| 447 | try { |
| 448 | // Iterate over srcs in the directory; copy to the destination directory. |
| 449 | cursor = mSrcClient.query(queryUri, queryColumns, null, null, null); |
| 450 | while (cursor.moveToNext()) { |
| 451 | final String childMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE); |
| 452 | final Uri dstUri = DocumentsContract.createDocument(mDstClient, dstDirUri, |
| 453 | childMimeType, getCursorString(cursor, Document.COLUMN_DISPLAY_NAME)); |
| 454 | final Uri childUri = DocumentsContract.buildDocumentUri(srcDirUri.getAuthority(), |
| 455 | getCursorString(cursor, Document.COLUMN_DOCUMENT_ID)); |
| 456 | if (Document.MIME_TYPE_DIR.equals(childMimeType)) { |
Ben Kwa | cb4461f | 2015-05-05 11:50:11 -0700 | [diff] [blame] | 457 | copyDirectoryHelper(childUri, dstUri, mode); |
Ben Kwa | f527c63 | 2015-04-08 15:03:35 -0700 | [diff] [blame] | 458 | } else { |
Ben Kwa | cb4461f | 2015-05-05 11:50:11 -0700 | [diff] [blame] | 459 | copyFileHelper(childUri, dstUri, mode); |
| 460 | } |
| 461 | } |
| 462 | if (mode == TRANSFER_MODE_MOVE) { |
| 463 | try { |
| 464 | DocumentsContract.deleteDocument(mSrcClient, srcDirUri); |
| 465 | } catch (RemoteException e) { |
| 466 | // RemoteExceptions usually signal that the connection is dead, so there's no |
| 467 | // point attempting to continue. Propagate the exception up so the copy job is |
| 468 | // cancelled. |
| 469 | Log.w(TAG, "Failed to clean up after move: " + srcDirUri, e); |
| 470 | throw e; |
Ben Kwa | f527c63 | 2015-04-08 15:03:35 -0700 | [diff] [blame] | 471 | } |
| 472 | } |
| 473 | } finally { |
| 474 | IoUtils.closeQuietly(cursor); |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | /** |
| 479 | * Handles copying a single file. |
| 480 | * |
| 481 | * @param srcUri URI of the file to copy from. |
| 482 | * @param dstUri URI of the *file* to copy to. Must be created beforehand. |
| 483 | * @throws RemoteException |
| 484 | */ |
Ben Kwa | cb4461f | 2015-05-05 11:50:11 -0700 | [diff] [blame] | 485 | private void copyFileHelper(Uri srcUri, Uri dstUri, int mode) |
| 486 | throws RemoteException { |
Ben Kwa | f527c63 | 2015-04-08 15:03:35 -0700 | [diff] [blame] | 487 | // Copy an individual file. |
Ben Kwa | f585893 | 2015-04-07 15:43:39 -0700 | [diff] [blame] | 488 | CancellationSignal canceller = new CancellationSignal(); |
| 489 | ParcelFileDescriptor srcFile = null; |
| 490 | ParcelFileDescriptor dstFile = null; |
| 491 | InputStream src = null; |
| 492 | OutputStream dst = null; |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 493 | |
Ben Kwa | 37c7659 | 2015-04-24 15:35:25 -0700 | [diff] [blame] | 494 | IOException copyError = null; |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 495 | try { |
Ben Kwa | f527c63 | 2015-04-08 15:03:35 -0700 | [diff] [blame] | 496 | srcFile = mSrcClient.openFile(srcUri, "r", canceller); |
| 497 | dstFile = mDstClient.openFile(dstUri, "w", canceller); |
Ben Kwa | f585893 | 2015-04-07 15:43:39 -0700 | [diff] [blame] | 498 | src = new ParcelFileDescriptor.AutoCloseInputStream(srcFile); |
| 499 | dst = new ParcelFileDescriptor.AutoCloseOutputStream(dstFile); |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 500 | |
| 501 | byte[] buffer = new byte[8192]; |
| 502 | int len; |
Ben Kwa | f585893 | 2015-04-07 15:43:39 -0700 | [diff] [blame] | 503 | while (!mIsCancelled && ((len = src.read(buffer)) != -1)) { |
| 504 | dst.write(buffer, 0, len); |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 505 | makeProgress(len); |
| 506 | } |
Ben Kwa | 37c7659 | 2015-04-24 15:35:25 -0700 | [diff] [blame] | 507 | |
Ben Kwa | f585893 | 2015-04-07 15:43:39 -0700 | [diff] [blame] | 508 | srcFile.checkError(); |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 509 | } catch (IOException e) { |
Ben Kwa | 37c7659 | 2015-04-24 15:35:25 -0700 | [diff] [blame] | 510 | copyError = e; |
Ben Kwa | 5f88199 | 2015-08-31 15:57:19 -0700 | [diff] [blame] | 511 | |
Ben Kwa | 2d88d49 | 2015-05-05 11:58:38 -0700 | [diff] [blame] | 512 | try { |
Ben Kwa | 5f88199 | 2015-08-31 15:57:19 -0700 | [diff] [blame] | 513 | DocumentInfo info = DocumentInfo.fromUri(getContentResolver(), srcUri); |
| 514 | mFailedFiles.add(info); |
| 515 | Log.e(TAG, "Error while copying " + info.displayName + " (" + info.derivedUri + ")", |
| 516 | copyError); |
| 517 | } catch (FileNotFoundException ignore) { |
| 518 | // Generate a dummy DocumentInfo so an error still gets reflected in the UI for this |
| 519 | // file. |
| 520 | DocumentInfo info = new DocumentInfo(); |
| 521 | info.derivedUri = srcUri; |
| 522 | info.displayName = "Unknown [" + srcUri + "]"; |
| 523 | mFailedFiles.add(info); |
| 524 | Log.e(TAG, "Error while copying " + srcUri, copyError); |
| 525 | } |
| 526 | |
| 527 | if (dstFile != null) { |
| 528 | try { |
| 529 | dstFile.closeWithError(copyError.getMessage()); |
| 530 | } catch (IOException closeError) { |
| 531 | Log.e(TAG, "Error closing destination", closeError); |
| 532 | } |
Ben Kwa | 37c7659 | 2015-04-24 15:35:25 -0700 | [diff] [blame] | 533 | } |
Ben Kwa | 2d88d49 | 2015-05-05 11:58:38 -0700 | [diff] [blame] | 534 | } finally { |
Ben Kwa | f585893 | 2015-04-07 15:43:39 -0700 | [diff] [blame] | 535 | // This also ensures the file descriptors are closed. |
| 536 | IoUtils.closeQuietly(src); |
| 537 | IoUtils.closeQuietly(dst); |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 538 | } |
| 539 | |
Ben Kwa | 37c7659 | 2015-04-24 15:35:25 -0700 | [diff] [blame] | 540 | |
| 541 | if (copyError != null || mIsCancelled) { |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 542 | // Clean up half-copied files. |
Ben Kwa | f585893 | 2015-04-07 15:43:39 -0700 | [diff] [blame] | 543 | canceller.cancel(); |
Ben Kwa | f527c63 | 2015-04-08 15:03:35 -0700 | [diff] [blame] | 544 | try { |
| 545 | DocumentsContract.deleteDocument(mDstClient, dstUri); |
| 546 | } catch (RemoteException e) { |
Ben Kwa | cb4461f | 2015-05-05 11:50:11 -0700 | [diff] [blame] | 547 | Log.w(TAG, "Failed to clean up after copy error: " + dstUri, e); |
Ben Kwa | f527c63 | 2015-04-08 15:03:35 -0700 | [diff] [blame] | 548 | // RemoteExceptions usually signal that the connection is dead, so there's no point |
| 549 | // attempting to continue. Propagate the exception up so the copy job is cancelled. |
| 550 | throw e; |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 551 | } |
Ben Kwa | cb4461f | 2015-05-05 11:50:11 -0700 | [diff] [blame] | 552 | } else if (mode == TRANSFER_MODE_MOVE) { |
| 553 | // Clean up src files after a successful move. |
| 554 | try { |
| 555 | DocumentsContract.deleteDocument(mSrcClient, srcUri); |
| 556 | } catch (RemoteException e) { |
| 557 | Log.w(TAG, "Failed to clean up after move: " + srcUri, e); |
| 558 | throw e; |
| 559 | } |
Ben Kwa | 41b26c1 | 2015-03-31 10:11:43 -0700 | [diff] [blame] | 560 | } |
| 561 | } |
| 562 | } |