OC-3121: (fix bug) An empty message is sent, when renaming a file in the web of a...
[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 java.io.File;
21 import java.io.IOException;
22 import org.apache.http.protocol.HTTP;
23
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;
30
31 import com.owncloud.android.R;
32 import com.owncloud.android.datamodel.OCFile;
33
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;
42
43 /**
44 *
45 * @author masensio
46 * @author David A. Velasco
47 */
48 public class FileOperationsHelper {
49
50 private static final String TAG = FileOperationsHelper.class.getName();
51
52 private static final String FTAG_CHOOSER_DIALOG = "CHOOSER_DIALOG";
53
54 public final static int REQUEST_CODE_FILE_OPEN_HELPER = 100;
55
56
57 public void openFile(OCFile file, FileActivity callerActivity) {
58 if (file != null) {
59 String storagePath = file.getStoragePath();
60 String encodedStoragePath = WebdavUtils.encodePath(storagePath);
61
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);
65
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);
73 }
74 }
75
76 Intent chooserIntent = null;
77 if (intentForGuessedMimeType != null) {
78 chooserIntent = Intent.createChooser(intentForGuessedMimeType, callerActivity.getString(R.string.actionbar_open_with));
79 } else {
80 chooserIntent = Intent.createChooser(intentForSavedMimeType, callerActivity.getString(R.string.actionbar_open_with));
81 }
82
83 callerActivity.startActivity(chooserIntent);
84
85 } else {
86 Log_OC.wtf(TAG, "Trying to open a NULL OCFile");
87 }
88 }
89
90
91 public void shareFileWithLink(OCFile file, FileActivity callerActivity) {
92
93 if (isSharedSupported(callerActivity)) {
94 if (file != null) {
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);
100
101 } else {
102 Log_OC.wtf(TAG, "Trying to share a NULL OCFile");
103 }
104
105 } else {
106 // Show a Message
107 Toast t = Toast.makeText(callerActivity, callerActivity.getString(R.string.share_link_no_support_share_api), Toast.LENGTH_LONG);
108 t.show();
109 }
110 }
111
112
113 public void shareFileWithLinkToApp(OCFile file, Intent sendIntent, FileActivity callerActivity) {
114
115 if (file != null) {
116 callerActivity.showLoadingDialog();
117
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);
124
125 } else {
126 Log_OC.wtf(TAG, "Trying to open a NULL OCFile");
127 }
128 }
129
130
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;
136 }
137
138
139 /**
140 * @return 'True' if the server supports the Share API
141 */
142 public boolean isSharedSupported(FileActivity callerActivity) {
143 if (callerActivity.getAccount() != null) {
144 AccountManager accountManager = AccountManager.get(callerActivity);
145
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));
150 }
151 return false;
152 }
153
154
155 public void unshareFileWithLink(OCFile file, FileActivity callerActivity) {
156
157 if (isSharedSupported(callerActivity)) {
158 // Unshare the file
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);
164
165 callerActivity.showLoadingDialog();
166
167 } else {
168 // Show a Message
169 Toast t = Toast.makeText(callerActivity, callerActivity.getString(R.string.share_link_no_support_share_api), Toast.LENGTH_LONG);
170 t.show();
171
172 }
173 }
174
175 public void sendDownloadedFile(OCFile file, FileActivity callerActivity) {
176 if (file != null) {
177 Intent sendIntent = new Intent(android.content.Intent.ACTION_SEND);
178 // set MimeType
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
182
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);
187
188 } else {
189 Log_OC.wtf(TAG, "Trying to send a NULL OCFile");
190 }
191 }
192
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());
197 if (file.exists()) {
198 File folder = new File(FileStorageUtils.getTemporalPath(callerActivity.getAccount().name) + "/send");
199 boolean success = true;
200 if (!folder.exists()) {
201 success = folder.mkdir();
202 }
203 if (success) {
204 File tmpFile = new File(folder.getAbsolutePath()+ "/" + file.getName());
205 try {
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());
210 }
211 // Update Uri
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);
216 }
217 } else {
218 // Show a Message
219 Toast t = Toast.makeText(callerActivity, callerActivity.getString(R.string.send_file_missing_file), Toast.LENGTH_LONG);
220 t.show();
221 Log_OC.d(TAG, "Missing file");
222 }
223
224 }
225
226
227
228
229 }