96c8c95f0314cb2089c7209b4c0d82d5d4c230e8
[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
33 import com.owncloud.android.lib.common.accounts.AccountUtils.Constants;
34 import com.owncloud.android.lib.common.network.WebdavUtils;
35 import com.owncloud.android.lib.common.operations.RemoteOperation;
36 import com.owncloud.android.lib.resources.status.OwnCloudVersion;
37 import com.owncloud.android.operations.RemoveFileOperation;
38 import com.owncloud.android.operations.SynchronizeFileOperation;
39 import com.owncloud.android.services.OperationsService;
40 import com.owncloud.android.ui.activity.FileActivity;
41 import com.owncloud.android.ui.dialog.ShareLinkToDialog;
42 import com.owncloud.android.utils.Log_OC;
43
44 /**
45 *
46 * @author masensio
47 * @author David A. Velasco
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 public FileOperationsHelper(FileActivity fileActivity) {
58 mFileActivity = fileActivity;
59 }
60
61
62 public void openFile(OCFile file) {
63 if (file != null) {
64 String storagePath = file.getStoragePath();
65 String encodedStoragePath = WebdavUtils.encodePath(storagePath);
66
67 Intent intentForSavedMimeType = new Intent(Intent.ACTION_VIEW);
68 intentForSavedMimeType.setDataAndType(Uri.parse("file://"+ encodedStoragePath), file.getMimetype());
69 intentForSavedMimeType.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
70
71 Intent intentForGuessedMimeType = null;
72 if (storagePath.lastIndexOf('.') >= 0) {
73 String guessedMimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(storagePath.substring(storagePath.lastIndexOf('.') + 1));
74 if (guessedMimeType != null && !guessedMimeType.equals(file.getMimetype())) {
75 intentForGuessedMimeType = new Intent(Intent.ACTION_VIEW);
76 intentForGuessedMimeType.setDataAndType(Uri.parse("file://"+ encodedStoragePath), guessedMimeType);
77 intentForGuessedMimeType.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
78 }
79 }
80
81 Intent chooserIntent = null;
82 if (intentForGuessedMimeType != null) {
83 chooserIntent = Intent.createChooser(intentForGuessedMimeType, mFileActivity.getString(R.string.actionbar_open_with));
84 } else {
85 chooserIntent = Intent.createChooser(intentForSavedMimeType, mFileActivity.getString(R.string.actionbar_open_with));
86 }
87
88 mFileActivity.startActivity(chooserIntent);
89
90 } else {
91 Log_OC.wtf(TAG, "Trying to open a NULL OCFile");
92 }
93 }
94
95
96 public void shareFileWithLink(OCFile file) {
97
98 if (isSharedSupported()) {
99 if (file != null) {
100 String link = "https://fake.url";
101 Intent intent = createShareWithLinkIntent(link);
102 String[] packagesToExclude = new String[] { mFileActivity.getPackageName() };
103 DialogFragment chooserDialog = ShareLinkToDialog.newInstance(intent, packagesToExclude, file);
104 chooserDialog.show(mFileActivity.getSupportFragmentManager(), FTAG_CHOOSER_DIALOG);
105
106 } else {
107 Log_OC.wtf(TAG, "Trying to share a NULL OCFile");
108 }
109
110 } else {
111 // Show a Message
112 Toast t = Toast.makeText(mFileActivity, mFileActivity.getString(R.string.share_link_no_support_share_api), Toast.LENGTH_LONG);
113 t.show();
114 }
115 }
116
117
118 public void shareFileWithLinkToApp(OCFile file, Intent sendIntent) {
119
120 if (file != null) {
121 mFileActivity.showLoadingDialog();
122
123 Intent service = new Intent(mFileActivity, OperationsService.class);
124 service.setAction(OperationsService.ACTION_CREATE_SHARE);
125 service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
126 service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
127 service.putExtra(OperationsService.EXTRA_SEND_INTENT, sendIntent);
128 mFileActivity.getOperationsServiceBinder().newOperation(service);
129
130 } else {
131 Log_OC.wtf(TAG, "Trying to open a NULL OCFile");
132 }
133 }
134
135
136 private Intent createShareWithLinkIntent(String link) {
137 Intent intentToShareLink = new Intent(Intent.ACTION_SEND);
138 intentToShareLink.putExtra(Intent.EXTRA_TEXT, link);
139 intentToShareLink.setType(HTTP.PLAIN_TEXT_TYPE);
140 return intentToShareLink;
141 }
142
143
144 /**
145 * @return 'True' if the server supports the Share API
146 */
147 public boolean isSharedSupported() {
148 if (mFileActivity.getAccount() != null) {
149 AccountManager accountManager = AccountManager.get(mFileActivity);
150
151 String version = accountManager.getUserData(mFileActivity.getAccount(), Constants.KEY_OC_VERSION);
152 return (new OwnCloudVersion(version)).isSharedSupported();
153 }
154 return false;
155 }
156
157
158 public void unshareFileWithLink(OCFile file) {
159
160 if (isSharedSupported()) {
161 // Unshare the file
162 Intent service = new Intent(mFileActivity, OperationsService.class);
163 service.setAction(OperationsService.ACTION_UNSHARE);
164 service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
165 service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
166 mFileActivity.getOperationsServiceBinder().newOperation(service);
167
168 mFileActivity.showLoadingDialog();
169
170 } else {
171 // Show a Message
172 Toast t = Toast.makeText(mFileActivity, mFileActivity.getString(R.string.share_link_no_support_share_api), Toast.LENGTH_LONG);
173 t.show();
174
175 }
176 }
177
178 public void sendDownloadedFile(OCFile file) {
179 if (file != null) {
180 Intent sendIntent = new Intent(android.content.Intent.ACTION_SEND);
181 // set MimeType
182 sendIntent.setType(file.getMimetype());
183 sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + file.getStoragePath()));
184 sendIntent.putExtra(Intent.ACTION_SEND, true); // Send Action
185
186 // Show dialog, without the own app
187 String[] packagesToExclude = new String[] { mFileActivity.getPackageName() };
188 DialogFragment chooserDialog = ShareLinkToDialog.newInstance(sendIntent, packagesToExclude, file);
189 chooserDialog.show(mFileActivity.getSupportFragmentManager(), FTAG_CHOOSER_DIALOG);
190
191 } else {
192 Log_OC.wtf(TAG, "Trying to send a NULL OCFile");
193 }
194 }
195
196
197 public void syncFile(OCFile file) {
198 Account account = mFileActivity.getAccount();
199 RemoteOperation operation = new SynchronizeFileOperation(
200 file,
201 null,
202 mFileActivity.getStorageManager(),
203 account,
204 true,
205 mFileActivity);
206 operation.execute(account, mFileActivity, mFileActivity, mFileActivity.getHandler(), mFileActivity);
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 mFileActivity.getOperationsServiceBinder().newOperation(service);
219
220 mFileActivity.showLoadingDialog();
221 }
222
223
224 public void removeFile(OCFile file, boolean removeLocalCopy) {
225 Account account = mFileActivity.getAccount();
226 RemoteOperation operation = new RemoveFileOperation(
227 file,
228 removeLocalCopy,
229 mFileActivity.getStorageManager());
230
231 operation.execute(
232 account,
233 mFileActivity,
234 mFileActivity,
235 mFileActivity.getHandler(),
236 mFileActivity);
237
238 mFileActivity.showLoadingDialog();
239 }
240
241 }