blob: 0f1730a3e8bbe32c2a3b9cdf4e1a2540c2c1e917 [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 android.os.SystemClock.elapsedRealtime;
Steve McKayc83baa02016-01-06 18:32:13 -080020import static com.android.documentsui.Shared.DEBUG;
21import static com.android.documentsui.Shared.EXTRA_STACK;
22import static com.android.documentsui.Shared.asArrayList;
23import static com.android.documentsui.Shared.getQuantityString;
24import static com.android.documentsui.services.FileOperationService.EXTRA_CANCEL;
25import static com.android.documentsui.services.FileOperationService.EXTRA_JOB_ID;
26import static com.android.documentsui.services.FileOperationService.EXTRA_OPERATION;
27import static com.android.documentsui.services.FileOperationService.EXTRA_SRC_LIST;
28import static com.android.documentsui.services.FileOperationService.OPERATION_COPY;
29import static com.android.documentsui.services.FileOperationService.OPERATION_DELETE;
30import static com.android.documentsui.services.FileOperationService.OPERATION_MOVE;
31
32import android.app.Activity;
Steve McKaybbeba522016-01-13 17:17:39 -080033import android.content.Context;
Steve McKayc83baa02016-01-06 18:32:13 -080034import android.content.Intent;
35import android.content.res.Resources;
36import android.os.Parcelable;
37import android.support.design.widget.Snackbar;
38import android.util.Log;
39
40import com.android.documentsui.R;
41import com.android.documentsui.Snackbars;
42import com.android.documentsui.model.DocumentInfo;
43import com.android.documentsui.model.DocumentStack;
44import com.android.documentsui.services.FileOperationService.OpType;
45
46import java.util.List;
47
48/**
49 * Helper functions for starting various file operations.
50 */
51public final class FileOperations {
52
53 private static final String TAG = "FileOperations";
54
55 private FileOperations() {}
56
Steve McKaybbeba522016-01-13 17:17:39 -080057 public static String createJobId() {
58 return String.valueOf(elapsedRealtime());
59 }
60
Steve McKayc83baa02016-01-06 18:32:13 -080061 /**
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 McKaybbeba522016-01-13 17:17:39 -080087 * Use {@link #createJobId} if you don't have one handy.
Steve McKayc83baa02016-01-06 18:32:13 -080088 * @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 McKaybbeba522016-01-13 17:17:39 -0800106 * Use {@link #createJobId} if you don't have one handy.
Steve McKayc83baa02016-01-06 18:32:13 -0800107 * @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 McKaybbeba522016-01-13 17:17:39 -0800112 String jobId = createJobId();
Steve McKayc83baa02016-01-06 18:32:13 -0800113 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 McKaybbeba522016-01-13 17:17:39 -0800129 * Use {@link #createJobId} if you don't have one handy.
Steve McKayc83baa02016-01-06 18:32:13 -0800130 * @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 McKaybbeba522016-01-13 17:17:39 -0800135 String jobId = createJobId();
Steve McKayc83baa02016-01-06 18:32:13 -0800136 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 McKaybbeba522016-01-13 17:17:39 -0800152 * Use {@link #createJobId} if you don't have one handy.
Steve McKayc83baa02016-01-06 18:32:13 -0800153 * @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 McKaybbeba522016-01-13 17:17:39 -0800158 String jobId = createJobId();
Steve McKayc83baa02016-01-06 18:32:13 -0800159 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 McKaybbeba522016-01-13 17:17:39 -0800171 * Use {@link #createJobId} if you don't have one handy.
Steve McKayc83baa02016-01-06 18:32:13 -0800172 * @param srcDocs A list of src files to copy.
173 * @return Id of the job.
174 */
175 public static Intent createBaseIntent(
Steve McKaybbeba522016-01-13 17:17:39 -0800176 @OpType int operationType, Context context, String jobId,
Steve McKayc83baa02016-01-06 18:32:13 -0800177 List<DocumentInfo> srcDocs, DocumentStack localeStack) {
178
Steve McKaybbeba522016-01-13 17:17:39 -0800179 Intent intent = new Intent(context, FileOperationService.class);
Steve McKayc83baa02016-01-06 18:32:13 -0800180 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}