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
;
21 import java
.io
.IOException
;
22 import org
.apache
.http
.protocol
.HTTP
;
24 import android
.accounts
.AccountManager
;
25 import android
.content
.Intent
;
26 import android
.net
.Uri
;
27 import android
.support
.v4
.app
.DialogFragment
;
28 import android
.webkit
.MimeTypeMap
;
29 import android
.widget
.Toast
;
31 import com
.owncloud
.android
.R
;
32 import com
.owncloud
.android
.datamodel
.OCFile
;
34 import com
.owncloud
.android
.lib
.common
.accounts
.AccountUtils
.Constants
;
35 import com
.owncloud
.android
.lib
.common
.network
.WebdavUtils
;
36 import com
.owncloud
.android
.lib
.resources
.status
.OwnCloudVersion
;
37 import com
.owncloud
.android
.services
.OperationsService
;
38 import com
.owncloud
.android
.ui
.activity
.FileActivity
;
39 import com
.owncloud
.android
.ui
.dialog
.ShareLinkToDialog
;
40 import com
.owncloud
.android
.utils
.FileStorageUtils
;
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 public final static int REQUEST_CODE_FILE_OPEN_HELPER
= 100;
57 public void openFile(OCFile file
, FileActivity callerActivity
) {
59 String storagePath
= file
.getStoragePath();
60 String encodedStoragePath
= WebdavUtils
.encodePath(storagePath
);
62 Intent intentForSavedMimeType
= new Intent(Intent
.ACTION_VIEW
);
63 intentForSavedMimeType
.setDataAndType(Uri
.parse("file://"+ encodedStoragePath
), file
.getMimetype());
64 intentForSavedMimeType
.setFlags(Intent
.FLAG_GRANT_READ_URI_PERMISSION
| Intent
.FLAG_GRANT_WRITE_URI_PERMISSION
);
66 Intent intentForGuessedMimeType
= null
;
67 if (storagePath
.lastIndexOf('.') >= 0) {
68 String guessedMimeType
= MimeTypeMap
.getSingleton().getMimeTypeFromExtension(storagePath
.substring(storagePath
.lastIndexOf('.') + 1));
69 if (guessedMimeType
!= null
&& !guessedMimeType
.equals(file
.getMimetype())) {
70 intentForGuessedMimeType
= new Intent(Intent
.ACTION_VIEW
);
71 intentForGuessedMimeType
.setDataAndType(Uri
.parse("file://"+ encodedStoragePath
), guessedMimeType
);
72 intentForGuessedMimeType
.setFlags(Intent
.FLAG_GRANT_READ_URI_PERMISSION
| Intent
.FLAG_GRANT_WRITE_URI_PERMISSION
);
76 Intent chooserIntent
= null
;
77 if (intentForGuessedMimeType
!= null
) {
78 chooserIntent
= Intent
.createChooser(intentForGuessedMimeType
, callerActivity
.getString(R
.string
.actionbar_open_with
));
80 chooserIntent
= Intent
.createChooser(intentForSavedMimeType
, callerActivity
.getString(R
.string
.actionbar_open_with
));
83 callerActivity
.startActivity(chooserIntent
);
86 Log_OC
.wtf(TAG
, "Trying to open a NULL OCFile");
91 public void shareFileWithLink(OCFile file
, FileActivity callerActivity
) {
93 if (isSharedSupported(callerActivity
)) {
95 String link
= "https://fake.url";
96 Intent intent
= createShareWithLinkIntent(link
);
97 String
[] packagesToExclude
= new String
[] { callerActivity
.getPackageName() };
98 DialogFragment chooserDialog
= ShareLinkToDialog
.newInstance(intent
, packagesToExclude
, file
);
99 chooserDialog
.show(callerActivity
.getSupportFragmentManager(), FTAG_CHOOSER_DIALOG
);
102 Log_OC
.wtf(TAG
, "Trying to share a NULL OCFile");
107 Toast t
= Toast
.makeText(callerActivity
, callerActivity
.getString(R
.string
.share_link_no_support_share_api
), Toast
.LENGTH_LONG
);
113 public void shareFileWithLinkToApp(OCFile file
, Intent sendIntent
, FileActivity callerActivity
) {
116 callerActivity
.showLoadingDialog();
118 Intent service
= new Intent(callerActivity
, OperationsService
.class);
119 service
.setAction(OperationsService
.ACTION_CREATE_SHARE
);
120 service
.putExtra(OperationsService
.EXTRA_ACCOUNT
, callerActivity
.getAccount());
121 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
122 service
.putExtra(OperationsService
.EXTRA_SEND_INTENT
, sendIntent
);
123 callerActivity
.startService(service
);
126 Log_OC
.wtf(TAG
, "Trying to open a NULL OCFile");
131 private Intent
createShareWithLinkIntent(String link
) {
132 Intent intentToShareLink
= new Intent(Intent
.ACTION_SEND
);
133 intentToShareLink
.putExtra(Intent
.EXTRA_TEXT
, link
);
134 intentToShareLink
.setType(HTTP
.PLAIN_TEXT_TYPE
);
135 return intentToShareLink
;
140 * @return 'True' if the server supports the Share API
142 public boolean isSharedSupported(FileActivity callerActivity
) {
143 if (callerActivity
.getAccount() != null
) {
144 AccountManager accountManager
= AccountManager
.get(callerActivity
);
146 String version
= accountManager
.getUserData(callerActivity
.getAccount(), Constants
.KEY_OC_VERSION
);
147 String versionString
= accountManager
.getUserData(callerActivity
.getAccount(), Constants
.KEY_OC_VERSION_STRING
);
148 return (new OwnCloudVersion(version
, versionString
)).isSharedSupported();
149 //return Boolean.parseBoolean(accountManager.getUserData(callerActivity.getAccount(), OwnCloudAccount.Constants.KEY_SUPPORTS_SHARE_API));
155 public void unshareFileWithLink(OCFile file
, FileActivity callerActivity
) {
157 if (isSharedSupported(callerActivity
)) {
159 Intent service
= new Intent(callerActivity
, OperationsService
.class);
160 service
.setAction(OperationsService
.ACTION_UNSHARE
);
161 service
.putExtra(OperationsService
.EXTRA_ACCOUNT
, callerActivity
.getAccount());
162 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
163 callerActivity
.startService(service
);
165 callerActivity
.showLoadingDialog();
169 Toast t
= Toast
.makeText(callerActivity
, callerActivity
.getString(R
.string
.share_link_no_support_share_api
), Toast
.LENGTH_LONG
);
175 public void sendDownloadedFile(OCFile file
, FileActivity callerActivity
) {
177 Intent sendIntent
= new Intent(android
.content
.Intent
.ACTION_SEND
);
179 sendIntent
.setType(file
.getMimetype());
180 sendIntent
.putExtra(Intent
.EXTRA_STREAM
, Uri
.parse("file://" + file
.getStoragePath()));
181 sendIntent
.putExtra(Intent
.ACTION_SEND
, true
); // Send Action
183 // Show dialog, without the own app
184 String
[] packagesToExclude
= new String
[] { callerActivity
.getPackageName() };
185 DialogFragment chooserDialog
= ShareLinkToDialog
.newInstance(sendIntent
, packagesToExclude
, file
);
186 chooserDialog
.show(callerActivity
.getSupportFragmentManager(), FTAG_CHOOSER_DIALOG
);
189 Log_OC
.wtf(TAG
, "Trying to send a NULL OCFile");
193 public void sendFileToApp(Intent sendIntent
, FileActivity callerActivity
) {
194 Uri filePath
= sendIntent
.getParcelableExtra(Intent
.EXTRA_STREAM
);
195 File file
= new File(filePath
.getPath());
196 Log_OC
.d(TAG
, "FILE " + filePath
.getPath());
198 File folder
= new File(FileStorageUtils
.getTemporalPath(callerActivity
.getAccount().name
) + "/send");
199 boolean success
= true
;
200 if (!folder
.exists()) {
201 success
= folder
.mkdir();
204 File tmpFile
= new File(folder
.getAbsolutePath()+ "/" + file
.getName());
206 tmpFile
.createNewFile();
207 FileStorageUtils
.copyFile(file
, tmpFile
);
208 } catch (IOException e
) {
209 Log_OC
.e(TAG
, "An error occurred while it was trying to copy in a temporal folder " + e
.getMessage());
212 Uri uri
= Uri
.fromFile(tmpFile
);
213 sendIntent
.removeExtra(Intent
.EXTRA_STREAM
);
214 sendIntent
.putExtra(Intent
.EXTRA_STREAM
, uri
);
215 callerActivity
.startActivityForResult(sendIntent
, REQUEST_CODE_FILE_OPEN_HELPER
);
219 Toast t
= Toast
.makeText(callerActivity
, callerActivity
.getString(R
.string
.send_file_missing_file
), Toast
.LENGTH_LONG
);
221 Log_OC
.d(TAG
, "Missing file");