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