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 org
.apache
.http
.protocol
.HTTP
;
26 import android
.accounts
.Account
;
27 import android
.accounts
.AccountManager
;
28 import android
.content
.Intent
;
29 import android
.net
.Uri
;
30 import android
.support
.v4
.app
.DialogFragment
;
31 import android
.webkit
.MimeTypeMap
;
32 import android
.widget
.Toast
;
34 import com
.owncloud
.android
.R
;
35 import com
.owncloud
.android
.datamodel
.OCFile
;
36 import com
.owncloud
.android
.files
.services
.FileDownloader
.FileDownloaderBinder
;
37 import com
.owncloud
.android
.files
.services
.FileUploader
.FileUploaderBinder
;
39 import com
.owncloud
.android
.lib
.common
.accounts
.AccountUtils
.Constants
;
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
.ui
.activity
.FileActivity
;
45 import com
.owncloud
.android
.ui
.dialog
.ShareLinkToDialog
;
50 public class FileOperationsHelper
{
52 private static final String TAG
= FileOperationsHelper
.class.getName();
54 private static final String FTAG_CHOOSER_DIALOG
= "CHOOSER_DIALOG";
56 protected FileActivity mFileActivity
= null
;
58 /// Identifier of operation in progress which result shouldn't be lost
59 private long mWaitingForOpId
= Long
.MAX_VALUE
;
61 public FileOperationsHelper(FileActivity fileActivity
) {
62 mFileActivity
= fileActivity
;
66 public void openFile(OCFile file
) {
68 String storagePath
= file
.getStoragePath();
69 String encodedStoragePath
= WebdavUtils
.encodePath(storagePath
);
71 Intent intentForSavedMimeType
= new Intent(Intent
.ACTION_VIEW
);
72 intentForSavedMimeType
.setDataAndType(Uri
.parse("file://"+ encodedStoragePath
), file
.getMimetype());
73 intentForSavedMimeType
.setFlags(Intent
.FLAG_GRANT_READ_URI_PERMISSION
| Intent
.FLAG_GRANT_WRITE_URI_PERMISSION
);
75 Intent intentForGuessedMimeType
= null
;
76 if (storagePath
.lastIndexOf('.') >= 0) {
77 String guessedMimeType
= MimeTypeMap
.getSingleton().getMimeTypeFromExtension(storagePath
.substring(storagePath
.lastIndexOf('.') + 1));
78 if (guessedMimeType
!= null
&& !guessedMimeType
.equals(file
.getMimetype())) {
79 intentForGuessedMimeType
= new Intent(Intent
.ACTION_VIEW
);
80 intentForGuessedMimeType
.setDataAndType(Uri
.parse("file://"+ encodedStoragePath
), guessedMimeType
);
81 intentForGuessedMimeType
.setFlags(Intent
.FLAG_GRANT_READ_URI_PERMISSION
| Intent
.FLAG_GRANT_WRITE_URI_PERMISSION
);
85 Intent chooserIntent
= null
;
86 if (intentForGuessedMimeType
!= null
) {
87 chooserIntent
= Intent
.createChooser(intentForGuessedMimeType
, mFileActivity
.getString(R
.string
.actionbar_open_with
));
89 chooserIntent
= Intent
.createChooser(intentForSavedMimeType
, mFileActivity
.getString(R
.string
.actionbar_open_with
));
92 mFileActivity
.startActivity(chooserIntent
);
95 Log_OC
.wtf(TAG
, "Trying to open a NULL OCFile");
100 public void shareFileWithLink(OCFile file
) {
102 if (isSharedSupported()) {
104 String link
= "https://fake.url";
105 Intent intent
= createShareWithLinkIntent(link
);
106 String
[] packagesToExclude
= new String
[] { mFileActivity
.getPackageName() };
107 DialogFragment chooserDialog
= ShareLinkToDialog
.newInstance(intent
, packagesToExclude
, file
);
108 chooserDialog
.show(mFileActivity
.getSupportFragmentManager(), FTAG_CHOOSER_DIALOG
);
111 Log_OC
.wtf(TAG
, "Trying to share a NULL OCFile");
116 Toast t
= Toast
.makeText(mFileActivity
, mFileActivity
.getString(R
.string
.share_link_no_support_share_api
), Toast
.LENGTH_LONG
);
122 public void shareFileWithLinkToApp(OCFile file
, Intent sendIntent
) {
125 mFileActivity
.showLoadingDialog();
127 Intent service
= new Intent(mFileActivity
, OperationsService
.class);
128 service
.setAction(OperationsService
.ACTION_CREATE_SHARE
);
129 service
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
130 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
131 service
.putExtra(OperationsService
.EXTRA_SEND_INTENT
, sendIntent
);
132 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().queueNewOperation(service
);
135 Log_OC
.wtf(TAG
, "Trying to open a NULL OCFile");
140 private Intent
createShareWithLinkIntent(String link
) {
141 Intent intentToShareLink
= new Intent(Intent
.ACTION_SEND
);
142 intentToShareLink
.putExtra(Intent
.EXTRA_TEXT
, link
);
143 intentToShareLink
.setType(HTTP
.PLAIN_TEXT_TYPE
);
144 return intentToShareLink
;
149 * @return 'True' if the server supports the Share API
151 public boolean isSharedSupported() {
152 if (mFileActivity
.getAccount() != null
) {
153 AccountManager accountManager
= AccountManager
.get(mFileActivity
);
155 String version
= accountManager
.getUserData(mFileActivity
.getAccount(), Constants
.KEY_OC_VERSION
);
156 return (new OwnCloudVersion(version
)).isSharedSupported();
162 public void unshareFileWithLink(OCFile file
) {
164 if (isSharedSupported()) {
166 Intent service
= new Intent(mFileActivity
, OperationsService
.class);
167 service
.setAction(OperationsService
.ACTION_UNSHARE
);
168 service
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
169 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
170 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().queueNewOperation(service
);
172 mFileActivity
.showLoadingDialog();
176 Toast t
= Toast
.makeText(mFileActivity
, mFileActivity
.getString(R
.string
.share_link_no_support_share_api
), Toast
.LENGTH_LONG
);
182 public void sendDownloadedFile(OCFile file
) {
184 Intent sendIntent
= new Intent(android
.content
.Intent
.ACTION_SEND
);
186 sendIntent
.setType(file
.getMimetype());
187 sendIntent
.putExtra(Intent
.EXTRA_STREAM
, Uri
.parse("file://" + file
.getStoragePath()));
188 sendIntent
.putExtra(Intent
.ACTION_SEND
, true
); // Send Action
190 // Show dialog, without the own app
191 String
[] packagesToExclude
= new String
[] { mFileActivity
.getPackageName() };
192 DialogFragment chooserDialog
= ShareLinkToDialog
.newInstance(sendIntent
, packagesToExclude
, file
);
193 chooserDialog
.show(mFileActivity
.getSupportFragmentManager(), FTAG_CHOOSER_DIALOG
);
196 Log_OC
.wtf(TAG
, "Trying to send a NULL OCFile");
201 public void syncFile(OCFile file
) {
203 if (!file
.isFolder()){
204 Intent intent
= new Intent(mFileActivity
, OperationsService
.class);
205 intent
.setAction(OperationsService
.ACTION_SYNC_FILE
);
206 intent
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
207 intent
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
208 intent
.putExtra(OperationsService
.EXTRA_SYNC_FILE_CONTENTS
, true
);
209 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().queueNewOperation(intent
);
210 mFileActivity
.showLoadingDialog();
213 Intent intent
= new Intent(mFileActivity
, OperationsService
.class);
214 intent
.setAction(OperationsService
.ACTION_SYNC_FOLDER
);
215 intent
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
216 intent
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
217 mFileActivity
.startService(intent
);
221 public void renameFile(OCFile file
, String newFilename
) {
223 Intent service
= new Intent(mFileActivity
, OperationsService
.class);
224 service
.setAction(OperationsService
.ACTION_RENAME
);
225 service
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
226 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
227 service
.putExtra(OperationsService
.EXTRA_NEWNAME
, newFilename
);
228 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().queueNewOperation(service
);
230 mFileActivity
.showLoadingDialog();
234 public void removeFile(OCFile file
, boolean onlyLocalCopy
) {
236 Intent service
= new Intent(mFileActivity
, OperationsService
.class);
237 service
.setAction(OperationsService
.ACTION_REMOVE
);
238 service
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
239 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
240 service
.putExtra(OperationsService
.EXTRA_REMOVE_ONLY_LOCAL
, onlyLocalCopy
);
241 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().queueNewOperation(service
);
243 mFileActivity
.showLoadingDialog();
247 public void createFolder(String remotePath
, boolean createFullPath
) {
249 Intent service
= new Intent(mFileActivity
, OperationsService
.class);
250 service
.setAction(OperationsService
.ACTION_CREATE_FOLDER
);
251 service
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
252 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, remotePath
);
253 service
.putExtra(OperationsService
.EXTRA_CREATE_FULL_PATH
, createFullPath
);
254 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().queueNewOperation(service
);
256 mFileActivity
.showLoadingDialog();
260 * Cancel the transference in downloads (files/folders) and file uploads
263 public void cancelTransference(OCFile file
) {
264 Account account
= mFileActivity
.getAccount();
265 if (file
.isFolder()) {
266 OperationsService
.OperationsServiceBinder opsBinder
= mFileActivity
.getOperationsServiceBinder();
267 if (opsBinder
!= null
) {
268 opsBinder
.cancel(account
, file
);
272 // for both files and folders
273 FileDownloaderBinder downloaderBinder
= mFileActivity
.getFileDownloaderBinder();
274 FileUploaderBinder uploaderBinder
= mFileActivity
.getFileUploaderBinder();
275 if (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(account
, file
)) {
276 downloaderBinder
.cancel(account
, file
);
278 // TODO - review why is this here, and solve in a better way
279 // Remove etag for parent, if file is a keep_in_sync
280 if (file
.keepInSync()) {
281 OCFile parent
= mFileActivity
.getStorageManager().getFileById(file
.getParentId());
283 mFileActivity
.getStorageManager().saveFile(parent
);
286 } else if (uploaderBinder
!= null
&& uploaderBinder
.isUploading(account
, file
)) {
287 uploaderBinder
.cancel(account
, file
);
292 * Start move file operation
293 * @param newfile File where it is going to be moved
294 * @param currentFile File with the previous info
296 public void moveFile(OCFile newfile
, OCFile currentFile
) {
298 Intent service
= new Intent(mFileActivity
, OperationsService
.class);
299 service
.setAction(OperationsService
.ACTION_MOVE_FILE
);
300 service
.putExtra(OperationsService
.EXTRA_NEW_PARENT_PATH
, newfile
.getRemotePath());
301 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, currentFile
.getRemotePath());
302 service
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
303 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().queueNewOperation(service
);
305 mFileActivity
.showLoadingDialog();
309 public long getOpIdWaitingFor() {
310 return mWaitingForOpId
;
314 public void setOpIdWaitingFor(long waitingForOpId
) {
315 mWaitingForOpId
= waitingForOpId
;