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
.Intent
;
26 import android
.net
.Uri
;
27 import android
.support
.v4
.app
.DialogFragment
;
28 import android
.webkit
.MimeTypeMap
;
29 import android
.widget
.Toast
;
31 import com
.owncloud
.android
.R
;
32 import com
.owncloud
.android
.authentication
.AccountUtils
;
33 import com
.owncloud
.android
.datamodel
.OCFile
;
34 import com
.owncloud
.android
.files
.services
.FileDownloader
.FileDownloaderBinder
;
35 import com
.owncloud
.android
.files
.services
.FileUploader
.FileUploaderBinder
;
36 import com
.owncloud
.android
.lib
.common
.network
.WebdavUtils
;
37 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
38 import com
.owncloud
.android
.lib
.resources
.status
.OwnCloudVersion
;
39 import com
.owncloud
.android
.services
.OperationsService
;
40 import com
.owncloud
.android
.services
.observer
.FileObserverService
;
41 import com
.owncloud
.android
.ui
.activity
.FileActivity
;
42 import com
.owncloud
.android
.ui
.dialog
.ShareLinkToDialog
;
44 import org
.apache
.http
.protocol
.HTTP
;
49 public class FileOperationsHelper
{
51 private static final String TAG
= FileOperationsHelper
.class.getName();
53 private static final String FTAG_CHOOSER_DIALOG
= "CHOOSER_DIALOG";
55 protected FileActivity mFileActivity
= null
;
57 /// Identifier of operation in progress which result shouldn't be lost
58 private long mWaitingForOpId
= Long
.MAX_VALUE
;
60 public FileOperationsHelper(FileActivity fileActivity
) {
61 mFileActivity
= fileActivity
;
65 public void openFile(OCFile file
) {
67 String storagePath
= file
.getStoragePath();
68 String encodedStoragePath
= WebdavUtils
.encodePath(storagePath
);
70 Intent intentForSavedMimeType
= new Intent(Intent
.ACTION_VIEW
);
71 intentForSavedMimeType
.setDataAndType(Uri
.parse("file://"+ encodedStoragePath
), file
.getMimetype());
72 intentForSavedMimeType
.setFlags(
73 Intent
.FLAG_GRANT_READ_URI_PERMISSION
| Intent
.FLAG_GRANT_WRITE_URI_PERMISSION
76 Intent intentForGuessedMimeType
= null
;
77 if (storagePath
.lastIndexOf('.') >= 0) {
78 String guessedMimeType
= MimeTypeMap
.getSingleton().getMimeTypeFromExtension(
79 storagePath
.substring(storagePath
.lastIndexOf('.') + 1)
81 if (guessedMimeType
!= null
&& !guessedMimeType
.equals(file
.getMimetype())) {
82 intentForGuessedMimeType
= new Intent(Intent
.ACTION_VIEW
);
83 intentForGuessedMimeType
.setDataAndType(Uri
.parse("file://"+ encodedStoragePath
), guessedMimeType
);
84 intentForGuessedMimeType
.setFlags(
85 Intent
.FLAG_GRANT_READ_URI_PERMISSION
| Intent
.FLAG_GRANT_WRITE_URI_PERMISSION
91 if (intentForGuessedMimeType
!= null
) {
92 chooserIntent
= Intent
.createChooser(intentForGuessedMimeType
, mFileActivity
.getString(R
.string
.actionbar_open_with
));
94 chooserIntent
= Intent
.createChooser(intentForSavedMimeType
, mFileActivity
.getString(R
.string
.actionbar_open_with
));
97 mFileActivity
.startActivity(chooserIntent
);
100 Log_OC
.wtf(TAG
, "Trying to open a NULL OCFile");
105 public void shareFileWithLink(OCFile file
) {
107 if (isSharedSupported()) {
109 String link
= "https://fake.url";
110 Intent intent
= createShareWithLinkIntent(link
);
111 String
[] packagesToExclude
= new String
[]{mFileActivity
.getPackageName()};
112 DialogFragment chooserDialog
= ShareLinkToDialog
.newInstance(intent
, packagesToExclude
, file
);
113 chooserDialog
.show(mFileActivity
.getSupportFragmentManager(), FTAG_CHOOSER_DIALOG
);
116 Log_OC
.wtf(TAG
, "Trying to share a NULL OCFile");
121 Toast t
= Toast
.makeText(
122 mFileActivity
, mFileActivity
.getString(R
.string
.share_link_no_support_share_api
), Toast
.LENGTH_LONG
129 public void shareFileWithLinkToApp(OCFile file
, String password
, Intent sendIntent
) {
132 mFileActivity
.showLoadingDialog();
134 Intent service
= new Intent(mFileActivity
, OperationsService
.class);
135 service
.setAction(OperationsService
.ACTION_CREATE_SHARE
);
136 service
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
137 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
138 service
.putExtra(OperationsService
.EXTRA_PASSWORD_SHARE
, password
);
139 service
.putExtra(OperationsService
.EXTRA_SEND_INTENT
, sendIntent
);
140 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().queueNewOperation(service
);
143 Log_OC
.wtf(TAG
, "Trying to open a NULL OCFile");
148 private Intent
createShareWithLinkIntent(String link
) {
149 Intent intentToShareLink
= new Intent(Intent
.ACTION_SEND
);
150 intentToShareLink
.putExtra(Intent
.EXTRA_TEXT
, link
);
151 intentToShareLink
.setType(HTTP
.PLAIN_TEXT_TYPE
);
152 return intentToShareLink
;
157 * @return 'True' if the server supports the Share API
159 public boolean isSharedSupported() {
160 if (mFileActivity
.getAccount() != null
) {
161 OwnCloudVersion serverVersion
= AccountUtils
.getServerVersion(mFileActivity
.getAccount());
162 return (serverVersion
!= null
&& serverVersion
.isSharedSupported());
168 public void unshareFileWithLink(OCFile file
) {
170 if (isSharedSupported()) {
172 Intent service
= new Intent(mFileActivity
, OperationsService
.class);
173 service
.setAction(OperationsService
.ACTION_UNSHARE
);
174 service
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
175 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
176 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().queueNewOperation(service
);
178 mFileActivity
.showLoadingDialog();
182 Toast t
= Toast
.makeText(mFileActivity
, mFileActivity
.getString(R
.string
.share_link_no_support_share_api
), Toast
.LENGTH_LONG
);
188 public void sendDownloadedFile(OCFile file
) {
190 String storagePath
= file
.getStoragePath();
191 String encodedStoragePath
= WebdavUtils
.encodePath(storagePath
);
192 Intent sendIntent
= new Intent(android
.content
.Intent
.ACTION_SEND
);
194 sendIntent
.setType(file
.getMimetype());
195 sendIntent
.putExtra(Intent
.EXTRA_STREAM
, Uri
.parse("file://" + encodedStoragePath
));
196 sendIntent
.putExtra(Intent
.ACTION_SEND
, true
); // Send Action
198 // Show dialog, without the own app
199 String
[] packagesToExclude
= new String
[]{mFileActivity
.getPackageName()};
200 DialogFragment chooserDialog
= ShareLinkToDialog
.newInstance(sendIntent
, packagesToExclude
, file
);
201 chooserDialog
.show(mFileActivity
.getSupportFragmentManager(), FTAG_CHOOSER_DIALOG
);
204 Log_OC
.wtf(TAG
, "Trying to send a NULL OCFile");
209 * Request the synchronization of a file or folder with the OC server, including its contents.
211 * @param file The file or folder to synchronize
213 public void syncFile(OCFile file
) {
214 if (!file
.isFolder()){
215 Intent intent
= new Intent(mFileActivity
, OperationsService
.class);
216 intent
.setAction(OperationsService
.ACTION_SYNC_FILE
);
217 intent
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
218 intent
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
219 intent
.putExtra(OperationsService
.EXTRA_SYNC_FILE_CONTENTS
, true
);
220 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().queueNewOperation(intent
);
221 mFileActivity
.showLoadingDialog();
224 Intent intent
= new Intent(mFileActivity
, OperationsService
.class);
225 intent
.setAction(OperationsService
.ACTION_SYNC_FOLDER
);
226 intent
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
227 intent
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
228 mFileActivity
.startService(intent
);
235 * Request the synchronization of a file or the DOWNLOAD OF A FOLDER, including its contents.
237 * For files, it's the same as syncFile(OCFile file); for folders, this method does not trigger uploads for
238 * file locally modified.
240 * Kept 'til synchronization of full folders is considered good enough.
242 * @param file The file or folder to synchronize
244 public void downloadFile(OCFile file
) {
245 if (!file
.isFolder()){
249 Intent intent
= new Intent(mFileActivity
, OperationsService
.class);
250 intent
.setAction(OperationsService
.ACTION_DOWNLOAD_FOLDER
);
251 intent
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
252 intent
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
253 mFileActivity
.startService(intent
);
257 public void toggleFavorite(OCFile file
, boolean isFavorite
) {
258 file
.setFavorite(isFavorite
);
259 mFileActivity
.getStorageManager().saveFile(file
);
261 /// register the OCFile instance in the observer service to monitor local updates
262 Intent observedFileIntent
= FileObserverService
.makeObservedFileIntent(
265 mFileActivity
.getAccount(),
267 mFileActivity
.startService(observedFileIntent
);
269 /// immediate content synchronization
270 if (file
.isFavorite()) {
275 public void renameFile(OCFile file
, String newFilename
) {
277 Intent service
= new Intent(mFileActivity
, OperationsService
.class);
278 service
.setAction(OperationsService
.ACTION_RENAME
);
279 service
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
280 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
281 service
.putExtra(OperationsService
.EXTRA_NEWNAME
, newFilename
);
282 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().queueNewOperation(service
);
284 mFileActivity
.showLoadingDialog();
288 public void removeFile(OCFile file
, boolean onlyLocalCopy
) {
290 Intent service
= new Intent(mFileActivity
, OperationsService
.class);
291 service
.setAction(OperationsService
.ACTION_REMOVE
);
292 service
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
293 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
294 service
.putExtra(OperationsService
.EXTRA_REMOVE_ONLY_LOCAL
, onlyLocalCopy
);
295 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().queueNewOperation(service
);
297 mFileActivity
.showLoadingDialog();
301 public void createFolder(String remotePath
, boolean createFullPath
) {
303 Intent service
= new Intent(mFileActivity
, OperationsService
.class);
304 service
.setAction(OperationsService
.ACTION_CREATE_FOLDER
);
305 service
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
306 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, remotePath
);
307 service
.putExtra(OperationsService
.EXTRA_CREATE_FULL_PATH
, createFullPath
);
308 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().queueNewOperation(service
);
310 mFileActivity
.showLoadingDialog();
314 * Cancel the transference in downloads (files/folders) and file uploads
317 public void cancelTransference(OCFile file
) {
318 Account account
= mFileActivity
.getAccount();
319 if (file
.isFolder()) {
320 OperationsService
.OperationsServiceBinder opsBinder
= mFileActivity
.getOperationsServiceBinder();
321 if (opsBinder
!= null
) {
322 opsBinder
.cancel(account
, file
);
326 // for both files and folders
327 FileDownloaderBinder downloaderBinder
= mFileActivity
.getFileDownloaderBinder();
328 if (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(account
, file
)) {
329 downloaderBinder
.cancel(account
, file
);
331 // TODO - review why is this here, and solve in a better way
332 // Remove etag for parent, if file is a favorite
333 if (file
.isFavorite()) {
334 OCFile parent
= mFileActivity
.getStorageManager().getFileById(file
.getParentId());
336 mFileActivity
.getStorageManager().saveFile(parent
);
340 FileUploaderBinder uploaderBinder
= mFileActivity
.getFileUploaderBinder();
341 if (uploaderBinder
!= null
&& uploaderBinder
.isUploading(account
, file
)) {
342 uploaderBinder
.cancel(account
, file
);
347 * Start move file operation
349 * @param newfile File where it is going to be moved
350 * @param currentFile File with the previous info
352 public void moveFile(OCFile newfile
, OCFile currentFile
) {
354 Intent service
= new Intent(mFileActivity
, OperationsService
.class);
355 service
.setAction(OperationsService
.ACTION_MOVE_FILE
);
356 service
.putExtra(OperationsService
.EXTRA_NEW_PARENT_PATH
, newfile
.getRemotePath());
357 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, currentFile
.getRemotePath());
358 service
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
359 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().queueNewOperation(service
);
361 mFileActivity
.showLoadingDialog();
365 * Start copy file operation
367 * @param newfile File where it is going to be moved
368 * @param currentFile File with the previous info
370 public void copyFile(OCFile newfile
, OCFile currentFile
) {
372 Intent service
= new Intent(mFileActivity
, OperationsService
.class);
373 service
.setAction(OperationsService
.ACTION_COPY_FILE
);
374 service
.putExtra(OperationsService
.EXTRA_NEW_PARENT_PATH
, newfile
.getRemotePath());
375 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, currentFile
.getRemotePath());
376 service
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
377 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().queueNewOperation(service
);
379 mFileActivity
.showLoadingDialog();
382 public long getOpIdWaitingFor() {
383 return mWaitingForOpId
;
387 public void setOpIdWaitingFor(long waitingForOpId
) {
388 mWaitingForOpId
= waitingForOpId
;
392 * @return 'True' if the server doesn't need to check forbidden characters
394 public boolean isVersionWithForbiddenCharacters() {
395 if (mFileActivity
.getAccount() != null
) {
396 OwnCloudVersion serverVersion
= AccountUtils
.getServerVersion(mFileActivity
.getAccount());
397 return (serverVersion
!= null
&& serverVersion
.isVersionWithForbiddenCharacters());