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(
337 mFileActivity
.getSupportFragmentManager(),
338 SharePasswordDialogFragment
.PASSWORD_FRAGMENT
343 * Updates a public share on a file to set its password.
344 * Starts a request to do it in {@link OperationsService}
346 * @param file File which public share will be protected with a password.
347 * @param password Password to set for the public link; null or empty string to clear
348 * the current password
350 public void setPasswordToShareViaLink(OCFile file
, String password
) {
351 // Set password updating share
352 Intent updateShareIntent
= new Intent(mFileActivity
, OperationsService
.class);
353 updateShareIntent
.setAction(OperationsService
.ACTION_UPDATE_SHARE
);
354 updateShareIntent
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
355 updateShareIntent
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
356 updateShareIntent
.putExtra(
357 OperationsService
.EXTRA_SHARE_PASSWORD
,
358 (password
== null
) ?
"" : password
361 queueShareIntent(updateShareIntent
);
366 * Updates a public share on a file to set its expiration date.
367 * Starts a request to do it in {@link OperationsService}
369 * @param file File which public share will be constrained with an expiration date.
370 * @param expirationTimeInMillis Expiration date to set. A negative value clears the current expiration
371 * date, leaving the link unrestricted. Zero makes no change.
373 public void setExpirationDateToShareViaLink(OCFile file
, long expirationTimeInMillis
) {
374 Intent updateShareIntent
= new Intent(mFileActivity
, OperationsService
.class);
375 updateShareIntent
.setAction(OperationsService
.ACTION_UPDATE_SHARE
);
376 updateShareIntent
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
377 updateShareIntent
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
378 updateShareIntent
.putExtra(
379 OperationsService
.EXTRA_SHARE_EXPIRATION_DATE_IN_MILLIS
,
380 expirationTimeInMillis
382 queueShareIntent(updateShareIntent
);
387 * @return 'True' if the server supports the Search Users API
389 public boolean isSearchUsersSupportedSupported() {
390 if (mFileActivity
.getAccount() != null
) {
391 OwnCloudVersion serverVersion
= AccountUtils
.getServerVersion(mFileActivity
.getAccount());
392 return (serverVersion
!= null
&& serverVersion
.isSearchUsersSupported());
397 public void sendDownloadedFile(OCFile file
) {
399 String storagePath
= file
.getStoragePath();
400 String encodedStoragePath
= WebdavUtils
.encodePath(storagePath
);
401 Intent sendIntent
= new Intent(android
.content
.Intent
.ACTION_SEND
);
403 sendIntent
.setType(file
.getMimetype());
404 sendIntent
.putExtra(Intent
.EXTRA_STREAM
, Uri
.parse("file://" + encodedStoragePath
));
405 sendIntent
.putExtra(Intent
.ACTION_SEND
, true
); // Send Action
407 // Show dialog, without the own app
408 String
[] packagesToExclude
= new String
[]{mFileActivity
.getPackageName()};
409 DialogFragment chooserDialog
= ShareLinkToDialog
.newInstance(sendIntent
,
410 packagesToExclude
, file
);
411 chooserDialog
.show(mFileActivity
.getSupportFragmentManager(), FTAG_CHOOSER_DIALOG
);
414 Log_OC
.wtf(TAG
, "Trying to send a NULL OCFile");
419 * Request the synchronization of a file or folder with the OC server, including its contents.
421 * @param file The file or folder to synchronize
423 public void syncFile(OCFile file
) {
424 if (!file
.isFolder()){
425 Intent intent
= new Intent(mFileActivity
, OperationsService
.class);
426 intent
.setAction(OperationsService
.ACTION_SYNC_FILE
);
427 intent
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
428 intent
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
429 intent
.putExtra(OperationsService
.EXTRA_SYNC_FILE_CONTENTS
, true
);
430 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().queueNewOperation(intent
);
431 mFileActivity
.showLoadingDialog(mFileActivity
.getApplicationContext().
432 getString(R
.string
.wait_a_moment
));
435 Intent intent
= new Intent(mFileActivity
, OperationsService
.class);
436 intent
.setAction(OperationsService
.ACTION_SYNC_FOLDER
);
437 intent
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
438 intent
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
439 mFileActivity
.startService(intent
);
444 public void toggleFavorite(OCFile file
, boolean isFavorite
) {
445 file
.setFavorite(isFavorite
);
446 mFileActivity
.getStorageManager().saveFile(file
);
448 /// register the OCFile instance in the observer service to monitor local updates
449 Intent observedFileIntent
= FileObserverService
.makeObservedFileIntent(
452 mFileActivity
.getAccount(),
454 mFileActivity
.startService(observedFileIntent
);
456 /// immediate content synchronization
457 if (file
.isFavorite()) {
462 public void renameFile(OCFile file
, String newFilename
) {
464 Intent service
= new Intent(mFileActivity
, OperationsService
.class);
465 service
.setAction(OperationsService
.ACTION_RENAME
);
466 service
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
467 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
468 service
.putExtra(OperationsService
.EXTRA_NEWNAME
, newFilename
);
469 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().queueNewOperation(service
);
471 mFileActivity
.showLoadingDialog(mFileActivity
.getApplicationContext().
472 getString(R
.string
.wait_a_moment
));
476 public void removeFile(OCFile file
, boolean onlyLocalCopy
) {
478 Intent service
= new Intent(mFileActivity
, OperationsService
.class);
479 service
.setAction(OperationsService
.ACTION_REMOVE
);
480 service
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
481 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
482 service
.putExtra(OperationsService
.EXTRA_REMOVE_ONLY_LOCAL
, onlyLocalCopy
);
483 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().queueNewOperation(service
);
485 mFileActivity
.showLoadingDialog(mFileActivity
.getApplicationContext().
486 getString(R
.string
.wait_a_moment
));
490 public void createFolder(String remotePath
, boolean createFullPath
) {
492 Intent service
= new Intent(mFileActivity
, OperationsService
.class);
493 service
.setAction(OperationsService
.ACTION_CREATE_FOLDER
);
494 service
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
495 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, remotePath
);
496 service
.putExtra(OperationsService
.EXTRA_CREATE_FULL_PATH
, createFullPath
);
497 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().queueNewOperation(service
);
499 mFileActivity
.showLoadingDialog(mFileActivity
.getApplicationContext().
500 getString(R
.string
.wait_a_moment
));
504 * Cancel the transference in downloads (files/folders) and file uploads
507 public void cancelTransference(OCFile file
) {
508 Account account
= mFileActivity
.getAccount();
509 if (file
.isFolder()) {
510 OperationsService
.OperationsServiceBinder opsBinder
=
511 mFileActivity
.getOperationsServiceBinder();
512 if (opsBinder
!= null
) {
513 opsBinder
.cancel(account
, file
);
517 // for both files and folders
518 FileDownloaderBinder downloaderBinder
= mFileActivity
.getFileDownloaderBinder();
519 if (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(account
, file
)) {
520 downloaderBinder
.cancel(account
, file
);
522 FileUploaderBinder uploaderBinder
= mFileActivity
.getFileUploaderBinder();
523 if (uploaderBinder
!= null
&& uploaderBinder
.isUploading(account
, file
)) {
524 uploaderBinder
.cancel(account
, file
);
529 * Start move file operation
531 * @param newfile File where it is going to be moved
532 * @param currentFile File with the previous info
534 public void moveFile(OCFile newfile
, OCFile currentFile
) {
536 Intent service
= new Intent(mFileActivity
, OperationsService
.class);
537 service
.setAction(OperationsService
.ACTION_MOVE_FILE
);
538 service
.putExtra(OperationsService
.EXTRA_NEW_PARENT_PATH
, newfile
.getRemotePath());
539 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, currentFile
.getRemotePath());
540 service
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
541 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().queueNewOperation(service
);
543 mFileActivity
.showLoadingDialog(mFileActivity
.getApplicationContext().
544 getString(R
.string
.wait_a_moment
));
548 * Start copy file operation
550 * @param newfile File where it is going to be moved
551 * @param currentFile File with the previous info
553 public void copyFile(OCFile newfile
, OCFile currentFile
) {
555 Intent service
= new Intent(mFileActivity
, OperationsService
.class);
556 service
.setAction(OperationsService
.ACTION_COPY_FILE
);
557 service
.putExtra(OperationsService
.EXTRA_NEW_PARENT_PATH
, newfile
.getRemotePath());
558 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, currentFile
.getRemotePath());
559 service
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
560 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().queueNewOperation(service
);
562 mFileActivity
.showLoadingDialog(mFileActivity
.getApplicationContext().
563 getString(R
.string
.wait_a_moment
));
566 public long getOpIdWaitingFor() {
567 return mWaitingForOpId
;
571 public void setOpIdWaitingFor(long waitingForOpId
) {
572 mWaitingForOpId
= waitingForOpId
;
576 * @return 'True' if the server doesn't need to check forbidden characters
578 public boolean isVersionWithForbiddenCharacters() {
579 if (mFileActivity
.getAccount() != null
) {
580 OwnCloudVersion serverVersion
=
581 AccountUtils
.getServerVersion(mFileActivity
.getAccount());
582 return (serverVersion
!= null
&& serverVersion
.isVersionWithForbiddenCharacters());