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
.AccountManager
;
23 import android
.content
.Intent
;
24 import android
.net
.Uri
;
25 import android
.sax
.StartElementListener
;
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
.lib
.accounts
.OwnCloudAccount
;
33 import com
.owncloud
.android
.lib
.network
.webdav
.WebdavUtils
;
34 import com
.owncloud
.android
.lib
.operations
.common
.ShareType
;
35 import com
.owncloud
.android
.operations
.CreateShareOperation
;
36 import com
.owncloud
.android
.operations
.UnshareLinkOperation
;
37 import com
.owncloud
.android
.services
.OperationsService
;
38 import com
.owncloud
.android
.ui
.activity
.FileActivity
;
39 import com
.owncloud
.android
.ui
.dialog
.ActivityChooserDialog
;
40 import com
.owncloud
.android
.utils
.Log_OC
;
45 * @author David A. Velasco
47 public class FileOperationsHelper
{
49 private static final String TAG
= FileOperationsHelper
.class.getName();
51 private static final String FTAG_CHOOSER_DIALOG
= "CHOOSER_DIALOG";
54 public void openFile(OCFile file
, FileActivity callerActivity
) {
56 String storagePath
= file
.getStoragePath();
57 String encodedStoragePath
= WebdavUtils
.encodePath(storagePath
);
59 Intent intentForSavedMimeType
= new Intent(Intent
.ACTION_VIEW
);
60 intentForSavedMimeType
.setDataAndType(Uri
.parse("file://"+ encodedStoragePath
), file
.getMimetype());
61 intentForSavedMimeType
.setFlags(Intent
.FLAG_GRANT_READ_URI_PERMISSION
| Intent
.FLAG_GRANT_WRITE_URI_PERMISSION
);
63 Intent intentForGuessedMimeType
= null
;
64 if (storagePath
.lastIndexOf('.') >= 0) {
65 String guessedMimeType
= MimeTypeMap
.getSingleton().getMimeTypeFromExtension(storagePath
.substring(storagePath
.lastIndexOf('.') + 1));
66 if (guessedMimeType
!= null
&& !guessedMimeType
.equals(file
.getMimetype())) {
67 intentForGuessedMimeType
= new Intent(Intent
.ACTION_VIEW
);
68 intentForGuessedMimeType
.setDataAndType(Uri
.parse("file://"+ encodedStoragePath
), guessedMimeType
);
69 intentForGuessedMimeType
.setFlags(Intent
.FLAG_GRANT_READ_URI_PERMISSION
| Intent
.FLAG_GRANT_WRITE_URI_PERMISSION
);
73 Intent chooserIntent
= null
;
74 if (intentForGuessedMimeType
!= null
) {
75 chooserIntent
= Intent
.createChooser(intentForGuessedMimeType
, callerActivity
.getString(R
.string
.actionbar_open_with
));
77 chooserIntent
= Intent
.createChooser(intentForSavedMimeType
, callerActivity
.getString(R
.string
.actionbar_open_with
));
80 callerActivity
.startActivity(chooserIntent
);
83 Log_OC
.wtf(TAG
, "Trying to open a NULL OCFile");
88 public void shareFileWithLink(OCFile file
, FileActivity callerActivity
) {
90 if (isSharedSupported(callerActivity
)) {
92 String link
= "https://fake.url";
93 Intent intent
= createShareWithLinkIntent(link
);
94 String
[] packagesToExclude
= new String
[] { callerActivity
.getPackageName() };
95 DialogFragment chooserDialog
= ActivityChooserDialog
.newInstance(intent
, packagesToExclude
, file
);
96 chooserDialog
.show(callerActivity
.getSupportFragmentManager(), FTAG_CHOOSER_DIALOG
);
99 Log_OC
.wtf(TAG
, "Trying to share a NULL OCFile");
104 Toast t
= Toast
.makeText(callerActivity
, callerActivity
.getString(R
.string
.share_link_no_support_share_api
), Toast
.LENGTH_LONG
);
110 public void shareFileWithLinkToApp(OCFile file
, Intent sendIntent
, FileActivity callerActivity
) {
113 callerActivity
.showLoadingDialog();
115 Intent service
= new Intent(callerActivity
, OperationsService
.class);
116 service
.setAction(OperationsService
.ACTION_CREATE_SHARE
);
117 service
.putExtra(OperationsService
.EXTRA_ACCOUNT
, callerActivity
.getAccount());
118 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
119 service
.putExtra(OperationsService
.EXTRA_SEND_INTENT
, sendIntent
);
120 callerActivity
.startService(service
);
123 Log_OC
.wtf(TAG
, "Trying to open a NULL OCFile");
128 private Intent
createShareWithLinkIntent(String link
) {
129 Intent intentToShareLink
= new Intent(Intent
.ACTION_SEND
);
130 intentToShareLink
.putExtra(Intent
.EXTRA_TEXT
, link
);
131 intentToShareLink
.setType(HTTP
.PLAIN_TEXT_TYPE
);
132 return intentToShareLink
;
137 * @return 'True' if the server supports the Share API
139 public boolean isSharedSupported(FileActivity callerActivity
) {
140 if (callerActivity
.getAccount() != null
) {
141 AccountManager accountManager
= AccountManager
.get(callerActivity
);
142 return Boolean
.parseBoolean(accountManager
.getUserData(callerActivity
.getAccount(), OwnCloudAccount
.Constants
.KEY_SUPPORTS_SHARE_API
));
148 public void unshareFileWithLink(OCFile file
, FileActivity callerActivity
) {
150 if (isSharedSupported(callerActivity
)) {
152 UnshareLinkOperation unshare
= new UnshareLinkOperation(file
, callerActivity
);
153 unshare
.execute(callerActivity
.getStorageManager(),
155 callerActivity
.getRemoteOperationListener(),
156 callerActivity
.getHandler(),
159 callerActivity
.showLoadingDialog();
163 Toast t
= Toast
.makeText(callerActivity
, callerActivity
.getString(R
.string
.share_link_no_support_share_api
), Toast
.LENGTH_LONG
);