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.net.Uri;
 35import android.text.SpannableStringBuilder;
 36import android.widget.Toast;
 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 eu.siacs.conversations.utils.XmppUri;
 44import eu.siacs.conversations.xmpp.Jid;
 45
 46public class ShareUtil {
 47
 48    public static void share(XmppActivity activity, Message message) {
 49        Intent shareIntent = new Intent();
 50        shareIntent.setAction(Intent.ACTION_SEND);
 51        if (message.isGeoUri()) {
 52            shareIntent.putExtra(Intent.EXTRA_TEXT, message.getBody());
 53            shareIntent.setType("text/plain");
 54        } else if (!message.isFileOrImage()) {
 55            shareIntent.putExtra(Intent.EXTRA_TEXT, message.getBody());
 56            shareIntent.setType("text/plain");
 57            shareIntent.putExtra(
 58                    ConversationsActivity.EXTRA_AS_QUOTE,
 59                    message.getStatus() == Message.STATUS_RECEIVED);
 60        } else {
 61            final DownloadableFile file =
 62                    activity.xmppConnectionService.getFileBackend().getFile(message);
 63            try {
 64                shareIntent.putExtra(
 65                        Intent.EXTRA_STREAM, FileBackend.getUriForFile(activity, file));
 66            } catch (SecurityException e) {
 67                Toast.makeText(
 68                                activity,
 69                                activity.getString(
 70                                        R.string.no_permission_to_access_x, file.getAbsolutePath()),
 71                                Toast.LENGTH_SHORT)
 72                        .show();
 73                return;
 74            }
 75            shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
 76            String mime = message.getMimeType();
 77            if (mime == null) {
 78                mime = "*/*";
 79            }
 80            shareIntent.setType(mime);
 81        }
 82        try {
 83            activity.startActivity(
 84                    Intent.createChooser(shareIntent, activity.getText(R.string.share_with)));
 85        } catch (ActivityNotFoundException e) {
 86            // This should happen only on faulty androids because normally chooser is always
 87            // available
 88            Toast.makeText(activity, R.string.no_application_found_to_open_file, Toast.LENGTH_SHORT)
 89                    .show();
 90        }
 91    }
 92
 93    public static void copyToClipboard(XmppActivity activity, Message message) {
 94        if (activity.copyTextToClipboard(message.getBody(), R.string.message)) {
 95            Toast.makeText(activity, R.string.message_copied_to_clipboard, Toast.LENGTH_SHORT)
 96                    .show();
 97        }
 98    }
 99
100    public static void copyUrlToClipboard(XmppActivity activity, Message message) {
101        final String url;
102        final int resId;
103        if (message.isGeoUri()) {
104            resId = R.string.location;
105            url = message.getBody();
106        } else if (message.hasFileOnRemoteHost()) {
107            resId = R.string.file_url;
108            url = message.getFileParams().url;
109        } else {
110            final Message.FileParams fileParams = message.getFileParams();
111            url =
112                    (fileParams != null && fileParams.url != null)
113                            ? fileParams.url
114                            : message.getBody().trim();
115            resId = R.string.file_url;
116        }
117        if (activity.copyTextToClipboard(url, resId)) {
118            Toast.makeText(activity, R.string.url_copied_to_clipboard, Toast.LENGTH_SHORT).show();
119        }
120    }
121
122    public static void copyLinkToClipboard(final XmppActivity activity, final Message message) {
123        final SpannableStringBuilder body = new SpannableStringBuilder(message.getBody());
124        for (final String url : MyLinkify.extractLinks(body)) {
125            final Uri uri = Uri.parse(url);
126            if ("xmpp".equals(uri.getScheme())) {
127                try {
128                    final Jid jid = new XmppUri(uri).getJid();
129                    if (activity.copyTextToClipboard(
130                            jid.asBareJid().toString(), R.string.account_settings_jabber_id)) {
131                        Toast.makeText(
132                                        activity,
133                                        R.string.jabber_id_copied_to_clipboard,
134                                        Toast.LENGTH_SHORT)
135                                .show();
136                    }
137                    return;
138                } catch (final Exception e) {
139                    return;
140                }
141            } else {
142                if (activity.copyTextToClipboard(url, R.string.web_address)) {
143                    Toast.makeText(activity, R.string.url_copied_to_clipboard, Toast.LENGTH_SHORT)
144                            .show();
145                }
146                return;
147            }
148        }
149    }
150
151    public static String getLinkScheme(final SpannableStringBuilder body) {
152        MyLinkify.addLinks(body, false);
153        for (final String url : MyLinkify.extractLinks(body)) {
154            final Uri uri = Uri.parse(url);
155            if ("xmpp".equals(uri.getScheme())) {
156                return uri.getScheme();
157            } else {
158                return "http";
159            }
160        }
161        return null;
162    }
163}