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