f47ab51964e29fa62fcd53c945ae5f67c368dc21
[pub/Android/ownCloud.git] / src / com / owncloud / android / files / FileOperationsHelper.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012-2014 ownCloud Inc.
3 *
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.
7 *
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.
12 *
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/>.
15 *
16 */
17
18 package com.owncloud.android.files;
19
20 import org.apache.http.protocol.HTTP;
21
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;
28
29 import com.owncloud.android.R;
30 import com.owncloud.android.datamodel.OCFile;
31
32 import com.owncloud.android.lib.common.accounts.AccountUtils.Constants;
33 import com.owncloud.android.lib.common.network.WebdavUtils;
34 import com.owncloud.android.lib.resources.status.OwnCloudVersion;
35 import com.owncloud.android.services.OperationsService;
36 import com.owncloud.android.ui.activity.FileActivity;
37 import com.owncloud.android.ui.dialog.ShareLinkToDialog;
38 import com.owncloud.android.utils.Log_OC;
39
40 /**
41 *
42 * @author masensio
43 * @author David A. Velasco
44 */
45 public class FileOperationsHelper {
46
47 private static final String TAG = FileOperationsHelper.class.getName();
48
49 private static final String FTAG_CHOOSER_DIALOG = "CHOOSER_DIALOG";
50
51
52 public void openFile(OCFile file, FileActivity callerActivity) {
53 if (file != null) {
54 String storagePath = file.getStoragePath();
55 String encodedStoragePath = WebdavUtils.encodePath(storagePath);
56
57 Intent intentForSavedMimeType = new Intent(Intent.ACTION_VIEW);
58 intentForSavedMimeType.setDataAndType(Uri.parse("file://"+ encodedStoragePath), file.getMimetype());
59 intentForSavedMimeType.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
60
61 Intent intentForGuessedMimeType = null;
62 if (storagePath.lastIndexOf('.') >= 0) {
63 String guessedMimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(storagePath.substring(storagePath.lastIndexOf('.') + 1));
64 if (guessedMimeType != null && !guessedMimeType.equals(file.getMimetype())) {
65 intentForGuessedMimeType = new Intent(Intent.ACTION_VIEW);
66 intentForGuessedMimeType.setDataAndType(Uri.parse("file://"+ encodedStoragePath), guessedMimeType);
67 intentForGuessedMimeType.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
68 }
69 }
70
71 Intent chooserIntent = null;
72 if (intentForGuessedMimeType != null) {
73 chooserIntent = Intent.createChooser(intentForGuessedMimeType, callerActivity.getString(R.string.actionbar_open_with));
74 } else {
75 chooserIntent = Intent.createChooser(intentForSavedMimeType, callerActivity.getString(R.string.actionbar_open_with));
76 }
77
78 callerActivity.startActivity(chooserIntent);
79
80 } else {
81 Log_OC.wtf(TAG, "Trying to open a NULL OCFile");
82 }
83 }
84
85
86 public void shareFileWithLink(OCFile file, FileActivity callerActivity) {
87
88 if (isSharedSupported(callerActivity)) {
89 if (file != null) {
90 String link = "https://fake.url";
91 Intent intent = createShareWithLinkIntent(link);
92 String[] packagesToExclude = new String[] { callerActivity.getPackageName() };
93 DialogFragment chooserDialog = ShareLinkToDialog.newInstance(intent, packagesToExclude, file);
94 chooserDialog.show(callerActivity.getSupportFragmentManager(), FTAG_CHOOSER_DIALOG);
95
96 } else {
97 Log_OC.wtf(TAG, "Trying to share a NULL OCFile");
98 }
99
100 } else {
101 // Show a Message
102 Toast t = Toast.makeText(callerActivity, callerActivity.getString(R.string.share_link_no_support_share_api), Toast.LENGTH_LONG);
103 t.show();
104 }
105 }
106
107
108 public void shareFileWithLinkToApp(OCFile file, Intent sendIntent, FileActivity callerActivity) {
109
110 if (file != null) {
111 callerActivity.showLoadingDialog();
112
113 Intent service = new Intent(callerActivity, OperationsService.class);
114 service.setAction(OperationsService.ACTION_CREATE_SHARE);
115 service.putExtra(OperationsService.EXTRA_ACCOUNT, callerActivity.getAccount());
116 service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
117 service.putExtra(OperationsService.EXTRA_SEND_INTENT, sendIntent);
118 callerActivity.startService(service);
119
120 } else {
121 Log_OC.wtf(TAG, "Trying to open a NULL OCFile");
122 }
123 }
124
125
126 private Intent createShareWithLinkIntent(String link) {
127 Intent intentToShareLink = new Intent(Intent.ACTION_SEND);
128 intentToShareLink.putExtra(Intent.EXTRA_TEXT, link);
129 intentToShareLink.setType(HTTP.PLAIN_TEXT_TYPE);
130 return intentToShareLink;
131 }
132
133
134 /**
135 * @return 'True' if the server supports the Share API
136 */
137 public boolean isSharedSupported(FileActivity callerActivity) {
138 if (callerActivity.getAccount() != null) {
139 AccountManager accountManager = AccountManager.get(callerActivity);
140
141 String version = accountManager.getUserData(callerActivity.getAccount(), Constants.KEY_OC_VERSION);
142 String versionString = accountManager.getUserData(callerActivity.getAccount(), Constants.KEY_OC_VERSION_STRING);
143 return (new OwnCloudVersion(version)).isSharedSupported();
144 //return Boolean.parseBoolean(accountManager.getUserData(callerActivity.getAccount(), OwnCloudAccount.Constants.KEY_SUPPORTS_SHARE_API));
145 }
146 return false;
147 }
148
149
150 public void unshareFileWithLink(OCFile file, FileActivity callerActivity) {
151
152 if (isSharedSupported(callerActivity)) {
153 // Unshare the file
154 Intent service = new Intent(callerActivity, OperationsService.class);
155 service.setAction(OperationsService.ACTION_UNSHARE);
156 service.putExtra(OperationsService.EXTRA_ACCOUNT, callerActivity.getAccount());
157 service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
158 callerActivity.startService(service);
159
160 callerActivity.showLoadingDialog();
161
162 } else {
163 // Show a Message
164 Toast t = Toast.makeText(callerActivity, callerActivity.getString(R.string.share_link_no_support_share_api), Toast.LENGTH_LONG);
165 t.show();
166
167 }
168 }
169
170 public void sendDownloadedFile(OCFile file, FileActivity callerActivity) {
171 if (file != null) {
172 Intent sendIntent = new Intent(android.content.Intent.ACTION_SEND);
173 // set MimeType
174 sendIntent.setType(file.getMimetype());
175 sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + file.getStoragePath()));
176 sendIntent.putExtra(Intent.ACTION_SEND, true); // Send Action
177
178 // Show dialog, without the own app
179 String[] packagesToExclude = new String[] { callerActivity.getPackageName() };
180 DialogFragment chooserDialog = ShareLinkToDialog.newInstance(sendIntent, packagesToExclude, file);
181 chooserDialog.show(callerActivity.getSupportFragmentManager(), FTAG_CHOOSER_DIALOG);
182
183 } else {
184 Log_OC.wtf(TAG, "Trying to send a NULL OCFile");
185 }
186 }
187
188 }