OC-2896: Move execution of CreateShareOperation to OperationsService
[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.sax.StartElementListener;
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.lib.accounts.OwnCloudAccount;
33 import com.owncloud.android.lib.network.webdav.WebdavUtils;
34 import com.owncloud.android.lib.operations.common.ShareType;
35 import com.owncloud.android.operations.CreateShareOperation;
36 import com.owncloud.android.operations.UnshareLinkOperation;
37 import com.owncloud.android.services.OperationsService;
38 import com.owncloud.android.ui.activity.FileActivity;
39 import com.owncloud.android.ui.dialog.ActivityChooserDialog;
40 import com.owncloud.android.utils.Log_OC;
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
54 public void openFile(OCFile file, FileActivity callerActivity) {
55 if (file != null) {
56 String storagePath = file.getStoragePath();
57 String encodedStoragePath = WebdavUtils.encodePath(storagePath);
58
59 Intent intentForSavedMimeType = new Intent(Intent.ACTION_VIEW);
60 intentForSavedMimeType.setDataAndType(Uri.parse("file://"+ encodedStoragePath), file.getMimetype());
61 intentForSavedMimeType.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
62
63 Intent intentForGuessedMimeType = null;
64 if (storagePath.lastIndexOf('.') >= 0) {
65 String guessedMimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(storagePath.substring(storagePath.lastIndexOf('.') + 1));
66 if (guessedMimeType != null && !guessedMimeType.equals(file.getMimetype())) {
67 intentForGuessedMimeType = new Intent(Intent.ACTION_VIEW);
68 intentForGuessedMimeType.setDataAndType(Uri.parse("file://"+ encodedStoragePath), guessedMimeType);
69 intentForGuessedMimeType.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
70 }
71 }
72
73 Intent chooserIntent = null;
74 if (intentForGuessedMimeType != null) {
75 chooserIntent = Intent.createChooser(intentForGuessedMimeType, callerActivity.getString(R.string.actionbar_open_with));
76 } else {
77 chooserIntent = Intent.createChooser(intentForSavedMimeType, callerActivity.getString(R.string.actionbar_open_with));
78 }
79
80 callerActivity.startActivity(chooserIntent);
81
82 } else {
83 Log_OC.wtf(TAG, "Trying to open a NULL OCFile");
84 }
85 }
86
87
88 public void shareFileWithLink(OCFile file, FileActivity callerActivity) {
89
90 if (isSharedSupported(callerActivity)) {
91 if (file != null) {
92 String link = "https://fake.url";
93 Intent intent = createShareWithLinkIntent(link);
94 String[] packagesToExclude = new String[] { callerActivity.getPackageName() };
95 DialogFragment chooserDialog = ActivityChooserDialog.newInstance(intent, packagesToExclude, file);
96 chooserDialog.show(callerActivity.getSupportFragmentManager(), FTAG_CHOOSER_DIALOG);
97
98 } else {
99 Log_OC.wtf(TAG, "Trying to share a NULL OCFile");
100 }
101
102 } else {
103 // Show a Message
104 Toast t = Toast.makeText(callerActivity, callerActivity.getString(R.string.share_link_no_support_share_api), Toast.LENGTH_LONG);
105 t.show();
106 }
107 }
108
109
110 public void shareFileWithLinkToApp(OCFile file, Intent sendIntent, FileActivity callerActivity) {
111
112 if (file != null) {
113 callerActivity.showLoadingDialog();
114
115 Intent service = new Intent(callerActivity, OperationsService.class);
116 service.setAction(OperationsService.ACTION_CREATE_SHARE);
117 service.putExtra(OperationsService.EXTRA_ACCOUNT, callerActivity.getAccount());
118 service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
119 service.putExtra(OperationsService.EXTRA_SEND_INTENT, sendIntent);
120 callerActivity.startService(service);
121
122 } else {
123 Log_OC.wtf(TAG, "Trying to open a NULL OCFile");
124 }
125 }
126
127
128 private Intent createShareWithLinkIntent(String link) {
129 Intent intentToShareLink = new Intent(Intent.ACTION_SEND);
130 intentToShareLink.putExtra(Intent.EXTRA_TEXT, link);
131 intentToShareLink.setType(HTTP.PLAIN_TEXT_TYPE);
132 return intentToShareLink;
133 }
134
135
136 /**
137 * @return 'True' if the server supports the Share API
138 */
139 public boolean isSharedSupported(FileActivity callerActivity) {
140 if (callerActivity.getAccount() != null) {
141 AccountManager accountManager = AccountManager.get(callerActivity);
142 return Boolean.parseBoolean(accountManager.getUserData(callerActivity.getAccount(), OwnCloudAccount.Constants.KEY_SUPPORTS_SHARE_API));
143 }
144 return false;
145 }
146
147
148 public void unshareFileWithLink(OCFile file, FileActivity callerActivity) {
149
150 if (isSharedSupported(callerActivity)) {
151 // Unshare the file
152 UnshareLinkOperation unshare = new UnshareLinkOperation(file, callerActivity);
153 unshare.execute(callerActivity.getStorageManager(),
154 callerActivity,
155 callerActivity.getRemoteOperationListener(),
156 callerActivity.getHandler(),
157 callerActivity);
158
159 callerActivity.showLoadingDialog();
160
161 } else {
162 // Show a Message
163 Toast t = Toast.makeText(callerActivity, callerActivity.getString(R.string.share_link_no_support_share_api), Toast.LENGTH_LONG);
164 t.show();
165
166 }
167 }
168 }