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
.status
.OwnCloudVersion
;
43 import com
.owncloud
.android
.services
.OperationsService
;
44 import com
.owncloud
.android
.services
.observer
.FileObserverService
;
45 import com
.owncloud
.android
.ui
.activity
.FileActivity
;
46 import com
.owncloud
.android
.ui
.activity
.ShareActivity
;
47 import com
.owncloud
.android
.ui
.dialog
.ShareLinkToDialog
;
49 import org
.apache
.http
.protocol
.HTTP
;
51 import java
.util
.List
;
56 public class FileOperationsHelper
{
58 private static final String TAG
= FileOperationsHelper
.class.getName();
60 private static final String FTAG_CHOOSER_DIALOG
= "CHOOSER_DIALOG";
62 protected FileActivity mFileActivity
= null
;
64 /// Identifier of operation in progress which result shouldn't be lost
65 private long mWaitingForOpId
= Long
.MAX_VALUE
;
67 public FileOperationsHelper(FileActivity fileActivity
) {
68 mFileActivity
= fileActivity
;
72 public void openFile(OCFile file
) {
74 String storagePath
= file
.getStoragePath();
75 String encodedStoragePath
= WebdavUtils
.encodePath(storagePath
);
77 Intent intentForSavedMimeType
= new Intent(Intent
.ACTION_VIEW
);
78 intentForSavedMimeType
.setDataAndType(Uri
.parse("file://"+ encodedStoragePath
),
80 intentForSavedMimeType
.setFlags(
81 Intent
.FLAG_GRANT_READ_URI_PERMISSION
| Intent
.FLAG_GRANT_WRITE_URI_PERMISSION
84 Intent intentForGuessedMimeType
= null
;
85 if (storagePath
.lastIndexOf('.') >= 0) {
86 String guessedMimeType
= MimeTypeMap
.getSingleton().getMimeTypeFromExtension(
87 storagePath
.substring(storagePath
.lastIndexOf('.') + 1)
89 if (guessedMimeType
!= null
&& !guessedMimeType
.equals(file
.getMimetype())) {
90 intentForGuessedMimeType
= new Intent(Intent
.ACTION_VIEW
);
91 intentForGuessedMimeType
.
92 setDataAndType(Uri
.parse("file://"+ encodedStoragePath
),
94 intentForGuessedMimeType
.setFlags(
95 Intent
.FLAG_GRANT_READ_URI_PERMISSION
|
96 Intent
.FLAG_GRANT_WRITE_URI_PERMISSION
101 Intent openFileWithIntent
;
102 if (intentForGuessedMimeType
!= null
) {
103 openFileWithIntent
= intentForGuessedMimeType
;
105 openFileWithIntent
= intentForSavedMimeType
;
108 List
<ResolveInfo
> launchables
= mFileActivity
.getPackageManager().
109 queryIntentActivities(openFileWithIntent
, PackageManager
.GET_INTENT_FILTERS
);
111 if(launchables
!= null
&& launchables
.size() > 0) {
113 mFileActivity
.startActivity(
114 Intent
.createChooser(
115 openFileWithIntent
, mFileActivity
.getString(R
.string
.actionbar_open_with
)
118 } catch (ActivityNotFoundException anfe
) {
119 showNoAppForFileTypeToast(mFileActivity
.getApplicationContext());
122 showNoAppForFileTypeToast(mFileActivity
.getApplicationContext());
126 Log_OC
.wtf(TAG
, "Trying to open a NULL OCFile");
131 * Displays a toast stating that no application could be found to open the file.
133 * @param context the context to be able to show a toast.
135 private void showNoAppForFileTypeToast(Context context
) {
136 Toast
.makeText(context
,
137 R
.string
.file_list_no_app_for_file_type
, Toast
.LENGTH_SHORT
)
141 public void shareFileWithLink(OCFile file
) {
143 if (isSharedSupported()) {
145 String link
= "https://fake.url";
146 Intent intent
= createShareWithLinkIntent(link
);
147 String
[] packagesToExclude
= new String
[]{mFileActivity
.getPackageName()};
148 DialogFragment chooserDialog
= ShareLinkToDialog
.newInstance(intent
,
149 packagesToExclude
, file
);
150 chooserDialog
.show(mFileActivity
.getSupportFragmentManager(), FTAG_CHOOSER_DIALOG
);
153 Log_OC
.wtf(TAG
, "Trying to share a NULL OCFile");
158 Toast t
= Toast
.makeText(
159 mFileActivity
, mFileActivity
.getString(R
.string
.share_link_no_support_share_api
),
167 public void shareFileWithLinkToApp(OCFile file
, String password
, Intent sendIntent
) {
170 mFileActivity
.showLoadingDialog(mFileActivity
.getApplicationContext().
171 getString(R
.string
.wait_a_moment
));
173 Intent service
= new Intent(mFileActivity
, OperationsService
.class);
174 service
.setAction(OperationsService
.ACTION_CREATE_SHARE
);
175 service
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
176 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
177 service
.putExtra(OperationsService
.EXTRA_PASSWORD_SHARE
, password
);
178 service
.putExtra(OperationsService
.EXTRA_SEND_INTENT
, sendIntent
);
179 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().queueNewOperation(service
);
182 Log_OC
.wtf(TAG
, "Trying to open a NULL OCFile");
187 private Intent
createShareWithLinkIntent(String link
) {
188 Intent intentToShareLink
= new Intent(Intent
.ACTION_SEND
);
189 intentToShareLink
.putExtra(Intent
.EXTRA_TEXT
, link
);
190 intentToShareLink
.setType(HTTP
.PLAIN_TEXT_TYPE
);
191 return intentToShareLink
;
196 * @return 'True' if the server supports the Share API
198 public boolean isSharedSupported() {
199 if (mFileActivity
.getAccount() != null
) {
200 OwnCloudVersion serverVersion
= AccountUtils
.getServerVersion(mFileActivity
.getAccount());
201 return (serverVersion
!= null
&& serverVersion
.isSharedSupported());
207 public void unshareFileWithLink(OCFile file
) {
209 if (isSharedSupported()) {
211 Intent service
= new Intent(mFileActivity
, OperationsService
.class);
212 service
.setAction(OperationsService
.ACTION_UNSHARE
);
213 service
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
214 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
215 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().queueNewOperation(service
);
217 mFileActivity
.showLoadingDialog(mFileActivity
.getApplicationContext().
218 getString(R
.string
.wait_a_moment
));
222 Toast t
= Toast
.makeText(mFileActivity
,
223 mFileActivity
.getString(R
.string
.share_link_no_support_share_api
),
230 public void showShareFile(OCFile file
){
231 Intent intent
= new Intent(mFileActivity
, ShareActivity
.class);
232 intent
.putExtra(mFileActivity
.EXTRA_FILE
, file
);
233 intent
.putExtra(mFileActivity
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
234 mFileActivity
.startActivity(intent
);
238 public void sendDownloadedFile(OCFile file
) {
240 String storagePath
= file
.getStoragePath();
241 String encodedStoragePath
= WebdavUtils
.encodePath(storagePath
);
242 Intent sendIntent
= new Intent(android
.content
.Intent
.ACTION_SEND
);
244 sendIntent
.setType(file
.getMimetype());
245 sendIntent
.putExtra(Intent
.EXTRA_STREAM
, Uri
.parse("file://" + encodedStoragePath
));
246 sendIntent
.putExtra(Intent
.ACTION_SEND
, true
); // Send Action
248 // Show dialog, without the own app
249 String
[] packagesToExclude
= new String
[]{mFileActivity
.getPackageName()};
250 DialogFragment chooserDialog
= ShareLinkToDialog
.newInstance(sendIntent
,
251 packagesToExclude
, file
);
252 chooserDialog
.show(mFileActivity
.getSupportFragmentManager(), FTAG_CHOOSER_DIALOG
);
255 Log_OC
.wtf(TAG
, "Trying to send a NULL OCFile");
260 * Request the synchronization of a file or folder with the OC server, including its contents.
262 * @param file The file or folder to synchronize
264 public void syncFile(OCFile file
) {
265 if (!file
.isFolder()){
266 Intent intent
= new Intent(mFileActivity
, OperationsService
.class);
267 intent
.setAction(OperationsService
.ACTION_SYNC_FILE
);
268 intent
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
269 intent
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
270 intent
.putExtra(OperationsService
.EXTRA_SYNC_FILE_CONTENTS
, true
);
271 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().queueNewOperation(intent
);
272 mFileActivity
.showLoadingDialog(mFileActivity
.getApplicationContext().
273 getString(R
.string
.wait_a_moment
));
276 Intent intent
= new Intent(mFileActivity
, OperationsService
.class);
277 intent
.setAction(OperationsService
.ACTION_SYNC_FOLDER
);
278 intent
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
279 intent
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
280 mFileActivity
.startService(intent
);
285 public void toggleFavorite(OCFile file
, boolean isFavorite
) {
286 file
.setFavorite(isFavorite
);
287 mFileActivity
.getStorageManager().saveFile(file
);
289 /// register the OCFile instance in the observer service to monitor local updates
290 Intent observedFileIntent
= FileObserverService
.makeObservedFileIntent(
293 mFileActivity
.getAccount(),
295 mFileActivity
.startService(observedFileIntent
);
297 /// immediate content synchronization
298 if (file
.isFavorite()) {
303 public void renameFile(OCFile file
, String newFilename
) {
305 Intent service
= new Intent(mFileActivity
, OperationsService
.class);
306 service
.setAction(OperationsService
.ACTION_RENAME
);
307 service
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
308 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
309 service
.putExtra(OperationsService
.EXTRA_NEWNAME
, newFilename
);
310 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().queueNewOperation(service
);
312 mFileActivity
.showLoadingDialog(mFileActivity
.getApplicationContext().
313 getString(R
.string
.wait_a_moment
));
317 public void removeFile(OCFile file
, boolean onlyLocalCopy
) {
319 Intent service
= new Intent(mFileActivity
, OperationsService
.class);
320 service
.setAction(OperationsService
.ACTION_REMOVE
);
321 service
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
322 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
323 service
.putExtra(OperationsService
.EXTRA_REMOVE_ONLY_LOCAL
, onlyLocalCopy
);
324 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().queueNewOperation(service
);
326 mFileActivity
.showLoadingDialog(mFileActivity
.getApplicationContext().
327 getString(R
.string
.wait_a_moment
));
331 public void createFolder(String remotePath
, boolean createFullPath
) {
333 Intent service
= new Intent(mFileActivity
, OperationsService
.class);
334 service
.setAction(OperationsService
.ACTION_CREATE_FOLDER
);
335 service
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
336 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, remotePath
);
337 service
.putExtra(OperationsService
.EXTRA_CREATE_FULL_PATH
, createFullPath
);
338 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().queueNewOperation(service
);
340 mFileActivity
.showLoadingDialog(mFileActivity
.getApplicationContext().
341 getString(R
.string
.wait_a_moment
));
345 * Cancel the transference in downloads (files/folders) and file uploads
348 public void cancelTransference(OCFile file
) {
349 Account account
= mFileActivity
.getAccount();
350 if (file
.isFolder()) {
351 OperationsService
.OperationsServiceBinder opsBinder
=
352 mFileActivity
.getOperationsServiceBinder();
353 if (opsBinder
!= null
) {
354 opsBinder
.cancel(account
, file
);
358 // for both files and folders
359 FileDownloaderBinder downloaderBinder
= mFileActivity
.getFileDownloaderBinder();
360 if (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(account
, file
)) {
361 downloaderBinder
.cancel(account
, file
);
363 FileUploaderBinder uploaderBinder
= mFileActivity
.getFileUploaderBinder();
364 if (uploaderBinder
!= null
&& uploaderBinder
.isUploading(account
, file
)) {
365 uploaderBinder
.cancel(account
, file
);
370 * Start move file operation
372 * @param newfile File where it is going to be moved
373 * @param currentFile File with the previous info
375 public void moveFile(OCFile newfile
, OCFile currentFile
) {
377 Intent service
= new Intent(mFileActivity
, OperationsService
.class);
378 service
.setAction(OperationsService
.ACTION_MOVE_FILE
);
379 service
.putExtra(OperationsService
.EXTRA_NEW_PARENT_PATH
, newfile
.getRemotePath());
380 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, currentFile
.getRemotePath());
381 service
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
382 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().queueNewOperation(service
);
384 mFileActivity
.showLoadingDialog(mFileActivity
.getApplicationContext().
385 getString(R
.string
.wait_a_moment
));
389 * Start copy file operation
391 * @param newfile File where it is going to be moved
392 * @param currentFile File with the previous info
394 public void copyFile(OCFile newfile
, OCFile currentFile
) {
396 Intent service
= new Intent(mFileActivity
, OperationsService
.class);
397 service
.setAction(OperationsService
.ACTION_COPY_FILE
);
398 service
.putExtra(OperationsService
.EXTRA_NEW_PARENT_PATH
, newfile
.getRemotePath());
399 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, currentFile
.getRemotePath());
400 service
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
401 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().queueNewOperation(service
);
403 mFileActivity
.showLoadingDialog(mFileActivity
.getApplicationContext().
404 getString(R
.string
.wait_a_moment
));
407 public long getOpIdWaitingFor() {
408 return mWaitingForOpId
;
412 public void setOpIdWaitingFor(long waitingForOpId
) {
413 mWaitingForOpId
= waitingForOpId
;
417 * @return 'True' if the server doesn't need to check forbidden characters
419 public boolean isVersionWithForbiddenCharacters() {
420 if (mFileActivity
.getAccount() != null
) {
421 OwnCloudVersion serverVersion
=
422 AccountUtils
.getServerVersion(mFileActivity
.getAccount());
423 return (serverVersion
!= null
&& serverVersion
.isVersionWithForbiddenCharacters());