5ed2bb148f0665dea4b084101dd9be54a0896b65
[pub/Android/ownCloud.git] / src / com / owncloud / android / files / FileOperationsHelper.java
1 /* ownCloud Android client application
2 *
3 * @author masensio
4 * @author David A. Velasco
5 * Copyright (C) 2012-2015 ownCloud Inc.
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2,
9 * as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
21 package com.owncloud.android.files;
22
23 import org.apache.http.protocol.HTTP;
24
25 import android.accounts.Account;
26 import android.accounts.AccountManager;
27 import android.content.Intent;
28 import android.net.Uri;
29 import android.support.v4.app.DialogFragment;
30 import android.webkit.MimeTypeMap;
31 import android.widget.Toast;
32
33 import com.owncloud.android.R;
34 import com.owncloud.android.datamodel.OCFile;
35 import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
36 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
37
38 import com.owncloud.android.lib.common.accounts.AccountUtils.Constants;
39 import com.owncloud.android.lib.common.network.WebdavUtils;
40 import com.owncloud.android.lib.common.utils.Log_OC;
41 import com.owncloud.android.lib.resources.status.OwnCloudVersion;
42 import com.owncloud.android.services.OperationsService;
43 import com.owncloud.android.ui.activity.FileActivity;
44 import com.owncloud.android.ui.dialog.ShareLinkToDialog;
45
46 /**
47 *
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 Intent intent = new Intent(mFileActivity, OperationsService.class);
213 intent.setAction(OperationsService.ACTION_SYNC_FOLDER);
214 intent.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
215 intent.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
216 mFileActivity.startService(intent);
217 }
218 }
219
220 public void renameFile(OCFile file, String newFilename) {
221 // RenameFile
222 Intent service = new Intent(mFileActivity, OperationsService.class);
223 service.setAction(OperationsService.ACTION_RENAME);
224 service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
225 service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
226 service.putExtra(OperationsService.EXTRA_NEWNAME, newFilename);
227 mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
228
229 mFileActivity.showLoadingDialog();
230 }
231
232
233 public void removeFile(OCFile file, boolean onlyLocalCopy) {
234 // RemoveFile
235 Intent service = new Intent(mFileActivity, OperationsService.class);
236 service.setAction(OperationsService.ACTION_REMOVE);
237 service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
238 service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
239 service.putExtra(OperationsService.EXTRA_REMOVE_ONLY_LOCAL, onlyLocalCopy);
240 mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
241
242 mFileActivity.showLoadingDialog();
243 }
244
245
246 public void createFolder(String remotePath, boolean createFullPath) {
247 // Create Folder
248 Intent service = new Intent(mFileActivity, OperationsService.class);
249 service.setAction(OperationsService.ACTION_CREATE_FOLDER);
250 service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
251 service.putExtra(OperationsService.EXTRA_REMOTE_PATH, remotePath);
252 service.putExtra(OperationsService.EXTRA_CREATE_FULL_PATH, createFullPath);
253 mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
254
255 mFileActivity.showLoadingDialog();
256 }
257
258 /**
259 * Cancel the transference in downloads (files/folders) and file uploads
260 * @param file OCFile
261 */
262 public void cancelTransference(OCFile file) {
263 Account account = mFileActivity.getAccount();
264 if (file.isFolder()) {
265 OperationsService.OperationsServiceBinder opsBinder = mFileActivity.getOperationsServiceBinder();
266 if (opsBinder != null) {
267 opsBinder.cancel(account, file);
268 }
269 }
270
271 // for both files and folders
272 FileDownloaderBinder downloaderBinder = mFileActivity.getFileDownloaderBinder();
273 FileUploaderBinder uploaderBinder = mFileActivity.getFileUploaderBinder();
274 if (downloaderBinder != null && downloaderBinder.isDownloading(account, file)) {
275 downloaderBinder.cancel(account, file);
276
277 // TODO - review why is this here, and solve in a better way
278 // Remove etag for parent, if file is a keep_in_sync
279 if (file.keepInSync()) {
280 OCFile parent = mFileActivity.getStorageManager().getFileById(file.getParentId());
281 parent.setEtag("");
282 mFileActivity.getStorageManager().saveFile(parent);
283 }
284
285 } else if (uploaderBinder != null && uploaderBinder.isUploading(account, file)) {
286 uploaderBinder.cancel(account, file);
287 }
288 }
289
290 /**
291 * Start move file operation
292 * @param newfile File where it is going to be moved
293 * @param currentFile File with the previous info
294 */
295 public void moveFile(OCFile newfile, OCFile currentFile) {
296 // Move files
297 Intent service = new Intent(mFileActivity, OperationsService.class);
298 service.setAction(OperationsService.ACTION_MOVE_FILE);
299 service.putExtra(OperationsService.EXTRA_NEW_PARENT_PATH, newfile.getRemotePath());
300 service.putExtra(OperationsService.EXTRA_REMOTE_PATH, currentFile.getRemotePath());
301 service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
302 mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
303
304 mFileActivity.showLoadingDialog();
305 }
306
307
308 public long getOpIdWaitingFor() {
309 return mWaitingForOpId;
310 }
311
312
313 public void setOpIdWaitingFor(long waitingForOpId) {
314 mWaitingForOpId = waitingForOpId;
315 }
316
317
318 }