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