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