495d7a31bb594a25fd41896bbb8b6700983785f9
[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 android.accounts.Account;
25 import android.content.ActivityNotFoundException;
26 import android.content.Context;
27 import android.content.Intent;
28 import android.content.pm.PackageManager;
29 import android.content.pm.ResolveInfo;
30 import android.net.Uri;
31 import android.support.v4.app.DialogFragment;
32 import android.webkit.MimeTypeMap;
33 import android.widget.Toast;
34
35 import com.owncloud.android.R;
36 import com.owncloud.android.authentication.AccountUtils;
37 import com.owncloud.android.datamodel.OCFile;
38 import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
39 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
40 import com.owncloud.android.lib.common.network.WebdavUtils;
41 import com.owncloud.android.lib.common.utils.Log_OC;
42 import com.owncloud.android.lib.resources.shares.ShareType;
43 import com.owncloud.android.lib.resources.status.OwnCloudVersion;
44 import com.owncloud.android.services.OperationsService;
45 import com.owncloud.android.services.observer.FileObserverService;
46 import com.owncloud.android.ui.activity.FileActivity;
47 import com.owncloud.android.ui.activity.ShareActivity;
48 import com.owncloud.android.ui.dialog.ShareLinkToDialog;
49
50 import org.apache.http.protocol.HTTP;
51
52 import java.util.List;
53
54 /**
55 *
56 */
57 public class FileOperationsHelper {
58
59 private static final String TAG = FileOperationsHelper.class.getName();
60
61 private static final String FTAG_CHOOSER_DIALOG = "CHOOSER_DIALOG";
62
63 protected FileActivity mFileActivity = null;
64
65 /// Identifier of operation in progress which result shouldn't be lost
66 private long mWaitingForOpId = Long.MAX_VALUE;
67
68 public FileOperationsHelper(FileActivity fileActivity) {
69 mFileActivity = fileActivity;
70 }
71
72
73 public void openFile(OCFile file) {
74 if (file != null) {
75 String storagePath = file.getStoragePath();
76 String encodedStoragePath = WebdavUtils.encodePath(storagePath);
77
78 Intent intentForSavedMimeType = new Intent(Intent.ACTION_VIEW);
79 intentForSavedMimeType.setDataAndType(Uri.parse("file://"+ encodedStoragePath),
80 file.getMimetype());
81 intentForSavedMimeType.setFlags(
82 Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION
83 );
84
85 Intent intentForGuessedMimeType = null;
86 if (storagePath.lastIndexOf('.') >= 0) {
87 String guessedMimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(
88 storagePath.substring(storagePath.lastIndexOf('.') + 1)
89 );
90 if (guessedMimeType != null && !guessedMimeType.equals(file.getMimetype())) {
91 intentForGuessedMimeType = new Intent(Intent.ACTION_VIEW);
92 intentForGuessedMimeType.
93 setDataAndType(Uri.parse("file://"+ encodedStoragePath),
94 guessedMimeType);
95 intentForGuessedMimeType.setFlags(
96 Intent.FLAG_GRANT_READ_URI_PERMISSION |
97 Intent.FLAG_GRANT_WRITE_URI_PERMISSION
98 );
99 }
100 }
101
102 Intent openFileWithIntent;
103 if (intentForGuessedMimeType != null) {
104 openFileWithIntent = intentForGuessedMimeType;
105 } else {
106 openFileWithIntent = intentForSavedMimeType;
107 }
108
109 List<ResolveInfo> launchables = mFileActivity.getPackageManager().
110 queryIntentActivities(openFileWithIntent, PackageManager.GET_INTENT_FILTERS);
111
112 if(launchables != null && launchables.size() > 0) {
113 try {
114 mFileActivity.startActivity(
115 Intent.createChooser(
116 openFileWithIntent, mFileActivity.getString(R.string.actionbar_open_with)
117 )
118 );
119 } catch (ActivityNotFoundException anfe) {
120 showNoAppForFileTypeToast(mFileActivity.getApplicationContext());
121 }
122 } else {
123 showNoAppForFileTypeToast(mFileActivity.getApplicationContext());
124 }
125
126 } else {
127 Log_OC.wtf(TAG, "Trying to open a NULL OCFile");
128 }
129 }
130
131 /**
132 * Displays a toast stating that no application could be found to open the file.
133 *
134 * @param context the context to be able to show a toast.
135 */
136 private void showNoAppForFileTypeToast(Context context) {
137 Toast.makeText(context,
138 R.string.file_list_no_app_for_file_type, Toast.LENGTH_SHORT)
139 .show();
140 }
141
142 public void shareFileWithLink(OCFile file) {
143
144 if (isSharedSupported()) {
145 if (file != null) {
146 String link = "https://fake.url";
147 Intent intent = createShareWithLinkIntent(link);
148 String[] packagesToExclude = new String[]{mFileActivity.getPackageName()};
149 DialogFragment chooserDialog = ShareLinkToDialog.newInstance(intent,
150 packagesToExclude, file);
151 chooserDialog.show(mFileActivity.getSupportFragmentManager(), FTAG_CHOOSER_DIALOG);
152
153 } else {
154 Log_OC.wtf(TAG, "Trying to share a NULL OCFile");
155 }
156
157 } else {
158 // Show a Message
159 Toast t = Toast.makeText(
160 mFileActivity, mFileActivity.getString(R.string.share_link_no_support_share_api),
161 Toast.LENGTH_LONG
162 );
163 t.show();
164 }
165 }
166
167
168 public void shareFileWithLinkToApp(OCFile file, String password, Intent sendIntent) {
169
170 if (file != null) {
171 mFileActivity.showLoadingDialog(mFileActivity.getApplicationContext().
172 getString(R.string.wait_a_moment));
173
174 Intent service = new Intent(mFileActivity, OperationsService.class);
175 service.setAction(OperationsService.ACTION_CREATE_SHARE);
176 service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
177 service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
178 service.putExtra(OperationsService.EXTRA_PASSWORD_SHARE, password);
179 service.putExtra(OperationsService.EXTRA_SEND_INTENT, sendIntent);
180 mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
181
182 } else {
183 Log_OC.wtf(TAG, "Trying to open a NULL OCFile");
184 }
185 }
186
187
188 private Intent createShareWithLinkIntent(String link) {
189 Intent intentToShareLink = new Intent(Intent.ACTION_SEND);
190 intentToShareLink.putExtra(Intent.EXTRA_TEXT, link);
191 intentToShareLink.setType(HTTP.PLAIN_TEXT_TYPE);
192 return intentToShareLink;
193 }
194
195
196 /**
197 * @return 'True' if the server supports the Share API
198 */
199 public boolean isSharedSupported() {
200 if (mFileActivity.getAccount() != null) {
201 OwnCloudVersion serverVersion = AccountUtils.getServerVersion(mFileActivity.getAccount());
202 return (serverVersion != null && serverVersion.isSharedSupported());
203 }
204 return false;
205 }
206
207
208 public void unshareFileWithLink(OCFile file) {
209
210 // Unshare the file: Create the intent
211 Intent unshareService = new Intent(mFileActivity, OperationsService.class);
212 unshareService.setAction(OperationsService.ACTION_UNSHARE);
213 unshareService.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
214 unshareService.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
215 unshareService.putExtra(OperationsService.EXTRA_SHARE_TYPE, ShareType.PUBLIC_LINK);
216 unshareService.putExtra(OperationsService.EXTRA_SHARE_WITH, "");
217
218 unshareFile(unshareService);
219 }
220
221 public void unshareFileWithUserOrGroup(OCFile file, ShareType shareType, String userOrGroup){
222
223 // Unshare the file: Create the intent
224 Intent unshareService = new Intent(mFileActivity, OperationsService.class);
225 unshareService.setAction(OperationsService.ACTION_UNSHARE);
226 unshareService.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
227 unshareService.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
228 unshareService.putExtra(OperationsService.EXTRA_SHARE_TYPE, shareType);
229 unshareService.putExtra(OperationsService.EXTRA_SHARE_WITH, userOrGroup);
230
231 unshareFile(unshareService);
232 }
233
234
235 private void unshareFile(Intent unshareService){
236 if (isSharedSupported()) {
237 // Unshare the file
238 mWaitingForOpId = mFileActivity.getOperationsServiceBinder().
239 queueNewOperation(unshareService);
240
241 mFileActivity.showLoadingDialog(mFileActivity.getApplicationContext().
242 getString(R.string.wait_a_moment));
243
244 } else {
245 // Show a Message
246 Toast t = Toast.makeText(mFileActivity,
247 mFileActivity.getString(R.string.share_link_no_support_share_api),
248 Toast.LENGTH_LONG);
249 t.show();
250
251 }
252 }
253
254 public void showShareFile(OCFile file){
255 Intent intent = new Intent(mFileActivity, ShareActivity.class);
256 intent.putExtra(mFileActivity.EXTRA_FILE, file);
257 intent.putExtra(mFileActivity.EXTRA_ACCOUNT, mFileActivity.getAccount());
258 mFileActivity.startActivity(intent);
259
260 }
261
262 public void sendDownloadedFile(OCFile file) {
263 if (file != null) {
264 String storagePath = file.getStoragePath();
265 String encodedStoragePath = WebdavUtils.encodePath(storagePath);
266 Intent sendIntent = new Intent(android.content.Intent.ACTION_SEND);
267 // set MimeType
268 sendIntent.setType(file.getMimetype());
269 sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + encodedStoragePath));
270 sendIntent.putExtra(Intent.ACTION_SEND, true); // Send Action
271
272 // Show dialog, without the own app
273 String[] packagesToExclude = new String[]{mFileActivity.getPackageName()};
274 DialogFragment chooserDialog = ShareLinkToDialog.newInstance(sendIntent,
275 packagesToExclude, file);
276 chooserDialog.show(mFileActivity.getSupportFragmentManager(), FTAG_CHOOSER_DIALOG);
277
278 } else {
279 Log_OC.wtf(TAG, "Trying to send a NULL OCFile");
280 }
281 }
282
283 /**
284 * Request the synchronization of a file or folder with the OC server, including its contents.
285 *
286 * @param file The file or folder to synchronize
287 */
288 public void syncFile(OCFile file) {
289 if (!file.isFolder()){
290 Intent intent = new Intent(mFileActivity, OperationsService.class);
291 intent.setAction(OperationsService.ACTION_SYNC_FILE);
292 intent.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
293 intent.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
294 intent.putExtra(OperationsService.EXTRA_SYNC_FILE_CONTENTS, true);
295 mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(intent);
296 mFileActivity.showLoadingDialog(mFileActivity.getApplicationContext().
297 getString(R.string.wait_a_moment));
298
299 } else {
300 Intent intent = new Intent(mFileActivity, OperationsService.class);
301 intent.setAction(OperationsService.ACTION_SYNC_FOLDER);
302 intent.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
303 intent.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
304 mFileActivity.startService(intent);
305
306 }
307 }
308
309 public void toggleFavorite(OCFile file, boolean isFavorite) {
310 file.setFavorite(isFavorite);
311 mFileActivity.getStorageManager().saveFile(file);
312
313 /// register the OCFile instance in the observer service to monitor local updates
314 Intent observedFileIntent = FileObserverService.makeObservedFileIntent(
315 mFileActivity,
316 file,
317 mFileActivity.getAccount(),
318 isFavorite);
319 mFileActivity.startService(observedFileIntent);
320
321 /// immediate content synchronization
322 if (file.isFavorite()) {
323 syncFile(file);
324 }
325 }
326
327 public void renameFile(OCFile file, String newFilename) {
328 // RenameFile
329 Intent service = new Intent(mFileActivity, OperationsService.class);
330 service.setAction(OperationsService.ACTION_RENAME);
331 service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
332 service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
333 service.putExtra(OperationsService.EXTRA_NEWNAME, newFilename);
334 mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
335
336 mFileActivity.showLoadingDialog(mFileActivity.getApplicationContext().
337 getString(R.string.wait_a_moment));
338 }
339
340
341 public void removeFile(OCFile file, boolean onlyLocalCopy) {
342 // RemoveFile
343 Intent service = new Intent(mFileActivity, OperationsService.class);
344 service.setAction(OperationsService.ACTION_REMOVE);
345 service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
346 service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
347 service.putExtra(OperationsService.EXTRA_REMOVE_ONLY_LOCAL, onlyLocalCopy);
348 mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
349
350 mFileActivity.showLoadingDialog(mFileActivity.getApplicationContext().
351 getString(R.string.wait_a_moment));
352 }
353
354
355 public void createFolder(String remotePath, boolean createFullPath) {
356 // Create Folder
357 Intent service = new Intent(mFileActivity, OperationsService.class);
358 service.setAction(OperationsService.ACTION_CREATE_FOLDER);
359 service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
360 service.putExtra(OperationsService.EXTRA_REMOTE_PATH, remotePath);
361 service.putExtra(OperationsService.EXTRA_CREATE_FULL_PATH, createFullPath);
362 mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
363
364 mFileActivity.showLoadingDialog(mFileActivity.getApplicationContext().
365 getString(R.string.wait_a_moment));
366 }
367
368 /**
369 * Cancel the transference in downloads (files/folders) and file uploads
370 * @param file OCFile
371 */
372 public void cancelTransference(OCFile file) {
373 Account account = mFileActivity.getAccount();
374 if (file.isFolder()) {
375 OperationsService.OperationsServiceBinder opsBinder =
376 mFileActivity.getOperationsServiceBinder();
377 if (opsBinder != null) {
378 opsBinder.cancel(account, file);
379 }
380 }
381
382 // for both files and folders
383 FileDownloaderBinder downloaderBinder = mFileActivity.getFileDownloaderBinder();
384 if (downloaderBinder != null && downloaderBinder.isDownloading(account, file)) {
385 downloaderBinder.cancel(account, file);
386 }
387 FileUploaderBinder uploaderBinder = mFileActivity.getFileUploaderBinder();
388 if (uploaderBinder != null && uploaderBinder.isUploading(account, file)) {
389 uploaderBinder.cancel(account, file);
390 }
391 }
392
393 /**
394 * Start move file operation
395 *
396 * @param newfile File where it is going to be moved
397 * @param currentFile File with the previous info
398 */
399 public void moveFile(OCFile newfile, OCFile currentFile) {
400 // Move files
401 Intent service = new Intent(mFileActivity, OperationsService.class);
402 service.setAction(OperationsService.ACTION_MOVE_FILE);
403 service.putExtra(OperationsService.EXTRA_NEW_PARENT_PATH, newfile.getRemotePath());
404 service.putExtra(OperationsService.EXTRA_REMOTE_PATH, currentFile.getRemotePath());
405 service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
406 mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
407
408 mFileActivity.showLoadingDialog(mFileActivity.getApplicationContext().
409 getString(R.string.wait_a_moment));
410 }
411
412 /**
413 * Start copy file operation
414 *
415 * @param newfile File where it is going to be moved
416 * @param currentFile File with the previous info
417 */
418 public void copyFile(OCFile newfile, OCFile currentFile) {
419 // Copy files
420 Intent service = new Intent(mFileActivity, OperationsService.class);
421 service.setAction(OperationsService.ACTION_COPY_FILE);
422 service.putExtra(OperationsService.EXTRA_NEW_PARENT_PATH, newfile.getRemotePath());
423 service.putExtra(OperationsService.EXTRA_REMOTE_PATH, currentFile.getRemotePath());
424 service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
425 mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
426
427 mFileActivity.showLoadingDialog(mFileActivity.getApplicationContext().
428 getString(R.string.wait_a_moment));
429 }
430
431 public long getOpIdWaitingFor() {
432 return mWaitingForOpId;
433 }
434
435
436 public void setOpIdWaitingFor(long waitingForOpId) {
437 mWaitingForOpId = waitingForOpId;
438 }
439
440 /**
441 * @return 'True' if the server doesn't need to check forbidden characters
442 */
443 public boolean isVersionWithForbiddenCharacters() {
444 if (mFileActivity.getAccount() != null) {
445 OwnCloudVersion serverVersion =
446 AccountUtils.getServerVersion(mFileActivity.getAccount());
447 return (serverVersion != null && serverVersion.isVersionWithForbiddenCharacters());
448 }
449 return false;
450 }
451 }