Merge remote-tracking branch 'remotes/upstream/removeRadiosInMigration' into beta
[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.graphics.Bitmap;
29 import android.content.pm.PackageManager;
30 import android.content.pm.ResolveInfo;
31 import android.graphics.Bitmap;
32 import android.net.Uri;
33 import android.support.v4.app.DialogFragment;
34 import android.webkit.MimeTypeMap;
35 import android.widget.Toast;
36
37 import com.owncloud.android.MainApp;
38 import com.owncloud.android.R;
39 import com.owncloud.android.authentication.AccountUtils;
40 import com.owncloud.android.datamodel.OCFile;
41 import com.owncloud.android.datamodel.ThumbnailsCacheManager;
42 import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
43 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
44 import com.owncloud.android.lib.common.network.WebdavUtils;
45 import com.owncloud.android.lib.common.utils.Log_OC;
46 import com.owncloud.android.lib.resources.shares.ShareType;
47 import com.owncloud.android.lib.resources.status.OwnCloudVersion;
48 import com.owncloud.android.services.OperationsService;
49 import com.owncloud.android.services.observer.FileObserverService;
50 import com.owncloud.android.ui.activity.FileActivity;
51 import com.owncloud.android.ui.adapter.DiskLruImageCacheFileProvider;
52 import com.owncloud.android.ui.activity.ShareActivity;
53 import com.owncloud.android.ui.dialog.ShareLinkToDialog;
54 import com.owncloud.android.ui.dialog.SharePasswordDialogFragment;
55
56
57 import java.io.File;
58 import java.util.List;
59
60 import java.io.ByteArrayOutputStream;
61 import java.io.File;
62 import java.io.FileNotFoundException;
63 import java.io.FileOutputStream;
64 import java.io.IOException;
65
66 import org.apache.http.protocol.HTTP;
67
68 import java.util.ArrayList;
69 import java.util.List;
70
71 /**
72 *
73 */
74 public class FileOperationsHelper {
75
76 private static final String TAG = FileOperationsHelper.class.getName();
77
78 private static final String FTAG_CHOOSER_DIALOG = "CHOOSER_DIALOG";
79
80 protected FileActivity mFileActivity = null;
81
82 /// Identifier of operation in progress which result shouldn't be lost
83 private long mWaitingForOpId = Long.MAX_VALUE;
84
85 public FileOperationsHelper(FileActivity fileActivity) {
86 mFileActivity = fileActivity;
87 }
88
89
90 public void openFile(OCFile file) {
91 if (file != null) {
92 String storagePath = file.getStoragePath();
93 String encodedStoragePath = WebdavUtils.encodePath(storagePath);
94
95 Intent intentForSavedMimeType = new Intent(Intent.ACTION_VIEW);
96 intentForSavedMimeType.setDataAndType(Uri.parse("file://"+ encodedStoragePath),
97 file.getMimetype());
98 intentForSavedMimeType.setFlags(
99 Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION
100 );
101
102 Intent intentForGuessedMimeType = null;
103 if (storagePath.lastIndexOf('.') >= 0) {
104 String guessedMimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(
105 storagePath.substring(storagePath.lastIndexOf('.') + 1)
106 );
107 if (guessedMimeType != null && !guessedMimeType.equals(file.getMimetype())) {
108 intentForGuessedMimeType = new Intent(Intent.ACTION_VIEW);
109 intentForGuessedMimeType.
110 setDataAndType(Uri.parse("file://"+ encodedStoragePath),
111 guessedMimeType);
112 intentForGuessedMimeType.setFlags(
113 Intent.FLAG_GRANT_READ_URI_PERMISSION |
114 Intent.FLAG_GRANT_WRITE_URI_PERMISSION
115 );
116 }
117 }
118
119 Intent openFileWithIntent;
120 if (intentForGuessedMimeType != null) {
121 openFileWithIntent = intentForGuessedMimeType;
122 } else {
123 openFileWithIntent = intentForSavedMimeType;
124 }
125
126 List<ResolveInfo> launchables = mFileActivity.getPackageManager().
127 queryIntentActivities(openFileWithIntent, PackageManager.GET_INTENT_FILTERS);
128
129 if(launchables != null && launchables.size() > 0) {
130 try {
131 mFileActivity.startActivity(
132 Intent.createChooser(
133 openFileWithIntent, mFileActivity.getString(R.string.actionbar_open_with)
134 )
135 );
136 } catch (ActivityNotFoundException anfe) {
137 showNoAppForFileTypeToast(mFileActivity.getApplicationContext());
138 }
139 } else {
140 showNoAppForFileTypeToast(mFileActivity.getApplicationContext());
141 }
142
143 } else {
144 Log_OC.wtf(TAG, "Trying to open a NULL OCFile");
145 }
146 }
147
148 /**
149 * Displays a toast stating that no application could be found to open the file.
150 *
151 * @param context the context to be able to show a toast.
152 */
153 private void showNoAppForFileTypeToast(Context context) {
154 Toast.makeText(context,
155 R.string.file_list_no_app_for_file_type, Toast.LENGTH_SHORT)
156 .show();
157 }
158
159
160 /**
161 * Helper method to share a file via a public link. Starts a request to do it in {@link OperationsService}
162 *
163 * @param file The file to share.
164 * @param password Optional password to protect the public share.
165 */
166 public void shareFileViaLink(OCFile file, String password) {
167 if (isSharedSupported()) {
168 if (file != null) {
169 mFileActivity.showLoadingDialog(
170 mFileActivity.getApplicationContext().
171 getString(R.string.wait_a_moment)
172 );
173 Intent service = new Intent(mFileActivity, OperationsService.class);
174 service.setAction(OperationsService.ACTION_CREATE_SHARE_VIA_LINK);
175 service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
176 if (password != null && password.length() > 0) {
177 service.putExtra(OperationsService.EXTRA_SHARE_PASSWORD, password);
178 }
179 service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
180 mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
181
182 } else {
183 Log_OC.wtf(TAG, "Trying to share a NULL OCFile");
184 // TODO user-level error?
185 }
186
187 } else {
188 // Show a Message
189 Toast t = Toast.makeText(
190 mFileActivity, mFileActivity.getString(R.string.share_link_no_support_share_api),
191 Toast.LENGTH_LONG
192 );
193 t.show();
194 }
195 }
196
197 public void getFileWithLink(OCFile file){
198 if (isSharedSupported()) {
199 if (file != null) {
200 mFileActivity.showLoadingDialog(mFileActivity.getApplicationContext().
201 getString(R.string.wait_a_moment));
202
203 Intent service = new Intent(mFileActivity, OperationsService.class);
204 service.setAction(OperationsService.ACTION_CREATE_SHARE_VIA_LINK);
205 service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
206 service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
207 mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
208
209 } else {
210 Log_OC.wtf(TAG, "Trying to share a NULL OCFile");
211 }
212 } else {
213 // Show a Message
214 Toast t = Toast.makeText(
215 mFileActivity, mFileActivity.getString(R.string.share_link_no_support_share_api),
216 Toast.LENGTH_LONG
217 );
218 t.show();
219 }
220 }
221
222 public void shareFileWithLinkToApp(OCFile file, String password, Intent sendIntent) {
223
224 if (file != null) {
225 mFileActivity.showLoadingDialog(mFileActivity.getApplicationContext().
226 getString(R.string.wait_a_moment));
227
228 Intent service = new Intent(mFileActivity, OperationsService.class);
229 service.setAction(OperationsService.ACTION_CREATE_SHARE_VIA_LINK);
230 service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
231 service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
232 service.putExtra(OperationsService.EXTRA_SHARE_PASSWORD, password);
233 service.putExtra(OperationsService.EXTRA_SEND_INTENT, sendIntent);
234 mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
235
236 } else {
237 Log_OC.wtf(TAG, "Trying to open a NULL OCFile");
238 }
239 }
240
241 /**
242 * Helper method to share a file with a known sharee. Starts a request to do it in {@link OperationsService}
243 *
244 * @param file The file to share.
245 * @param shareeName Name (user name or group name) of the target sharee.
246 * @param shareType The share type determines the sharee type.
247 */
248 public void shareFileWithSharee(OCFile file, String shareeName, ShareType shareType) {
249 if (file != null) {
250 // TODO check capability?
251 mFileActivity.showLoadingDialog(mFileActivity.getApplicationContext().
252 getString(R.string.wait_a_moment));
253
254 Intent service = new Intent(mFileActivity, OperationsService.class);
255 service.setAction(OperationsService.ACTION_CREATE_SHARE_WITH_SHAREE);
256 service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
257 service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
258 service.putExtra(OperationsService.EXTRA_SHARE_WITH, shareeName);
259 service.putExtra(OperationsService.EXTRA_SHARE_TYPE, shareType);
260 mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
261
262 } else {
263 Log_OC.wtf(TAG, "Trying to share a NULL OCFile");
264 }
265 }
266
267
268 /**
269 * @return 'True' if the server supports the Share API
270 */
271 public boolean isSharedSupported() {
272 if (mFileActivity.getAccount() != null) {
273 OwnCloudVersion serverVersion = AccountUtils.getServerVersion(mFileActivity.getAccount());
274 return (serverVersion != null && serverVersion.isSharedSupported());
275 }
276 return false;
277 }
278
279
280 /**
281 * Helper method to unshare a file publicly shared via link.
282 * Starts a request to do it in {@link OperationsService}
283 *
284 * @param file The file to unshare.
285 */
286 public void unshareFileViaLink(OCFile file) {
287
288 // Unshare the file: Create the intent
289 Intent unshareService = new Intent(mFileActivity, OperationsService.class);
290 unshareService.setAction(OperationsService.ACTION_UNSHARE);
291 unshareService.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
292 unshareService.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
293 unshareService.putExtra(OperationsService.EXTRA_SHARE_TYPE, ShareType.PUBLIC_LINK);
294 unshareService.putExtra(OperationsService.EXTRA_SHARE_WITH, "");
295
296 queueShareIntent(unshareService);
297 }
298
299 public void unshareFileWithUserOrGroup(OCFile file, ShareType shareType, String userOrGroup){
300
301 // Unshare the file: Create the intent
302 Intent unshareService = new Intent(mFileActivity, OperationsService.class);
303 unshareService.setAction(OperationsService.ACTION_UNSHARE);
304 unshareService.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
305 unshareService.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
306 unshareService.putExtra(OperationsService.EXTRA_SHARE_TYPE, shareType);
307 unshareService.putExtra(OperationsService.EXTRA_SHARE_WITH, userOrGroup);
308
309 queueShareIntent(unshareService);
310 }
311
312
313 private void queueShareIntent(Intent shareIntent){
314 if (isSharedSupported()) {
315 // Unshare the file
316 mWaitingForOpId = mFileActivity.getOperationsServiceBinder().
317 queueNewOperation(shareIntent);
318
319 mFileActivity.showLoadingDialog(mFileActivity.getApplicationContext().
320 getString(R.string.wait_a_moment));
321
322 } else {
323 // Show a Message
324 Toast t = Toast.makeText(mFileActivity,
325 mFileActivity.getString(R.string.share_link_no_support_share_api),
326 Toast.LENGTH_LONG);
327 t.show();
328
329 }
330 }
331
332 /**
333 * Show an instance of {@link ShareType} for sharing or unsharing the {@OCFile} received as parameter.
334 *
335 * @param file File to share or unshare.
336 */
337 public void showShareFile(OCFile file){
338 Intent intent = new Intent(mFileActivity, ShareActivity.class);
339 intent.putExtra(mFileActivity.EXTRA_FILE, file);
340 intent.putExtra(mFileActivity.EXTRA_ACCOUNT, mFileActivity.getAccount());
341 mFileActivity.startActivity(intent);
342
343 }
344
345
346 /**
347 * Starts a dialog that requests a password to the user to protect a share link.
348 *
349 * @param file File which public share will be protected by the requested password
350 * @param createShare When 'true', the request for password will be followed by the creation of a new
351 * public link; when 'false', a public share is assumed to exist, and the password
352 * is bound to it.
353 */
354 public void requestPasswordForShareViaLink(OCFile file, boolean createShare) {
355 SharePasswordDialogFragment dialog =
356 SharePasswordDialogFragment.newInstance(file, createShare);
357 dialog.show(
358 mFileActivity.getSupportFragmentManager(),
359 SharePasswordDialogFragment.PASSWORD_FRAGMENT
360 );
361 }
362
363 /**
364 * Updates a public share on a file to set its password.
365 * Starts a request to do it in {@link OperationsService}
366 *
367 * @param file File which public share will be protected with a password.
368 * @param password Password to set for the public link; null or empty string to clear
369 * the current password
370 */
371 public void setPasswordToShareViaLink(OCFile file, String password) {
372 // Set password updating share
373 Intent updateShareIntent = new Intent(mFileActivity, OperationsService.class);
374 updateShareIntent.setAction(OperationsService.ACTION_UPDATE_SHARE);
375 updateShareIntent.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
376 updateShareIntent.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
377 updateShareIntent.putExtra(
378 OperationsService.EXTRA_SHARE_PASSWORD,
379 (password == null) ? "" : password
380 );
381
382 queueShareIntent(updateShareIntent);
383 }
384
385
386 /**
387 * Updates a public share on a file to set its expiration date.
388 * Starts a request to do it in {@link OperationsService}
389 *
390 * @param file File which public share will be constrained with an expiration date.
391 * @param expirationTimeInMillis Expiration date to set. A negative value clears the current expiration
392 * date, leaving the link unrestricted. Zero makes no change.
393 */
394 public void setExpirationDateToShareViaLink(OCFile file, long expirationTimeInMillis) {
395 Intent updateShareIntent = new Intent(mFileActivity, OperationsService.class);
396 updateShareIntent.setAction(OperationsService.ACTION_UPDATE_SHARE);
397 updateShareIntent.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
398 updateShareIntent.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
399 updateShareIntent.putExtra(
400 OperationsService.EXTRA_SHARE_EXPIRATION_DATE_IN_MILLIS,
401 expirationTimeInMillis
402 );
403 queueShareIntent(updateShareIntent);
404 }
405
406
407 /**
408 * @return 'True' if the server supports the Search Users API
409 */
410 public boolean isSearchUsersSupportedSupported() {
411 if (mFileActivity.getAccount() != null) {
412 OwnCloudVersion serverVersion = AccountUtils.getServerVersion(mFileActivity.getAccount());
413 return (serverVersion != null && serverVersion.isSearchUsersSupported());
414 }
415 return false;
416 }
417
418 public void sendDownloadedFile(OCFile file) {
419 if (file != null) {
420 String storagePath = file.getStoragePath();
421 String encodedStoragePath = WebdavUtils.encodePath(storagePath);
422 Intent sendIntent = new Intent(android.content.Intent.ACTION_SEND);
423 // set MimeType
424 sendIntent.setType(file.getMimetype());
425 sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + encodedStoragePath));
426 sendIntent.putExtra(Intent.ACTION_SEND, true); // Send Action
427
428 // Show dialog, without the own app
429 String[] packagesToExclude = new String[]{mFileActivity.getPackageName()};
430 DialogFragment chooserDialog = ShareLinkToDialog.newInstance(sendIntent, packagesToExclude);
431 chooserDialog.show(mFileActivity.getSupportFragmentManager(), FTAG_CHOOSER_DIALOG);
432
433 } else {
434 Log_OC.wtf(TAG, "Trying to send a NULL OCFile");
435 }
436 }
437
438 public void setPictureAs(OCFile file) {
439 if (file != null){
440 if (file.isDown()) {
441 File externalFile = new File(file.getStoragePath());
442 Uri sendUri = Uri.fromFile(externalFile);
443 Intent intent = new Intent(Intent.ACTION_ATTACH_DATA);
444 intent.setDataAndType(sendUri, file.getMimetype());
445 intent.putExtra("mimeType", file.getMimetype());
446 mFileActivity.startActivityForResult(Intent.createChooser(intent,
447 mFileActivity.getString(R.string.set_as)), 200);
448 } else {
449 // TODO re-enable after resized images is available
450 Uri sendUri = Uri.parse("content://" + DiskLruImageCacheFileProvider.AUTHORITY + file.getRemotePath());
451 Intent intent = new Intent(Intent.ACTION_ATTACH_DATA);
452 intent.setDataAndType(sendUri, file.getMimetype());
453 intent.putExtra("mimeType", file.getMimetype());
454 mFileActivity.startActivityForResult(Intent.createChooser(intent, "Set As"), 200);
455
456 // Intent sendIntent = new Intent(android.content.Intent.ACTION_SEND);
457 // // set MimeType
458 // sendIntent.setType(file.getMimetype());
459 //// sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("content://" + DiskLruImageCacheFileProvider.AUTHORITY + "/#" + file.getRemoteId() + "#" + file.getFileName()));
460 // sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("content://" + DiskLruImageCacheFileProvider.AUTHORITY + file.getRemotePath()));
461 // sendIntent.putExtra(Intent.ACTION_SEND, true); // Send Action
462 //
463 // // Show dialog, without the own app
464 // String[] packagesToExclude = new String[] { mFileActivity.getPackageName() };
465 // DialogFragment chooserDialog = ShareLinkToDialog.newInstance(sendIntent, packagesToExclude, file);
466 // chooserDialog.show(mFileActivity.getSupportFragmentManager(), FTAG_CHOOSER_DIALOG);
467 }
468 } else {
469 Log_OC.wtf(TAG, "Trying to send a NULL OCFile");
470 }
471 }
472
473 public void sendCachedImage(OCFile file) {
474 if (file != null) {
475 Intent sendIntent = new Intent(android.content.Intent.ACTION_SEND);
476 // set MimeType
477 sendIntent.setType(file.getMimetype());
478 // sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("content://" + DiskLruImageCacheFileProvider.AUTHORITY + "/#" + file.getRemoteId() + "#" + file.getFileName()));
479 sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("content://" + DiskLruImageCacheFileProvider.AUTHORITY + file.getRemotePath()));
480 sendIntent.putExtra(Intent.ACTION_SEND, true); // Send Action
481
482 // Show dialog, without the own app
483 String[] packagesToExclude = new String[] { mFileActivity.getPackageName() };
484 DialogFragment chooserDialog = ShareLinkToDialog.newInstance(sendIntent, packagesToExclude, file);
485 chooserDialog.show(mFileActivity.getSupportFragmentManager(), FTAG_CHOOSER_DIALOG);
486 } else {
487 Log_OC.wtf(TAG, "Trying to send a NULL OCFile");
488 }
489 }
490
491 public void syncFiles(ArrayList<OCFile> files) {
492 for (OCFile file: files) {
493 syncFile(file);
494 }
495 }
496
497 /**
498 * Request the synchronization of a file or folder with the OC server, including its contents.
499 *
500 * @param file The file or folder to synchronize
501 */
502 public void syncFile(OCFile file) {
503 if (!file.isFolder()){
504 Intent intent = new Intent(mFileActivity, OperationsService.class);
505 intent.setAction(OperationsService.ACTION_SYNC_FILE);
506 intent.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
507 intent.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
508 intent.putExtra(OperationsService.EXTRA_SYNC_FILE_CONTENTS, true);
509 mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(intent);
510 mFileActivity.showLoadingDialog(mFileActivity.getApplicationContext().
511 getString(R.string.wait_a_moment));
512
513 } else {
514 Intent intent = new Intent(mFileActivity, OperationsService.class);
515 intent.setAction(OperationsService.ACTION_SYNC_FOLDER);
516 intent.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
517 intent.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
518 mFileActivity.startService(intent);
519
520 }
521 }
522
523 public void toggleFavorites(ArrayList<OCFile> files, boolean isFavorite){
524 for (OCFile file: files) {
525 toggleFavorite(file, isFavorite);
526 }
527 }
528
529 public void toggleFavorite(OCFile file, boolean isFavorite) {
530 file.setFavorite(isFavorite);
531 mFileActivity.getStorageManager().saveFile(file);
532
533 /// register the OCFile instance in the observer service to monitor local updates
534 Intent observedFileIntent = FileObserverService.makeObservedFileIntent(
535 mFileActivity,
536 file,
537 mFileActivity.getAccount(),
538 isFavorite);
539 mFileActivity.startService(observedFileIntent);
540
541 /// immediate content synchronization
542 if (file.isFavorite()) {
543 syncFile(file);
544 }
545 }
546
547 public void renameFile(OCFile file, String newFilename) {
548 // RenameFile
549 Intent service = new Intent(mFileActivity, OperationsService.class);
550 service.setAction(OperationsService.ACTION_RENAME);
551 service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
552 service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
553 service.putExtra(OperationsService.EXTRA_NEWNAME, newFilename);
554 mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
555
556 mFileActivity.showLoadingDialog(mFileActivity.getApplicationContext().
557 getString(R.string.wait_a_moment));
558 }
559
560
561 public void removeFile(OCFile file, boolean onlyLocalCopy) {
562 // RemoveFile
563 Intent service = new Intent(mFileActivity, OperationsService.class);
564 service.setAction(OperationsService.ACTION_REMOVE);
565 service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
566 service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
567 service.putExtra(OperationsService.EXTRA_REMOVE_ONLY_LOCAL, onlyLocalCopy);
568 mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
569
570 mFileActivity.showLoadingDialog(mFileActivity.getApplicationContext().
571 getString(R.string.wait_a_moment));
572 }
573
574
575 public void createFolder(String remotePath, boolean createFullPath) {
576 // Create Folder
577 Intent service = new Intent(mFileActivity, OperationsService.class);
578 service.setAction(OperationsService.ACTION_CREATE_FOLDER);
579 service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
580 service.putExtra(OperationsService.EXTRA_REMOTE_PATH, remotePath);
581 service.putExtra(OperationsService.EXTRA_CREATE_FULL_PATH, createFullPath);
582 mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
583
584 mFileActivity.showLoadingDialog(mFileActivity.getApplicationContext().
585 getString(R.string.wait_a_moment));
586 }
587
588 /**
589 * Cancel the transference in downloads (files/folders) and file uploads
590 * @param file OCFile
591 */
592 public void cancelTransference(OCFile file) {
593 Account account = mFileActivity.getAccount();
594 if (file.isFolder()) {
595 OperationsService.OperationsServiceBinder opsBinder =
596 mFileActivity.getOperationsServiceBinder();
597 if (opsBinder != null) {
598 opsBinder.cancel(account, file);
599 }
600 }
601
602 // for both files and folders
603 FileDownloaderBinder downloaderBinder = mFileActivity.getFileDownloaderBinder();
604 if (downloaderBinder != null && downloaderBinder.isDownloading(account, file)) {
605 downloaderBinder.cancel(account, file);
606 }
607 FileUploaderBinder uploaderBinder = mFileActivity.getFileUploaderBinder();
608 if (uploaderBinder != null && uploaderBinder.isUploading(account, file)) {
609 uploaderBinder.cancel(account, file);
610 }
611 }
612
613 /**
614 * Start move file operation
615 *
616 * @param newfile File where it is going to be moved
617 * @param currentFile File with the previous info
618 */
619 public void moveFile(OCFile newfile, OCFile currentFile) {
620 // Move files
621 Intent service = new Intent(mFileActivity, OperationsService.class);
622 service.setAction(OperationsService.ACTION_MOVE_FILE);
623 service.putExtra(OperationsService.EXTRA_NEW_PARENT_PATH, newfile.getRemotePath());
624 service.putExtra(OperationsService.EXTRA_REMOTE_PATH, currentFile.getRemotePath());
625 service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
626 mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
627
628 // TODO Tobi loading dialog?
629 // mFileActivity.showLoadingDialog(mFileActivity.getApplicationContext().
630 // getString(R.string.wait_a_moment));
631 }
632
633 /**
634 * Start copy file operation
635 *
636 * @param newfile File where it is going to be moved
637 * @param currentFile File with the previous info
638 */
639 public void copyFile(OCFile newfile, OCFile currentFile) {
640 // Copy files
641 Intent service = new Intent(mFileActivity, OperationsService.class);
642 service.setAction(OperationsService.ACTION_COPY_FILE);
643 service.putExtra(OperationsService.EXTRA_NEW_PARENT_PATH, newfile.getRemotePath());
644 service.putExtra(OperationsService.EXTRA_REMOTE_PATH, currentFile.getRemotePath());
645 service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
646 mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
647
648 mFileActivity.showLoadingDialog(mFileActivity.getApplicationContext().
649 getString(R.string.wait_a_moment));
650 }
651
652 public long getOpIdWaitingFor() {
653 return mWaitingForOpId;
654 }
655
656
657 public void setOpIdWaitingFor(long waitingForOpId) {
658 mWaitingForOpId = waitingForOpId;
659 }
660
661 /**
662 * @return 'True' if the server doesn't need to check forbidden characters
663 */
664 public boolean isVersionWithForbiddenCharacters() {
665 if (mFileActivity.getAccount() != null) {
666 OwnCloudVersion serverVersion =
667 AccountUtils.getServerVersion(mFileActivity.getAccount());
668 return (serverVersion != null && serverVersion.isVersionWithForbiddenCharacters());
669 }
670 return false;
671 }
672
673 }