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.
147 * @param password Optional password to protect the public share.
149 public void shareFileViaLink(OCFile file
, String password
) {
150 if (isSharedSupported()) {
152 mFileActivity
.showLoadingDialog(
153 mFileActivity
.getApplicationContext().
154 getString(R
.string
.wait_a_moment
)
156 Intent service
= new Intent(mFileActivity
, OperationsService
.class);
157 service
.setAction(OperationsService
.ACTION_CREATE_SHARE_VIA_LINK
);
158 service
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
159 if (password
!= null
&& password
.length() > 0) {
160 service
.putExtra(OperationsService
.EXTRA_SHARE_PASSWORD
, password
);
162 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
163 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().queueNewOperation(service
);
166 Log_OC
.wtf(TAG
, "Trying to share a NULL OCFile");
167 // TODO user-level error?
172 Toast t
= Toast
.makeText(
173 mFileActivity
, mFileActivity
.getString(R
.string
.share_link_no_support_share_api
),
180 public void getFileWithLink(OCFile file
){
181 if (isSharedSupported()) {
183 mFileActivity
.showLoadingDialog(mFileActivity
.getApplicationContext().
184 getString(R
.string
.wait_a_moment
));
186 Intent service
= new Intent(mFileActivity
, OperationsService
.class);
187 service
.setAction(OperationsService
.ACTION_CREATE_SHARE_VIA_LINK
);
188 service
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
189 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
190 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().queueNewOperation(service
);
193 Log_OC
.wtf(TAG
, "Trying to share a NULL OCFile");
197 Toast t
= Toast
.makeText(
198 mFileActivity
, mFileActivity
.getString(R
.string
.share_link_no_support_share_api
),
205 public void shareFileWithLinkToApp(OCFile file
, String password
, Intent sendIntent
) {
208 mFileActivity
.showLoadingDialog(mFileActivity
.getApplicationContext().
209 getString(R
.string
.wait_a_moment
));
211 Intent service
= new Intent(mFileActivity
, OperationsService
.class);
212 service
.setAction(OperationsService
.ACTION_CREATE_SHARE_VIA_LINK
);
213 service
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
214 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
215 service
.putExtra(OperationsService
.EXTRA_SHARE_PASSWORD
, password
);
216 service
.putExtra(OperationsService
.EXTRA_SEND_INTENT
, sendIntent
);
217 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().queueNewOperation(service
);
220 Log_OC
.wtf(TAG
, "Trying to open a NULL OCFile");
225 * Helper method to share a file with a known sharee. Starts a request to do it in {@link OperationsService}
227 * @param file The file to share.
228 * @param shareeName Name (user name or group name) of the target sharee.
229 * @param shareType The share type determines the sharee type.
231 public void shareFileWithSharee(OCFile file
, String shareeName
, ShareType shareType
) {
233 // TODO check capability?
234 mFileActivity
.showLoadingDialog(mFileActivity
.getApplicationContext().
235 getString(R
.string
.wait_a_moment
));
237 Intent service
= new Intent(mFileActivity
, OperationsService
.class);
238 service
.setAction(OperationsService
.ACTION_CREATE_SHARE_WITH_SHAREE
);
239 service
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
240 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
241 service
.putExtra(OperationsService
.EXTRA_SHARE_WITH
, shareeName
);
242 service
.putExtra(OperationsService
.EXTRA_SHARE_TYPE
, shareType
);
243 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().queueNewOperation(service
);
246 Log_OC
.wtf(TAG
, "Trying to share a NULL OCFile");
252 * @return 'True' if the server supports the Share API
254 public boolean isSharedSupported() {
255 if (mFileActivity
.getAccount() != null
) {
256 OwnCloudVersion serverVersion
= AccountUtils
.getServerVersion(mFileActivity
.getAccount());
257 return (serverVersion
!= null
&& serverVersion
.isSharedSupported());
264 * Helper method to unshare a file publicly shared via link.
265 * Starts a request to do it in {@link OperationsService}
267 * @param file The file to unshare.
269 public void unshareFileViaLink(OCFile file
) {
271 // Unshare the file: Create the intent
272 Intent unshareService
= new Intent(mFileActivity
, OperationsService
.class);
273 unshareService
.setAction(OperationsService
.ACTION_UNSHARE
);
274 unshareService
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
275 unshareService
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
276 unshareService
.putExtra(OperationsService
.EXTRA_SHARE_TYPE
, ShareType
.PUBLIC_LINK
);
277 unshareService
.putExtra(OperationsService
.EXTRA_SHARE_WITH
, "");
279 queueShareIntent(unshareService
);
282 public void unshareFileWithUserOrGroup(OCFile file
, ShareType shareType
, String userOrGroup
){
284 // Unshare the file: Create the intent
285 Intent unshareService
= new Intent(mFileActivity
, OperationsService
.class);
286 unshareService
.setAction(OperationsService
.ACTION_UNSHARE
);
287 unshareService
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
288 unshareService
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
289 unshareService
.putExtra(OperationsService
.EXTRA_SHARE_TYPE
, shareType
);
290 unshareService
.putExtra(OperationsService
.EXTRA_SHARE_WITH
, userOrGroup
);
292 queueShareIntent(unshareService
);
296 private void queueShareIntent(Intent shareIntent
){
297 if (isSharedSupported()) {
299 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().
300 queueNewOperation(shareIntent
);
302 mFileActivity
.showLoadingDialog(mFileActivity
.getApplicationContext().
303 getString(R
.string
.wait_a_moment
));
307 Toast t
= Toast
.makeText(mFileActivity
,
308 mFileActivity
.getString(R
.string
.share_link_no_support_share_api
),
316 * Show an instance of {@link ShareType} for sharing or unsharing the {@OCFile} received as parameter.
318 * @param file File to share or unshare.
320 public void showShareFile(OCFile file
){
321 Intent intent
= new Intent(mFileActivity
, ShareActivity
.class);
322 intent
.putExtra(mFileActivity
.EXTRA_FILE
, file
);
323 intent
.putExtra(mFileActivity
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
324 mFileActivity
.startActivity(intent
);
330 * Starts a dialog that requests a password to the user to protect a share link.
332 * @param file File which public share will be protected by the requested password
333 * @param createShare When 'true', the request for password will be followed by the creation of a new
334 * public link; when 'false', a public share is assumed to exist, and the password
337 public void requestPasswordForShareViaLink(OCFile file
, boolean createShare
) {
338 SharePasswordDialogFragment dialog
=
339 SharePasswordDialogFragment
.newInstance(file
, createShare
);
341 mFileActivity
.getSupportFragmentManager(),
342 SharePasswordDialogFragment
.PASSWORD_FRAGMENT
347 * Updates a public share on a file to set its password.
348 * Starts a request to do it in {@link OperationsService}
350 * @param file File which public share will be protected with a password.
351 * @param password Password to set for the public link; null or empty string to clear
352 * the current password
354 public void setPasswordToShareViaLink(OCFile file
, String password
) {
355 // Set password updating share
356 Intent updateShareIntent
= new Intent(mFileActivity
, OperationsService
.class);
357 updateShareIntent
.setAction(OperationsService
.ACTION_UPDATE_SHARE
);
358 updateShareIntent
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
359 updateShareIntent
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
360 updateShareIntent
.putExtra(
361 OperationsService
.EXTRA_SHARE_PASSWORD
,
362 (password
== null
) ?
"" : password
365 queueShareIntent(updateShareIntent
);
370 * Updates a public share on a file to set its expiration date.
371 * Starts a request to do it in {@link OperationsService}
373 * @param file File which public share will be constrained with an expiration date.
374 * @param expirationTimeInMillis Expiration date to set. A negative value clears the current expiration
375 * date, leaving the link unrestricted. Zero makes no change.
377 public void setExpirationDateToShareViaLink(OCFile file
, long expirationTimeInMillis
) {
378 Intent updateShareIntent
= new Intent(mFileActivity
, OperationsService
.class);
379 updateShareIntent
.setAction(OperationsService
.ACTION_UPDATE_SHARE
);
380 updateShareIntent
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
381 updateShareIntent
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
382 updateShareIntent
.putExtra(
383 OperationsService
.EXTRA_SHARE_EXPIRATION_DATE_IN_MILLIS
,
384 expirationTimeInMillis
386 queueShareIntent(updateShareIntent
);
391 * @return 'True' if the server supports the Search Users API
393 public boolean isSearchUsersSupportedSupported() {
394 if (mFileActivity
.getAccount() != null
) {
395 OwnCloudVersion serverVersion
= AccountUtils
.getServerVersion(mFileActivity
.getAccount());
396 return (serverVersion
!= null
&& serverVersion
.isSearchUsersSupported());
401 public void sendDownloadedFile(OCFile file
) {
403 String storagePath
= file
.getStoragePath();
404 String encodedStoragePath
= WebdavUtils
.encodePath(storagePath
);
405 Intent sendIntent
= new Intent(android
.content
.Intent
.ACTION_SEND
);
407 sendIntent
.setType(file
.getMimetype());
408 sendIntent
.putExtra(Intent
.EXTRA_STREAM
, Uri
.parse("file://" + encodedStoragePath
));
409 sendIntent
.putExtra(Intent
.ACTION_SEND
, true
); // Send Action
411 // Show dialog, without the own app
412 String
[] packagesToExclude
= new String
[]{mFileActivity
.getPackageName()};
413 DialogFragment chooserDialog
= ShareLinkToDialog
.newInstance(sendIntent
, packagesToExclude
);
414 chooserDialog
.show(mFileActivity
.getSupportFragmentManager(), FTAG_CHOOSER_DIALOG
);
417 Log_OC
.wtf(TAG
, "Trying to send a NULL OCFile");
422 * Request the synchronization of a file or folder with the OC server, including its contents.
424 * @param file The file or folder to synchronize
426 public void syncFile(OCFile file
) {
427 if (!file
.isFolder()){
428 Intent intent
= new Intent(mFileActivity
, OperationsService
.class);
429 intent
.setAction(OperationsService
.ACTION_SYNC_FILE
);
430 intent
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
431 intent
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
432 intent
.putExtra(OperationsService
.EXTRA_SYNC_FILE_CONTENTS
, true
);
433 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().queueNewOperation(intent
);
434 mFileActivity
.showLoadingDialog(mFileActivity
.getApplicationContext().
435 getString(R
.string
.wait_a_moment
));
438 Intent intent
= new Intent(mFileActivity
, OperationsService
.class);
439 intent
.setAction(OperationsService
.ACTION_SYNC_FOLDER
);
440 intent
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
441 intent
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
442 mFileActivity
.startService(intent
);
447 public void toggleFavorite(OCFile file
, boolean isFavorite
) {
448 file
.setFavorite(isFavorite
);
449 mFileActivity
.getStorageManager().saveFile(file
);
451 /// register the OCFile instance in the observer service to monitor local updates
452 Intent observedFileIntent
= FileObserverService
.makeObservedFileIntent(
455 mFileActivity
.getAccount(),
457 mFileActivity
.startService(observedFileIntent
);
459 /// immediate content synchronization
460 if (file
.isFavorite()) {
465 public void renameFile(OCFile file
, String newFilename
) {
467 Intent service
= new Intent(mFileActivity
, OperationsService
.class);
468 service
.setAction(OperationsService
.ACTION_RENAME
);
469 service
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
470 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
471 service
.putExtra(OperationsService
.EXTRA_NEWNAME
, newFilename
);
472 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().queueNewOperation(service
);
474 mFileActivity
.showLoadingDialog(mFileActivity
.getApplicationContext().
475 getString(R
.string
.wait_a_moment
));
479 public void removeFile(OCFile file
, boolean onlyLocalCopy
) {
481 Intent service
= new Intent(mFileActivity
, OperationsService
.class);
482 service
.setAction(OperationsService
.ACTION_REMOVE
);
483 service
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
484 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
485 service
.putExtra(OperationsService
.EXTRA_REMOVE_ONLY_LOCAL
, onlyLocalCopy
);
486 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().queueNewOperation(service
);
488 mFileActivity
.showLoadingDialog(mFileActivity
.getApplicationContext().
489 getString(R
.string
.wait_a_moment
));
493 public void createFolder(String remotePath
, boolean createFullPath
) {
495 Intent service
= new Intent(mFileActivity
, OperationsService
.class);
496 service
.setAction(OperationsService
.ACTION_CREATE_FOLDER
);
497 service
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
498 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, remotePath
);
499 service
.putExtra(OperationsService
.EXTRA_CREATE_FULL_PATH
, createFullPath
);
500 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().queueNewOperation(service
);
502 mFileActivity
.showLoadingDialog(mFileActivity
.getApplicationContext().
503 getString(R
.string
.wait_a_moment
));
507 * Cancel the transference in downloads (files/folders) and file uploads
510 public void cancelTransference(OCFile file
) {
511 Account account
= mFileActivity
.getAccount();
512 if (file
.isFolder()) {
513 OperationsService
.OperationsServiceBinder opsBinder
=
514 mFileActivity
.getOperationsServiceBinder();
515 if (opsBinder
!= null
) {
516 opsBinder
.cancel(account
, file
);
520 // for both files and folders
521 FileDownloaderBinder downloaderBinder
= mFileActivity
.getFileDownloaderBinder();
522 if (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(account
, file
)) {
523 downloaderBinder
.cancel(account
, file
);
525 FileUploaderBinder uploaderBinder
= mFileActivity
.getFileUploaderBinder();
526 if (uploaderBinder
!= null
&& uploaderBinder
.isUploading(account
, file
)) {
527 uploaderBinder
.cancel(account
, file
);
532 * Start move file operation
534 * @param newfile File where it is going to be moved
535 * @param currentFile File with the previous info
537 public void moveFile(OCFile newfile
, OCFile currentFile
) {
539 Intent service
= new Intent(mFileActivity
, OperationsService
.class);
540 service
.setAction(OperationsService
.ACTION_MOVE_FILE
);
541 service
.putExtra(OperationsService
.EXTRA_NEW_PARENT_PATH
, newfile
.getRemotePath());
542 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, currentFile
.getRemotePath());
543 service
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
544 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().queueNewOperation(service
);
546 mFileActivity
.showLoadingDialog(mFileActivity
.getApplicationContext().
547 getString(R
.string
.wait_a_moment
));
551 * Start copy file operation
553 * @param newfile File where it is going to be moved
554 * @param currentFile File with the previous info
556 public void copyFile(OCFile newfile
, OCFile currentFile
) {
558 Intent service
= new Intent(mFileActivity
, OperationsService
.class);
559 service
.setAction(OperationsService
.ACTION_COPY_FILE
);
560 service
.putExtra(OperationsService
.EXTRA_NEW_PARENT_PATH
, newfile
.getRemotePath());
561 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, currentFile
.getRemotePath());
562 service
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
563 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().queueNewOperation(service
);
565 mFileActivity
.showLoadingDialog(mFileActivity
.getApplicationContext().
566 getString(R
.string
.wait_a_moment
));
569 public long getOpIdWaitingFor() {
570 return mWaitingForOpId
;
574 public void setOpIdWaitingFor(long waitingForOpId
) {
575 mWaitingForOpId
= waitingForOpId
;
579 * @return 'True' if the server doesn't need to check forbidden characters
581 public boolean isVersionWithForbiddenCharacters() {
582 if (mFileActivity
.getAccount() != null
) {
583 OwnCloudVersion serverVersion
=
584 AccountUtils
.getServerVersion(mFileActivity
.getAccount());
585 return (serverVersion
!= null
&& serverVersion
.isVersionWithForbiddenCharacters());