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