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