3b5e062b478f0002c6a2eb3ec01a3534c09981cc
[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 if (isSharedSupported()) {
211 // Unshare the file
212 Intent service = new Intent(mFileActivity, OperationsService.class);
213 service.setAction(OperationsService.ACTION_UNSHARE);
214 service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
215 service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
216 service.putExtra(OperationsService.EXTRA_SHARE_TYPE, ShareType.PUBLIC_LINK);
217 service.putExtra(OperationsService.EXTRA_SHARE_WITH, "");
218 mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
219
220 mFileActivity.showLoadingDialog(mFileActivity.getApplicationContext().
221 getString(R.string.wait_a_moment));
222
223 } else {
224 // Show a Message
225 Toast t = Toast.makeText(mFileActivity,
226 mFileActivity.getString(R.string.share_link_no_support_share_api),
227 Toast.LENGTH_LONG);
228 t.show();
229
230 }
231 }
232
233 public void showShareFile(OCFile file){
234 Intent intent = new Intent(mFileActivity, ShareActivity.class);
235 intent.putExtra(mFileActivity.EXTRA_FILE, file);
236 intent.putExtra(mFileActivity.EXTRA_ACCOUNT, mFileActivity.getAccount());
237 mFileActivity.startActivity(intent);
238
239 }
240
241 public void sendDownloadedFile(OCFile file) {
242 if (file != null) {
243 String storagePath = file.getStoragePath();
244 String encodedStoragePath = WebdavUtils.encodePath(storagePath);
245 Intent sendIntent = new Intent(android.content.Intent.ACTION_SEND);
246 // set MimeType
247 sendIntent.setType(file.getMimetype());
248 sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + encodedStoragePath));
249 sendIntent.putExtra(Intent.ACTION_SEND, true); // Send Action
250
251 // Show dialog, without the own app
252 String[] packagesToExclude = new String[]{mFileActivity.getPackageName()};
253 DialogFragment chooserDialog = ShareLinkToDialog.newInstance(sendIntent,
254 packagesToExclude, file);
255 chooserDialog.show(mFileActivity.getSupportFragmentManager(), FTAG_CHOOSER_DIALOG);
256
257 } else {
258 Log_OC.wtf(TAG, "Trying to send a NULL OCFile");
259 }
260 }
261
262 /**
263 * Request the synchronization of a file or folder with the OC server, including its contents.
264 *
265 * @param file The file or folder to synchronize
266 */
267 public void syncFile(OCFile file) {
268 if (!file.isFolder()){
269 Intent intent = new Intent(mFileActivity, OperationsService.class);
270 intent.setAction(OperationsService.ACTION_SYNC_FILE);
271 intent.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
272 intent.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
273 intent.putExtra(OperationsService.EXTRA_SYNC_FILE_CONTENTS, true);
274 mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(intent);
275 mFileActivity.showLoadingDialog(mFileActivity.getApplicationContext().
276 getString(R.string.wait_a_moment));
277
278 } else {
279 Intent intent = new Intent(mFileActivity, OperationsService.class);
280 intent.setAction(OperationsService.ACTION_SYNC_FOLDER);
281 intent.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
282 intent.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
283 mFileActivity.startService(intent);
284
285 }
286 }
287
288 public void toggleFavorite(OCFile file, boolean isFavorite) {
289 file.setFavorite(isFavorite);
290 mFileActivity.getStorageManager().saveFile(file);
291
292 /// register the OCFile instance in the observer service to monitor local updates
293 Intent observedFileIntent = FileObserverService.makeObservedFileIntent(
294 mFileActivity,
295 file,
296 mFileActivity.getAccount(),
297 isFavorite);
298 mFileActivity.startService(observedFileIntent);
299
300 /// immediate content synchronization
301 if (file.isFavorite()) {
302 syncFile(file);
303 }
304 }
305
306 public void renameFile(OCFile file, String newFilename) {
307 // RenameFile
308 Intent service = new Intent(mFileActivity, OperationsService.class);
309 service.setAction(OperationsService.ACTION_RENAME);
310 service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
311 service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
312 service.putExtra(OperationsService.EXTRA_NEWNAME, newFilename);
313 mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
314
315 mFileActivity.showLoadingDialog(mFileActivity.getApplicationContext().
316 getString(R.string.wait_a_moment));
317 }
318
319
320 public void removeFile(OCFile file, boolean onlyLocalCopy) {
321 // RemoveFile
322 Intent service = new Intent(mFileActivity, OperationsService.class);
323 service.setAction(OperationsService.ACTION_REMOVE);
324 service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
325 service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
326 service.putExtra(OperationsService.EXTRA_REMOVE_ONLY_LOCAL, onlyLocalCopy);
327 mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
328
329 mFileActivity.showLoadingDialog(mFileActivity.getApplicationContext().
330 getString(R.string.wait_a_moment));
331 }
332
333
334 public void createFolder(String remotePath, boolean createFullPath) {
335 // Create Folder
336 Intent service = new Intent(mFileActivity, OperationsService.class);
337 service.setAction(OperationsService.ACTION_CREATE_FOLDER);
338 service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
339 service.putExtra(OperationsService.EXTRA_REMOTE_PATH, remotePath);
340 service.putExtra(OperationsService.EXTRA_CREATE_FULL_PATH, createFullPath);
341 mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
342
343 mFileActivity.showLoadingDialog(mFileActivity.getApplicationContext().
344 getString(R.string.wait_a_moment));
345 }
346
347 /**
348 * Cancel the transference in downloads (files/folders) and file uploads
349 * @param file OCFile
350 */
351 public void cancelTransference(OCFile file) {
352 Account account = mFileActivity.getAccount();
353 if (file.isFolder()) {
354 OperationsService.OperationsServiceBinder opsBinder =
355 mFileActivity.getOperationsServiceBinder();
356 if (opsBinder != null) {
357 opsBinder.cancel(account, file);
358 }
359 }
360
361 // for both files and folders
362 FileDownloaderBinder downloaderBinder = mFileActivity.getFileDownloaderBinder();
363 if (downloaderBinder != null && downloaderBinder.isDownloading(account, file)) {
364 downloaderBinder.cancel(account, file);
365 }
366 FileUploaderBinder uploaderBinder = mFileActivity.getFileUploaderBinder();
367 if (uploaderBinder != null && uploaderBinder.isUploading(account, file)) {
368 uploaderBinder.cancel(account, file);
369 }
370 }
371
372 /**
373 * Start move file operation
374 *
375 * @param newfile File where it is going to be moved
376 * @param currentFile File with the previous info
377 */
378 public void moveFile(OCFile newfile, OCFile currentFile) {
379 // Move files
380 Intent service = new Intent(mFileActivity, OperationsService.class);
381 service.setAction(OperationsService.ACTION_MOVE_FILE);
382 service.putExtra(OperationsService.EXTRA_NEW_PARENT_PATH, newfile.getRemotePath());
383 service.putExtra(OperationsService.EXTRA_REMOTE_PATH, currentFile.getRemotePath());
384 service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
385 mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
386
387 mFileActivity.showLoadingDialog(mFileActivity.getApplicationContext().
388 getString(R.string.wait_a_moment));
389 }
390
391 /**
392 * Start copy file operation
393 *
394 * @param newfile File where it is going to be moved
395 * @param currentFile File with the previous info
396 */
397 public void copyFile(OCFile newfile, OCFile currentFile) {
398 // Copy files
399 Intent service = new Intent(mFileActivity, OperationsService.class);
400 service.setAction(OperationsService.ACTION_COPY_FILE);
401 service.putExtra(OperationsService.EXTRA_NEW_PARENT_PATH, newfile.getRemotePath());
402 service.putExtra(OperationsService.EXTRA_REMOTE_PATH, currentFile.getRemotePath());
403 service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
404 mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
405
406 mFileActivity.showLoadingDialog(mFileActivity.getApplicationContext().
407 getString(R.string.wait_a_moment));
408 }
409
410 public long getOpIdWaitingFor() {
411 return mWaitingForOpId;
412 }
413
414
415 public void setOpIdWaitingFor(long waitingForOpId) {
416 mWaitingForOpId = waitingForOpId;
417 }
418
419 /**
420 * @return 'True' if the server doesn't need to check forbidden characters
421 */
422 public boolean isVersionWithForbiddenCharacters() {
423 if (mFileActivity.getAccount() != null) {
424 OwnCloudVersion serverVersion =
425 AccountUtils.getServerVersion(mFileActivity.getAccount());
426 return (serverVersion != null && serverVersion.isVersionWithForbiddenCharacters());
427 }
428 return false;
429 }
430 }