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