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;
35import androidx.annotation.StringRes;
36import com.google.common.collect.Iterables;
37import eu.siacs.conversations.R;
38import eu.siacs.conversations.entities.DownloadableFile;
39import eu.siacs.conversations.entities.Message;
40import eu.siacs.conversations.persistance.FileBackend;
41import eu.siacs.conversations.ui.ConversationsActivity;
42import eu.siacs.conversations.ui.XmppActivity;
43import java.util.Arrays;
44import java.util.Collection;
45
46public class ShareUtil {
47
48 private static final Collection<String> SCHEMES_COPY_PATH_ONLY =
49 Arrays.asList("xmpp", "mailto", "tel");
50
51 public static void share(XmppActivity activity, Message message) {
52 Intent shareIntent = new Intent();
53 shareIntent.setAction(Intent.ACTION_SEND);
54 if (message.isGeoUri()) {
55 shareIntent.putExtra(Intent.EXTRA_TEXT, message.getBody());
56 shareIntent.setType("text/plain");
57 } else if (!message.isFileOrImage()) {
58 shareIntent.putExtra(Intent.EXTRA_TEXT, message.getBody());
59 shareIntent.setType("text/plain");
60 shareIntent.putExtra(
61 ConversationsActivity.EXTRA_AS_QUOTE,
62 message.getStatus() == Message.STATUS_RECEIVED);
63 } else {
64 final DownloadableFile file =
65 activity.xmppConnectionService.getFileBackend().getFile(message);
66 try {
67 shareIntent.putExtra(
68 Intent.EXTRA_STREAM, FileBackend.getUriForFile(activity, file));
69 } catch (SecurityException e) {
70 Toast.makeText(
71 activity,
72 activity.getString(
73 R.string.no_permission_to_access_x, file.getAbsolutePath()),
74 Toast.LENGTH_SHORT)
75 .show();
76 return;
77 }
78 shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
79 String mime = message.getMimeType();
80 if (mime == null) {
81 mime = "*/*";
82 }
83 shareIntent.setType(mime);
84 }
85 try {
86 activity.startActivity(
87 Intent.createChooser(shareIntent, activity.getText(R.string.share_with)));
88 } catch (ActivityNotFoundException e) {
89 // This should happen only on faulty androids because normally chooser is always
90 // available
91 Toast.makeText(activity, R.string.no_application_found_to_open_file, Toast.LENGTH_SHORT)
92 .show();
93 }
94 }
95
96 public static void copyToClipboard(XmppActivity activity, Message message) {
97 if (activity.copyTextToClipboard(message.getBody(), R.string.message)) {
98 Toast.makeText(activity, R.string.message_copied_to_clipboard, Toast.LENGTH_SHORT)
99 .show();
100 }
101 }
102
103 public static void copyUrlToClipboard(XmppActivity activity, Message message) {
104 final String url;
105 final int resId;
106 if (message.isGeoUri()) {
107 resId = R.string.location;
108 url = message.getBody();
109 } else if (message.hasFileOnRemoteHost()) {
110 resId = R.string.file_url;
111 url = message.getFileParams().url;
112 } else {
113 final Message.FileParams fileParams = message.getFileParams();
114 url =
115 (fileParams != null && fileParams.url != null)
116 ? fileParams.url
117 : message.getBody().trim();
118 resId = R.string.file_url;
119 }
120 if (activity.copyTextToClipboard(url, resId)) {
121 Toast.makeText(activity, R.string.url_copied_to_clipboard, Toast.LENGTH_SHORT).show();
122 }
123 }
124
125 public static void copyLinkToClipboard(final XmppActivity activity, final Message message) {
126 final var firstUri = Iterables.getFirst(MyLinkify.getLinks(message.getBody()), null);
127 if (firstUri == null) {
128 return;
129 }
130 final String clip;
131 if (SCHEMES_COPY_PATH_ONLY.contains(firstUri.getScheme())) {
132 clip = firstUri.getPath();
133 } else {
134 clip = firstUri.getRaw();
135 }
136 final @StringRes int label =
137 switch (firstUri.getScheme()) {
138 case "http", "https", "gemini" -> R.string.web_address;
139 case "xmpp" -> R.string.account_settings_jabber_id;
140 default -> R.string.uri;
141 };
142 final @StringRes int toast =
143 switch (firstUri.getScheme()) {
144 case "http", "https", "gemini", "web+ap" -> R.string.url_copied_to_clipboard;
145 case "xmpp" -> R.string.jabber_id_copied_to_clipboard;
146 case "tel" -> R.string.copied_phone_number;
147 case "mailto" -> R.string.copied_email_address;
148 default -> R.string.uri_copied_to_clipboard;
149 };
150 if (activity.copyTextToClipboard(clip, label)) {
151 Toast.makeText(activity, toast, Toast.LENGTH_SHORT).show();
152 }
153 }
154}