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
.graphics
.Bitmap
;
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
.MainApp
;
36 import com
.owncloud
.android
.R
;
37 import com
.owncloud
.android
.datamodel
.OCFile
;
38 import com
.owncloud
.android
.datamodel
.ThumbnailsCacheManager
;
39 import com
.owncloud
.android
.files
.services
.FileDownloader
.FileDownloaderBinder
;
40 import com
.owncloud
.android
.files
.services
.FileUploader
.FileUploaderBinder
;
42 import com
.owncloud
.android
.lib
.common
.accounts
.AccountUtils
.Constants
;
43 import com
.owncloud
.android
.lib
.common
.network
.WebdavUtils
;
44 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
45 import com
.owncloud
.android
.lib
.resources
.status
.OwnCloudVersion
;
46 import com
.owncloud
.android
.services
.OperationsService
;
47 import com
.owncloud
.android
.ui
.activity
.FileActivity
;
48 import com
.owncloud
.android
.ui
.adapter
.DiskLruImageCacheFileProvider
;
49 import com
.owncloud
.android
.ui
.dialog
.ShareLinkToDialog
;
51 import java
.io
.ByteArrayOutputStream
;
53 import java
.io
.FileNotFoundException
;
54 import java
.io
.FileOutputStream
;
55 import java
.io
.IOException
;
60 public class FileOperationsHelper
{
62 private static final String TAG
= FileOperationsHelper
.class.getName();
64 private static final String FTAG_CHOOSER_DIALOG
= "CHOOSER_DIALOG";
66 protected FileActivity mFileActivity
= null
;
68 /// Identifier of operation in progress which result shouldn't be lost
69 private long mWaitingForOpId
= Long
.MAX_VALUE
;
71 public FileOperationsHelper(FileActivity fileActivity
) {
72 mFileActivity
= fileActivity
;
76 public void openFile(OCFile file
) {
78 String storagePath
= file
.getStoragePath();
79 String encodedStoragePath
= WebdavUtils
.encodePath(storagePath
);
81 Intent intentForSavedMimeType
= new Intent(Intent
.ACTION_VIEW
);
82 intentForSavedMimeType
.setDataAndType(Uri
.parse("file://"+ encodedStoragePath
), file
.getMimetype());
83 intentForSavedMimeType
.setFlags(Intent
.FLAG_GRANT_READ_URI_PERMISSION
| Intent
.FLAG_GRANT_WRITE_URI_PERMISSION
);
85 Intent intentForGuessedMimeType
= null
;
86 if (storagePath
.lastIndexOf('.') >= 0) {
87 String guessedMimeType
= MimeTypeMap
.getSingleton().getMimeTypeFromExtension(storagePath
.substring(storagePath
.lastIndexOf('.') + 1));
88 if (guessedMimeType
!= null
&& !guessedMimeType
.equals(file
.getMimetype())) {
89 intentForGuessedMimeType
= new Intent(Intent
.ACTION_VIEW
);
90 intentForGuessedMimeType
.setDataAndType(Uri
.parse("file://"+ encodedStoragePath
), guessedMimeType
);
91 intentForGuessedMimeType
.setFlags(Intent
.FLAG_GRANT_READ_URI_PERMISSION
| Intent
.FLAG_GRANT_WRITE_URI_PERMISSION
);
95 Intent chooserIntent
= null
;
96 if (intentForGuessedMimeType
!= null
) {
97 chooserIntent
= Intent
.createChooser(intentForGuessedMimeType
, mFileActivity
.getString(R
.string
.actionbar_open_with
));
99 chooserIntent
= Intent
.createChooser(intentForSavedMimeType
, mFileActivity
.getString(R
.string
.actionbar_open_with
));
102 mFileActivity
.startActivity(chooserIntent
);
105 Log_OC
.wtf(TAG
, "Trying to open a NULL OCFile");
110 public void shareFileWithLink(OCFile file
) {
112 if (isSharedSupported()) {
114 String link
= "https://fake.url";
115 Intent intent
= createShareWithLinkIntent(link
);
116 String
[] packagesToExclude
= new String
[] { mFileActivity
.getPackageName() };
117 DialogFragment chooserDialog
= ShareLinkToDialog
.newInstance(intent
, packagesToExclude
, file
);
118 chooserDialog
.show(mFileActivity
.getSupportFragmentManager(), FTAG_CHOOSER_DIALOG
);
121 Log_OC
.wtf(TAG
, "Trying to share a NULL OCFile");
126 Toast t
= Toast
.makeText(mFileActivity
, mFileActivity
.getString(R
.string
.share_link_no_support_share_api
), Toast
.LENGTH_LONG
);
132 public void shareFileWithLinkToApp(OCFile file
, String password
, Intent sendIntent
) {
135 mFileActivity
.showLoadingDialog();
137 Intent service
= new Intent(mFileActivity
, OperationsService
.class);
138 service
.setAction(OperationsService
.ACTION_CREATE_SHARE
);
139 service
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
140 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
141 service
.putExtra(OperationsService
.EXTRA_PASSWORD_SHARE
, password
);
142 service
.putExtra(OperationsService
.EXTRA_SEND_INTENT
, sendIntent
);
143 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().queueNewOperation(service
);
146 Log_OC
.wtf(TAG
, "Trying to open a NULL OCFile");
151 private Intent
createShareWithLinkIntent(String link
) {
152 Intent intentToShareLink
= new Intent(Intent
.ACTION_SEND
);
153 intentToShareLink
.putExtra(Intent
.EXTRA_TEXT
, link
);
154 intentToShareLink
.setType(HTTP
.PLAIN_TEXT_TYPE
);
155 return intentToShareLink
;
160 * @return 'True' if the server supports the Share API
162 public boolean isSharedSupported() {
163 if (mFileActivity
.getAccount() != null
) {
164 AccountManager accountManager
= AccountManager
.get(mFileActivity
);
166 String version
= accountManager
.getUserData(mFileActivity
.getAccount(), Constants
.KEY_OC_VERSION
);
167 return (new OwnCloudVersion(version
)).isSharedSupported();
173 public void unshareFileWithLink(OCFile file
) {
175 if (isSharedSupported()) {
177 Intent service
= new Intent(mFileActivity
, OperationsService
.class);
178 service
.setAction(OperationsService
.ACTION_UNSHARE
);
179 service
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
180 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
181 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().queueNewOperation(service
);
183 mFileActivity
.showLoadingDialog();
187 Toast t
= Toast
.makeText(mFileActivity
, mFileActivity
.getString(R
.string
.share_link_no_support_share_api
), Toast
.LENGTH_LONG
);
193 public void sendDownloadedFile(OCFile file
) {
195 Intent sendIntent
= new Intent(android
.content
.Intent
.ACTION_SEND
);
197 sendIntent
.setType(file
.getMimetype());
198 sendIntent
.putExtra(Intent
.EXTRA_STREAM
, Uri
.parse("file://" + file
.getStoragePath()));
199 sendIntent
.putExtra(Intent
.ACTION_SEND
, true
); // Send Action
201 // Show dialog, without the own app
202 String
[] packagesToExclude
= new String
[] { mFileActivity
.getPackageName() };
203 DialogFragment chooserDialog
= ShareLinkToDialog
.newInstance(sendIntent
, packagesToExclude
, file
);
204 chooserDialog
.show(mFileActivity
.getSupportFragmentManager(), FTAG_CHOOSER_DIALOG
);
207 Log_OC
.wtf(TAG
, "Trying to send a NULL OCFile");
211 public void sendCachedImage(OCFile file
) {
213 Intent sendIntent
= new Intent(android
.content
.Intent
.ACTION_SEND
);
215 sendIntent
.setType(file
.getMimetype());
216 // sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("content://" + DiskLruImageCacheFileProvider.AUTHORITY + "/#" + file.getRemoteId() + "#" + file.getFileName()));
217 sendIntent
.putExtra(Intent
.EXTRA_STREAM
, Uri
.parse("content://" + DiskLruImageCacheFileProvider
.AUTHORITY
+ file
.getRemotePath()));
218 sendIntent
.putExtra(Intent
.ACTION_SEND
, true
); // Send Action
220 // Show dialog, without the own app
221 String
[] packagesToExclude
= new String
[] { mFileActivity
.getPackageName() };
222 DialogFragment chooserDialog
= ShareLinkToDialog
.newInstance(sendIntent
, packagesToExclude
, file
);
223 chooserDialog
.show(mFileActivity
.getSupportFragmentManager(), FTAG_CHOOSER_DIALOG
);
225 Log_OC
.wtf(TAG
, "Trying to send a NULL OCFile");
230 public void syncFile(OCFile file
) {
232 if (!file
.isFolder()){
233 Intent intent
= new Intent(mFileActivity
, OperationsService
.class);
234 intent
.setAction(OperationsService
.ACTION_SYNC_FILE
);
235 intent
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
236 intent
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
237 intent
.putExtra(OperationsService
.EXTRA_SYNC_FILE_CONTENTS
, true
);
238 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().queueNewOperation(intent
);
239 mFileActivity
.showLoadingDialog();
242 Intent intent
= new Intent(mFileActivity
, OperationsService
.class);
243 intent
.setAction(OperationsService
.ACTION_SYNC_FOLDER
);
244 intent
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
245 intent
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
246 mFileActivity
.startService(intent
);
250 public void renameFile(OCFile file
, String newFilename
) {
252 Intent service
= new Intent(mFileActivity
, OperationsService
.class);
253 service
.setAction(OperationsService
.ACTION_RENAME
);
254 service
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
255 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
256 service
.putExtra(OperationsService
.EXTRA_NEWNAME
, newFilename
);
257 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().queueNewOperation(service
);
259 mFileActivity
.showLoadingDialog();
263 public void removeFile(OCFile file
, boolean onlyLocalCopy
) {
265 Intent service
= new Intent(mFileActivity
, OperationsService
.class);
266 service
.setAction(OperationsService
.ACTION_REMOVE
);
267 service
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
268 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
269 service
.putExtra(OperationsService
.EXTRA_REMOVE_ONLY_LOCAL
, onlyLocalCopy
);
270 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().queueNewOperation(service
);
272 mFileActivity
.showLoadingDialog();
276 public void createFolder(String remotePath
, boolean createFullPath
) {
278 Intent service
= new Intent(mFileActivity
, OperationsService
.class);
279 service
.setAction(OperationsService
.ACTION_CREATE_FOLDER
);
280 service
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
281 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, remotePath
);
282 service
.putExtra(OperationsService
.EXTRA_CREATE_FULL_PATH
, createFullPath
);
283 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().queueNewOperation(service
);
285 mFileActivity
.showLoadingDialog();
289 * Cancel the transference in downloads (files/folders) and file uploads
292 public void cancelTransference(OCFile file
) {
293 Account account
= mFileActivity
.getAccount();
294 if (file
.isFolder()) {
295 OperationsService
.OperationsServiceBinder opsBinder
= mFileActivity
.getOperationsServiceBinder();
296 if (opsBinder
!= null
) {
297 opsBinder
.cancel(account
, file
);
301 // for both files and folders
302 FileDownloaderBinder downloaderBinder
= mFileActivity
.getFileDownloaderBinder();
303 FileUploaderBinder uploaderBinder
= mFileActivity
.getFileUploaderBinder();
304 if (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(account
, file
)) {
305 downloaderBinder
.cancel(account
, file
);
307 // TODO - review why is this here, and solve in a better way
308 // Remove etag for parent, if file is a keep_in_sync
309 if (file
.keepInSync()) {
310 OCFile parent
= mFileActivity
.getStorageManager().getFileById(file
.getParentId());
312 mFileActivity
.getStorageManager().saveFile(parent
);
315 } else if (uploaderBinder
!= null
&& uploaderBinder
.isUploading(account
, file
)) {
316 uploaderBinder
.cancel(account
, file
);
321 * Start move file operation
322 * @param newfile File where it is going to be moved
323 * @param currentFile File with the previous info
325 public void moveFile(OCFile newfile
, OCFile currentFile
) {
327 Intent service
= new Intent(mFileActivity
, OperationsService
.class);
328 service
.setAction(OperationsService
.ACTION_MOVE_FILE
);
329 service
.putExtra(OperationsService
.EXTRA_NEW_PARENT_PATH
, newfile
.getRemotePath());
330 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, currentFile
.getRemotePath());
331 service
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
332 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().queueNewOperation(service
);
334 mFileActivity
.showLoadingDialog();
338 public long getOpIdWaitingFor() {
339 return mWaitingForOpId
;
343 public void setOpIdWaitingFor(long waitingForOpId
) {
344 mWaitingForOpId
= waitingForOpId
;