Merge remote-tracking branch 'remotes/upstream/multiSelect' into multiSelect
[pub/Android/ownCloud.git] / src / com / owncloud / android / files / FileOperationsHelper.java
1 /**
2 * ownCloud Android client application
3 *
4 * @author masensio
5 * @author David A. Velasco
6 * Copyright (C) 2015 ownCloud Inc.
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2,
10 * as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21
22 package com.owncloud.android.files;
23
24 import android.accounts.Account;
25 import android.content.Intent;
26 import android.net.Uri;
27 import android.support.v4.app.DialogFragment;
28 import android.webkit.MimeTypeMap;
29 import android.widget.Toast;
30
31 import com.owncloud.android.R;
32 import com.owncloud.android.authentication.AccountUtils;
33 import com.owncloud.android.datamodel.OCFile;
34 import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
35 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
36 import com.owncloud.android.lib.common.network.WebdavUtils;
37 import com.owncloud.android.lib.common.utils.Log_OC;
38 import com.owncloud.android.lib.resources.status.OwnCloudVersion;
39 import com.owncloud.android.services.OperationsService;
40 import com.owncloud.android.services.observer.FileObserverService;
41 import com.owncloud.android.ui.activity.FileActivity;
42 import com.owncloud.android.ui.dialog.ShareLinkToDialog;
43
44 import org.apache.http.protocol.HTTP;
45
46 import java.util.ArrayList;
47
48 /**
49 *
50 */
51 public class FileOperationsHelper {
52
53 private static final String TAG = FileOperationsHelper.class.getName();
54
55 private static final String FTAG_CHOOSER_DIALOG = "CHOOSER_DIALOG";
56
57 protected FileActivity mFileActivity = null;
58
59 /// Identifier of operation in progress which result shouldn't be lost
60 private long mWaitingForOpId = Long.MAX_VALUE;
61
62 public FileOperationsHelper(FileActivity fileActivity) {
63 mFileActivity = fileActivity;
64 }
65
66
67 public void openFile(OCFile file) {
68 if (file != null) {
69 String storagePath = file.getStoragePath();
70 String encodedStoragePath = WebdavUtils.encodePath(storagePath);
71
72 Intent intentForSavedMimeType = new Intent(Intent.ACTION_VIEW);
73 intentForSavedMimeType.setDataAndType(Uri.parse("file://"+ encodedStoragePath), file.getMimetype());
74 intentForSavedMimeType.setFlags(
75 Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION
76 );
77
78 Intent intentForGuessedMimeType = null;
79 if (storagePath.lastIndexOf('.') >= 0) {
80 String guessedMimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(
81 storagePath.substring(storagePath.lastIndexOf('.') + 1)
82 );
83 if (guessedMimeType != null && !guessedMimeType.equals(file.getMimetype())) {
84 intentForGuessedMimeType = new Intent(Intent.ACTION_VIEW);
85 intentForGuessedMimeType.setDataAndType(Uri.parse("file://"+ encodedStoragePath), guessedMimeType);
86 intentForGuessedMimeType.setFlags(
87 Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION
88 );
89 }
90 }
91
92 Intent chooserIntent;
93 if (intentForGuessedMimeType != null) {
94 chooserIntent = Intent.createChooser(intentForGuessedMimeType, mFileActivity.getString(R.string.actionbar_open_with));
95 } else {
96 chooserIntent = Intent.createChooser(intentForSavedMimeType, mFileActivity.getString(R.string.actionbar_open_with));
97 }
98
99 mFileActivity.startActivity(chooserIntent);
100
101 } else {
102 Log_OC.wtf(TAG, "Trying to open a NULL OCFile");
103 }
104 }
105
106
107 public void shareFileWithLink(OCFile file) {
108
109 if (isSharedSupported()) {
110 if (file != null) {
111 String link = "https://fake.url";
112 Intent intent = createShareWithLinkIntent(link);
113 String[] packagesToExclude = new String[]{mFileActivity.getPackageName()};
114 DialogFragment chooserDialog = ShareLinkToDialog.newInstance(intent, packagesToExclude, file);
115 chooserDialog.show(mFileActivity.getSupportFragmentManager(), FTAG_CHOOSER_DIALOG);
116
117 } else {
118 Log_OC.wtf(TAG, "Trying to share a NULL OCFile");
119 }
120
121 } else {
122 // Show a Message
123 Toast t = Toast.makeText(
124 mFileActivity, mFileActivity.getString(R.string.share_link_no_support_share_api), Toast.LENGTH_LONG
125 );
126 t.show();
127 }
128 }
129
130
131 public void shareFileWithLinkToApp(OCFile file, String password, Intent sendIntent) {
132
133 if (file != null) {
134 mFileActivity.showLoadingDialog();
135
136 Intent service = new Intent(mFileActivity, OperationsService.class);
137 service.setAction(OperationsService.ACTION_CREATE_SHARE);
138 service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
139 service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
140 service.putExtra(OperationsService.EXTRA_PASSWORD_SHARE, password);
141 service.putExtra(OperationsService.EXTRA_SEND_INTENT, sendIntent);
142 mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
143
144 } else {
145 Log_OC.wtf(TAG, "Trying to open a NULL OCFile");
146 }
147 }
148
149
150 private Intent createShareWithLinkIntent(String link) {
151 Intent intentToShareLink = new Intent(Intent.ACTION_SEND);
152 intentToShareLink.putExtra(Intent.EXTRA_TEXT, link);
153 intentToShareLink.setType(HTTP.PLAIN_TEXT_TYPE);
154 return intentToShareLink;
155 }
156
157
158 /**
159 * @return 'True' if the server supports the Share API
160 */
161 public boolean isSharedSupported() {
162 if (mFileActivity.getAccount() != null) {
163 OwnCloudVersion serverVersion = AccountUtils.getServerVersion(mFileActivity.getAccount());
164 return (serverVersion != null && serverVersion.isSharedSupported());
165 }
166 return false;
167 }
168
169
170 public void unshareFileWithLink(OCFile file) {
171
172 if (isSharedSupported()) {
173 // Unshare the file
174 Intent service = new Intent(mFileActivity, OperationsService.class);
175 service.setAction(OperationsService.ACTION_UNSHARE);
176 service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
177 service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
178 mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
179
180 mFileActivity.showLoadingDialog();
181
182 } else {
183 // Show a Message
184 Toast t = Toast.makeText(mFileActivity, mFileActivity.getString(R.string.share_link_no_support_share_api), Toast.LENGTH_LONG);
185 t.show();
186
187 }
188 }
189
190 public void sendDownloadedFile(OCFile file) {
191 if (file != null) {
192 String storagePath = file.getStoragePath();
193 String encodedStoragePath = WebdavUtils.encodePath(storagePath);
194 Intent sendIntent = new Intent(android.content.Intent.ACTION_SEND);
195 // set MimeType
196 sendIntent.setType(file.getMimetype());
197 sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + encodedStoragePath));
198 sendIntent.putExtra(Intent.ACTION_SEND, true); // Send Action
199
200 // Show dialog, without the own app
201 String[] packagesToExclude = new String[]{mFileActivity.getPackageName()};
202 DialogFragment chooserDialog = ShareLinkToDialog.newInstance(sendIntent, packagesToExclude, file);
203 chooserDialog.show(mFileActivity.getSupportFragmentManager(), FTAG_CHOOSER_DIALOG);
204
205 } else {
206 Log_OC.wtf(TAG, "Trying to send a NULL OCFile");
207 }
208 }
209
210 public void syncFiles(ArrayList<OCFile> files) {
211 for (OCFile file: files) {
212 syncFile(file);
213 }
214 }
215
216 public void syncFile(OCFile file) {
217 if (!file.isFolder()) {
218 Intent intent = new Intent(mFileActivity, OperationsService.class);
219 intent.setAction(OperationsService.ACTION_SYNC_FILE);
220 intent.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
221 intent.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
222 intent.putExtra(OperationsService.EXTRA_SYNC_FILE_CONTENTS, true);
223 mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(intent);
224 mFileActivity.showLoadingDialog();
225
226 } else {
227 Intent intent = new Intent(mFileActivity, OperationsService.class);
228 intent.setAction(OperationsService.ACTION_SYNC_FOLDER);
229 intent.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
230 intent.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
231 mFileActivity.startService(intent);
232 }
233 }
234
235 public void toggleFavorites(ArrayList<OCFile> files, boolean isFavorite){
236 for (OCFile file: files) {
237 toggleFavorite(file, isFavorite);
238 }
239 }
240
241 public void toggleFavorite(OCFile file, boolean isFavorite) {
242 file.setFavorite(isFavorite);
243 mFileActivity.getStorageManager().saveFile(file);
244
245 /// register the OCFile instance in the observer service to monitor local updates
246 Intent observedFileIntent = FileObserverService.makeObservedFileIntent(
247 mFileActivity,
248 file,
249 mFileActivity.getAccount(),
250 isFavorite);
251 mFileActivity.startService(observedFileIntent);
252
253 /// immediate content synchronization
254 if (file.isFavorite()) {
255 syncFile(file);
256 }
257 }
258
259 public void renameFile(OCFile file, String newFilename) {
260 // RenameFile
261 Intent service = new Intent(mFileActivity, OperationsService.class);
262 service.setAction(OperationsService.ACTION_RENAME);
263 service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
264 service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
265 service.putExtra(OperationsService.EXTRA_NEWNAME, newFilename);
266 mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
267
268 mFileActivity.showLoadingDialog();
269 }
270
271
272 public void removeFile(OCFile file, boolean onlyLocalCopy) {
273 // RemoveFile
274 Intent service = new Intent(mFileActivity, OperationsService.class);
275 service.setAction(OperationsService.ACTION_REMOVE);
276 service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
277 service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
278 service.putExtra(OperationsService.EXTRA_REMOVE_ONLY_LOCAL, onlyLocalCopy);
279 mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
280
281 mFileActivity.showLoadingDialog();
282 }
283
284
285 public void createFolder(String remotePath, boolean createFullPath) {
286 // Create Folder
287 Intent service = new Intent(mFileActivity, OperationsService.class);
288 service.setAction(OperationsService.ACTION_CREATE_FOLDER);
289 service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
290 service.putExtra(OperationsService.EXTRA_REMOTE_PATH, remotePath);
291 service.putExtra(OperationsService.EXTRA_CREATE_FULL_PATH, createFullPath);
292 mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
293
294 mFileActivity.showLoadingDialog();
295 }
296
297 /**
298 * Cancel the transference in downloads (files/folders) and file uploads
299 * @param file OCFile
300 */
301 public void cancelTransference(OCFile file) {
302 Account account = mFileActivity.getAccount();
303 if (file.isFolder()) {
304 OperationsService.OperationsServiceBinder opsBinder = mFileActivity.getOperationsServiceBinder();
305 if (opsBinder != null) {
306 opsBinder.cancel(account, file);
307 }
308 }
309
310 // for both files and folders
311 FileDownloaderBinder downloaderBinder = mFileActivity.getFileDownloaderBinder();
312 FileUploaderBinder uploaderBinder = mFileActivity.getFileUploaderBinder();
313 if (downloaderBinder != null && downloaderBinder.isDownloading(account, file)) {
314 downloaderBinder.cancel(account, file);
315
316 // TODO - review why is this here, and solve in a better way
317 // Remove etag for parent, if file is a favorite
318 if (file.isFavorite()) {
319 OCFile parent = mFileActivity.getStorageManager().getFileById(file.getParentId());
320 parent.setEtag("");
321 mFileActivity.getStorageManager().saveFile(parent);
322 }
323
324 } else if (uploaderBinder != null && uploaderBinder.isUploading(account, file)) {
325 uploaderBinder.cancel(account, file);
326 }
327 }
328
329 /**
330 * Start move file operation
331 *
332 * @param newfile File where it is going to be moved
333 * @param currentFile File with the previous info
334 */
335 public void moveFile(OCFile newfile, OCFile currentFile) {
336 // Move files
337 Intent service = new Intent(mFileActivity, OperationsService.class);
338 service.setAction(OperationsService.ACTION_MOVE_FILE);
339 service.putExtra(OperationsService.EXTRA_NEW_PARENT_PATH, newfile.getRemotePath());
340 service.putExtra(OperationsService.EXTRA_REMOTE_PATH, currentFile.getRemotePath());
341 service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
342 mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
343
344 // TODO Tobi loading dialog?
345 // mFileActivity.showLoadingDialog();
346 }
347
348 /**
349 * Start copy file operation
350 *
351 * @param newfile File where it is going to be moved
352 * @param currentFile File with the previous info
353 */
354 public void copyFile(OCFile newfile, OCFile currentFile) {
355 // Copy files
356 Intent service = new Intent(mFileActivity, OperationsService.class);
357 service.setAction(OperationsService.ACTION_COPY_FILE);
358 service.putExtra(OperationsService.EXTRA_NEW_PARENT_PATH, newfile.getRemotePath());
359 service.putExtra(OperationsService.EXTRA_REMOTE_PATH, currentFile.getRemotePath());
360 service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
361 mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
362
363 mFileActivity.showLoadingDialog();
364 }
365
366 public long getOpIdWaitingFor() {
367 return mWaitingForOpId;
368 }
369
370
371 public void setOpIdWaitingFor(long waitingForOpId) {
372 mWaitingForOpId = waitingForOpId;
373 }
374
375 /**
376 * @return 'True' if the server doesn't need to check forbidden characters
377 */
378 public boolean isVersionWithForbiddenCharacters() {
379 if (mFileActivity.getAccount() != null) {
380 OwnCloudVersion serverVersion = AccountUtils.getServerVersion(mFileActivity.getAccount());
381 return (serverVersion != null && serverVersion.isVersionWithForbiddenCharacters());
382 }
383 return false;
384 }
385 }