ShareUtil.java

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