2 * ownCloud Android client application
5 * @author David A. Velasco
6 * Copyright (C) 2015 ownCloud Inc.
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.
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.
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/>.
22 package com
.owncloud
.android
.files
;
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
;
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 import com
.owncloud
.android
.ui
.dialog
.SharePasswordDialogFragment
;
52 import java
.util
.List
;
57 public class FileOperationsHelper
{
59 private static final String TAG
= FileOperationsHelper
.class.getName();
61 private static final String FTAG_CHOOSER_DIALOG
= "CHOOSER_DIALOG";
63 protected FileActivity mFileActivity
= null
;
65 /// Identifier of operation in progress which result shouldn't be lost
66 private long mWaitingForOpId
= Long
.MAX_VALUE
;
68 public FileOperationsHelper(FileActivity fileActivity
) {
69 mFileActivity
= fileActivity
;
73 public void openFile(OCFile file
) {
75 String storagePath
= file
.getStoragePath();
76 String encodedStoragePath
= WebdavUtils
.encodePath(storagePath
);
78 Intent intentForSavedMimeType
= new Intent(Intent
.ACTION_VIEW
);
79 intentForSavedMimeType
.setDataAndType(Uri
.parse("file://"+ encodedStoragePath
),
81 intentForSavedMimeType
.setFlags(
82 Intent
.FLAG_GRANT_READ_URI_PERMISSION
| Intent
.FLAG_GRANT_WRITE_URI_PERMISSION
85 Intent intentForGuessedMimeType
= null
;
86 if (storagePath
.lastIndexOf('.') >= 0) {
87 String guessedMimeType
= MimeTypeMap
.getSingleton().getMimeTypeFromExtension(
88 storagePath
.substring(storagePath
.lastIndexOf('.') + 1)
90 if (guessedMimeType
!= null
&& !guessedMimeType
.equals(file
.getMimetype())) {
91 intentForGuessedMimeType
= new Intent(Intent
.ACTION_VIEW
);
92 intentForGuessedMimeType
.
93 setDataAndType(Uri
.parse("file://"+ encodedStoragePath
),
95 intentForGuessedMimeType
.setFlags(
96 Intent
.FLAG_GRANT_READ_URI_PERMISSION
|
97 Intent
.FLAG_GRANT_WRITE_URI_PERMISSION
102 Intent openFileWithIntent
;
103 if (intentForGuessedMimeType
!= null
) {
104 openFileWithIntent
= intentForGuessedMimeType
;
106 openFileWithIntent
= intentForSavedMimeType
;
109 List
<ResolveInfo
> launchables
= mFileActivity
.getPackageManager().
110 queryIntentActivities(openFileWithIntent
, PackageManager
.GET_INTENT_FILTERS
);
112 if(launchables
!= null
&& launchables
.size() > 0) {
114 mFileActivity
.startActivity(
115 Intent
.createChooser(
116 openFileWithIntent
, mFileActivity
.getString(R
.string
.actionbar_open_with
)
119 } catch (ActivityNotFoundException anfe
) {
120 showNoAppForFileTypeToast(mFileActivity
.getApplicationContext());
123 showNoAppForFileTypeToast(mFileActivity
.getApplicationContext());
127 Log_OC
.wtf(TAG
, "Trying to open a NULL OCFile");
132 * Displays a toast stating that no application could be found to open the file.
134 * @param context the context to be able to show a toast.
136 private void showNoAppForFileTypeToast(Context context
) {
137 Toast
.makeText(context
,
138 R
.string
.file_list_no_app_for_file_type
, Toast
.LENGTH_SHORT
)
144 * Helper method to share a file via a public link. Starts a request to do it in {@link OperationsService}
146 * @param file The file to share.
148 public void shareFileViaLink(OCFile file
) {
149 if (isSharedSupported()) {
151 mFileActivity
.showLoadingDialog(
152 mFileActivity
.getApplicationContext().
153 getString(R
.string
.wait_a_moment
)
155 Intent service
= new Intent(mFileActivity
, OperationsService
.class);
156 service
.setAction(OperationsService
.ACTION_CREATE_SHARE_VIA_LINK
);
157 service
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
158 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
159 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().queueNewOperation(service
);
162 Log_OC
.wtf(TAG
, "Trying to share a NULL OCFile");
163 // TODO user-level error?
168 Toast t
= Toast
.makeText(
169 mFileActivity
, mFileActivity
.getString(R
.string
.share_link_no_support_share_api
),
176 public void getFileWithLink(OCFile file
){
177 if (isSharedSupported()) {
179 mFileActivity
.showLoadingDialog(mFileActivity
.getApplicationContext().
180 getString(R
.string
.wait_a_moment
));
182 Intent service
= new Intent(mFileActivity
, OperationsService
.class);
183 service
.setAction(OperationsService
.ACTION_CREATE_SHARE_VIA_LINK
);
184 service
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
185 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
186 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().queueNewOperation(service
);
189 Log_OC
.wtf(TAG
, "Trying to share a NULL OCFile");
193 Toast t
= Toast
.makeText(
194 mFileActivity
, mFileActivity
.getString(R
.string
.share_link_no_support_share_api
),
201 public void shareFileWithLinkToApp(OCFile file
, String password
, Intent sendIntent
) {
204 mFileActivity
.showLoadingDialog(mFileActivity
.getApplicationContext().
205 getString(R
.string
.wait_a_moment
));
207 Intent service
= new Intent(mFileActivity
, OperationsService
.class);
208 service
.setAction(OperationsService
.ACTION_CREATE_SHARE_VIA_LINK
);
209 service
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
210 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
211 service
.putExtra(OperationsService
.EXTRA_SHARE_PASSWORD
, password
);
212 service
.putExtra(OperationsService
.EXTRA_SEND_INTENT
, sendIntent
);
213 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().queueNewOperation(service
);
216 Log_OC
.wtf(TAG
, "Trying to open a NULL OCFile");
221 * Helper method to share a file with a known sharee. Starts a request to do it in {@link OperationsService}
223 * @param file The file to share.
224 * @param shareeName Name (user name or group name) of the target sharee.
225 * @param shareType The share type determines the sharee type.
227 public void shareFileWithSharee(OCFile file
, String shareeName
, ShareType shareType
) {
229 // TODO check capability?
230 mFileActivity
.showLoadingDialog(mFileActivity
.getApplicationContext().
231 getString(R
.string
.wait_a_moment
));
233 Intent service
= new Intent(mFileActivity
, OperationsService
.class);
234 service
.setAction(OperationsService
.ACTION_CREATE_SHARE_WITH_SHAREE
);
235 service
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
236 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
237 service
.putExtra(OperationsService
.EXTRA_SHARE_WITH
, shareeName
);
238 service
.putExtra(OperationsService
.EXTRA_SHARE_TYPE
, shareType
);
239 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().queueNewOperation(service
);
242 Log_OC
.wtf(TAG
, "Trying to share a NULL OCFile");
248 * @return 'True' if the server supports the Share API
250 public boolean isSharedSupported() {
251 if (mFileActivity
.getAccount() != null
) {
252 OwnCloudVersion serverVersion
= AccountUtils
.getServerVersion(mFileActivity
.getAccount());
253 return (serverVersion
!= null
&& serverVersion
.isSharedSupported());
260 * Helper method to unshare a file publicly shared via link.
261 * Starts a request to do it in {@link OperationsService}
263 * @param file The file to unshare.
265 public void unshareFileViaLink(OCFile file
) {
267 // Unshare the file: Create the intent
268 Intent unshareService
= new Intent(mFileActivity
, OperationsService
.class);
269 unshareService
.setAction(OperationsService
.ACTION_UNSHARE
);
270 unshareService
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
271 unshareService
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
272 unshareService
.putExtra(OperationsService
.EXTRA_SHARE_TYPE
, ShareType
.PUBLIC_LINK
);
273 unshareService
.putExtra(OperationsService
.EXTRA_SHARE_WITH
, "");
275 queueShareIntent(unshareService
);
278 public void unshareFileWithUserOrGroup(OCFile file
, ShareType shareType
, String userOrGroup
){
280 // Unshare the file: Create the intent
281 Intent unshareService
= new Intent(mFileActivity
, OperationsService
.class);
282 unshareService
.setAction(OperationsService
.ACTION_UNSHARE
);
283 unshareService
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
284 unshareService
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
285 unshareService
.putExtra(OperationsService
.EXTRA_SHARE_TYPE
, shareType
);
286 unshareService
.putExtra(OperationsService
.EXTRA_SHARE_WITH
, userOrGroup
);
288 queueShareIntent(unshareService
);
292 private void queueShareIntent(Intent shareIntent
){
293 if (isSharedSupported()) {
295 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().
296 queueNewOperation(shareIntent
);
298 mFileActivity
.showLoadingDialog(mFileActivity
.getApplicationContext().
299 getString(R
.string
.wait_a_moment
));
303 Toast t
= Toast
.makeText(mFileActivity
,
304 mFileActivity
.getString(R
.string
.share_link_no_support_share_api
),
312 * Show an instance of {@link ShareType} for sharing or unsharing the {@OCFile} received as parameter.
314 * @param file File to share or unshare.
316 public void showShareFile(OCFile file
){
317 Intent intent
= new Intent(mFileActivity
, ShareActivity
.class);
318 intent
.putExtra(mFileActivity
.EXTRA_FILE
, file
);
319 intent
.putExtra(mFileActivity
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
320 mFileActivity
.startActivity(intent
);
326 * Starts a dialog that requests a password to the user to protect a share link.
328 * @param file File which public share will be protected by the requested password
330 public void requestPasswordForShareViaLink(OCFile file
) {
331 SharePasswordDialogFragment dialog
=
332 SharePasswordDialogFragment
.newInstance(file
);
334 mFileActivity
.getSupportFragmentManager(),
335 SharePasswordDialogFragment
.PASSWORD_FRAGMENT
340 * Updates a public share on a file to set its password.
341 * Starts a request to do it in {@link OperationsService}
343 * @param file File which public share will be protected with a password.
344 * @param password Password to set for the public link; null or empty string to clear
345 * the current password
347 public void setPasswordToShareViaLink(OCFile file
, String password
) {
348 // Set password updating share
349 Intent updateShareIntent
= new Intent(mFileActivity
, OperationsService
.class);
350 updateShareIntent
.setAction(OperationsService
.ACTION_UPDATE_SHARE
);
351 updateShareIntent
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
352 updateShareIntent
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
353 updateShareIntent
.putExtra(
354 OperationsService
.EXTRA_SHARE_PASSWORD
,
355 (password
== null
) ?
"" : password
358 queueShareIntent(updateShareIntent
);
363 * Updates a public share on a file to set its expiration date.
364 * Starts a request to do it in {@link OperationsService}
366 * @param file File which public share will be constrained with an expiration date.
367 * @param expirationTimeInMillis Expiration date to set. A negative value clears the current expiration
368 * date, leaving the link unrestricted. Zero makes no change.
370 public void setExpirationDateToShareViaLink(OCFile file
, long expirationTimeInMillis
) {
371 Intent updateShareIntent
= new Intent(mFileActivity
, OperationsService
.class);
372 updateShareIntent
.setAction(OperationsService
.ACTION_UPDATE_SHARE
);
373 updateShareIntent
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
374 updateShareIntent
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
375 updateShareIntent
.putExtra(
376 OperationsService
.EXTRA_SHARE_EXPIRATION_DATE_IN_MILLIS
,
377 expirationTimeInMillis
379 queueShareIntent(updateShareIntent
);
384 * @return 'True' if the server supports the Search Users API
386 public boolean isSearchUsersSupportedSupported() {
387 if (mFileActivity
.getAccount() != null
) {
388 OwnCloudVersion serverVersion
= AccountUtils
.getServerVersion(mFileActivity
.getAccount());
389 return (serverVersion
!= null
&& serverVersion
.isSearchUsersSupported());
394 public void sendDownloadedFile(OCFile file
) {
396 String storagePath
= file
.getStoragePath();
397 String encodedStoragePath
= WebdavUtils
.encodePath(storagePath
);
398 Intent sendIntent
= new Intent(android
.content
.Intent
.ACTION_SEND
);
400 sendIntent
.setType(file
.getMimetype());
401 sendIntent
.putExtra(Intent
.EXTRA_STREAM
, Uri
.parse("file://" + encodedStoragePath
));
402 sendIntent
.putExtra(Intent
.ACTION_SEND
, true
); // Send Action
404 // Show dialog, without the own app
405 String
[] packagesToExclude
= new String
[]{mFileActivity
.getPackageName()};
406 DialogFragment chooserDialog
= ShareLinkToDialog
.newInstance(sendIntent
, packagesToExclude
);
407 chooserDialog
.show(mFileActivity
.getSupportFragmentManager(), FTAG_CHOOSER_DIALOG
);
410 Log_OC
.wtf(TAG
, "Trying to send a NULL OCFile");
415 * Request the synchronization of a file or folder with the OC server, including its contents.
417 * @param file The file or folder to synchronize
419 public void syncFile(OCFile file
) {
420 if (!file
.isFolder()){
421 Intent intent
= new Intent(mFileActivity
, OperationsService
.class);
422 intent
.setAction(OperationsService
.ACTION_SYNC_FILE
);
423 intent
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
424 intent
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
425 intent
.putExtra(OperationsService
.EXTRA_SYNC_FILE_CONTENTS
, true
);
426 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().queueNewOperation(intent
);
427 mFileActivity
.showLoadingDialog(mFileActivity
.getApplicationContext().
428 getString(R
.string
.wait_a_moment
));
431 Intent intent
= new Intent(mFileActivity
, OperationsService
.class);
432 intent
.setAction(OperationsService
.ACTION_SYNC_FOLDER
);
433 intent
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
434 intent
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
435 mFileActivity
.startService(intent
);
440 public void toggleFavorite(OCFile file
, boolean isFavorite
) {
441 file
.setFavorite(isFavorite
);
442 mFileActivity
.getStorageManager().saveFile(file
);
444 /// register the OCFile instance in the observer service to monitor local updates
445 Intent observedFileIntent
= FileObserverService
.makeObservedFileIntent(
448 mFileActivity
.getAccount(),
450 mFileActivity
.startService(observedFileIntent
);
452 /// immediate content synchronization
453 if (file
.isFavorite()) {
458 public void renameFile(OCFile file
, String newFilename
) {
460 Intent service
= new Intent(mFileActivity
, OperationsService
.class);
461 service
.setAction(OperationsService
.ACTION_RENAME
);
462 service
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
463 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
464 service
.putExtra(OperationsService
.EXTRA_NEWNAME
, newFilename
);
465 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().queueNewOperation(service
);
467 mFileActivity
.showLoadingDialog(mFileActivity
.getApplicationContext().
468 getString(R
.string
.wait_a_moment
));
472 public void removeFile(OCFile file
, boolean onlyLocalCopy
) {
474 Intent service
= new Intent(mFileActivity
, OperationsService
.class);
475 service
.setAction(OperationsService
.ACTION_REMOVE
);
476 service
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
477 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
478 service
.putExtra(OperationsService
.EXTRA_REMOVE_ONLY_LOCAL
, onlyLocalCopy
);
479 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().queueNewOperation(service
);
481 mFileActivity
.showLoadingDialog(mFileActivity
.getApplicationContext().
482 getString(R
.string
.wait_a_moment
));
486 public void createFolder(String remotePath
, boolean createFullPath
) {
488 Intent service
= new Intent(mFileActivity
, OperationsService
.class);
489 service
.setAction(OperationsService
.ACTION_CREATE_FOLDER
);
490 service
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
491 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, remotePath
);
492 service
.putExtra(OperationsService
.EXTRA_CREATE_FULL_PATH
, createFullPath
);
493 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().queueNewOperation(service
);
495 mFileActivity
.showLoadingDialog(mFileActivity
.getApplicationContext().
496 getString(R
.string
.wait_a_moment
));
500 * Cancel the transference in downloads (files/folders) and file uploads
503 public void cancelTransference(OCFile file
) {
504 Account account
= mFileActivity
.getAccount();
505 if (file
.isFolder()) {
506 OperationsService
.OperationsServiceBinder opsBinder
=
507 mFileActivity
.getOperationsServiceBinder();
508 if (opsBinder
!= null
) {
509 opsBinder
.cancel(account
, file
);
513 // for both files and folders
514 FileDownloaderBinder downloaderBinder
= mFileActivity
.getFileDownloaderBinder();
515 if (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(account
, file
)) {
516 downloaderBinder
.cancel(account
, file
);
518 FileUploaderBinder uploaderBinder
= mFileActivity
.getFileUploaderBinder();
519 if (uploaderBinder
!= null
&& uploaderBinder
.isUploading(account
, file
)) {
520 uploaderBinder
.cancel(account
, file
);
525 * Start move file operation
527 * @param newfile File where it is going to be moved
528 * @param currentFile File with the previous info
530 public void moveFile(OCFile newfile
, OCFile currentFile
) {
532 Intent service
= new Intent(mFileActivity
, OperationsService
.class);
533 service
.setAction(OperationsService
.ACTION_MOVE_FILE
);
534 service
.putExtra(OperationsService
.EXTRA_NEW_PARENT_PATH
, newfile
.getRemotePath());
535 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, currentFile
.getRemotePath());
536 service
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
537 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().queueNewOperation(service
);
539 mFileActivity
.showLoadingDialog(mFileActivity
.getApplicationContext().
540 getString(R
.string
.wait_a_moment
));
544 * Start copy file operation
546 * @param newfile File where it is going to be moved
547 * @param currentFile File with the previous info
549 public void copyFile(OCFile newfile
, OCFile currentFile
) {
551 Intent service
= new Intent(mFileActivity
, OperationsService
.class);
552 service
.setAction(OperationsService
.ACTION_COPY_FILE
);
553 service
.putExtra(OperationsService
.EXTRA_NEW_PARENT_PATH
, newfile
.getRemotePath());
554 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, currentFile
.getRemotePath());
555 service
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
556 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().queueNewOperation(service
);
558 mFileActivity
.showLoadingDialog(mFileActivity
.getApplicationContext().
559 getString(R
.string
.wait_a_moment
));
562 public long getOpIdWaitingFor() {
563 return mWaitingForOpId
;
567 public void setOpIdWaitingFor(long waitingForOpId
) {
568 mWaitingForOpId
= waitingForOpId
;
572 * @return 'True' if the server doesn't need to check forbidden characters
574 public boolean isVersionWithForbiddenCharacters() {
575 if (mFileActivity
.getAccount() != null
) {
576 OwnCloudVersion serverVersion
=
577 AccountUtils
.getServerVersion(mFileActivity
.getAccount());
578 return (serverVersion
!= null
&& serverVersion
.isVersionWithForbiddenCharacters());