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
.support
.v4
.app
.DialogFragment
;
26 import android
.webkit
.MimeTypeMap
;
27 import android
.widget
.Toast
;
29 import com
.owncloud
.android
.R
;
30 import com
.owncloud
.android
.datamodel
.OCFile
;
31 import com
.owncloud
.android
.lib
.common
.accounts
.AccountUtils
.Constants
;
32 import com
.owncloud
.android
.lib
.common
.network
.WebdavUtils
;
33 import com
.owncloud
.android
.services
.OperationsService
;
34 import com
.owncloud
.android
.ui
.activity
.FileActivity
;
35 import com
.owncloud
.android
.ui
.dialog
.ActivityChooserDialog
;
36 import com
.owncloud
.android
.utils
.Log_OC
;
41 * @author David A. Velasco
43 public class FileOperationsHelper
{
45 private static final String TAG
= FileOperationsHelper
.class.getName();
47 private static final String FTAG_CHOOSER_DIALOG
= "CHOOSER_DIALOG";
50 public void openFile(OCFile file
, FileActivity callerActivity
) {
52 String storagePath
= file
.getStoragePath();
53 String encodedStoragePath
= WebdavUtils
.encodePath(storagePath
);
55 Intent intentForSavedMimeType
= new Intent(Intent
.ACTION_VIEW
);
56 intentForSavedMimeType
.setDataAndType(Uri
.parse("file://"+ encodedStoragePath
), file
.getMimetype());
57 intentForSavedMimeType
.setFlags(Intent
.FLAG_GRANT_READ_URI_PERMISSION
| Intent
.FLAG_GRANT_WRITE_URI_PERMISSION
);
59 Intent intentForGuessedMimeType
= null
;
60 if (storagePath
.lastIndexOf('.') >= 0) {
61 String guessedMimeType
= MimeTypeMap
.getSingleton().getMimeTypeFromExtension(storagePath
.substring(storagePath
.lastIndexOf('.') + 1));
62 if (guessedMimeType
!= null
&& !guessedMimeType
.equals(file
.getMimetype())) {
63 intentForGuessedMimeType
= new Intent(Intent
.ACTION_VIEW
);
64 intentForGuessedMimeType
.setDataAndType(Uri
.parse("file://"+ encodedStoragePath
), guessedMimeType
);
65 intentForGuessedMimeType
.setFlags(Intent
.FLAG_GRANT_READ_URI_PERMISSION
| Intent
.FLAG_GRANT_WRITE_URI_PERMISSION
);
69 Intent chooserIntent
= null
;
70 if (intentForGuessedMimeType
!= null
) {
71 chooserIntent
= Intent
.createChooser(intentForGuessedMimeType
, callerActivity
.getString(R
.string
.actionbar_open_with
));
73 chooserIntent
= Intent
.createChooser(intentForSavedMimeType
, callerActivity
.getString(R
.string
.actionbar_open_with
));
76 callerActivity
.startActivity(chooserIntent
);
79 Log_OC
.wtf(TAG
, "Trying to open a NULL OCFile");
84 public void shareFileWithLink(OCFile file
, FileActivity callerActivity
) {
86 if (isSharedSupported(callerActivity
)) {
88 String link
= "https://fake.url";
89 Intent intent
= createShareWithLinkIntent(link
);
90 String
[] packagesToExclude
= new String
[] { callerActivity
.getPackageName() };
91 DialogFragment chooserDialog
= ActivityChooserDialog
.newInstance(intent
, packagesToExclude
, file
);
92 chooserDialog
.show(callerActivity
.getSupportFragmentManager(), FTAG_CHOOSER_DIALOG
);
95 Log_OC
.wtf(TAG
, "Trying to share a NULL OCFile");
100 Toast t
= Toast
.makeText(callerActivity
, callerActivity
.getString(R
.string
.share_link_no_support_share_api
), Toast
.LENGTH_LONG
);
106 public void shareFileWithLinkToApp(OCFile file
, Intent sendIntent
, FileActivity callerActivity
) {
109 callerActivity
.showLoadingDialog();
111 Intent service
= new Intent(callerActivity
, OperationsService
.class);
112 service
.setAction(OperationsService
.ACTION_CREATE_SHARE
);
113 service
.putExtra(OperationsService
.EXTRA_ACCOUNT
, callerActivity
.getAccount());
114 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
115 service
.putExtra(OperationsService
.EXTRA_SEND_INTENT
, sendIntent
);
116 callerActivity
.startService(service
);
119 Log_OC
.wtf(TAG
, "Trying to open a NULL OCFile");
124 private Intent
createShareWithLinkIntent(String link
) {
125 Intent intentToShareLink
= new Intent(Intent
.ACTION_SEND
);
126 intentToShareLink
.putExtra(Intent
.EXTRA_TEXT
, link
);
127 intentToShareLink
.setType(HTTP
.PLAIN_TEXT_TYPE
);
128 return intentToShareLink
;
133 * @return 'True' if the server supports the Share API
135 public boolean isSharedSupported(FileActivity callerActivity
) {
136 if (callerActivity
.getAccount() != null
) {
137 AccountManager accountManager
= AccountManager
.get(callerActivity
);
138 return Boolean
.parseBoolean(accountManager
.getUserData(callerActivity
.getAccount(), Constants
.KEY_SUPPORTS_SHARE_API
));
144 public void unshareFileWithLink(OCFile file
, FileActivity callerActivity
) {
146 if (isSharedSupported(callerActivity
)) {
148 Intent service
= new Intent(callerActivity
, OperationsService
.class);
149 service
.setAction(OperationsService
.ACTION_UNSHARE
);
150 service
.putExtra(OperationsService
.EXTRA_ACCOUNT
, callerActivity
.getAccount());
151 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
152 callerActivity
.startService(service
);
154 callerActivity
.showLoadingDialog();
158 Toast t
= Toast
.makeText(callerActivity
, callerActivity
.getString(R
.string
.share_link_no_support_share_api
), Toast
.LENGTH_LONG
);