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
;
33 import com
.owncloud
.android
.lib
.common
.accounts
.AccountUtils
.Constants
;
34 import com
.owncloud
.android
.lib
.common
.network
.WebdavUtils
;
35 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperation
;
36 import com
.owncloud
.android
.lib
.resources
.status
.OwnCloudVersion
;
37 import com
.owncloud
.android
.operations
.RemoveFileOperation
;
38 import com
.owncloud
.android
.operations
.RenameFileOperation
;
39 import com
.owncloud
.android
.operations
.SynchronizeFileOperation
;
40 import com
.owncloud
.android
.services
.OperationsService
;
41 import com
.owncloud
.android
.ui
.activity
.FileActivity
;
42 import com
.owncloud
.android
.ui
.dialog
.ShareLinkToDialog
;
43 import com
.owncloud
.android
.utils
.Log_OC
;
48 * @author David A. Velasco
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 public FileOperationsHelper(FileActivity fileActivity
) {
59 mFileActivity
= fileActivity
;
63 public void openFile(OCFile file
) {
65 String storagePath
= file
.getStoragePath();
66 String encodedStoragePath
= WebdavUtils
.encodePath(storagePath
);
68 Intent intentForSavedMimeType
= new Intent(Intent
.ACTION_VIEW
);
69 intentForSavedMimeType
.setDataAndType(Uri
.parse("file://"+ encodedStoragePath
), file
.getMimetype());
70 intentForSavedMimeType
.setFlags(Intent
.FLAG_GRANT_READ_URI_PERMISSION
| Intent
.FLAG_GRANT_WRITE_URI_PERMISSION
);
72 Intent intentForGuessedMimeType
= null
;
73 if (storagePath
.lastIndexOf('.') >= 0) {
74 String guessedMimeType
= MimeTypeMap
.getSingleton().getMimeTypeFromExtension(storagePath
.substring(storagePath
.lastIndexOf('.') + 1));
75 if (guessedMimeType
!= null
&& !guessedMimeType
.equals(file
.getMimetype())) {
76 intentForGuessedMimeType
= new Intent(Intent
.ACTION_VIEW
);
77 intentForGuessedMimeType
.setDataAndType(Uri
.parse("file://"+ encodedStoragePath
), guessedMimeType
);
78 intentForGuessedMimeType
.setFlags(Intent
.FLAG_GRANT_READ_URI_PERMISSION
| Intent
.FLAG_GRANT_WRITE_URI_PERMISSION
);
82 Intent chooserIntent
= null
;
83 if (intentForGuessedMimeType
!= null
) {
84 chooserIntent
= Intent
.createChooser(intentForGuessedMimeType
, mFileActivity
.getString(R
.string
.actionbar_open_with
));
86 chooserIntent
= Intent
.createChooser(intentForSavedMimeType
, mFileActivity
.getString(R
.string
.actionbar_open_with
));
89 mFileActivity
.startActivity(chooserIntent
);
92 Log_OC
.wtf(TAG
, "Trying to open a NULL OCFile");
97 public void shareFileWithLink(OCFile file
) {
99 if (isSharedSupported()) {
101 String link
= "https://fake.url";
102 Intent intent
= createShareWithLinkIntent(link
);
103 String
[] packagesToExclude
= new String
[] { mFileActivity
.getPackageName() };
104 DialogFragment chooserDialog
= ShareLinkToDialog
.newInstance(intent
, packagesToExclude
, file
);
105 chooserDialog
.show(mFileActivity
.getSupportFragmentManager(), FTAG_CHOOSER_DIALOG
);
108 Log_OC
.wtf(TAG
, "Trying to share a NULL OCFile");
113 Toast t
= Toast
.makeText(mFileActivity
, mFileActivity
.getString(R
.string
.share_link_no_support_share_api
), Toast
.LENGTH_LONG
);
119 public void shareFileWithLinkToApp(OCFile file
, Intent sendIntent
) {
122 mFileActivity
.showLoadingDialog();
124 Intent service
= new Intent(mFileActivity
, OperationsService
.class);
125 service
.setAction(OperationsService
.ACTION_CREATE_SHARE
);
126 service
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
127 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
128 service
.putExtra(OperationsService
.EXTRA_SEND_INTENT
, sendIntent
);
129 mFileActivity
.getOperationsServiceBinder().newOperation(service
);
132 Log_OC
.wtf(TAG
, "Trying to open a NULL OCFile");
137 private Intent
createShareWithLinkIntent(String link
) {
138 Intent intentToShareLink
= new Intent(Intent
.ACTION_SEND
);
139 intentToShareLink
.putExtra(Intent
.EXTRA_TEXT
, link
);
140 intentToShareLink
.setType(HTTP
.PLAIN_TEXT_TYPE
);
141 return intentToShareLink
;
146 * @return 'True' if the server supports the Share API
148 public boolean isSharedSupported() {
149 if (mFileActivity
.getAccount() != null
) {
150 AccountManager accountManager
= AccountManager
.get(mFileActivity
);
152 String version
= accountManager
.getUserData(mFileActivity
.getAccount(), Constants
.KEY_OC_VERSION
);
153 return (new OwnCloudVersion(version
)).isSharedSupported();
159 public void unshareFileWithLink(OCFile file
) {
161 if (isSharedSupported()) {
163 Intent service
= new Intent(mFileActivity
, OperationsService
.class);
164 service
.setAction(OperationsService
.ACTION_UNSHARE
);
165 service
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
166 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
167 mFileActivity
.getOperationsServiceBinder().newOperation(service
);
169 mFileActivity
.showLoadingDialog();
173 Toast t
= Toast
.makeText(mFileActivity
, mFileActivity
.getString(R
.string
.share_link_no_support_share_api
), Toast
.LENGTH_LONG
);
179 public void sendDownloadedFile(OCFile file
) {
181 Intent sendIntent
= new Intent(android
.content
.Intent
.ACTION_SEND
);
183 sendIntent
.setType(file
.getMimetype());
184 sendIntent
.putExtra(Intent
.EXTRA_STREAM
, Uri
.parse("file://" + file
.getStoragePath()));
185 sendIntent
.putExtra(Intent
.ACTION_SEND
, true
); // Send Action
187 // Show dialog, without the own app
188 String
[] packagesToExclude
= new String
[] { mFileActivity
.getPackageName() };
189 DialogFragment chooserDialog
= ShareLinkToDialog
.newInstance(sendIntent
, packagesToExclude
, file
);
190 chooserDialog
.show(mFileActivity
.getSupportFragmentManager(), FTAG_CHOOSER_DIALOG
);
193 Log_OC
.wtf(TAG
, "Trying to send a NULL OCFile");
198 public void syncFile(OCFile file
) {
199 Account account
= mFileActivity
.getAccount();
200 RemoteOperation operation
= new SynchronizeFileOperation(
203 mFileActivity
.getStorageManager(),
207 operation
.execute(account
, mFileActivity
, mFileActivity
, mFileActivity
.getHandler(), mFileActivity
);
208 mFileActivity
.showLoadingDialog();
212 public void renameFile(OCFile file
, String newFilename
) {
213 Account account
= mFileActivity
.getAccount();
214 RemoteOperation operation
= new RenameFileOperation(
218 mFileActivity
.getStorageManager());
224 mFileActivity
.getHandler(),
227 mFileActivity
.showLoadingDialog();
231 public void removeFile(OCFile file
, boolean removeLocalCopy
) {
232 Account account
= mFileActivity
.getAccount();
233 RemoteOperation operation
= new RemoveFileOperation(
236 mFileActivity
.getStorageManager());
242 mFileActivity
.getHandler(),
245 mFileActivity
.showLoadingDialog();