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 android.os.SystemClock.elapsedRealtime; |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 20 | import static com.android.documentsui.Shared.DEBUG; |
| 21 | import static com.android.documentsui.Shared.EXTRA_STACK; |
| 22 | import static com.android.documentsui.Shared.asArrayList; |
| 23 | import static com.android.documentsui.Shared.getQuantityString; |
| 24 | import static com.android.documentsui.services.FileOperationService.EXTRA_CANCEL; |
| 25 | import static com.android.documentsui.services.FileOperationService.EXTRA_JOB_ID; |
| 26 | import static com.android.documentsui.services.FileOperationService.EXTRA_OPERATION; |
| 27 | import static com.android.documentsui.services.FileOperationService.EXTRA_SRC_LIST; |
| 28 | import static com.android.documentsui.services.FileOperationService.OPERATION_COPY; |
| 29 | import static com.android.documentsui.services.FileOperationService.OPERATION_DELETE; |
| 30 | import static com.android.documentsui.services.FileOperationService.OPERATION_MOVE; |
| 31 | |
| 32 | import android.app.Activity; |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame^] | 33 | import android.content.Context; |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 34 | import android.content.Intent; |
| 35 | import android.content.res.Resources; |
| 36 | import android.os.Parcelable; |
| 37 | import android.support.design.widget.Snackbar; |
| 38 | import android.util.Log; |
| 39 | |
| 40 | import com.android.documentsui.R; |
| 41 | import com.android.documentsui.Snackbars; |
| 42 | import com.android.documentsui.model.DocumentInfo; |
| 43 | import com.android.documentsui.model.DocumentStack; |
| 44 | import com.android.documentsui.services.FileOperationService.OpType; |
| 45 | |
| 46 | import java.util.List; |
| 47 | |
| 48 | /** |
| 49 | * Helper functions for starting various file operations. |
| 50 | */ |
| 51 | public final class FileOperations { |
| 52 | |
| 53 | private static final String TAG = "FileOperations"; |
| 54 | |
| 55 | private FileOperations() {} |
| 56 | |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame^] | 57 | public static String createJobId() { |
| 58 | return String.valueOf(elapsedRealtime()); |
| 59 | } |
| 60 | |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 61 | /** |
| 62 | * Tries to start the activity. Returns the job id. |
| 63 | */ |
| 64 | public static String start( |
| 65 | Activity activity, List<DocumentInfo> srcDocs, DocumentStack stack, |
| 66 | int operationType) { |
| 67 | |
| 68 | if (DEBUG) Log.d(TAG, "Handling generic 'start' call."); |
| 69 | |
| 70 | switch (operationType) { |
| 71 | case OPERATION_COPY: |
| 72 | return FileOperations.copy(activity, srcDocs, stack); |
| 73 | case OPERATION_MOVE: |
| 74 | return FileOperations.move(activity, srcDocs, stack); |
| 75 | case OPERATION_DELETE: |
| 76 | return FileOperations.delete(activity, srcDocs, stack); |
| 77 | default: |
| 78 | throw new UnsupportedOperationException("Unknown operation: " + operationType); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Makes a best effort to cancel operation identified by jobId. |
| 84 | * |
| 85 | * @param context Context for the intent. |
| 86 | * @param jobId The id of the job to cancel. |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame^] | 87 | * Use {@link #createJobId} if you don't have one handy. |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 88 | * @param srcDocs A list of src files to copy. |
| 89 | * @param dstStack The copy destination stack. |
| 90 | */ |
| 91 | public static void cancel(Activity activity, String jobId) { |
| 92 | if (DEBUG) Log.d(TAG, "Attempting to canceling operation: " + jobId); |
| 93 | |
| 94 | Intent intent = new Intent(activity, FileOperationService.class); |
| 95 | intent.putExtra(EXTRA_CANCEL, true); |
| 96 | intent.putExtra(EXTRA_JOB_ID, jobId); |
| 97 | |
| 98 | activity.startService(intent); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Starts the service for a copy operation. |
| 103 | * |
| 104 | * @param context Context for the intent. |
| 105 | * @param jobId A unique jobid for this job. |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame^] | 106 | * Use {@link #createJobId} if you don't have one handy. |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 107 | * @param srcDocs A list of src files to copy. |
| 108 | * @param destination The copy destination stack. |
| 109 | */ |
| 110 | public static String copy( |
| 111 | Activity activity, List<DocumentInfo> srcDocs, DocumentStack destination) { |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame^] | 112 | String jobId = createJobId(); |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 113 | if (DEBUG) Log.d(TAG, "Initiating 'copy' operation id: " + jobId); |
| 114 | |
| 115 | Intent intent = createBaseIntent(OPERATION_COPY, activity, jobId, srcDocs, destination); |
| 116 | |
| 117 | createSharedSnackBar(activity, R.plurals.copy_begin, srcDocs.size()) |
| 118 | .show(); |
| 119 | |
| 120 | activity.startService(intent); |
| 121 | |
| 122 | return jobId; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Starts the service for a move operation. |
| 127 | * |
| 128 | * @param jobId A unique jobid for this job. |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame^] | 129 | * Use {@link #createJobId} if you don't have one handy. |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 130 | * @param srcDocs A list of src files to copy. |
| 131 | * @param destination The move destination stack. |
| 132 | */ |
| 133 | public static String move( |
| 134 | Activity activity, List<DocumentInfo> srcDocs, DocumentStack destination) { |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame^] | 135 | String jobId = createJobId(); |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 136 | if (DEBUG) Log.d(TAG, "Initiating 'move' operation id: " + jobId); |
| 137 | |
| 138 | Intent intent = createBaseIntent(OPERATION_MOVE, activity, jobId, srcDocs, destination); |
| 139 | |
| 140 | createSharedSnackBar(activity, R.plurals.move_begin, srcDocs.size()) |
| 141 | .show(); |
| 142 | |
| 143 | activity.startService(intent); |
| 144 | |
| 145 | return jobId; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Starts the service for a move operation. |
| 150 | * |
| 151 | * @param jobId A unique jobid for this job. |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame^] | 152 | * Use {@link #createJobId} if you don't have one handy. |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 153 | * @param srcDocs A list of src files to copy. |
| 154 | * @return Id of the job. |
| 155 | */ |
| 156 | public static String delete( |
| 157 | Activity activity, List<DocumentInfo> srcDocs, DocumentStack location) { |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame^] | 158 | String jobId = createJobId(); |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 159 | if (DEBUG) Log.d(TAG, "Initiating 'delete' operation id: " + jobId); |
| 160 | |
| 161 | Intent intent = createBaseIntent(OPERATION_DELETE, activity, jobId, srcDocs, location); |
| 162 | activity.startService(intent); |
| 163 | |
| 164 | return jobId; |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Starts the service for a move operation. |
| 169 | * |
| 170 | * @param jobId A unique jobid for this job. |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame^] | 171 | * Use {@link #createJobId} if you don't have one handy. |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 172 | * @param srcDocs A list of src files to copy. |
| 173 | * @return Id of the job. |
| 174 | */ |
| 175 | public static Intent createBaseIntent( |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame^] | 176 | @OpType int operationType, Context context, String jobId, |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 177 | List<DocumentInfo> srcDocs, DocumentStack localeStack) { |
| 178 | |
Steve McKay | bbeba52 | 2016-01-13 17:17:39 -0800 | [diff] [blame^] | 179 | Intent intent = new Intent(context, FileOperationService.class); |
Steve McKay | c83baa0 | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 180 | intent.putExtra(EXTRA_JOB_ID, jobId); |
| 181 | intent.putParcelableArrayListExtra( |
| 182 | EXTRA_SRC_LIST, asArrayList(srcDocs)); |
| 183 | intent.putExtra(EXTRA_STACK, (Parcelable) localeStack); |
| 184 | intent.putExtra(EXTRA_OPERATION, operationType); |
| 185 | |
| 186 | return intent; |
| 187 | } |
| 188 | |
| 189 | private static Snackbar createSharedSnackBar(Activity activity, int contentId, int fileCount) { |
| 190 | Resources res = activity.getResources(); |
| 191 | return Snackbars.makeSnackbar( |
| 192 | activity, |
| 193 | getQuantityString(activity, contentId, fileCount), |
| 194 | Snackbar.LENGTH_SHORT); |
| 195 | } |
| 196 | } |