Merge branch 'develop' into refactor_update_filelist_from_database
[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 protected FileActivity mFileActivity = null;
52
53 public FileOperationsHelper(FileActivity fileActivity) {
54 mFileActivity = fileActivity;
55 }
56
57
58 public void openFile(OCFile file) {
59 if (file != null) {
60 String storagePath = file.getStoragePath();
61 String encodedStoragePath = WebdavUtils.encodePath(storagePath);
62
63 Intent intentForSavedMimeType = new Intent(Intent.ACTION_VIEW);
64 intentForSavedMimeType.setDataAndType(Uri.parse("file://"+ encodedStoragePath), file.getMimetype());
65 intentForSavedMimeType.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
66
67 Intent intentForGuessedMimeType = null;
68 if (storagePath.lastIndexOf('.') >= 0) {
69 String guessedMimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(storagePath.substring(storagePath.lastIndexOf('.') + 1));
70 if (guessedMimeType != null && !guessedMimeType.equals(file.getMimetype())) {
71 intentForGuessedMimeType = new Intent(Intent.ACTION_VIEW);
72 intentForGuessedMimeType.setDataAndType(Uri.parse("file://"+ encodedStoragePath), guessedMimeType);
73 intentForGuessedMimeType.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
74 }
75 }
76
77 Intent chooserIntent = null;
78 if (intentForGuessedMimeType != null) {
79 chooserIntent = Intent.createChooser(intentForGuessedMimeType, mFileActivity.getString(R.string.actionbar_open_with));
80 } else {
81 chooserIntent = Intent.createChooser(intentForSavedMimeType, mFileActivity.getString(R.string.actionbar_open_with));
82 }
83
84 mFileActivity.startActivity(chooserIntent);
85
86 } else {
87 Log_OC.wtf(TAG, "Trying to open a NULL OCFile");
88 }
89 }
90
91
92 public void shareFileWithLink(OCFile file) {
93
94 if (isSharedSupported()) {
95 if (file != null) {
96 String link = "https://fake.url";
97 Intent intent = createShareWithLinkIntent(link);
98 String[] packagesToExclude = new String[] { mFileActivity.getPackageName() };
99 DialogFragment chooserDialog = ShareLinkToDialog.newInstance(intent, packagesToExclude, file);
100 chooserDialog.show(mFileActivity.getSupportFragmentManager(), FTAG_CHOOSER_DIALOG);
101
102 } else {
103 Log_OC.wtf(TAG, "Trying to share a NULL OCFile");
104 }
105
106 } else {
107 // Show a Message
108 Toast t = Toast.makeText(mFileActivity, mFileActivity.getString(R.string.share_link_no_support_share_api), Toast.LENGTH_LONG);
109 t.show();
110 }
111 }
112
113
114 public void shareFileWithLinkToApp(OCFile file, Intent sendIntent) {
115
116 if (file != null) {
117 mFileActivity.showLoadingDialog();
118
119 Intent service = new Intent(mFileActivity, OperationsService.class);
120 service.setAction(OperationsService.ACTION_CREATE_SHARE);
121 service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
122 service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
123 service.putExtra(OperationsService.EXTRA_SEND_INTENT, sendIntent);
124 mFileActivity.getOperationsServiceBinder().newOperation(service);
125
126 } else {
127 Log_OC.wtf(TAG, "Trying to open a NULL OCFile");
128 }
129 }
130
131
132 private Intent createShareWithLinkIntent(String link) {
133 Intent intentToShareLink = new Intent(Intent.ACTION_SEND);
134 intentToShareLink.putExtra(Intent.EXTRA_TEXT, link);
135 intentToShareLink.setType(HTTP.PLAIN_TEXT_TYPE);
136 return intentToShareLink;
137 }
138
139
140 /**
141 * @return 'True' if the server supports the Share API
142 */
143 public boolean isSharedSupported() {
144 if (mFileActivity.getAccount() != null) {
145 AccountManager accountManager = AccountManager.get(mFileActivity);
146
147 String version = accountManager.getUserData(mFileActivity.getAccount(), Constants.KEY_OC_VERSION);
148 return (new OwnCloudVersion(version)).isSharedSupported();
149 }
150 return false;
151 }
152
153
154 public void unshareFileWithLink(OCFile file) {
155
156 if (isSharedSupported()) {
157 // Unshare the file
158 Intent service = new Intent(mFileActivity, OperationsService.class);
159 service.setAction(OperationsService.ACTION_UNSHARE);
160 service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
161 service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
162 mFileActivity.getOperationsServiceBinder().newOperation(service);
163
164 mFileActivity.showLoadingDialog();
165
166 } else {
167 // Show a Message
168 Toast t = Toast.makeText(mFileActivity, mFileActivity.getString(R.string.share_link_no_support_share_api), Toast.LENGTH_LONG);
169 t.show();
170
171 }
172 }
173
174 public void sendDownloadedFile(OCFile file) {
175 if (file != null) {
176 Intent sendIntent = new Intent(android.content.Intent.ACTION_SEND);
177 // set MimeType
178 sendIntent.setType(file.getMimetype());
179 sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + file.getStoragePath()));
180 sendIntent.putExtra(Intent.ACTION_SEND, true); // Send Action
181
182 // Show dialog, without the own app
183 String[] packagesToExclude = new String[] { mFileActivity.getPackageName() };
184 DialogFragment chooserDialog = ShareLinkToDialog.newInstance(sendIntent, packagesToExclude, file);
185 chooserDialog.show(mFileActivity.getSupportFragmentManager(), FTAG_CHOOSER_DIALOG);
186
187 } else {
188 Log_OC.wtf(TAG, "Trying to send a NULL OCFile");
189 }
190 }
191
192
193 public void syncFile(OCFile file) {
194 // Sync file
195 Intent service = new Intent(mFileActivity, OperationsService.class);
196 service.setAction(OperationsService.ACTION_SYNC_FILE);
197 service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
198 service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
199 service.putExtra(OperationsService.EXTRA_SYNC_FILE_CONTENTS, true);
200 mFileActivity.getOperationsServiceBinder().newOperation(service);
201
202 mFileActivity.showLoadingDialog();
203 }
204
205
206 public void renameFile(OCFile file, String newFilename) {
207 // RenameFile
208 Intent service = new Intent(mFileActivity, OperationsService.class);
209 service.setAction(OperationsService.ACTION_RENAME);
210 service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
211 service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
212 service.putExtra(OperationsService.EXTRA_NEWNAME, newFilename);
213 mFileActivity.getOperationsServiceBinder().newOperation(service);
214
215 mFileActivity.showLoadingDialog();
216 }
217
218
219 public void removeFile(OCFile file, boolean onlyLocalCopy) {
220 // RemoveFile
221 Intent service = new Intent(mFileActivity, OperationsService.class);
222 service.setAction(OperationsService.ACTION_REMOVE);
223 service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
224 service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
225 service.putExtra(OperationsService.EXTRA_REMOVE_ONLY_LOCAL, onlyLocalCopy);
226 mFileActivity.getOperationsServiceBinder().newOperation(service);
227
228 mFileActivity.showLoadingDialog();
229 }
230
231
232 public void createFolder(String remotePath, boolean createFullPath) {
233 // Create Folder
234 Intent service = new Intent(mFileActivity, OperationsService.class);
235 service.setAction(OperationsService.ACTION_CREATE_FOLDER);
236 service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
237 service.putExtra(OperationsService.EXTRA_REMOTE_PATH, remotePath);
238 service.putExtra(OperationsService.EXTRA_CREATE_FULL_PATH, createFullPath);
239 mFileActivity.getOperationsServiceBinder().newOperation(service);
240
241 mFileActivity.showLoadingDialog();
242 }
243 }