Merge pull request #918 from owncloud/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, String password, 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_PASSWORD_SHARE, password);
132 service.putExtra(OperationsService.EXTRA_SEND_INTENT, sendIntent);
133 mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
134
135 } else {
136 Log_OC.wtf(TAG, "Trying to open a NULL OCFile");
137 }
138 }
139
140
141 private Intent createShareWithLinkIntent(String link) {
142 Intent intentToShareLink = new Intent(Intent.ACTION_SEND);
143 intentToShareLink.putExtra(Intent.EXTRA_TEXT, link);
144 intentToShareLink.setType(HTTP.PLAIN_TEXT_TYPE);
145 return intentToShareLink;
146 }
147
148
149 /**
150 * @return 'True' if the server supports the Share API
151 */
152 public boolean isSharedSupported() {
153 if (mFileActivity.getAccount() != null) {
154 AccountManager accountManager = AccountManager.get(mFileActivity);
155
156 String version = accountManager.getUserData(mFileActivity.getAccount(), Constants.KEY_OC_VERSION);
157 return (new OwnCloudVersion(version)).isSharedSupported();
158 }
159 return false;
160 }
161
162
163 public void unshareFileWithLink(OCFile file) {
164
165 if (isSharedSupported()) {
166 // Unshare the file
167 Intent service = new Intent(mFileActivity, OperationsService.class);
168 service.setAction(OperationsService.ACTION_UNSHARE);
169 service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
170 service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
171 mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
172
173 mFileActivity.showLoadingDialog();
174
175 } else {
176 // Show a Message
177 Toast t = Toast.makeText(mFileActivity, mFileActivity.getString(R.string.share_link_no_support_share_api), Toast.LENGTH_LONG);
178 t.show();
179
180 }
181 }
182
183 public void sendDownloadedFile(OCFile file) {
184 if (file != null) {
185 Intent sendIntent = new Intent(android.content.Intent.ACTION_SEND);
186 // set MimeType
187 sendIntent.setType(file.getMimetype());
188 sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + file.getStoragePath()));
189 sendIntent.putExtra(Intent.ACTION_SEND, true); // Send Action
190
191 // Show dialog, without the own app
192 String[] packagesToExclude = new String[] { mFileActivity.getPackageName() };
193 DialogFragment chooserDialog = ShareLinkToDialog.newInstance(sendIntent, packagesToExclude, file);
194 chooserDialog.show(mFileActivity.getSupportFragmentManager(), FTAG_CHOOSER_DIALOG);
195
196 } else {
197 Log_OC.wtf(TAG, "Trying to send a NULL OCFile");
198 }
199 }
200
201
202 public void syncFile(OCFile file) {
203
204 if (!file.isFolder()){
205 Intent intent = new Intent(mFileActivity, OperationsService.class);
206 intent.setAction(OperationsService.ACTION_SYNC_FILE);
207 intent.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
208 intent.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
209 intent.putExtra(OperationsService.EXTRA_SYNC_FILE_CONTENTS, true);
210 mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(intent);
211 mFileActivity.showLoadingDialog();
212
213 } else {
214 Intent intent = new Intent(mFileActivity, OperationsService.class);
215 intent.setAction(OperationsService.ACTION_SYNC_FOLDER);
216 intent.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
217 intent.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
218 mFileActivity.startService(intent);
219 }
220 }
221
222 public void renameFile(OCFile file, String newFilename) {
223 // RenameFile
224 Intent service = new Intent(mFileActivity, OperationsService.class);
225 service.setAction(OperationsService.ACTION_RENAME);
226 service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
227 service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
228 service.putExtra(OperationsService.EXTRA_NEWNAME, newFilename);
229 mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
230
231 mFileActivity.showLoadingDialog();
232 }
233
234
235 public void removeFile(OCFile file, boolean onlyLocalCopy) {
236 // RemoveFile
237 Intent service = new Intent(mFileActivity, OperationsService.class);
238 service.setAction(OperationsService.ACTION_REMOVE);
239 service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
240 service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
241 service.putExtra(OperationsService.EXTRA_REMOVE_ONLY_LOCAL, onlyLocalCopy);
242 mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
243
244 mFileActivity.showLoadingDialog();
245 }
246
247
248 public void createFolder(String remotePath, boolean createFullPath) {
249 // Create Folder
250 Intent service = new Intent(mFileActivity, OperationsService.class);
251 service.setAction(OperationsService.ACTION_CREATE_FOLDER);
252 service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
253 service.putExtra(OperationsService.EXTRA_REMOTE_PATH, remotePath);
254 service.putExtra(OperationsService.EXTRA_CREATE_FULL_PATH, createFullPath);
255 mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
256
257 mFileActivity.showLoadingDialog();
258 }
259
260 /**
261 * Cancel the transference in downloads (files/folders) and file uploads
262 * @param file OCFile
263 */
264 public void cancelTransference(OCFile file) {
265 Account account = mFileActivity.getAccount();
266 if (file.isFolder()) {
267 OperationsService.OperationsServiceBinder opsBinder = mFileActivity.getOperationsServiceBinder();
268 if (opsBinder != null) {
269 opsBinder.cancel(account, file);
270 }
271 }
272
273 // for both files and folders
274 FileDownloaderBinder downloaderBinder = mFileActivity.getFileDownloaderBinder();
275 FileUploaderBinder uploaderBinder = mFileActivity.getFileUploaderBinder();
276 if (downloaderBinder != null && downloaderBinder.isDownloading(account, file)) {
277 downloaderBinder.cancel(account, file);
278
279 // TODO - review why is this here, and solve in a better way
280 // Remove etag for parent, if file is a keep_in_sync
281 if (file.keepInSync()) {
282 OCFile parent = mFileActivity.getStorageManager().getFileById(file.getParentId());
283 parent.setEtag("");
284 mFileActivity.getStorageManager().saveFile(parent);
285 }
286
287 } else if (uploaderBinder != null && uploaderBinder.isUploading(account, file)) {
288 uploaderBinder.cancel(account, file);
289 }
290 }
291
292 /**
293 * Start move file operation
294 * @param newfile File where it is going to be moved
295 * @param currentFile File with the previous info
296 */
297 public void moveFile(OCFile newfile, OCFile currentFile) {
298 // Move files
299 Intent service = new Intent(mFileActivity, OperationsService.class);
300 service.setAction(OperationsService.ACTION_MOVE_FILE);
301 service.putExtra(OperationsService.EXTRA_NEW_PARENT_PATH, newfile.getRemotePath());
302 service.putExtra(OperationsService.EXTRA_REMOTE_PATH, currentFile.getRemotePath());
303 service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
304 mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
305
306 mFileActivity.showLoadingDialog();
307 }
308
309
310 public long getOpIdWaitingFor() {
311 return mWaitingForOpId;
312 }
313
314
315 public void setOpIdWaitingFor(long waitingForOpId) {
316 mWaitingForOpId = waitingForOpId;
317 }
318
319
320 }