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