753dff1d5a44caa5202b0805ea73fc779cb3e280
[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_VIA_LINK);
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 * Helper method to share a file with a know sharee. Starts a request to do it in {@link OperationsService}
198 *
199 * @param file The file to share.
200 * @param shareeName Name (user name or group name) of the target sharee.
201 * @param shareType The share type determines the sharee type.
202 */
203 public void shareFileWithSharee(OCFile file, String shareeName, ShareType shareType) {
204 if (file != null) {
205 // TODO check capability?
206 mFileActivity.showLoadingDialog(mFileActivity.getApplicationContext().
207 getString(R.string.wait_a_moment));
208
209 Intent service = new Intent(mFileActivity, OperationsService.class);
210 service.setAction(OperationsService.ACTION_CREATE_SHARE_WITH_SHAREE);
211 service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
212 service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
213 service.putExtra(OperationsService.EXTRA_SHARE_WITH, shareeName);
214 service.putExtra(OperationsService.EXTRA_SHARE_TYPE, shareType);
215 mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
216
217 } else {
218 Log_OC.wtf(TAG, "Trying to share a NULL OCFile");
219 }
220 }
221
222
223 /**
224 * @return 'True' if the server supports the Share API
225 */
226 public boolean isSharedSupported() {
227 if (mFileActivity.getAccount() != null) {
228 OwnCloudVersion serverVersion = AccountUtils.getServerVersion(mFileActivity.getAccount());
229 return (serverVersion != null && serverVersion.isSharedSupported());
230 }
231 return false;
232 }
233
234
235 public void unshareFileWithLink(OCFile file) {
236
237 // Unshare the file: Create the intent
238 Intent unshareService = new Intent(mFileActivity, OperationsService.class);
239 unshareService.setAction(OperationsService.ACTION_UNSHARE);
240 unshareService.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
241 unshareService.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
242 unshareService.putExtra(OperationsService.EXTRA_SHARE_TYPE, ShareType.PUBLIC_LINK);
243 unshareService.putExtra(OperationsService.EXTRA_SHARE_WITH, "");
244
245 unshareFile(unshareService);
246 }
247
248 public void unshareFileWithUserOrGroup(OCFile file, ShareType shareType, String userOrGroup){
249
250 // Unshare the file: Create the intent
251 Intent unshareService = new Intent(mFileActivity, OperationsService.class);
252 unshareService.setAction(OperationsService.ACTION_UNSHARE);
253 unshareService.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
254 unshareService.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
255 unshareService.putExtra(OperationsService.EXTRA_SHARE_TYPE, shareType);
256 unshareService.putExtra(OperationsService.EXTRA_SHARE_WITH, userOrGroup);
257
258 unshareFile(unshareService);
259 }
260
261
262 private void unshareFile(Intent unshareService){
263 if (isSharedSupported()) {
264 // Unshare the file
265 mWaitingForOpId = mFileActivity.getOperationsServiceBinder().
266 queueNewOperation(unshareService);
267
268 mFileActivity.showLoadingDialog(mFileActivity.getApplicationContext().
269 getString(R.string.wait_a_moment));
270
271 } else {
272 // Show a Message
273 Toast t = Toast.makeText(mFileActivity,
274 mFileActivity.getString(R.string.share_link_no_support_share_api),
275 Toast.LENGTH_LONG);
276 t.show();
277
278 }
279 }
280
281 public void showShareFile(OCFile file){
282 Intent intent = new Intent(mFileActivity, ShareActivity.class);
283 intent.putExtra(mFileActivity.EXTRA_FILE, file);
284 intent.putExtra(mFileActivity.EXTRA_ACCOUNT, mFileActivity.getAccount());
285 mFileActivity.startActivity(intent);
286
287 }
288
289
290 /**
291 * @return 'True' if the server supports the Search Users API
292 */
293 public boolean isSearchUsersSupportedSupported() {
294 if (mFileActivity.getAccount() != null) {
295 OwnCloudVersion serverVersion = AccountUtils.getServerVersion(mFileActivity.getAccount());
296 return (serverVersion != null && serverVersion.isSearchUsersSupported());
297 }
298 return false;
299 }
300
301 public void sendDownloadedFile(OCFile file) {
302 if (file != null) {
303 String storagePath = file.getStoragePath();
304 String encodedStoragePath = WebdavUtils.encodePath(storagePath);
305 Intent sendIntent = new Intent(android.content.Intent.ACTION_SEND);
306 // set MimeType
307 sendIntent.setType(file.getMimetype());
308 sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + encodedStoragePath));
309 sendIntent.putExtra(Intent.ACTION_SEND, true); // Send Action
310
311 // Show dialog, without the own app
312 String[] packagesToExclude = new String[]{mFileActivity.getPackageName()};
313 DialogFragment chooserDialog = ShareLinkToDialog.newInstance(sendIntent,
314 packagesToExclude, file);
315 chooserDialog.show(mFileActivity.getSupportFragmentManager(), FTAG_CHOOSER_DIALOG);
316
317 } else {
318 Log_OC.wtf(TAG, "Trying to send a NULL OCFile");
319 }
320 }
321
322 /**
323 * Request the synchronization of a file or folder with the OC server, including its contents.
324 *
325 * @param file The file or folder to synchronize
326 */
327 public void syncFile(OCFile file) {
328 if (!file.isFolder()){
329 Intent intent = new Intent(mFileActivity, OperationsService.class);
330 intent.setAction(OperationsService.ACTION_SYNC_FILE);
331 intent.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
332 intent.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
333 intent.putExtra(OperationsService.EXTRA_SYNC_FILE_CONTENTS, true);
334 mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(intent);
335 mFileActivity.showLoadingDialog(mFileActivity.getApplicationContext().
336 getString(R.string.wait_a_moment));
337
338 } else {
339 Intent intent = new Intent(mFileActivity, OperationsService.class);
340 intent.setAction(OperationsService.ACTION_SYNC_FOLDER);
341 intent.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
342 intent.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
343 mFileActivity.startService(intent);
344
345 }
346 }
347
348 public void toggleFavorite(OCFile file, boolean isFavorite) {
349 file.setFavorite(isFavorite);
350 mFileActivity.getStorageManager().saveFile(file);
351
352 /// register the OCFile instance in the observer service to monitor local updates
353 Intent observedFileIntent = FileObserverService.makeObservedFileIntent(
354 mFileActivity,
355 file,
356 mFileActivity.getAccount(),
357 isFavorite);
358 mFileActivity.startService(observedFileIntent);
359
360 /// immediate content synchronization
361 if (file.isFavorite()) {
362 syncFile(file);
363 }
364 }
365
366 public void renameFile(OCFile file, String newFilename) {
367 // RenameFile
368 Intent service = new Intent(mFileActivity, OperationsService.class);
369 service.setAction(OperationsService.ACTION_RENAME);
370 service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
371 service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
372 service.putExtra(OperationsService.EXTRA_NEWNAME, newFilename);
373 mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
374
375 mFileActivity.showLoadingDialog(mFileActivity.getApplicationContext().
376 getString(R.string.wait_a_moment));
377 }
378
379
380 public void removeFile(OCFile file, boolean onlyLocalCopy) {
381 // RemoveFile
382 Intent service = new Intent(mFileActivity, OperationsService.class);
383 service.setAction(OperationsService.ACTION_REMOVE);
384 service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
385 service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
386 service.putExtra(OperationsService.EXTRA_REMOVE_ONLY_LOCAL, onlyLocalCopy);
387 mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
388
389 mFileActivity.showLoadingDialog(mFileActivity.getApplicationContext().
390 getString(R.string.wait_a_moment));
391 }
392
393
394 public void createFolder(String remotePath, boolean createFullPath) {
395 // Create Folder
396 Intent service = new Intent(mFileActivity, OperationsService.class);
397 service.setAction(OperationsService.ACTION_CREATE_FOLDER);
398 service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
399 service.putExtra(OperationsService.EXTRA_REMOTE_PATH, remotePath);
400 service.putExtra(OperationsService.EXTRA_CREATE_FULL_PATH, createFullPath);
401 mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
402
403 mFileActivity.showLoadingDialog(mFileActivity.getApplicationContext().
404 getString(R.string.wait_a_moment));
405 }
406
407 /**
408 * Cancel the transference in downloads (files/folders) and file uploads
409 * @param file OCFile
410 */
411 public void cancelTransference(OCFile file) {
412 Account account = mFileActivity.getAccount();
413 if (file.isFolder()) {
414 OperationsService.OperationsServiceBinder opsBinder =
415 mFileActivity.getOperationsServiceBinder();
416 if (opsBinder != null) {
417 opsBinder.cancel(account, file);
418 }
419 }
420
421 // for both files and folders
422 FileDownloaderBinder downloaderBinder = mFileActivity.getFileDownloaderBinder();
423 if (downloaderBinder != null && downloaderBinder.isDownloading(account, file)) {
424 downloaderBinder.cancel(account, file);
425 }
426 FileUploaderBinder uploaderBinder = mFileActivity.getFileUploaderBinder();
427 if (uploaderBinder != null && uploaderBinder.isUploading(account, file)) {
428 uploaderBinder.cancel(account, file);
429 }
430 }
431
432 /**
433 * Start move file operation
434 *
435 * @param newfile File where it is going to be moved
436 * @param currentFile File with the previous info
437 */
438 public void moveFile(OCFile newfile, OCFile currentFile) {
439 // Move files
440 Intent service = new Intent(mFileActivity, OperationsService.class);
441 service.setAction(OperationsService.ACTION_MOVE_FILE);
442 service.putExtra(OperationsService.EXTRA_NEW_PARENT_PATH, newfile.getRemotePath());
443 service.putExtra(OperationsService.EXTRA_REMOTE_PATH, currentFile.getRemotePath());
444 service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
445 mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
446
447 mFileActivity.showLoadingDialog(mFileActivity.getApplicationContext().
448 getString(R.string.wait_a_moment));
449 }
450
451 /**
452 * Start copy file operation
453 *
454 * @param newfile File where it is going to be moved
455 * @param currentFile File with the previous info
456 */
457 public void copyFile(OCFile newfile, OCFile currentFile) {
458 // Copy files
459 Intent service = new Intent(mFileActivity, OperationsService.class);
460 service.setAction(OperationsService.ACTION_COPY_FILE);
461 service.putExtra(OperationsService.EXTRA_NEW_PARENT_PATH, newfile.getRemotePath());
462 service.putExtra(OperationsService.EXTRA_REMOTE_PATH, currentFile.getRemotePath());
463 service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
464 mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
465
466 mFileActivity.showLoadingDialog(mFileActivity.getApplicationContext().
467 getString(R.string.wait_a_moment));
468 }
469
470 public long getOpIdWaitingFor() {
471 return mWaitingForOpId;
472 }
473
474
475 public void setOpIdWaitingFor(long waitingForOpId) {
476 mWaitingForOpId = waitingForOpId;
477 }
478
479 /**
480 * @return 'True' if the server doesn't need to check forbidden characters
481 */
482 public boolean isVersionWithForbiddenCharacters() {
483 if (mFileActivity.getAccount() != null) {
484 OwnCloudVersion serverVersion =
485 AccountUtils.getServerVersion(mFileActivity.getAccount());
486 return (serverVersion != null && serverVersion.isVersionWithForbiddenCharacters());
487 }
488 return false;
489 }
490 }