Soften requirement on cleanup step failures
[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.ActivityNotFoundException;
26 import android.content.Context;
27 import android.content.Intent;
28 import android.content.pm.PackageManager;
29 import android.content.pm.ResolveInfo;
30 import android.net.Uri;
31 import android.support.v4.app.DialogFragment;
32 import android.webkit.MimeTypeMap;
33 import android.widget.Toast;
34
35 import com.owncloud.android.R;
36 import com.owncloud.android.authentication.AccountUtils;
37 import com.owncloud.android.datamodel.OCFile;
38 import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
39 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
40 import com.owncloud.android.lib.common.network.WebdavUtils;
41 import com.owncloud.android.lib.common.utils.Log_OC;
42 import com.owncloud.android.lib.resources.shares.ShareType;
43 import com.owncloud.android.lib.resources.status.OwnCloudVersion;
44 import com.owncloud.android.services.OperationsService;
45 import com.owncloud.android.services.observer.FileObserverService;
46 import com.owncloud.android.ui.activity.FileActivity;
47 import com.owncloud.android.ui.activity.ShareActivity;
48 import com.owncloud.android.ui.dialog.ShareLinkToDialog;
49
50 import org.apache.http.protocol.HTTP;
51
52 import java.util.List;
53
54 /**
55 *
56 */
57 public class FileOperationsHelper {
58
59 private static final String TAG = FileOperationsHelper.class.getName();
60
61 private static final String FTAG_CHOOSER_DIALOG = "CHOOSER_DIALOG";
62
63 protected FileActivity mFileActivity = null;
64
65 /// Identifier of operation in progress which result shouldn't be lost
66 private long mWaitingForOpId = Long.MAX_VALUE;
67
68 public FileOperationsHelper(FileActivity fileActivity) {
69 mFileActivity = fileActivity;
70 }
71
72
73 public void openFile(OCFile file) {
74 if (file != null) {
75 String storagePath = file.getStoragePath();
76 String encodedStoragePath = WebdavUtils.encodePath(storagePath);
77
78 Intent intentForSavedMimeType = new Intent(Intent.ACTION_VIEW);
79 intentForSavedMimeType.setDataAndType(Uri.parse("file://"+ encodedStoragePath),
80 file.getMimetype());
81 intentForSavedMimeType.setFlags(
82 Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION
83 );
84
85 Intent intentForGuessedMimeType = null;
86 if (storagePath.lastIndexOf('.') >= 0) {
87 String guessedMimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(
88 storagePath.substring(storagePath.lastIndexOf('.') + 1)
89 );
90 if (guessedMimeType != null && !guessedMimeType.equals(file.getMimetype())) {
91 intentForGuessedMimeType = new Intent(Intent.ACTION_VIEW);
92 intentForGuessedMimeType.
93 setDataAndType(Uri.parse("file://"+ encodedStoragePath),
94 guessedMimeType);
95 intentForGuessedMimeType.setFlags(
96 Intent.FLAG_GRANT_READ_URI_PERMISSION |
97 Intent.FLAG_GRANT_WRITE_URI_PERMISSION
98 );
99 }
100 }
101
102 Intent openFileWithIntent;
103 if (intentForGuessedMimeType != null) {
104 openFileWithIntent = intentForGuessedMimeType;
105 } else {
106 openFileWithIntent = intentForSavedMimeType;
107 }
108
109 List<ResolveInfo> launchables = mFileActivity.getPackageManager().
110 queryIntentActivities(openFileWithIntent, PackageManager.GET_INTENT_FILTERS);
111
112 if(launchables != null && launchables.size() > 0) {
113 try {
114 mFileActivity.startActivity(
115 Intent.createChooser(
116 openFileWithIntent, mFileActivity.getString(R.string.actionbar_open_with)
117 )
118 );
119 } catch (ActivityNotFoundException anfe) {
120 showNoAppForFileTypeToast(mFileActivity.getApplicationContext());
121 }
122 } else {
123 showNoAppForFileTypeToast(mFileActivity.getApplicationContext());
124 }
125
126 } else {
127 Log_OC.wtf(TAG, "Trying to open a NULL OCFile");
128 }
129 }
130
131 /**
132 * Displays a toast stating that no application could be found to open the file.
133 *
134 * @param context the context to be able to show a toast.
135 */
136 private void showNoAppForFileTypeToast(Context context) {
137 Toast.makeText(context,
138 R.string.file_list_no_app_for_file_type, Toast.LENGTH_SHORT)
139 .show();
140 }
141
142 public void shareFileWithLink(OCFile file) {
143
144 if (isSharedSupported()) {
145 if (file != null) {
146 String link = "https://fake.url";
147 Intent intent = createShareWithLinkIntent(link);
148 String[] packagesToExclude = new String[]{mFileActivity.getPackageName()};
149 DialogFragment chooserDialog = ShareLinkToDialog.newInstance(intent,
150 packagesToExclude, file);
151 chooserDialog.show(mFileActivity.getSupportFragmentManager(), FTAG_CHOOSER_DIALOG);
152
153 } else {
154 Log_OC.wtf(TAG, "Trying to share a NULL OCFile");
155 }
156
157 } else {
158 // Show a Message
159 Toast t = Toast.makeText(
160 mFileActivity, mFileActivity.getString(R.string.share_link_no_support_share_api),
161 Toast.LENGTH_LONG
162 );
163 t.show();
164 }
165 }
166
167
168 public void shareFileWithLinkToApp(OCFile file, String password, Intent sendIntent) {
169
170 if (file != null) {
171 mFileActivity.showLoadingDialog(mFileActivity.getApplicationContext().
172 getString(R.string.wait_a_moment));
173
174 Intent service = new Intent(mFileActivity, OperationsService.class);
175 service.setAction(OperationsService.ACTION_CREATE_SHARE_VIA_LINK);
176 service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
177 service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
178 service.putExtra(OperationsService.EXTRA_PASSWORD_SHARE, password);
179 service.putExtra(OperationsService.EXTRA_SEND_INTENT, sendIntent);
180 mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
181
182 } else {
183 Log_OC.wtf(TAG, "Trying to open a NULL OCFile");
184 }
185 }
186
187
188 private Intent createShareWithLinkIntent(String link) {
189 Intent intentToShareLink = new Intent(Intent.ACTION_SEND);
190 intentToShareLink.putExtra(Intent.EXTRA_TEXT, link);
191 intentToShareLink.setType(HTTP.PLAIN_TEXT_TYPE);
192 return intentToShareLink;
193 }
194
195
196 /**
197 * Helper method to share a file with a know sharee. Starts a request to do it in {@link OperationsService}
198 *
199 * @param file The file to share.
200 * @param shareeName Name (user name or group name) of the target sharee.
201 * @param shareType The share type determines the sharee type.
202 */
203 public void shareFileWithSharee(OCFile file, String shareeName, ShareType shareType) {
204 if (file != null) {
205 // TODO check capability?
206 mFileActivity.showLoadingDialog(mFileActivity.getApplicationContext().
207 getString(R.string.wait_a_moment));
208
209 Intent service = new Intent(mFileActivity, OperationsService.class);
210 service.setAction(OperationsService.ACTION_CREATE_SHARE_WITH_SHAREE);
211 service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
212 service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
213 service.putExtra(OperationsService.EXTRA_SHARE_WITH, shareeName);
214 service.putExtra(OperationsService.EXTRA_SHARE_TYPE, shareType);
215 mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
216
217 } else {
218 Log_OC.wtf(TAG, "Trying to share a NULL OCFile");
219 }
220 }
221
222
223 /**
224 * @return 'True' if the server supports the Share API
225 */
226 public boolean isSharedSupported() {
227 if (mFileActivity.getAccount() != null) {
228 OwnCloudVersion serverVersion = AccountUtils.getServerVersion(mFileActivity.getAccount());
229 return (serverVersion != null && serverVersion.isSharedSupported());
230 }
231 return false;
232 }
233
234
235 public void unshareFileWithLink(OCFile file) {
236
237 // Unshare the file: Create the intent
238 Intent unshareService = new Intent(mFileActivity, OperationsService.class);
239 unshareService.setAction(OperationsService.ACTION_UNSHARE);
240 unshareService.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
241 unshareService.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
242 unshareService.putExtra(OperationsService.EXTRA_SHARE_TYPE, ShareType.PUBLIC_LINK);
243 unshareService.putExtra(OperationsService.EXTRA_SHARE_WITH, "");
244
245 unshareFile(unshareService);
246 }
247
248 public void unshareFileWithUserOrGroup(OCFile file, ShareType shareType, String userOrGroup){
249
250 // Unshare the file: Create the intent
251 Intent unshareService = new Intent(mFileActivity, OperationsService.class);
252 unshareService.setAction(OperationsService.ACTION_UNSHARE);
253 unshareService.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
254 unshareService.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
255 unshareService.putExtra(OperationsService.EXTRA_SHARE_TYPE, shareType);
256 unshareService.putExtra(OperationsService.EXTRA_SHARE_WITH, userOrGroup);
257
258 unshareFile(unshareService);
259 }
260
261
262 private void unshareFile(Intent unshareService){
263 if (isSharedSupported()) {
264 // Unshare the file
265 mWaitingForOpId = mFileActivity.getOperationsServiceBinder().
266 queueNewOperation(unshareService);
267
268 mFileActivity.showLoadingDialog(mFileActivity.getApplicationContext().
269 getString(R.string.wait_a_moment));
270
271 } else {
272 // Show a Message
273 Toast t = Toast.makeText(mFileActivity,
274 mFileActivity.getString(R.string.share_link_no_support_share_api),
275 Toast.LENGTH_LONG);
276 t.show();
277
278 }
279 }
280
281 /**
282 * Show an instance of {@link ShareType} for sharing or unsharing the {@OCFile} received as parameter.
283 *
284 * @param file File to share or unshare.
285 */
286 public void showShareFile(OCFile file){
287 Intent intent = new Intent(mFileActivity, ShareActivity.class);
288 intent.putExtra(mFileActivity.EXTRA_FILE, file);
289 intent.putExtra(mFileActivity.EXTRA_ACCOUNT, mFileActivity.getAccount());
290 mFileActivity.startActivity(intent);
291
292 }
293
294
295 /**
296 * @return 'True' if the server supports the Search Users API
297 */
298 public boolean isSearchUsersSupportedSupported() {
299 if (mFileActivity.getAccount() != null) {
300 OwnCloudVersion serverVersion = AccountUtils.getServerVersion(mFileActivity.getAccount());
301 return (serverVersion != null && serverVersion.isSearchUsersSupported());
302 }
303 return false;
304 }
305
306 public void sendDownloadedFile(OCFile file) {
307 if (file != null) {
308 String storagePath = file.getStoragePath();
309 String encodedStoragePath = WebdavUtils.encodePath(storagePath);
310 Intent sendIntent = new Intent(android.content.Intent.ACTION_SEND);
311 // set MimeType
312 sendIntent.setType(file.getMimetype());
313 sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + encodedStoragePath));
314 sendIntent.putExtra(Intent.ACTION_SEND, true); // Send Action
315
316 // Show dialog, without the own app
317 String[] packagesToExclude = new String[]{mFileActivity.getPackageName()};
318 DialogFragment chooserDialog = ShareLinkToDialog.newInstance(sendIntent,
319 packagesToExclude, file);
320 chooserDialog.show(mFileActivity.getSupportFragmentManager(), FTAG_CHOOSER_DIALOG);
321
322 } else {
323 Log_OC.wtf(TAG, "Trying to send a NULL OCFile");
324 }
325 }
326
327 /**
328 * Request the synchronization of a file or folder with the OC server, including its contents.
329 *
330 * @param file The file or folder to synchronize
331 */
332 public void syncFile(OCFile file) {
333 if (!file.isFolder()){
334 Intent intent = new Intent(mFileActivity, OperationsService.class);
335 intent.setAction(OperationsService.ACTION_SYNC_FILE);
336 intent.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
337 intent.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
338 intent.putExtra(OperationsService.EXTRA_SYNC_FILE_CONTENTS, true);
339 mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(intent);
340 mFileActivity.showLoadingDialog(mFileActivity.getApplicationContext().
341 getString(R.string.wait_a_moment));
342
343 } else {
344 Intent intent = new Intent(mFileActivity, OperationsService.class);
345 intent.setAction(OperationsService.ACTION_SYNC_FOLDER);
346 intent.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
347 intent.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
348 mFileActivity.startService(intent);
349
350 }
351 }
352
353 public void toggleFavorite(OCFile file, boolean isFavorite) {
354 file.setFavorite(isFavorite);
355 mFileActivity.getStorageManager().saveFile(file);
356
357 /// register the OCFile instance in the observer service to monitor local updates
358 Intent observedFileIntent = FileObserverService.makeObservedFileIntent(
359 mFileActivity,
360 file,
361 mFileActivity.getAccount(),
362 isFavorite);
363 mFileActivity.startService(observedFileIntent);
364
365 /// immediate content synchronization
366 if (file.isFavorite()) {
367 syncFile(file);
368 }
369 }
370
371 public void renameFile(OCFile file, String newFilename) {
372 // RenameFile
373 Intent service = new Intent(mFileActivity, OperationsService.class);
374 service.setAction(OperationsService.ACTION_RENAME);
375 service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
376 service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
377 service.putExtra(OperationsService.EXTRA_NEWNAME, newFilename);
378 mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
379
380 mFileActivity.showLoadingDialog(mFileActivity.getApplicationContext().
381 getString(R.string.wait_a_moment));
382 }
383
384
385 public void removeFile(OCFile file, boolean onlyLocalCopy) {
386 // RemoveFile
387 Intent service = new Intent(mFileActivity, OperationsService.class);
388 service.setAction(OperationsService.ACTION_REMOVE);
389 service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
390 service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
391 service.putExtra(OperationsService.EXTRA_REMOVE_ONLY_LOCAL, onlyLocalCopy);
392 mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
393
394 mFileActivity.showLoadingDialog(mFileActivity.getApplicationContext().
395 getString(R.string.wait_a_moment));
396 }
397
398
399 public void createFolder(String remotePath, boolean createFullPath) {
400 // Create Folder
401 Intent service = new Intent(mFileActivity, OperationsService.class);
402 service.setAction(OperationsService.ACTION_CREATE_FOLDER);
403 service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
404 service.putExtra(OperationsService.EXTRA_REMOTE_PATH, remotePath);
405 service.putExtra(OperationsService.EXTRA_CREATE_FULL_PATH, createFullPath);
406 mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
407
408 mFileActivity.showLoadingDialog(mFileActivity.getApplicationContext().
409 getString(R.string.wait_a_moment));
410 }
411
412 /**
413 * Cancel the transference in downloads (files/folders) and file uploads
414 * @param file OCFile
415 */
416 public void cancelTransference(OCFile file) {
417 Account account = mFileActivity.getAccount();
418 if (file.isFolder()) {
419 OperationsService.OperationsServiceBinder opsBinder =
420 mFileActivity.getOperationsServiceBinder();
421 if (opsBinder != null) {
422 opsBinder.cancel(account, file);
423 }
424 }
425
426 // for both files and folders
427 FileDownloaderBinder downloaderBinder = mFileActivity.getFileDownloaderBinder();
428 if (downloaderBinder != null && downloaderBinder.isDownloading(account, file)) {
429 downloaderBinder.cancel(account, file);
430 }
431 FileUploaderBinder uploaderBinder = mFileActivity.getFileUploaderBinder();
432 if (uploaderBinder != null && uploaderBinder.isUploading(account, file)) {
433 uploaderBinder.cancel(account, file);
434 }
435 }
436
437 /**
438 * Start move file operation
439 *
440 * @param newfile File where it is going to be moved
441 * @param currentFile File with the previous info
442 */
443 public void moveFile(OCFile newfile, OCFile currentFile) {
444 // Move files
445 Intent service = new Intent(mFileActivity, OperationsService.class);
446 service.setAction(OperationsService.ACTION_MOVE_FILE);
447 service.putExtra(OperationsService.EXTRA_NEW_PARENT_PATH, newfile.getRemotePath());
448 service.putExtra(OperationsService.EXTRA_REMOTE_PATH, currentFile.getRemotePath());
449 service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
450 mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
451
452 mFileActivity.showLoadingDialog(mFileActivity.getApplicationContext().
453 getString(R.string.wait_a_moment));
454 }
455
456 /**
457 * Start copy file operation
458 *
459 * @param newfile File where it is going to be moved
460 * @param currentFile File with the previous info
461 */
462 public void copyFile(OCFile newfile, OCFile currentFile) {
463 // Copy files
464 Intent service = new Intent(mFileActivity, OperationsService.class);
465 service.setAction(OperationsService.ACTION_COPY_FILE);
466 service.putExtra(OperationsService.EXTRA_NEW_PARENT_PATH, newfile.getRemotePath());
467 service.putExtra(OperationsService.EXTRA_REMOTE_PATH, currentFile.getRemotePath());
468 service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
469 mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
470
471 mFileActivity.showLoadingDialog(mFileActivity.getApplicationContext().
472 getString(R.string.wait_a_moment));
473 }
474
475 public long getOpIdWaitingFor() {
476 return mWaitingForOpId;
477 }
478
479
480 public void setOpIdWaitingFor(long waitingForOpId) {
481 mWaitingForOpId = waitingForOpId;
482 }
483
484 /**
485 * @return 'True' if the server doesn't need to check forbidden characters
486 */
487 public boolean isVersionWithForbiddenCharacters() {
488 if (mFileActivity.getAccount() != null) {
489 OwnCloudVersion serverVersion =
490 AccountUtils.getServerVersion(mFileActivity.getAccount());
491 return (serverVersion != null && serverVersion.isVersionWithForbiddenCharacters());
492 }
493 return false;
494 }
495 }