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