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 final var fp = message.getFileParams();
69 final var name = fp == null ? null : fp.getName();
70 final var displayName = name == null ? file.getName() : name;
71 try {
72 shareIntent.putExtra(Intent.EXTRA_STREAM, FileBackend.getUriForFile(activity, file, displayName));
73 } catch (SecurityException e) {
74 Toast.makeText(activity, activity.getString(R.string.no_permission_to_access_x, file.getAbsolutePath()), Toast.LENGTH_SHORT).show();
75 return;
76 }
77 shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
78 String mime = message.getMimeType();
79 if (mime == null) {
80 mime = "*/*";
81 }
82 shareIntent.setType(mime);
83 }
84 try {
85 activity.startActivity(Intent.createChooser(shareIntent, activity.getText(R.string.share_with)));
86 } catch (ActivityNotFoundException e) {
87 //This should happen only on faulty androids because normally chooser is always available
88 Toast.makeText(activity, R.string.no_application_found_to_open_file, Toast.LENGTH_SHORT).show();
89 }
90 }
91
92 public static void copyToClipboard(XmppActivity activity, Message message) {
93 if (activity.copyTextToClipboard(message.getQuoteableBody(), R.string.message)) {
94 Toast.makeText(activity, R.string.message_copied_to_clipboard, Toast.LENGTH_SHORT).show();
95 }
96 }
97
98 public static void copyUrlToClipboard(XmppActivity activity, Message message) {
99 final String url;
100 final int resId;
101 if (message.isGeoUri()) {
102 resId = R.string.location;
103 url = message.getRawBody();
104 } else if (message.hasFileOnRemoteHost()) {
105 resId = R.string.file_url;
106 url = message.getFileParams().url;
107 } else {
108 final Message.FileParams fileParams = message.getFileParams();
109 url = (fileParams != null && fileParams.url != null) ? fileParams.url : message.getBody().trim();
110 resId = R.string.file_url;
111 }
112 if (activity.copyTextToClipboard(url, resId)) {
113 Toast.makeText(activity, R.string.url_copied_to_clipboard, Toast.LENGTH_SHORT).show();
114 }
115 }
116
117 public static void copyLinkToClipboard(final Context context, final String url) {
118 final Uri uri = Uri.parse(url);
119 if ("xmpp".equals(uri.getScheme())) {
120 try {
121 final Jid jid = new XmppUri(uri).getJid();
122 if (copyTextToClipboard(context, jid.asBareJid().toString(), R.string.account_settings_jabber_id)) {
123 Toast.makeText(context, R.string.jabber_id_copied_to_clipboard, Toast.LENGTH_SHORT).show();
124 }
125 } catch (final Exception e) { }
126 } else {
127 if (copyTextToClipboard(context, url, R.string.web_address)) {
128 Toast.makeText(context, R.string.url_copied_to_clipboard, Toast.LENGTH_SHORT).show();
129 }
130 }
131 }
132
133 public static void copyLinkToClipboard(final XmppActivity activity, final Message message) {
134 final SpannableStringBuilder body = message.getMergedBody();
135 MyLinkify.addLinks(body, true);
136 for (final URLSpan urlspan : body.getSpans(0, body.length() - 1, URLSpan.class)) {
137 copyLinkToClipboard(activity, urlspan.getURL());
138 return;
139 }
140 }
141
142 public static boolean containsXmppUri(String body) {
143 Matcher xmppPatternMatcher = Patterns.XMPP_PATTERN.matcher(body);
144 if (xmppPatternMatcher.find()) {
145 try {
146 return new XmppUri(body.substring(xmppPatternMatcher.start(), xmppPatternMatcher.end())).isValidJid();
147 } catch (Exception e) {
148 return false;
149 }
150 }
151 return false;
152 }
153
154 public static boolean copyTextToClipboard(Context context, String text, int labelResId) {
155 ClipboardManager mClipBoardManager = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
156 String label = context.getResources().getString(labelResId);
157 if (mClipBoardManager != null) {
158 ClipData mClipData = ClipData.newPlainText(label, text);
159 mClipBoardManager.setPrimaryClip(mClipData);
160 return true;
161 }
162 return false;
163 }
164
165 public static String getLinkScheme(final SpannableStringBuilder body) {
166 MyLinkify.addLinks(body, false);
167 for (final String url : MyLinkify.extractLinks(body)) {
168 final Uri uri = Uri.parse(url);
169 if ("xmpp".equals(uri.getScheme())) {
170 return uri.getScheme();
171 } else {
172 return "http";
173 }
174 }
175 return null;
176 }
177}