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