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