1 /* ownCloud Android client application
4 * @author David A. Velasco
5 * Copyright (C) 2012-2015 ownCloud Inc.
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2,
9 * as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 package com
.owncloud
.android
.files
;
23 import org
.apache
.http
.protocol
.HTTP
;
25 import android
.accounts
.Account
;
26 import android
.accounts
.AccountManager
;
27 import android
.content
.Intent
;
28 import android
.net
.Uri
;
29 import android
.support
.v4
.app
.DialogFragment
;
30 import android
.webkit
.MimeTypeMap
;
31 import android
.widget
.Toast
;
33 import com
.owncloud
.android
.R
;
34 import com
.owncloud
.android
.datamodel
.OCFile
;
35 import com
.owncloud
.android
.files
.services
.FileDownloader
.FileDownloaderBinder
;
36 import com
.owncloud
.android
.files
.services
.FileUploader
.FileUploaderBinder
;
38 import com
.owncloud
.android
.lib
.common
.accounts
.AccountUtils
.Constants
;
39 import com
.owncloud
.android
.lib
.common
.network
.WebdavUtils
;
40 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
41 import com
.owncloud
.android
.lib
.resources
.status
.OwnCloudVersion
;
42 import com
.owncloud
.android
.services
.OperationsService
;
43 import com
.owncloud
.android
.ui
.activity
.FileActivity
;
44 import com
.owncloud
.android
.ui
.dialog
.ShareLinkToDialog
;
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(Intent
.FLAG_GRANT_READ_URI_PERMISSION
| Intent
.FLAG_GRANT_WRITE_URI_PERMISSION
);
74 Intent intentForGuessedMimeType
= null
;
75 if (storagePath
.lastIndexOf('.') >= 0) {
76 String guessedMimeType
= MimeTypeMap
.getSingleton().getMimeTypeFromExtension(storagePath
.substring(storagePath
.lastIndexOf('.') + 1));
77 if (guessedMimeType
!= null
&& !guessedMimeType
.equals(file
.getMimetype())) {
78 intentForGuessedMimeType
= new Intent(Intent
.ACTION_VIEW
);
79 intentForGuessedMimeType
.setDataAndType(Uri
.parse("file://"+ encodedStoragePath
), guessedMimeType
);
80 intentForGuessedMimeType
.setFlags(Intent
.FLAG_GRANT_READ_URI_PERMISSION
| Intent
.FLAG_GRANT_WRITE_URI_PERMISSION
);
84 Intent chooserIntent
= null
;
85 if (intentForGuessedMimeType
!= null
) {
86 chooserIntent
= Intent
.createChooser(intentForGuessedMimeType
, mFileActivity
.getString(R
.string
.actionbar_open_with
));
88 chooserIntent
= Intent
.createChooser(intentForSavedMimeType
, mFileActivity
.getString(R
.string
.actionbar_open_with
));
91 mFileActivity
.startActivity(chooserIntent
);
94 Log_OC
.wtf(TAG
, "Trying to open a NULL OCFile");
99 public void shareFileWithLink(OCFile file
) {
101 if (isSharedSupported()) {
103 String link
= "https://fake.url";
104 Intent intent
= createShareWithLinkIntent(link
);
105 String
[] packagesToExclude
= new String
[] { mFileActivity
.getPackageName() };
106 DialogFragment chooserDialog
= ShareLinkToDialog
.newInstance(intent
, packagesToExclude
, file
);
107 chooserDialog
.show(mFileActivity
.getSupportFragmentManager(), FTAG_CHOOSER_DIALOG
);
110 Log_OC
.wtf(TAG
, "Trying to share a NULL OCFile");
115 Toast t
= Toast
.makeText(mFileActivity
, mFileActivity
.getString(R
.string
.share_link_no_support_share_api
), Toast
.LENGTH_LONG
);
121 public void shareFileWithLinkToApp(OCFile file
, Intent sendIntent
) {
124 mFileActivity
.showLoadingDialog();
126 Intent service
= new Intent(mFileActivity
, OperationsService
.class);
127 service
.setAction(OperationsService
.ACTION_CREATE_SHARE
);
128 service
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
129 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
130 service
.putExtra(OperationsService
.EXTRA_SEND_INTENT
, sendIntent
);
131 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().queueNewOperation(service
);
134 Log_OC
.wtf(TAG
, "Trying to open a NULL OCFile");
139 private Intent
createShareWithLinkIntent(String link
) {
140 Intent intentToShareLink
= new Intent(Intent
.ACTION_SEND
);
141 intentToShareLink
.putExtra(Intent
.EXTRA_TEXT
, link
);
142 intentToShareLink
.setType(HTTP
.PLAIN_TEXT_TYPE
);
143 return intentToShareLink
;
148 * @return 'True' if the server supports the Share API
150 public boolean isSharedSupported() {
151 if (mFileActivity
.getAccount() != null
) {
152 AccountManager accountManager
= AccountManager
.get(mFileActivity
);
154 String version
= accountManager
.getUserData(mFileActivity
.getAccount(), Constants
.KEY_OC_VERSION
);
155 return (new OwnCloudVersion(version
)).isSharedSupported();
161 public void unshareFileWithLink(OCFile file
) {
163 if (isSharedSupported()) {
165 Intent service
= new Intent(mFileActivity
, OperationsService
.class);
166 service
.setAction(OperationsService
.ACTION_UNSHARE
);
167 service
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
168 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
169 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().queueNewOperation(service
);
171 mFileActivity
.showLoadingDialog();
175 Toast t
= Toast
.makeText(mFileActivity
, mFileActivity
.getString(R
.string
.share_link_no_support_share_api
), Toast
.LENGTH_LONG
);
181 public void sendDownloadedFile(OCFile file
) {
183 Intent sendIntent
= new Intent(android
.content
.Intent
.ACTION_SEND
);
185 sendIntent
.setType(file
.getMimetype());
186 sendIntent
.putExtra(Intent
.EXTRA_STREAM
, Uri
.parse("file://" + file
.getStoragePath()));
187 sendIntent
.putExtra(Intent
.ACTION_SEND
, true
); // Send Action
189 // Show dialog, without the own app
190 String
[] packagesToExclude
= new String
[] { mFileActivity
.getPackageName() };
191 DialogFragment chooserDialog
= ShareLinkToDialog
.newInstance(sendIntent
, packagesToExclude
, file
);
192 chooserDialog
.show(mFileActivity
.getSupportFragmentManager(), FTAG_CHOOSER_DIALOG
);
195 Log_OC
.wtf(TAG
, "Trying to send a NULL OCFile");
200 public void syncFile(OCFile file
) {
202 if (!file
.isFolder()){
203 Intent intent
= new Intent(mFileActivity
, OperationsService
.class);
204 intent
.setAction(OperationsService
.ACTION_SYNC_FILE
);
205 intent
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
206 intent
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
207 intent
.putExtra(OperationsService
.EXTRA_SYNC_FILE_CONTENTS
, true
);
208 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().queueNewOperation(intent
);
209 mFileActivity
.showLoadingDialog();
212 Intent intent
= new Intent(mFileActivity
, OperationsService
.class);
213 intent
.setAction(OperationsService
.ACTION_SYNC_FOLDER
);
214 intent
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
215 intent
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
216 mFileActivity
.startService(intent
);
220 public void renameFile(OCFile file
, String newFilename
) {
222 Intent service
= new Intent(mFileActivity
, OperationsService
.class);
223 service
.setAction(OperationsService
.ACTION_RENAME
);
224 service
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
225 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
226 service
.putExtra(OperationsService
.EXTRA_NEWNAME
, newFilename
);
227 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().queueNewOperation(service
);
229 mFileActivity
.showLoadingDialog();
233 public void removeFile(OCFile file
, boolean onlyLocalCopy
) {
235 Intent service
= new Intent(mFileActivity
, OperationsService
.class);
236 service
.setAction(OperationsService
.ACTION_REMOVE
);
237 service
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
238 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
239 service
.putExtra(OperationsService
.EXTRA_REMOVE_ONLY_LOCAL
, onlyLocalCopy
);
240 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().queueNewOperation(service
);
242 mFileActivity
.showLoadingDialog();
246 public void createFolder(String remotePath
, boolean createFullPath
) {
248 Intent service
= new Intent(mFileActivity
, OperationsService
.class);
249 service
.setAction(OperationsService
.ACTION_CREATE_FOLDER
);
250 service
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
251 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, remotePath
);
252 service
.putExtra(OperationsService
.EXTRA_CREATE_FULL_PATH
, createFullPath
);
253 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().queueNewOperation(service
);
255 mFileActivity
.showLoadingDialog();
259 * Cancel the transference in downloads (files/folders) and file uploads
262 public void cancelTransference(OCFile file
) {
263 Account account
= mFileActivity
.getAccount();
264 if (file
.isFolder()) {
265 OperationsService
.OperationsServiceBinder opsBinder
= mFileActivity
.getOperationsServiceBinder();
266 if (opsBinder
!= null
) {
267 opsBinder
.cancel(account
, file
);
271 // for both files and folders
272 FileDownloaderBinder downloaderBinder
= mFileActivity
.getFileDownloaderBinder();
273 FileUploaderBinder uploaderBinder
= mFileActivity
.getFileUploaderBinder();
274 if (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(account
, file
)) {
275 downloaderBinder
.cancel(account
, file
);
277 // TODO - review why is this here, and solve in a better way
278 // Remove etag for parent, if file is a keep_in_sync
279 if (file
.keepInSync()) {
280 OCFile parent
= mFileActivity
.getStorageManager().getFileById(file
.getParentId());
282 mFileActivity
.getStorageManager().saveFile(parent
);
285 } else if (uploaderBinder
!= null
&& uploaderBinder
.isUploading(account
, file
)) {
286 uploaderBinder
.cancel(account
, file
);
291 * Start move file operation
292 * @param newfile File where it is going to be moved
293 * @param currentFile File with the previous info
295 public void moveFile(OCFile newfile
, OCFile currentFile
) {
297 Intent service
= new Intent(mFileActivity
, OperationsService
.class);
298 service
.setAction(OperationsService
.ACTION_MOVE_FILE
);
299 service
.putExtra(OperationsService
.EXTRA_NEW_PARENT_PATH
, newfile
.getRemotePath());
300 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, currentFile
.getRemotePath());
301 service
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
302 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().queueNewOperation(service
);
304 mFileActivity
.showLoadingDialog();
308 public long getOpIdWaitingFor() {
309 return mWaitingForOpId
;
313 public void setOpIdWaitingFor(long waitingForOpId
) {
314 mWaitingForOpId
= waitingForOpId
;