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
;
51 import org
.apache
.http
.protocol
.HTTP
;
53 import java
.util
.List
;
58 public class FileOperationsHelper
{
60 private static final String TAG
= FileOperationsHelper
.class.getName();
62 private static final String FTAG_CHOOSER_DIALOG
= "CHOOSER_DIALOG";
64 protected FileActivity mFileActivity
= null
;
66 /// Identifier of operation in progress which result shouldn't be lost
67 private long mWaitingForOpId
= Long
.MAX_VALUE
;
69 public FileOperationsHelper(FileActivity fileActivity
) {
70 mFileActivity
= fileActivity
;
74 public void openFile(OCFile file
) {
76 String storagePath
= file
.getStoragePath();
77 String encodedStoragePath
= WebdavUtils
.encodePath(storagePath
);
79 Intent intentForSavedMimeType
= new Intent(Intent
.ACTION_VIEW
);
80 intentForSavedMimeType
.setDataAndType(Uri
.parse("file://"+ encodedStoragePath
),
82 intentForSavedMimeType
.setFlags(
83 Intent
.FLAG_GRANT_READ_URI_PERMISSION
| Intent
.FLAG_GRANT_WRITE_URI_PERMISSION
86 Intent intentForGuessedMimeType
= null
;
87 if (storagePath
.lastIndexOf('.') >= 0) {
88 String guessedMimeType
= MimeTypeMap
.getSingleton().getMimeTypeFromExtension(
89 storagePath
.substring(storagePath
.lastIndexOf('.') + 1)
91 if (guessedMimeType
!= null
&& !guessedMimeType
.equals(file
.getMimetype())) {
92 intentForGuessedMimeType
= new Intent(Intent
.ACTION_VIEW
);
93 intentForGuessedMimeType
.
94 setDataAndType(Uri
.parse("file://"+ encodedStoragePath
),
96 intentForGuessedMimeType
.setFlags(
97 Intent
.FLAG_GRANT_READ_URI_PERMISSION
|
98 Intent
.FLAG_GRANT_WRITE_URI_PERMISSION
103 Intent openFileWithIntent
;
104 if (intentForGuessedMimeType
!= null
) {
105 openFileWithIntent
= intentForGuessedMimeType
;
107 openFileWithIntent
= intentForSavedMimeType
;
110 List
<ResolveInfo
> launchables
= mFileActivity
.getPackageManager().
111 queryIntentActivities(openFileWithIntent
, PackageManager
.GET_INTENT_FILTERS
);
113 if(launchables
!= null
&& launchables
.size() > 0) {
115 mFileActivity
.startActivity(
116 Intent
.createChooser(
117 openFileWithIntent
, mFileActivity
.getString(R
.string
.actionbar_open_with
)
120 } catch (ActivityNotFoundException anfe
) {
121 showNoAppForFileTypeToast(mFileActivity
.getApplicationContext());
124 showNoAppForFileTypeToast(mFileActivity
.getApplicationContext());
128 Log_OC
.wtf(TAG
, "Trying to open a NULL OCFile");
133 * Displays a toast stating that no application could be found to open the file.
135 * @param context the context to be able to show a toast.
137 private void showNoAppForFileTypeToast(Context context
) {
138 Toast
.makeText(context
,
139 R
.string
.file_list_no_app_for_file_type
, Toast
.LENGTH_SHORT
)
145 * Helper method to share a file via a public link. Starts a request to do it in {@link OperationsService}
147 * @param file The file to share.
149 public void shareFileViaLink(OCFile file
) {
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 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
160 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().queueNewOperation(service
);
163 Log_OC
.wtf(TAG
, "Trying to share a NULL OCFile");
164 // TODO user-level error?
169 Toast t
= Toast
.makeText(
170 mFileActivity
, mFileActivity
.getString(R
.string
.share_link_no_support_share_api
),
178 public void shareFileWithLinkOLD(OCFile file
) {
180 if (isSharedSupported()) {
182 String link
= "https://fake.url";
183 Intent intent
= createShareWithLinkIntent(link
);
184 String
[] packagesToExclude
= new String
[]{mFileActivity
.getPackageName()};
185 DialogFragment chooserDialog
= ShareLinkToDialog
.newInstance(intent
,
186 packagesToExclude
, file
);
187 chooserDialog
.show(mFileActivity
.getSupportFragmentManager(), FTAG_CHOOSER_DIALOG
);
190 Log_OC
.wtf(TAG
, "Trying to share a NULL OCFile");
195 Toast t
= Toast
.makeText(
196 mFileActivity
, mFileActivity
.getString(R
.string
.share_link_no_support_share_api
),
204 public void shareFileWithLinkToApp(OCFile file
, String password
, Intent sendIntent
) {
207 mFileActivity
.showLoadingDialog(mFileActivity
.getApplicationContext().
208 getString(R
.string
.wait_a_moment
));
210 Intent service
= new Intent(mFileActivity
, OperationsService
.class);
211 service
.setAction(OperationsService
.ACTION_CREATE_SHARE_VIA_LINK
);
212 service
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
213 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
214 service
.putExtra(OperationsService
.EXTRA_SHARE_PASSWORD
, password
);
215 service
.putExtra(OperationsService
.EXTRA_SEND_INTENT
, sendIntent
);
216 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().queueNewOperation(service
);
219 Log_OC
.wtf(TAG
, "Trying to open a NULL OCFile");
224 private Intent
createShareWithLinkIntent(String link
) {
225 Intent intentToShareLink
= new Intent(Intent
.ACTION_SEND
);
226 intentToShareLink
.putExtra(Intent
.EXTRA_TEXT
, link
);
227 intentToShareLink
.setType(HTTP
.PLAIN_TEXT_TYPE
);
228 return intentToShareLink
;
233 * Helper method to share a file with a known sharee. Starts a request to do it in {@link OperationsService}
235 * @param file The file to share.
236 * @param shareeName Name (user name or group name) of the target sharee.
237 * @param shareType The share type determines the sharee type.
239 public void shareFileWithSharee(OCFile file
, String shareeName
, ShareType shareType
) {
241 // TODO check capability?
242 mFileActivity
.showLoadingDialog(mFileActivity
.getApplicationContext().
243 getString(R
.string
.wait_a_moment
));
245 Intent service
= new Intent(mFileActivity
, OperationsService
.class);
246 service
.setAction(OperationsService
.ACTION_CREATE_SHARE_WITH_SHAREE
);
247 service
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
248 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
249 service
.putExtra(OperationsService
.EXTRA_SHARE_WITH
, shareeName
);
250 service
.putExtra(OperationsService
.EXTRA_SHARE_TYPE
, shareType
);
251 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().queueNewOperation(service
);
254 Log_OC
.wtf(TAG
, "Trying to share a NULL OCFile");
260 * @return 'True' if the server supports the Share API
262 public boolean isSharedSupported() {
263 if (mFileActivity
.getAccount() != null
) {
264 OwnCloudVersion serverVersion
= AccountUtils
.getServerVersion(mFileActivity
.getAccount());
265 return (serverVersion
!= null
&& serverVersion
.isSharedSupported());
272 * Helper method to unshare a file publicly shared via link.
273 * Starts a request to do it in {@link OperationsService}
275 * @param file The file to unshare.
277 public void unshareFileViaLink(OCFile file
) {
279 // Unshare the file: Create the intent
280 Intent unshareService
= new Intent(mFileActivity
, OperationsService
.class);
281 unshareService
.setAction(OperationsService
.ACTION_UNSHARE
);
282 unshareService
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
283 unshareService
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
284 unshareService
.putExtra(OperationsService
.EXTRA_SHARE_TYPE
, ShareType
.PUBLIC_LINK
);
285 unshareService
.putExtra(OperationsService
.EXTRA_SHARE_WITH
, "");
287 queueShareIntent(unshareService
);
290 public void unshareFileWithUserOrGroup(OCFile file
, ShareType shareType
, String userOrGroup
){
292 // Unshare the file: Create the intent
293 Intent unshareService
= new Intent(mFileActivity
, OperationsService
.class);
294 unshareService
.setAction(OperationsService
.ACTION_UNSHARE
);
295 unshareService
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
296 unshareService
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
297 unshareService
.putExtra(OperationsService
.EXTRA_SHARE_TYPE
, shareType
);
298 unshareService
.putExtra(OperationsService
.EXTRA_SHARE_WITH
, userOrGroup
);
300 queueShareIntent(unshareService
);
304 private void queueShareIntent(Intent shareIntent
){
305 if (isSharedSupported()) {
307 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().
308 queueNewOperation(shareIntent
);
310 mFileActivity
.showLoadingDialog(mFileActivity
.getApplicationContext().
311 getString(R
.string
.wait_a_moment
));
315 Toast t
= Toast
.makeText(mFileActivity
,
316 mFileActivity
.getString(R
.string
.share_link_no_support_share_api
),
324 * Show an instance of {@link ShareType} for sharing or unsharing the {@OCFile} received as parameter.
326 * @param file File to share or unshare.
328 public void showShareFile(OCFile file
){
329 Intent intent
= new Intent(mFileActivity
, ShareActivity
.class);
330 intent
.putExtra(mFileActivity
.EXTRA_FILE
, file
);
331 intent
.putExtra(mFileActivity
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
332 mFileActivity
.startActivity(intent
);
338 * Starts a dialog that requests a password to the user to protect a share link.
340 * @param file File which public share will be protected by the requested password
342 public void requestPasswordForShareViaLink(OCFile file
) {
343 SharePasswordDialogFragment dialog
=
344 SharePasswordDialogFragment
.newInstance(
349 mFileActivity
.getSupportFragmentManager(),
350 SharePasswordDialogFragment
.PASSWORD_FRAGMENT
355 * Updates a public share on a file to set its password.
356 * Starts a request to do it in {@link OperationsService}
358 * @param file File which public share will be protected with a password.
359 * @param password Password to set for the public link; null or empty string to clear
360 * the current password
362 public void setPasswordToShareViaLink(OCFile file
, String password
) {
363 // Set password updating share
364 Intent updateShareIntent
= new Intent(mFileActivity
, OperationsService
.class);
365 updateShareIntent
.setAction(OperationsService
.ACTION_UPDATE_SHARE
);
366 updateShareIntent
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
367 updateShareIntent
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
368 updateShareIntent
.putExtra(
369 OperationsService
.EXTRA_SHARE_PASSWORD
,
370 (password
== null
) ?
"" : password
373 queueShareIntent(updateShareIntent
);
378 * Updates a public share on a file to set its expiration date.
379 * Starts a request to do it in {@link OperationsService}
381 * @param file File which public share will be constrained with an expiration date.
382 * @param expirationTimeInMillis Expiration date to set. A negative value clears the current expiration
383 * date, leaving the link unrestricted. Zero makes no change.
385 public void setExpirationDateToShareViaLink(OCFile file
, long expirationTimeInMillis
) {
386 Intent updateShareIntent
= new Intent(mFileActivity
, OperationsService
.class);
387 updateShareIntent
.setAction(OperationsService
.ACTION_UPDATE_SHARE
);
388 updateShareIntent
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
389 updateShareIntent
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
390 updateShareIntent
.putExtra(
391 OperationsService
.EXTRA_SHARE_EXPIRATION_DATE_IN_MILLIS
,
392 expirationTimeInMillis
394 queueShareIntent(updateShareIntent
);
399 * @return 'True' if the server supports the Search Users API
401 public boolean isSearchUsersSupportedSupported() {
402 if (mFileActivity
.getAccount() != null
) {
403 OwnCloudVersion serverVersion
= AccountUtils
.getServerVersion(mFileActivity
.getAccount());
404 return (serverVersion
!= null
&& serverVersion
.isSearchUsersSupported());
409 public void sendDownloadedFile(OCFile file
) {
411 String storagePath
= file
.getStoragePath();
412 String encodedStoragePath
= WebdavUtils
.encodePath(storagePath
);
413 Intent sendIntent
= new Intent(android
.content
.Intent
.ACTION_SEND
);
415 sendIntent
.setType(file
.getMimetype());
416 sendIntent
.putExtra(Intent
.EXTRA_STREAM
, Uri
.parse("file://" + encodedStoragePath
));
417 sendIntent
.putExtra(Intent
.ACTION_SEND
, true
); // Send Action
419 // Show dialog, without the own app
420 String
[] packagesToExclude
= new String
[]{mFileActivity
.getPackageName()};
421 DialogFragment chooserDialog
= ShareLinkToDialog
.newInstance(sendIntent
,
422 packagesToExclude
, file
);
423 chooserDialog
.show(mFileActivity
.getSupportFragmentManager(), FTAG_CHOOSER_DIALOG
);
426 Log_OC
.wtf(TAG
, "Trying to send a NULL OCFile");
431 * Request the synchronization of a file or folder with the OC server, including its contents.
433 * @param file The file or folder to synchronize
435 public void syncFile(OCFile file
) {
436 if (!file
.isFolder()){
437 Intent intent
= new Intent(mFileActivity
, OperationsService
.class);
438 intent
.setAction(OperationsService
.ACTION_SYNC_FILE
);
439 intent
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
440 intent
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
441 intent
.putExtra(OperationsService
.EXTRA_SYNC_FILE_CONTENTS
, true
);
442 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().queueNewOperation(intent
);
443 mFileActivity
.showLoadingDialog(mFileActivity
.getApplicationContext().
444 getString(R
.string
.wait_a_moment
));
447 Intent intent
= new Intent(mFileActivity
, OperationsService
.class);
448 intent
.setAction(OperationsService
.ACTION_SYNC_FOLDER
);
449 intent
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
450 intent
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
451 mFileActivity
.startService(intent
);
456 public void toggleFavorite(OCFile file
, boolean isFavorite
) {
457 file
.setFavorite(isFavorite
);
458 mFileActivity
.getStorageManager().saveFile(file
);
460 /// register the OCFile instance in the observer service to monitor local updates
461 Intent observedFileIntent
= FileObserverService
.makeObservedFileIntent(
464 mFileActivity
.getAccount(),
466 mFileActivity
.startService(observedFileIntent
);
468 /// immediate content synchronization
469 if (file
.isFavorite()) {
474 public void renameFile(OCFile file
, String newFilename
) {
476 Intent service
= new Intent(mFileActivity
, OperationsService
.class);
477 service
.setAction(OperationsService
.ACTION_RENAME
);
478 service
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
479 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
480 service
.putExtra(OperationsService
.EXTRA_NEWNAME
, newFilename
);
481 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().queueNewOperation(service
);
483 mFileActivity
.showLoadingDialog(mFileActivity
.getApplicationContext().
484 getString(R
.string
.wait_a_moment
));
488 public void removeFile(OCFile file
, boolean onlyLocalCopy
) {
490 Intent service
= new Intent(mFileActivity
, OperationsService
.class);
491 service
.setAction(OperationsService
.ACTION_REMOVE
);
492 service
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
493 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
494 service
.putExtra(OperationsService
.EXTRA_REMOVE_ONLY_LOCAL
, onlyLocalCopy
);
495 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().queueNewOperation(service
);
497 mFileActivity
.showLoadingDialog(mFileActivity
.getApplicationContext().
498 getString(R
.string
.wait_a_moment
));
502 public void createFolder(String remotePath
, boolean createFullPath
) {
504 Intent service
= new Intent(mFileActivity
, OperationsService
.class);
505 service
.setAction(OperationsService
.ACTION_CREATE_FOLDER
);
506 service
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
507 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, remotePath
);
508 service
.putExtra(OperationsService
.EXTRA_CREATE_FULL_PATH
, createFullPath
);
509 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().queueNewOperation(service
);
511 mFileActivity
.showLoadingDialog(mFileActivity
.getApplicationContext().
512 getString(R
.string
.wait_a_moment
));
516 * Cancel the transference in downloads (files/folders) and file uploads
519 public void cancelTransference(OCFile file
) {
520 Account account
= mFileActivity
.getAccount();
521 if (file
.isFolder()) {
522 OperationsService
.OperationsServiceBinder opsBinder
=
523 mFileActivity
.getOperationsServiceBinder();
524 if (opsBinder
!= null
) {
525 opsBinder
.cancel(account
, file
);
529 // for both files and folders
530 FileDownloaderBinder downloaderBinder
= mFileActivity
.getFileDownloaderBinder();
531 if (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(account
, file
)) {
532 downloaderBinder
.cancel(account
, file
);
534 FileUploaderBinder uploaderBinder
= mFileActivity
.getFileUploaderBinder();
535 if (uploaderBinder
!= null
&& uploaderBinder
.isUploading(account
, file
)) {
536 uploaderBinder
.cancel(account
, file
);
541 * Start move file operation
543 * @param newfile File where it is going to be moved
544 * @param currentFile File with the previous info
546 public void moveFile(OCFile newfile
, OCFile currentFile
) {
548 Intent service
= new Intent(mFileActivity
, OperationsService
.class);
549 service
.setAction(OperationsService
.ACTION_MOVE_FILE
);
550 service
.putExtra(OperationsService
.EXTRA_NEW_PARENT_PATH
, newfile
.getRemotePath());
551 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, currentFile
.getRemotePath());
552 service
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
553 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().queueNewOperation(service
);
555 mFileActivity
.showLoadingDialog(mFileActivity
.getApplicationContext().
556 getString(R
.string
.wait_a_moment
));
560 * Start copy file operation
562 * @param newfile File where it is going to be moved
563 * @param currentFile File with the previous info
565 public void copyFile(OCFile newfile
, OCFile currentFile
) {
567 Intent service
= new Intent(mFileActivity
, OperationsService
.class);
568 service
.setAction(OperationsService
.ACTION_COPY_FILE
);
569 service
.putExtra(OperationsService
.EXTRA_NEW_PARENT_PATH
, newfile
.getRemotePath());
570 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, currentFile
.getRemotePath());
571 service
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
572 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().queueNewOperation(service
);
574 mFileActivity
.showLoadingDialog(mFileActivity
.getApplicationContext().
575 getString(R
.string
.wait_a_moment
));
578 public long getOpIdWaitingFor() {
579 return mWaitingForOpId
;
583 public void setOpIdWaitingFor(long waitingForOpId
) {
584 mWaitingForOpId
= waitingForOpId
;
588 * @return 'True' if the server doesn't need to check forbidden characters
590 public boolean isVersionWithForbiddenCharacters() {
591 if (mFileActivity
.getAccount() != null
) {
592 OwnCloudVersion serverVersion
=
593 AccountUtils
.getServerVersion(mFileActivity
.getAccount());
594 return (serverVersion
!= null
&& serverVersion
.isVersionWithForbiddenCharacters());