fe8d6c5bcb6a04326d81f8a8db45dff9cadf3c7a
[pub/Android/ownCloud.git] / src / com / owncloud / android / files / FileOperationsHelper.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012-2014 ownCloud Inc.
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 */
17
18 package com.owncloud.android.files;
19
20 import org.apache.http.protocol.HTTP;
21
22 import android.accounts.Account;
23 import android.accounts.AccountManager;
24 import android.content.Intent;
25 import android.net.Uri;
26 import android.support.v4.app.DialogFragment;
27 import android.webkit.MimeTypeMap;
28 import android.widget.Toast;
29
30 import com.owncloud.android.R;
31 import com.owncloud.android.datamodel.OCFile;
32 import com.owncloud.android.files.services.FileDownloader;
33 import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
34 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
35
36 import com.owncloud.android.lib.common.accounts.AccountUtils.Constants;
37 import com.owncloud.android.lib.common.network.WebdavUtils;
38 import com.owncloud.android.lib.common.utils.Log_OC;
39 import com.owncloud.android.lib.resources.status.OwnCloudVersion;
40 import com.owncloud.android.services.OperationsService;
41 import com.owncloud.android.ui.activity.FileActivity;
42 import com.owncloud.android.ui.dialog.ShareLinkToDialog;
43
44 /**
45 *
46 * @author masensio
47 * @author David A. Velasco
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(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
73
74 Intent intentForGuessedMimeType = null;
75 if (storagePath.lastIndexOf('.') >= 0) {
76 String guessedMimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(storagePath.substring(storagePath.lastIndexOf('.') + 1));
77 if (guessedMimeType != null && !guessedMimeType.equals(file.getMimetype())) {
78 intentForGuessedMimeType = new Intent(Intent.ACTION_VIEW);
79 intentForGuessedMimeType.setDataAndType(Uri.parse("file://"+ encodedStoragePath), guessedMimeType);
80 intentForGuessedMimeType.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
81 }
82 }
83
84 Intent chooserIntent = null;
85 if (intentForGuessedMimeType != null) {
86 chooserIntent = Intent.createChooser(intentForGuessedMimeType, mFileActivity.getString(R.string.actionbar_open_with));
87 } else {
88 chooserIntent = Intent.createChooser(intentForSavedMimeType, mFileActivity.getString(R.string.actionbar_open_with));
89 }
90
91 mFileActivity.startActivity(chooserIntent);
92
93 } else {
94 Log_OC.wtf(TAG, "Trying to open a NULL OCFile");
95 }
96 }
97
98
99 public void shareFileWithLink(OCFile file) {
100
101 if (isSharedSupported()) {
102 if (file != null) {
103 String link = "https://fake.url";
104 Intent intent = createShareWithLinkIntent(link);
105 String[] packagesToExclude = new String[] { mFileActivity.getPackageName() };
106 DialogFragment chooserDialog = ShareLinkToDialog.newInstance(intent, packagesToExclude, file);
107 chooserDialog.show(mFileActivity.getSupportFragmentManager(), FTAG_CHOOSER_DIALOG);
108
109 } else {
110 Log_OC.wtf(TAG, "Trying to share a NULL OCFile");
111 }
112
113 } else {
114 // Show a Message
115 Toast t = Toast.makeText(mFileActivity, mFileActivity.getString(R.string.share_link_no_support_share_api), Toast.LENGTH_LONG);
116 t.show();
117 }
118 }
119
120
121 public void shareFileWithLinkToApp(OCFile file, Intent sendIntent) {
122
123 if (file != null) {
124 mFileActivity.showLoadingDialog();
125
126 Intent service = new Intent(mFileActivity, OperationsService.class);
127 service.setAction(OperationsService.ACTION_CREATE_SHARE);
128 service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
129 service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
130 service.putExtra(OperationsService.EXTRA_SEND_INTENT, sendIntent);
131 mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
132
133 } else {
134 Log_OC.wtf(TAG, "Trying to open a NULL OCFile");
135 }
136 }
137
138
139 private Intent createShareWithLinkIntent(String link) {
140 Intent intentToShareLink = new Intent(Intent.ACTION_SEND);
141 intentToShareLink.putExtra(Intent.EXTRA_TEXT, link);
142 intentToShareLink.setType(HTTP.PLAIN_TEXT_TYPE);
143 return intentToShareLink;
144 }
145
146
147 /**
148 * @return 'True' if the server supports the Share API
149 */
150 public boolean isSharedSupported() {
151 if (mFileActivity.getAccount() != null) {
152 AccountManager accountManager = AccountManager.get(mFileActivity);
153
154 String version = accountManager.getUserData(mFileActivity.getAccount(), Constants.KEY_OC_VERSION);
155 return (new OwnCloudVersion(version)).isSharedSupported();
156 }
157 return false;
158 }
159
160
161 public void unshareFileWithLink(OCFile file) {
162
163 if (isSharedSupported()) {
164 // Unshare the file
165 Intent service = new Intent(mFileActivity, OperationsService.class);
166 service.setAction(OperationsService.ACTION_UNSHARE);
167 service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
168 service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
169 mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
170
171 mFileActivity.showLoadingDialog();
172
173 } else {
174 // Show a Message
175 Toast t = Toast.makeText(mFileActivity, mFileActivity.getString(R.string.share_link_no_support_share_api), Toast.LENGTH_LONG);
176 t.show();
177
178 }
179 }
180
181 public void sendDownloadedFile(OCFile file) {
182 if (file != null) {
183 Intent sendIntent = new Intent(android.content.Intent.ACTION_SEND);
184 // set MimeType
185 sendIntent.setType(file.getMimetype());
186 sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + file.getStoragePath()));
187 sendIntent.putExtra(Intent.ACTION_SEND, true); // Send Action
188
189 // Show dialog, without the own app
190 String[] packagesToExclude = new String[] { mFileActivity.getPackageName() };
191 DialogFragment chooserDialog = ShareLinkToDialog.newInstance(sendIntent, packagesToExclude, file);
192 chooserDialog.show(mFileActivity.getSupportFragmentManager(), FTAG_CHOOSER_DIALOG);
193
194 } else {
195 Log_OC.wtf(TAG, "Trying to send a NULL OCFile");
196 }
197 }
198
199
200 public void syncFile(OCFile file) {
201
202 if (!file.isFolder()){
203 Intent intent = new Intent(mFileActivity, OperationsService.class);
204 intent.setAction(OperationsService.ACTION_SYNC_FILE);
205 intent.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
206 intent.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
207 intent.putExtra(OperationsService.EXTRA_SYNC_FILE_CONTENTS, true);
208 mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(intent);
209 mFileActivity.showLoadingDialog();
210
211 } else {
212 /*
213 // Add files recursivly
214 FileDataStorageManager storageManager = mFileActivity.getStorageManager();
215 filesList.addAll(storageManager.getFolderContent(file));
216 boolean newfiles;
217 do {
218 Vector<OCFile> tmpFolders = new Vector<OCFile>();
219 for (OCFile tmpfile : filesList) {
220 if (tmpfile.isFolder()) {
221 tmpFolders.add(tmpfile);
222 }
223 }
224 if (tmpFolders.isEmpty()){
225 newfiles = false;
226 }else {
227 for(OCFile tmpFolder : tmpFolders){
228 filesList.remove(tmpFolder);
229 filesList.addAll(storageManager.getFolderContent(tmpFolder));
230 }
231 newfiles = true;
232 }
233 } while(newfiles);
234 */
235 Intent intent = new Intent(mFileActivity, OperationsService.class);
236 intent.setAction(OperationsService.ACTION_SYNC_FOLDER);
237 intent.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
238 intent.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
239 mFileActivity.startService(intent); // reevaluating: with or without Binder?
240 //mFileActivity.getOperationsServiceBinder().queueNewOperation(intent);
241 }
242 }
243
244 public void renameFile(OCFile file, String newFilename) {
245 // RenameFile
246 Intent service = new Intent(mFileActivity, OperationsService.class);
247 service.setAction(OperationsService.ACTION_RENAME);
248 service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
249 service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
250 service.putExtra(OperationsService.EXTRA_NEWNAME, newFilename);
251 mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
252
253 mFileActivity.showLoadingDialog();
254 }
255
256
257 public void removeFile(OCFile file, boolean onlyLocalCopy) {
258 // RemoveFile
259 Intent service = new Intent(mFileActivity, OperationsService.class);
260 service.setAction(OperationsService.ACTION_REMOVE);
261 service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
262 service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
263 service.putExtra(OperationsService.EXTRA_REMOVE_ONLY_LOCAL, onlyLocalCopy);
264 mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
265
266 mFileActivity.showLoadingDialog();
267 }
268
269
270 public void createFolder(String remotePath, boolean createFullPath) {
271 // Create Folder
272 Intent service = new Intent(mFileActivity, OperationsService.class);
273 service.setAction(OperationsService.ACTION_CREATE_FOLDER);
274 service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
275 service.putExtra(OperationsService.EXTRA_REMOTE_PATH, remotePath);
276 service.putExtra(OperationsService.EXTRA_CREATE_FULL_PATH, createFullPath);
277 mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
278
279 mFileActivity.showLoadingDialog();
280 }
281
282 /**
283 * Cancel the transference in downloads (files/folders) and file uploads
284 * @param file OCFile
285 */
286 public void cancelTransference(OCFile file) {
287 Account account = mFileActivity.getAccount();
288 if (!file.isFolder()) {
289 FileDownloaderBinder downloaderBinder = mFileActivity.getFileDownloaderBinder();
290 FileUploaderBinder uploaderBinder = mFileActivity.getFileUploaderBinder();
291 if (downloaderBinder != null && downloaderBinder.isDownloading(account, file)) {
292 // Remove etag for parent, if file is a keep_in_sync
293 if (file.keepInSync()) {
294 OCFile parent = mFileActivity.getStorageManager().getFileById(file.getParentId());
295 parent.setEtag("");
296 mFileActivity.getStorageManager().saveFile(parent);
297 }
298
299 downloaderBinder.cancel(account, file);
300
301 } else if (uploaderBinder != null && uploaderBinder.isUploading(account, file)) {
302 uploaderBinder.cancel(account, file);
303 }
304 } else {
305 Intent intent = new Intent(mFileActivity, OperationsService.class);
306 intent.setAction(OperationsService.ACTION_CANCEL_SYNC_FOLDER);
307 intent.putExtra(OperationsService.EXTRA_ACCOUNT, account);
308 intent.putExtra(OperationsService.EXTRA_FILE, file);
309 mFileActivity.startService(intent);
310
311 }
312 }
313
314 /**
315 * Start move file operation
316 * @param newfile File where it is going to be moved
317 * @param currentFile File with the previous info
318 */
319 public void moveFile(OCFile newfile, OCFile currentFile) {
320 // Move files
321 Intent service = new Intent(mFileActivity, OperationsService.class);
322 service.setAction(OperationsService.ACTION_MOVE_FILE);
323 service.putExtra(OperationsService.EXTRA_NEW_PARENT_PATH, newfile.getRemotePath());
324 service.putExtra(OperationsService.EXTRA_REMOTE_PATH, currentFile.getRemotePath());
325 service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
326 mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
327
328 mFileActivity.showLoadingDialog();
329 }
330
331
332 public long getOpIdWaitingFor() {
333 return mWaitingForOpId;
334 }
335
336
337 public void setOpIdWaitingFor(long waitingForOpId) {
338 mWaitingForOpId = waitingForOpId;
339 }
340
341
342 }