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
.SynchronizeFileOperation
;
38 import com
.owncloud
.android
.services
.OperationsService
;
39 import com
.owncloud
.android
.ui
.activity
.FileActivity
;
40 import com
.owncloud
.android
.ui
.dialog
.ShareLinkToDialog
;
41 import com
.owncloud
.android
.utils
.Log_OC
;
46 * @author David A. Velasco
48 public class FileOperationsHelper
{
50 private static final String TAG
= FileOperationsHelper
.class.getName();
52 private static final String FTAG_CHOOSER_DIALOG
= "CHOOSER_DIALOG";
54 protected FileActivity mFileActivity
= null
;
56 public FileOperationsHelper(FileActivity fileActivity
) {
57 mFileActivity
= fileActivity
;
61 public void openFile(OCFile file
) {
63 String storagePath
= file
.getStoragePath();
64 String encodedStoragePath
= WebdavUtils
.encodePath(storagePath
);
66 Intent intentForSavedMimeType
= new Intent(Intent
.ACTION_VIEW
);
67 intentForSavedMimeType
.setDataAndType(Uri
.parse("file://"+ encodedStoragePath
), file
.getMimetype());
68 intentForSavedMimeType
.setFlags(Intent
.FLAG_GRANT_READ_URI_PERMISSION
| Intent
.FLAG_GRANT_WRITE_URI_PERMISSION
);
70 Intent intentForGuessedMimeType
= null
;
71 if (storagePath
.lastIndexOf('.') >= 0) {
72 String guessedMimeType
= MimeTypeMap
.getSingleton().getMimeTypeFromExtension(storagePath
.substring(storagePath
.lastIndexOf('.') + 1));
73 if (guessedMimeType
!= null
&& !guessedMimeType
.equals(file
.getMimetype())) {
74 intentForGuessedMimeType
= new Intent(Intent
.ACTION_VIEW
);
75 intentForGuessedMimeType
.setDataAndType(Uri
.parse("file://"+ encodedStoragePath
), guessedMimeType
);
76 intentForGuessedMimeType
.setFlags(Intent
.FLAG_GRANT_READ_URI_PERMISSION
| Intent
.FLAG_GRANT_WRITE_URI_PERMISSION
);
80 Intent chooserIntent
= null
;
81 if (intentForGuessedMimeType
!= null
) {
82 chooserIntent
= Intent
.createChooser(intentForGuessedMimeType
, mFileActivity
.getString(R
.string
.actionbar_open_with
));
84 chooserIntent
= Intent
.createChooser(intentForSavedMimeType
, mFileActivity
.getString(R
.string
.actionbar_open_with
));
87 mFileActivity
.startActivity(chooserIntent
);
90 Log_OC
.wtf(TAG
, "Trying to open a NULL OCFile");
95 public void shareFileWithLink(OCFile file
) {
97 if (isSharedSupported()) {
99 String link
= "https://fake.url";
100 Intent intent
= createShareWithLinkIntent(link
);
101 String
[] packagesToExclude
= new String
[] { mFileActivity
.getPackageName() };
102 DialogFragment chooserDialog
= ShareLinkToDialog
.newInstance(intent
, packagesToExclude
, file
);
103 chooserDialog
.show(mFileActivity
.getSupportFragmentManager(), FTAG_CHOOSER_DIALOG
);
106 Log_OC
.wtf(TAG
, "Trying to share a NULL OCFile");
111 Toast t
= Toast
.makeText(mFileActivity
, mFileActivity
.getString(R
.string
.share_link_no_support_share_api
), Toast
.LENGTH_LONG
);
117 public void shareFileWithLinkToApp(OCFile file
, Intent sendIntent
) {
120 mFileActivity
.showLoadingDialog();
122 Intent service
= new Intent(mFileActivity
, OperationsService
.class);
123 service
.setAction(OperationsService
.ACTION_CREATE_SHARE
);
124 service
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
125 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
126 service
.putExtra(OperationsService
.EXTRA_SEND_INTENT
, sendIntent
);
127 mFileActivity
.getOperationsServiceBinder().newOperation(service
);
130 Log_OC
.wtf(TAG
, "Trying to open a NULL OCFile");
135 private Intent
createShareWithLinkIntent(String link
) {
136 Intent intentToShareLink
= new Intent(Intent
.ACTION_SEND
);
137 intentToShareLink
.putExtra(Intent
.EXTRA_TEXT
, link
);
138 intentToShareLink
.setType(HTTP
.PLAIN_TEXT_TYPE
);
139 return intentToShareLink
;
144 * @return 'True' if the server supports the Share API
146 public boolean isSharedSupported() {
147 if (mFileActivity
.getAccount() != null
) {
148 AccountManager accountManager
= AccountManager
.get(mFileActivity
);
150 String version
= accountManager
.getUserData(mFileActivity
.getAccount(), Constants
.KEY_OC_VERSION
);
151 return (new OwnCloudVersion(version
)).isSharedSupported();
157 public void unshareFileWithLink(OCFile file
) {
159 if (isSharedSupported()) {
161 Intent service
= new Intent(mFileActivity
, OperationsService
.class);
162 service
.setAction(OperationsService
.ACTION_UNSHARE
);
163 service
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
164 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
165 mFileActivity
.getOperationsServiceBinder().newOperation(service
);
167 mFileActivity
.showLoadingDialog();
171 Toast t
= Toast
.makeText(mFileActivity
, mFileActivity
.getString(R
.string
.share_link_no_support_share_api
), Toast
.LENGTH_LONG
);
177 public void sendDownloadedFile(OCFile file
) {
179 Intent sendIntent
= new Intent(android
.content
.Intent
.ACTION_SEND
);
181 sendIntent
.setType(file
.getMimetype());
182 sendIntent
.putExtra(Intent
.EXTRA_STREAM
, Uri
.parse("file://" + file
.getStoragePath()));
183 sendIntent
.putExtra(Intent
.ACTION_SEND
, true
); // Send Action
185 // Show dialog, without the own app
186 String
[] packagesToExclude
= new String
[] { mFileActivity
.getPackageName() };
187 DialogFragment chooserDialog
= ShareLinkToDialog
.newInstance(sendIntent
, packagesToExclude
, file
);
188 chooserDialog
.show(mFileActivity
.getSupportFragmentManager(), FTAG_CHOOSER_DIALOG
);
191 Log_OC
.wtf(TAG
, "Trying to send a NULL OCFile");
196 public void syncFile(OCFile file
) {
197 Account account
= mFileActivity
.getAccount();
198 RemoteOperation operation
= new SynchronizeFileOperation(
201 mFileActivity
.getStorageManager(),
205 operation
.execute(account
, mFileActivity
, mFileActivity
, mFileActivity
.getHandler(), mFileActivity
);
206 mFileActivity
.showLoadingDialog();
210 public void renameFile(OCFile file
, String newFilename
) {
212 Intent service
= new Intent(mFileActivity
, OperationsService
.class);
213 service
.setAction(OperationsService
.ACTION_RENAME
);
214 service
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
215 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
216 service
.putExtra(OperationsService
.EXTRA_NEWNAME
, newFilename
);
217 mFileActivity
.getOperationsServiceBinder().newOperation(service
);
219 mFileActivity
.showLoadingDialog();
223 public void removeFile(OCFile file
, boolean removeLocalCopy
) {
225 Intent service
= new Intent(mFileActivity
, OperationsService
.class);
226 service
.setAction(OperationsService
.ACTION_REMOVE
);
227 service
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
228 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
229 service
.putExtra(OperationsService
.EXTRA_REMOVE_LOCAL_COPY
, removeLocalCopy
);
230 mFileActivity
.getOperationsServiceBinder().newOperation(service
);
232 mFileActivity
.showLoadingDialog();
236 public void createFolder(String remotePath
, boolean createFullPath
) {
238 Intent service
= new Intent(mFileActivity
, OperationsService
.class);
239 service
.setAction(OperationsService
.ACTION_CREATE_FOLDER
);
240 service
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
241 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, remotePath
);
242 service
.putExtra(OperationsService
.EXTRA_CREATE_FULL_PATH
, createFullPath
);
243 mFileActivity
.getOperationsServiceBinder().newOperation(service
);
245 mFileActivity
.showLoadingDialog();