OC-2967: Grant that files are downloaded automatically when the action is triggered...
[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 import com.owncloud.android.lib.accounts.OwnCloudAccount;
32 import com.owncloud.android.lib.network.webdav.WebdavUtils;
33 import com.owncloud.android.services.OperationsService;
34 import com.owncloud.android.ui.activity.FileActivity;
35 import com.owncloud.android.ui.dialog.ActivityChooserDialog;
36 import com.owncloud.android.utils.Log_OC;
37
38 /**
39 *
40 * @author masensio
41 * @author David A. Velasco
42 */
43 public class FileOperationsHelper {
44
45 private static final String TAG = FileOperationsHelper.class.getName();
46
47 private static final String FTAG_CHOOSER_DIALOG = "CHOOSER_DIALOG";
48
49
50 public void openFile(OCFile file, FileActivity callerActivity) {
51 if (file != null) {
52 String storagePath = file.getStoragePath();
53 String encodedStoragePath = WebdavUtils.encodePath(storagePath);
54
55 Intent intentForSavedMimeType = new Intent(Intent.ACTION_VIEW);
56 intentForSavedMimeType.setDataAndType(Uri.parse("file://"+ encodedStoragePath), file.getMimetype());
57 intentForSavedMimeType.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
58
59 Intent intentForGuessedMimeType = null;
60 if (storagePath.lastIndexOf('.') >= 0) {
61 String guessedMimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(storagePath.substring(storagePath.lastIndexOf('.') + 1));
62 if (guessedMimeType != null && !guessedMimeType.equals(file.getMimetype())) {
63 intentForGuessedMimeType = new Intent(Intent.ACTION_VIEW);
64 intentForGuessedMimeType.setDataAndType(Uri.parse("file://"+ encodedStoragePath), guessedMimeType);
65 intentForGuessedMimeType.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
66 }
67 }
68
69 Intent chooserIntent = null;
70 if (intentForGuessedMimeType != null) {
71 chooserIntent = Intent.createChooser(intentForGuessedMimeType, callerActivity.getString(R.string.actionbar_open_with));
72 } else {
73 chooserIntent = Intent.createChooser(intentForSavedMimeType, callerActivity.getString(R.string.actionbar_open_with));
74 }
75
76 callerActivity.startActivity(chooserIntent);
77
78 } else {
79 Log_OC.wtf(TAG, "Trying to open a NULL OCFile");
80 }
81 }
82
83
84 public void shareFileWithLink(OCFile file, FileActivity callerActivity) {
85
86 if (isSharedSupported(callerActivity)) {
87 if (file != null) {
88 String link = "https://fake.url";
89 Intent intent = createShareWithLinkIntent(link);
90 String[] packagesToExclude = new String[] { callerActivity.getPackageName() };
91 DialogFragment chooserDialog = ActivityChooserDialog.newInstance(intent, packagesToExclude, file);
92 chooserDialog.show(callerActivity.getSupportFragmentManager(), FTAG_CHOOSER_DIALOG);
93
94 } else {
95 Log_OC.wtf(TAG, "Trying to share a NULL OCFile");
96 }
97
98 } else {
99 // Show a Message
100 Toast t = Toast.makeText(callerActivity, callerActivity.getString(R.string.share_link_no_support_share_api), Toast.LENGTH_LONG);
101 t.show();
102 }
103 }
104
105
106 public void shareFileWithLinkToApp(OCFile file, Intent sendIntent, FileActivity callerActivity) {
107
108 if (file != null) {
109 callerActivity.showLoadingDialog();
110
111 Intent service = new Intent(callerActivity, OperationsService.class);
112 service.setAction(OperationsService.ACTION_CREATE_SHARE);
113 service.putExtra(OperationsService.EXTRA_ACCOUNT, callerActivity.getAccount());
114 service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
115 service.putExtra(OperationsService.EXTRA_SEND_INTENT, sendIntent);
116 callerActivity.startService(service);
117
118 } else {
119 Log_OC.wtf(TAG, "Trying to open a NULL OCFile");
120 }
121 }
122
123
124 private Intent createShareWithLinkIntent(String link) {
125 Intent intentToShareLink = new Intent(Intent.ACTION_SEND);
126 intentToShareLink.putExtra(Intent.EXTRA_TEXT, link);
127 intentToShareLink.setType(HTTP.PLAIN_TEXT_TYPE);
128 return intentToShareLink;
129 }
130
131
132 /**
133 * @return 'True' if the server supports the Share API
134 */
135 public boolean isSharedSupported(FileActivity callerActivity) {
136 if (callerActivity.getAccount() != null) {
137 AccountManager accountManager = AccountManager.get(callerActivity);
138 return Boolean.parseBoolean(accountManager.getUserData(callerActivity.getAccount(), OwnCloudAccount.Constants.KEY_SUPPORTS_SHARE_API));
139 }
140 return false;
141 }
142
143
144 public void unshareFileWithLink(OCFile file, FileActivity callerActivity) {
145
146 if (isSharedSupported(callerActivity)) {
147 // Unshare the file
148 Intent service = new Intent(callerActivity, OperationsService.class);
149 service.setAction(OperationsService.ACTION_UNSHARE);
150 service.putExtra(OperationsService.EXTRA_ACCOUNT, callerActivity.getAccount());
151 service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
152 callerActivity.startService(service);
153
154 callerActivity.showLoadingDialog();
155
156 } else {
157 // Show a Message
158 Toast t = Toast.makeText(callerActivity, callerActivity.getString(R.string.share_link_no_support_share_api), Toast.LENGTH_LONG);
159 t.show();
160
161 }
162 }
163
164 public void sendFile(OCFile file, FileActivity callerActivity) {
165 // Obtain the file
166 if (!file.isDown()) { // Download the file
167 Log_OC.d(TAG, file.getRemotePath() + " : File must be downloaded");
168 } else {
169 sendDownloadedFile(file, callerActivity);
170 }
171
172
173 }
174
175 public void sendDownloadedFile(OCFile file, FileActivity callerActivity) {
176 Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
177 // set MimeType
178 sharingIntent.setType(file.getMimetype());
179 sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + file.getStoragePath()));
180 callerActivity.startActivity(Intent.createChooser(sharingIntent, callerActivity.getString(R.string.send_file_title_intent)));
181 }
182
183
184 }