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