1/*
2 * Copyright (c) 2018, Daniel Gultsch All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification,
5 * are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation and/or
12 * other materials provided with the distribution.
13 *
14 * 3. Neither the name of the copyright holder nor the names of its contributors
15 * may be used to endorse or promote products derived from this software without
16 * specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
22 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30package eu.siacs.conversations.ui.util;
31
32import android.content.ActivityNotFoundException;
33import android.content.ClipboardManager;
34import android.content.ClipData;
35import android.content.Context;
36import android.content.Intent;
37import android.net.Uri;
38import android.text.SpannableStringBuilder;
39import android.text.style.URLSpan;
40import android.widget.Toast;
41
42import java.util.regex.Matcher;
43
44import eu.siacs.conversations.R;
45import eu.siacs.conversations.entities.DownloadableFile;
46import eu.siacs.conversations.entities.Message;
47import eu.siacs.conversations.persistance.FileBackend;
48import eu.siacs.conversations.ui.ConversationsActivity;
49import eu.siacs.conversations.ui.XmppActivity;
50import eu.siacs.conversations.utils.Patterns;
51import eu.siacs.conversations.utils.XmppUri;
52import eu.siacs.conversations.xmpp.Jid;
53
54public class ShareUtil {
55
56 public static void share(XmppActivity activity, Message message) {
57 Intent shareIntent = new Intent();
58 shareIntent.setAction(Intent.ACTION_SEND);
59 if (message.isGeoUri()) {
60 shareIntent.putExtra(Intent.EXTRA_TEXT, message.getBody());
61 shareIntent.setType("text/plain");
62 } else if (!message.isFileOrImage()) {
63 shareIntent.putExtra(Intent.EXTRA_TEXT, message.getMergedBody().toString());
64 shareIntent.setType("text/plain");
65 shareIntent.putExtra(ConversationsActivity.EXTRA_AS_QUOTE, message.getStatus() == Message.STATUS_RECEIVED);
66 } else {
67 final DownloadableFile file = activity.xmppConnectionService.getFileBackend().getFile(message);
68 try {
69 shareIntent.putExtra(Intent.EXTRA_STREAM, FileBackend.getUriForFile(activity, file));
70 } catch (SecurityException e) {
71 Toast.makeText(activity, activity.getString(R.string.no_permission_to_access_x, file.getAbsolutePath()), Toast.LENGTH_SHORT).show();
72 return;
73 }
74 shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
75 String mime = message.getMimeType();
76 if (mime == null) {
77 mime = "*/*";
78 }
79 shareIntent.setType(mime);
80 }
81 try {
82 activity.startActivity(Intent.createChooser(shareIntent, activity.getText(R.string.share_with)));
83 } catch (ActivityNotFoundException e) {
84 //This should happen only on faulty androids because normally chooser is always available
85 Toast.makeText(activity, R.string.no_application_found_to_open_file, Toast.LENGTH_SHORT).show();
86 }
87 }
88
89 public static void copyToClipboard(XmppActivity activity, Message message) {
90 if (activity.copyTextToClipboard(message.getMergedBody().toString(), R.string.message)) {
91 Toast.makeText(activity, R.string.message_copied_to_clipboard, Toast.LENGTH_SHORT).show();
92 }
93 }
94
95 public static void copyUrlToClipboard(XmppActivity activity, Message message) {
96 final String url;
97 final int resId;
98 if (message.isGeoUri()) {
99 resId = R.string.location;
100 url = message.getQuoteableBody();
101 } else if (message.hasFileOnRemoteHost()) {
102 resId = R.string.file_url;
103 url = message.getFileParams().url;
104 } else {
105 final Message.FileParams fileParams = message.getFileParams();
106 url = (fileParams != null && fileParams.url != null) ? fileParams.url : message.getBody().trim();
107 resId = R.string.file_url;
108 }
109 if (activity.copyTextToClipboard(url, resId)) {
110 Toast.makeText(activity, R.string.url_copied_to_clipboard, Toast.LENGTH_SHORT).show();
111 }
112 }
113
114 public static void copyLinkToClipboard(final Context context, final String url) {
115 final Uri uri = Uri.parse(url);
116 if ("xmpp".equals(uri.getScheme())) {
117 try {
118 final Jid jid = new XmppUri(uri).getJid();
119 if (copyTextToClipboard(context, jid.asBareJid().toString(), R.string.account_settings_jabber_id)) {
120 Toast.makeText(context, R.string.jabber_id_copied_to_clipboard, Toast.LENGTH_SHORT).show();
121 }
122 } catch (final Exception e) { }
123 } else {
124 if (copyTextToClipboard(context, url, R.string.web_address)) {
125 Toast.makeText(context, R.string.url_copied_to_clipboard, Toast.LENGTH_SHORT).show();
126 }
127 }
128 }
129
130 public static void copyLinkToClipboard(final XmppActivity activity, final Message message) {
131 final SpannableStringBuilder body = message.getMergedBody();
132 MyLinkify.addLinks(body, true);
133 for (final URLSpan urlspan : body.getSpans(0, body.length() - 1, URLSpan.class)) {
134 copyLinkToClipboard(activity, urlspan.getURL());
135 return;
136 }
137 }
138
139 public static boolean containsXmppUri(String body) {
140 Matcher xmppPatternMatcher = Patterns.XMPP_PATTERN.matcher(body);
141 if (xmppPatternMatcher.find()) {
142 try {
143 return new XmppUri(body.substring(xmppPatternMatcher.start(), xmppPatternMatcher.end())).isValidJid();
144 } catch (Exception e) {
145 return false;
146 }
147 }
148 return false;
149 }
150
151 public static boolean copyTextToClipboard(Context context, String text, int labelResId) {
152 ClipboardManager mClipBoardManager = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
153 String label = context.getResources().getString(labelResId);
154 if (mClipBoardManager != null) {
155 ClipData mClipData = ClipData.newPlainText(label, text);
156 mClipBoardManager.setPrimaryClip(mClipData);
157 return true;
158 }
159 return false;
160 }
161
162 public static String getLinkScheme(final SpannableStringBuilder body) {
163 MyLinkify.addLinks(body, false);
164 for (final String url : MyLinkify.extractLinks(body)) {
165 final Uri uri = Uri.parse(url);
166 if ("xmpp".equals(uri.getScheme())) {
167 return uri.getScheme();
168 } else {
169 return "http";
170 }
171 }
172 return null;
173 }
174}