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