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.Intent;
34import android.widget.Toast;
35
36import java.util.regex.Matcher;
37
38import eu.siacs.conversations.R;
39import eu.siacs.conversations.entities.DownloadableFile;
40import eu.siacs.conversations.entities.Message;
41import eu.siacs.conversations.persistance.FileBackend;
42import eu.siacs.conversations.ui.ConversationsActivity;
43import eu.siacs.conversations.ui.XmppActivity;
44import eu.siacs.conversations.utils.Patterns;
45import eu.siacs.conversations.utils.XmppUri;
46import eu.siacs.conversations.xmpp.Jid;
47
48public class ShareUtil {
49
50 public static void share(XmppActivity activity, Message message) {
51 Intent shareIntent = new Intent();
52 shareIntent.setAction(Intent.ACTION_SEND);
53 if (message.isGeoUri()) {
54 shareIntent.putExtra(Intent.EXTRA_TEXT, message.getBody());
55 shareIntent.setType("text/plain");
56 } else if (!message.isFileOrImage()) {
57 shareIntent.putExtra(Intent.EXTRA_TEXT, message.getMergedBody().toString());
58 shareIntent.setType("text/plain");
59 shareIntent.putExtra(ConversationsActivity.EXTRA_AS_QUOTE, message.getStatus() == Message.STATUS_RECEIVED);
60 } else {
61 final DownloadableFile file = activity.xmppConnectionService.getFileBackend().getFile(message);
62 try {
63 shareIntent.putExtra(Intent.EXTRA_STREAM, FileBackend.getUriForFile(activity, file));
64 } catch (SecurityException e) {
65 Toast.makeText(activity, activity.getString(R.string.no_permission_to_access_x, file.getAbsolutePath()), Toast.LENGTH_SHORT).show();
66 return;
67 }
68 shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
69 String mime = message.getMimeType();
70 if (mime == null) {
71 mime = "*/*";
72 }
73 shareIntent.setType(mime);
74 }
75 try {
76 activity.startActivity(Intent.createChooser(shareIntent, activity.getText(R.string.share_with)));
77 } catch (ActivityNotFoundException e) {
78 //This should happen only on faulty androids because normally chooser is always available
79 Toast.makeText(activity, R.string.no_application_found_to_open_file, Toast.LENGTH_SHORT).show();
80 }
81 }
82
83 public static void copyToClipboard(XmppActivity activity, Message message) {
84 if (activity.copyTextToClipboard(message.getMergedBody().toString(), R.string.message)) {
85 Toast.makeText(activity, R.string.message_copied_to_clipboard, Toast.LENGTH_SHORT).show();
86 }
87 }
88
89 public static void copyUrlToClipboard(XmppActivity activity, Message message) {
90 final String url;
91 final int resId;
92 if (message.isGeoUri()) {
93 resId = R.string.location;
94 url = message.getBody();
95 } else if (message.hasFileOnRemoteHost()) {
96 resId = R.string.file_url;
97 url = message.getFileParams().url.toString();
98 } else {
99 url = message.getBody().trim();
100 resId = R.string.file_url;
101 }
102 if (activity.copyTextToClipboard(url, resId)) {
103 Toast.makeText(activity, R.string.url_copied_to_clipboard, Toast.LENGTH_SHORT).show();
104 }
105 }
106
107 public static void copyLinkToClipboard(XmppActivity activity, Message message) {
108 String body = message.getMergedBody().toString();
109 Matcher xmppPatternMatcher = Patterns.XMPP_PATTERN.matcher(body);
110 if (xmppPatternMatcher.find()) {
111 try {
112 Jid jid = new XmppUri(body.substring(xmppPatternMatcher.start(), xmppPatternMatcher.end())).getJid();
113 if (activity.copyTextToClipboard(jid.asBareJid().toString(), R.string.account_settings_jabber_id)) {
114 Toast.makeText(activity,R.string.jabber_id_copied_to_clipboard, Toast.LENGTH_SHORT).show();
115 }
116 return;
117 } catch (Exception e) {
118 e.printStackTrace();
119 return;
120 }
121 }
122 Matcher webUrlPatternMatcher = Patterns.AUTOLINK_WEB_URL.matcher(body);
123 if (webUrlPatternMatcher.find()) {
124 String url = body.substring(webUrlPatternMatcher.start(),webUrlPatternMatcher.end());
125 if (activity.copyTextToClipboard(url,R.string.web_address)) {
126 Toast.makeText(activity,R.string.url_copied_to_clipboard, Toast.LENGTH_SHORT).show();
127 }
128 }
129 }
130
131 public static boolean containsXmppUri(String body) {
132 Matcher xmppPatternMatcher = Patterns.XMPP_PATTERN.matcher(body);
133 if (xmppPatternMatcher.find()) {
134 try {
135 return new XmppUri(body.substring(xmppPatternMatcher.start(), xmppPatternMatcher.end())).isValidJid();
136 } catch (Exception e) {
137 return false;
138 }
139 }
140 return false;
141 }
142}