Merge branch 'master' of https://github.com/owncloud/android into material_buttons
[pub/Android/ownCloud.git] / src / com / owncloud / android / files / FileOperationsHelper.java
1 /**
2 * ownCloud Android client application
3 *
4 * @author masensio
5 * @author David A. Velasco
6 * Copyright (C) 2015 ownCloud Inc.
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2,
10 * as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21
22 package com.owncloud.android.files;
23
24 import org.apache.http.protocol.HTTP;
25
26 import android.accounts.Account;
27 import android.content.Intent;
28 import android.net.Uri;
29 import android.support.v4.app.DialogFragment;
30 import android.webkit.MimeTypeMap;
31 import android.widget.Toast;
32
33 import com.owncloud.android.R;
34 import com.owncloud.android.authentication.AccountUtils;
35 import com.owncloud.android.datamodel.OCFile;
36 import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
37 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
38
39 import com.owncloud.android.lib.common.network.WebdavUtils;
40 import com.owncloud.android.lib.common.utils.Log_OC;
41 import com.owncloud.android.lib.resources.status.OwnCloudVersion;
42 import com.owncloud.android.services.OperationsService;
43 import com.owncloud.android.services.observer.FileObserverService;
44 import com.owncloud.android.ui.activity.FileActivity;
45 import com.owncloud.android.ui.dialog.ShareLinkToDialog;
46
47 /**
48 *
49 */
50 public class FileOperationsHelper {
51
52 private static final String TAG = FileOperationsHelper.class.getName();
53
54 private static final String FTAG_CHOOSER_DIALOG = "CHOOSER_DIALOG";
55
56 protected FileActivity mFileActivity = null;
57
58 /// Identifier of operation in progress which result shouldn't be lost
59 private long mWaitingForOpId = Long.MAX_VALUE;
60
61 public FileOperationsHelper(FileActivity fileActivity) {
62 mFileActivity = fileActivity;
63 }
64
65
66 public void openFile(OCFile file) {
67 if (file != null) {
68 String storagePath = file.getStoragePath();
69 String encodedStoragePath = WebdavUtils.encodePath(storagePath);
70
71 Intent intentForSavedMimeType = new Intent(Intent.ACTION_VIEW);
72 intentForSavedMimeType.setDataAndType(Uri.parse("file://"+ encodedStoragePath), file.getMimetype());
73 intentForSavedMimeType.setFlags(
74 Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION
75 );
76
77 Intent intentForGuessedMimeType = null;
78 if (storagePath.lastIndexOf('.') >= 0) {
79 String guessedMimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(
80 storagePath.substring(storagePath.lastIndexOf('.') + 1)
81 );
82 if (guessedMimeType != null && !guessedMimeType.equals(file.getMimetype())) {
83 intentForGuessedMimeType = new Intent(Intent.ACTION_VIEW);
84 intentForGuessedMimeType.setDataAndType(Uri.parse("file://"+ encodedStoragePath), guessedMimeType);
85 intentForGuessedMimeType.setFlags(
86 Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION
87 );
88 }
89 }
90
91 Intent chooserIntent;
92 if (intentForGuessedMimeType != null) {
93 chooserIntent = Intent.createChooser(intentForGuessedMimeType, mFileActivity.getString(R.string.actionbar_open_with));
94 } else {
95 chooserIntent = Intent.createChooser(intentForSavedMimeType, mFileActivity.getString(R.string.actionbar_open_with));
96 }
97
98 mFileActivity.startActivity(chooserIntent);
99
100 } else {
101 Log_OC.wtf(TAG, "Trying to open a NULL OCFile");
102 }
103 }
104
105
106 public void shareFileWithLink(OCFile file) {
107
108 if (isSharedSupported()) {
109 if (file != null) {
110 String link = "https://fake.url";
111 Intent intent = createShareWithLinkIntent(link);
112 String[] packagesToExclude = new String[] { mFileActivity.getPackageName() };
113 DialogFragment chooserDialog = ShareLinkToDialog.newInstance(intent, packagesToExclude, file);
114 chooserDialog.show(mFileActivity.getSupportFragmentManager(), FTAG_CHOOSER_DIALOG);
115
116 } else {
117 Log_OC.wtf(TAG, "Trying to share a NULL OCFile");
118 }
119
120 } else {
121 // Show a Message
122 Toast t = Toast.makeText(
123 mFileActivity, mFileActivity.getString(R.string.share_link_no_support_share_api), Toast.LENGTH_LONG
124 );
125 t.show();
126 }
127 }
128
129
130 public void shareFileWithLinkToApp(OCFile file, String password, Intent sendIntent) {
131
132 if (file != null) {
133 mFileActivity.showLoadingDialog();
134
135 Intent service = new Intent(mFileActivity, OperationsService.class);
136 service.setAction(OperationsService.ACTION_CREATE_SHARE);
137 service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
138 service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
139 service.putExtra(OperationsService.EXTRA_PASSWORD_SHARE, password);
140 service.putExtra(OperationsService.EXTRA_SEND_INTENT, sendIntent);
141 mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
142
143 } else {
144 Log_OC.wtf(TAG, "Trying to open a NULL OCFile");
145 }
146 }
147
148
149 private Intent createShareWithLinkIntent(String link) {
150 Intent intentToShareLink = new Intent(Intent.ACTION_SEND);
151 intentToShareLink.putExtra(Intent.EXTRA_TEXT, link);
152 intentToShareLink.setType(HTTP.PLAIN_TEXT_TYPE);
153 return intentToShareLink;
154 }
155
156
157 /**
158 * @return 'True' if the server supports the Share API
159 */
160 public boolean isSharedSupported() {
161 if (mFileActivity.getAccount() != null) {
162 OwnCloudVersion serverVersion = AccountUtils.getServerVersion(mFileActivity.getAccount());
163 return (serverVersion != null && serverVersion.isSharedSupported());
164 }
165 return false;
166 }
167
168
169 public void unshareFileWithLink(OCFile file) {
170
171 if (isSharedSupported()) {
172 // Unshare the file
173 Intent service = new Intent(mFileActivity, OperationsService.class);
174 service.setAction(OperationsService.ACTION_UNSHARE);
175 service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
176 service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
177 mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
178
179 mFileActivity.showLoadingDialog();
180
181 } else {
182 // Show a Message
183 Toast t = Toast.makeText(mFileActivity, mFileActivity.getString(R.string.share_link_no_support_share_api), Toast.LENGTH_LONG);
184 t.show();
185
186 }
187 }
188
189 public void sendDownloadedFile(OCFile file) {
190 if (file != null) {
191 String storagePath = file.getStoragePath();
192 String encodedStoragePath = WebdavUtils.encodePath(storagePath);
193 Intent sendIntent = new Intent(android.content.Intent.ACTION_SEND);
194 // set MimeType
195 sendIntent.setType(file.getMimetype());
196 sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + encodedStoragePath));
197 sendIntent.putExtra(Intent.ACTION_SEND, true); // Send Action
198
199 // Show dialog, without the own app
200 String[] packagesToExclude = new String[] { mFileActivity.getPackageName() };
201 DialogFragment chooserDialog = ShareLinkToDialog.newInstance(sendIntent, packagesToExclude, file);
202 chooserDialog.show(mFileActivity.getSupportFragmentManager(), FTAG_CHOOSER_DIALOG);
203
204 } else {
205 Log_OC.wtf(TAG, "Trying to send a NULL OCFile");
206 }
207 }
208
209
210 public void syncFile(OCFile file) {
211
212 if (!file.isFolder()){
213 Intent intent = new Intent(mFileActivity, OperationsService.class);
214 intent.setAction(OperationsService.ACTION_SYNC_FILE);
215 intent.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
216 intent.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
217 intent.putExtra(OperationsService.EXTRA_SYNC_FILE_CONTENTS, true);
218 mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(intent);
219 mFileActivity.showLoadingDialog();
220
221 } else {
222 Intent intent = new Intent(mFileActivity, OperationsService.class);
223 intent.setAction(OperationsService.ACTION_SYNC_FOLDER);
224 intent.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
225 intent.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
226 mFileActivity.startService(intent);
227 }
228 }
229
230 public void toggleFavorite(OCFile file, boolean isFavorite) {
231 file.setFavorite(isFavorite);
232 mFileActivity.getStorageManager().saveFile(file);
233
234 /// register the OCFile instance in the observer service to monitor local updates
235 Intent observedFileIntent = FileObserverService.makeObservedFileIntent(
236 mFileActivity,
237 file,
238 mFileActivity.getAccount(),
239 isFavorite);
240 mFileActivity.startService(observedFileIntent);
241
242 /// immediate content synchronization
243 if (file.isFavorite()) {
244 syncFile(file);
245 }
246 }
247
248 public void renameFile(OCFile file, String newFilename) {
249 // RenameFile
250 Intent service = new Intent(mFileActivity, OperationsService.class);
251 service.setAction(OperationsService.ACTION_RENAME);
252 service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
253 service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
254 service.putExtra(OperationsService.EXTRA_NEWNAME, newFilename);
255 mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
256
257 mFileActivity.showLoadingDialog();
258 }
259
260
261 public void removeFile(OCFile file, boolean onlyLocalCopy) {
262 // RemoveFile
263 Intent service = new Intent(mFileActivity, OperationsService.class);
264 service.setAction(OperationsService.ACTION_REMOVE);
265 service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
266 service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
267 service.putExtra(OperationsService.EXTRA_REMOVE_ONLY_LOCAL, onlyLocalCopy);
268 mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
269
270 mFileActivity.showLoadingDialog();
271 }
272
273
274 public void createFolder(String remotePath, boolean createFullPath) {
275 // Create Folder
276 Intent service = new Intent(mFileActivity, OperationsService.class);
277 service.setAction(OperationsService.ACTION_CREATE_FOLDER);
278 service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
279 service.putExtra(OperationsService.EXTRA_REMOTE_PATH, remotePath);
280 service.putExtra(OperationsService.EXTRA_CREATE_FULL_PATH, createFullPath);
281 mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
282
283 mFileActivity.showLoadingDialog();
284 }
285
286 /**
287 * Cancel the transference in downloads (files/folders) and file uploads
288 * @param file OCFile
289 */
290 public void cancelTransference(OCFile file) {
291 Account account = mFileActivity.getAccount();
292 if (file.isFolder()) {
293 OperationsService.OperationsServiceBinder opsBinder = mFileActivity.getOperationsServiceBinder();
294 if (opsBinder != null) {
295 opsBinder.cancel(account, file);
296 }
297 }
298
299 // for both files and folders
300 FileDownloaderBinder downloaderBinder = mFileActivity.getFileDownloaderBinder();
301 FileUploaderBinder uploaderBinder = mFileActivity.getFileUploaderBinder();
302 if (downloaderBinder != null && downloaderBinder.isDownloading(account, file)) {
303 downloaderBinder.cancel(account, file);
304
305 // TODO - review why is this here, and solve in a better way
306 // Remove etag for parent, if file is a favorite
307 if (file.isFavorite()) {
308 OCFile parent = mFileActivity.getStorageManager().getFileById(file.getParentId());
309 parent.setEtag("");
310 mFileActivity.getStorageManager().saveFile(parent);
311 }
312
313 } else if (uploaderBinder != null && uploaderBinder.isUploading(account, file)) {
314 uploaderBinder.cancel(account, file);
315 }
316 }
317
318 /**
319 * Start move file operation
320 * @param newfile File where it is going to be moved
321 * @param currentFile File with the previous info
322 */
323 public void moveFile(OCFile newfile, OCFile currentFile) {
324 // Move files
325 Intent service = new Intent(mFileActivity, OperationsService.class);
326 service.setAction(OperationsService.ACTION_MOVE_FILE);
327 service.putExtra(OperationsService.EXTRA_NEW_PARENT_PATH, newfile.getRemotePath());
328 service.putExtra(OperationsService.EXTRA_REMOTE_PATH, currentFile.getRemotePath());
329 service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
330 mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
331
332 mFileActivity.showLoadingDialog();
333 }
334
335
336 public long getOpIdWaitingFor() {
337 return mWaitingForOpId;
338 }
339
340
341 public void setOpIdWaitingFor(long waitingForOpId) {
342 mWaitingForOpId = waitingForOpId;
343 }
344
345 /**
346 * @return 'True' if the server doesn't need to check forbidden characters
347 */
348 public boolean isVersionWithForbiddenCharacters() {
349 if (mFileActivity.getAccount() != null) {
350 OwnCloudVersion serverVersion = AccountUtils.getServerVersion(mFileActivity.getAccount());
351 return (serverVersion != null && serverVersion.isVersionWithForbiddenCharacters());
352 }
353 return false;
354 }
355 }