Connected selection of app to send share link with creation of share resource, and...
[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.lib.operations.common.ShareType;
34 import com.owncloud.android.operations.CreateShareOperation;
35 import com.owncloud.android.ui.activity.FileActivity;
36 import com.owncloud.android.ui.dialog.ActivityChooserDialog;
37 import com.owncloud.android.utils.Log_OC;
38
39 /**
40 *
41 * @author masensio
42 * @author David A. Velasco
43 */
44 public class FileOperationsHelper {
45
46 private static final String TAG = FileOperationsHelper.class.getName();
47
48 private static final String FTAG_CHOOSER_DIALOG = "CHOOSER_DIALOG";
49
50
51 public void openFile(OCFile file, FileActivity callerActivity) {
52 if (file != null) {
53 String storagePath = file.getStoragePath();
54 String encodedStoragePath = WebdavUtils.encodePath(storagePath);
55
56 Intent intentForSavedMimeType = new Intent(Intent.ACTION_VIEW);
57 intentForSavedMimeType.setDataAndType(Uri.parse("file://"+ encodedStoragePath), file.getMimetype());
58 intentForSavedMimeType.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
59
60 Intent intentForGuessedMimeType = null;
61 if (storagePath.lastIndexOf('.') >= 0) {
62 String guessedMimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(storagePath.substring(storagePath.lastIndexOf('.') + 1));
63 if (guessedMimeType != null && !guessedMimeType.equals(file.getMimetype())) {
64 intentForGuessedMimeType = new Intent(Intent.ACTION_VIEW);
65 intentForGuessedMimeType.setDataAndType(Uri.parse("file://"+ encodedStoragePath), guessedMimeType);
66 intentForGuessedMimeType.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
67 }
68 }
69
70 Intent chooserIntent = null;
71 if (intentForGuessedMimeType != null) {
72 chooserIntent = Intent.createChooser(intentForGuessedMimeType, callerActivity.getString(R.string.actionbar_open_with));
73 } else {
74 chooserIntent = Intent.createChooser(intentForSavedMimeType, callerActivity.getString(R.string.actionbar_open_with));
75 }
76
77 callerActivity.startActivity(chooserIntent);
78
79 } else {
80 Log_OC.wtf(TAG, "Trying to open a NULL OCFile");
81 }
82 }
83
84
85 public void shareFileWithLink(OCFile file, FileActivity callerActivity) {
86
87 if (isSharedSupported(callerActivity)) {
88 if (file != null) {
89 String link = "https://fake.url";
90 Intent intent = createShareWithLinkIntent(link);
91 String[] packagesToExclude = new String[] { callerActivity.getPackageName() };
92 DialogFragment chooserDialog = ActivityChooserDialog.newInstance(intent, packagesToExclude, file);
93 chooserDialog.show(callerActivity.getSupportFragmentManager(), FTAG_CHOOSER_DIALOG);
94
95 } else {
96 Log_OC.wtf(TAG, "Trying to share a NULL OCFile");
97 }
98
99 } else {
100 // Show a Message
101 Toast t = Toast.makeText(callerActivity, callerActivity.getString(R.string.share_link_no_support_share_api), Toast.LENGTH_LONG);
102 t.show();
103 }
104 }
105
106
107 public void shareFileWithLinkToApp(OCFile file, Intent sendIntent, FileActivity callerActivity) {
108
109 if (file != null) {
110 CreateShareOperation createShare = new CreateShareOperation(file.getRemotePath(), ShareType.PUBLIC_LINK, "", false, "", 1, sendIntent);
111 createShare.execute(callerActivity.getStorageManager(),
112 callerActivity,
113 callerActivity.getRemoteOperationListener(),
114 callerActivity.getHandler(),
115 callerActivity);
116
117 } else {
118 Log_OC.wtf(TAG, "Trying to open a NULL OCFile");
119 }
120 }
121
122
123 private Intent createShareWithLinkIntent(String link) {
124 Intent intentToShareLink = new Intent(Intent.ACTION_SEND);
125 intentToShareLink.putExtra(Intent.EXTRA_TEXT, link);
126 intentToShareLink.setType(HTTP.PLAIN_TEXT_TYPE);
127 return intentToShareLink;
128 }
129
130
131 /**
132 * @return 'True' if the server supports the Share API
133 */
134 public boolean isSharedSupported(FileActivity callerActivity) {
135 if (callerActivity.getAccount() != null) {
136 AccountManager accountManager = AccountManager.get(callerActivity);
137 return Boolean.parseBoolean(accountManager.getUserData(callerActivity.getAccount(), OwnCloudAccount.Constants.KEY_SUPPORTS_SHARE_API));
138 }
139 return false;
140 }
141
142 }