1 /* ownCloud Android client application
2 * Copyright (C) 2012-2014 ownCloud Inc.
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2,
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package com
.owncloud
.android
.files
;
20 import org
.apache
.http
.protocol
.HTTP
;
22 import android
.accounts
.Account
;
23 import android
.accounts
.AccountManager
;
24 import android
.content
.Intent
;
25 import android
.net
.Uri
;
26 import android
.support
.v4
.app
.DialogFragment
;
27 import android
.webkit
.MimeTypeMap
;
28 import android
.widget
.Toast
;
30 import com
.owncloud
.android
.R
;
31 import com
.owncloud
.android
.datamodel
.OCFile
;
32 import com
.owncloud
.android
.files
.services
.FileDownloader
;
33 import com
.owncloud
.android
.files
.services
.FileDownloader
.FileDownloaderBinder
;
34 import com
.owncloud
.android
.files
.services
.FileUploader
.FileUploaderBinder
;
36 import com
.owncloud
.android
.lib
.common
.accounts
.AccountUtils
.Constants
;
37 import com
.owncloud
.android
.lib
.common
.network
.WebdavUtils
;
38 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
39 import com
.owncloud
.android
.lib
.resources
.status
.OwnCloudVersion
;
40 import com
.owncloud
.android
.services
.OperationsService
;
41 import com
.owncloud
.android
.ui
.activity
.FileActivity
;
42 import com
.owncloud
.android
.ui
.dialog
.ShareLinkToDialog
;
47 * @author David A. Velasco
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();
213 // Add files recursivly
214 FileDataStorageManager storageManager = mFileActivity.getStorageManager();
215 filesList.addAll(storageManager.getFolderContent(file));
218 Vector<OCFile> tmpFolders = new Vector<OCFile>();
219 for (OCFile tmpfile : filesList) {
220 if (tmpfile.isFolder()) {
221 tmpFolders.add(tmpfile);
224 if (tmpFolders.isEmpty()){
227 for(OCFile tmpFolder : tmpFolders){
228 filesList.remove(tmpFolder);
229 filesList.addAll(storageManager.getFolderContent(tmpFolder));
235 Intent intent
= new Intent(mFileActivity
, OperationsService
.class);
236 intent
.setAction(OperationsService
.ACTION_SYNC_FOLDER
);
237 intent
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
238 intent
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
239 mFileActivity
.startService(intent
); // reevaluating: with or without Binder?
240 //mFileActivity.getOperationsServiceBinder().queueNewOperation(intent);
244 public void renameFile(OCFile file
, String newFilename
) {
246 Intent service
= new Intent(mFileActivity
, OperationsService
.class);
247 service
.setAction(OperationsService
.ACTION_RENAME
);
248 service
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
249 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
250 service
.putExtra(OperationsService
.EXTRA_NEWNAME
, newFilename
);
251 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().queueNewOperation(service
);
253 mFileActivity
.showLoadingDialog();
257 public void removeFile(OCFile file
, boolean onlyLocalCopy
) {
259 Intent service
= new Intent(mFileActivity
, OperationsService
.class);
260 service
.setAction(OperationsService
.ACTION_REMOVE
);
261 service
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
262 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
263 service
.putExtra(OperationsService
.EXTRA_REMOVE_ONLY_LOCAL
, onlyLocalCopy
);
264 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().queueNewOperation(service
);
266 mFileActivity
.showLoadingDialog();
270 public void createFolder(String remotePath
, boolean createFullPath
) {
272 Intent service
= new Intent(mFileActivity
, OperationsService
.class);
273 service
.setAction(OperationsService
.ACTION_CREATE_FOLDER
);
274 service
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
275 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, remotePath
);
276 service
.putExtra(OperationsService
.EXTRA_CREATE_FULL_PATH
, createFullPath
);
277 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().queueNewOperation(service
);
279 mFileActivity
.showLoadingDialog();
283 * Cancel the transference in downloads (files/folders) and file uploads
286 public void cancelTransference(OCFile file
) {
287 Account account
= mFileActivity
.getAccount();
288 if (!file
.isFolder()) {
289 FileDownloaderBinder downloaderBinder
= mFileActivity
.getFileDownloaderBinder();
290 FileUploaderBinder uploaderBinder
= mFileActivity
.getFileUploaderBinder();
291 if (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(account
, file
)) {
292 // Remove etag for parent, if file is a keep_in_sync
293 if (file
.keepInSync()) {
294 OCFile parent
= mFileActivity
.getStorageManager().getFileById(file
.getParentId());
296 mFileActivity
.getStorageManager().saveFile(parent
);
299 downloaderBinder
.cancel(account
, file
);
301 } else if (uploaderBinder
!= null
&& uploaderBinder
.isUploading(account
, file
)) {
302 uploaderBinder
.cancel(account
, file
);
305 Intent intent
= new Intent(mFileActivity
, OperationsService
.class);
306 intent
.setAction(OperationsService
.ACTION_CANCEL_SYNC_FOLDER
);
307 intent
.putExtra(OperationsService
.EXTRA_ACCOUNT
, account
);
308 intent
.putExtra(OperationsService
.EXTRA_FILE
, file
);
309 mFileActivity
.startService(intent
);
315 * Start move file operation
316 * @param newfile File where it is going to be moved
317 * @param currentFile File with the previous info
319 public void moveFile(OCFile newfile
, OCFile currentFile
) {
321 Intent service
= new Intent(mFileActivity
, OperationsService
.class);
322 service
.setAction(OperationsService
.ACTION_MOVE_FILE
);
323 service
.putExtra(OperationsService
.EXTRA_NEW_PARENT_PATH
, newfile
.getRemotePath());
324 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, currentFile
.getRemotePath());
325 service
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
326 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().queueNewOperation(service
);
328 mFileActivity
.showLoadingDialog();
332 public long getOpIdWaitingFor() {
333 return mWaitingForOpId
;
337 public void setOpIdWaitingFor(long waitingForOpId
) {
338 mWaitingForOpId
= waitingForOpId
;