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