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