GeoHelper.java

  1package eu.siacs.conversations.utils;
  2
  3import android.app.Activity;
  4import android.content.Context;
  5import android.content.Intent;
  6import android.content.SharedPreferences;
  7import android.net.Uri;
  8import android.preference.PreferenceManager;
  9
 10import java.io.UnsupportedEncodingException;
 11import java.net.URLEncoder;
 12import java.util.ArrayList;
 13import java.util.regex.Matcher;
 14import java.util.regex.Pattern;
 15
 16import eu.siacs.conversations.R;
 17import eu.siacs.conversations.entities.Contact;
 18import eu.siacs.conversations.entities.Conversation;
 19import eu.siacs.conversations.entities.Conversational;
 20import eu.siacs.conversations.entities.Message;
 21import eu.siacs.conversations.ui.ShareLocationActivity;
 22import eu.siacs.conversations.ui.ShowLocationActivity;
 23
 24public class GeoHelper {
 25
 26	private static final String SHARE_LOCATION_PACKAGE_NAME = "eu.siacs.conversations.location.request";
 27	private static final String SHOW_LOCATION_PACKAGE_NAME = "eu.siacs.conversations.location.show";
 28
 29	public static Pattern GEO_URI = Pattern.compile("geo:(-?\\d+(?:\\.\\d+)?),(-?\\d+(?:\\.\\d+)?)(?:,-?\\d+(?:\\.\\d+)?)?(?:;crs=[\\w-]+)?(?:;u=\\d+(?:\\.\\d+)?)?(?:;[\\w-]+=(?:[\\w-_.!~*'()]|%[\\da-f][\\da-f])+)*", Pattern.CASE_INSENSITIVE);
 30
 31	public static boolean isLocationPluginInstalled(Context context) {
 32		return new Intent(SHARE_LOCATION_PACKAGE_NAME).resolveActivity(context.getPackageManager()) != null;
 33	}
 34
 35	public static boolean isLocationPluginInstalledAndDesired(Context context) {
 36		SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
 37		final boolean configured = preferences.getBoolean("use_share_location_plugin", context.getResources().getBoolean(R.bool.use_share_location_plugin));
 38		return configured && isLocationPluginInstalled(context);
 39	}
 40
 41	public static Intent getFetchIntent(Context context) {
 42		if (isLocationPluginInstalledAndDesired(context)) {
 43			return new Intent(SHARE_LOCATION_PACKAGE_NAME);
 44		} else {
 45			return new Intent(context, ShareLocationActivity.class);
 46		}
 47	}
 48
 49	public static ArrayList<Intent> createGeoIntentsFromMessage(Context context, Message message) {
 50		final ArrayList<Intent> intents = new ArrayList<>();
 51		Matcher matcher = GEO_URI.matcher(message.getBody());
 52		if (!matcher.matches()) {
 53			return intents;
 54		}
 55		double latitude;
 56		double longitude;
 57		try {
 58			latitude = Double.parseDouble(matcher.group(1));
 59			if (latitude > 90.0 || latitude < -90.0) {
 60				return intents;
 61			}
 62			longitude = Double.parseDouble(matcher.group(2));
 63			if (longitude > 180.0 || longitude < -180.0) {
 64				return intents;
 65			}
 66		} catch (NumberFormatException nfe) {
 67			return intents;
 68		}
 69		final Conversational conversation = message.getConversation();
 70		String label;
 71		if (conversation instanceof Conversation && conversation.getMode() == Conversation.MODE_SINGLE && message.getStatus() == Message.STATUS_RECEIVED) {
 72			try {
 73				label = "(" + URLEncoder.encode(((Conversation)conversation).getName().toString(), "UTF-8") + ")";
 74			} catch (UnsupportedEncodingException e) {
 75				label = "";
 76			}
 77		} else {
 78			label = "";
 79		}
 80
 81		if (isLocationPluginInstalledAndDesired(context)) {
 82			Intent locationPluginIntent = new Intent(SHOW_LOCATION_PACKAGE_NAME);
 83			locationPluginIntent.putExtra("latitude", latitude);
 84			locationPluginIntent.putExtra("longitude", longitude);
 85			if (message.getStatus() != Message.STATUS_RECEIVED) {
 86				locationPluginIntent.putExtra("jid", conversation.getAccount().getJid().toString());
 87				locationPluginIntent.putExtra("name", conversation.getAccount().getJid().getLocal());
 88			} else {
 89				Contact contact = message.getContact();
 90				if (contact != null) {
 91					locationPluginIntent.putExtra("name", contact.getDisplayName());
 92					locationPluginIntent.putExtra("jid", contact.getJid().toString());
 93				} else {
 94					locationPluginIntent.putExtra("name", UIHelper.getDisplayedMucCounterpart(message.getCounterpart()));
 95				}
 96			}
 97			intents.add(locationPluginIntent);
 98		} else {
 99			Intent intent = new Intent(context, ShowLocationActivity.class);
100			intent.setAction(SHOW_LOCATION_PACKAGE_NAME);
101			intent.putExtra("latitude", latitude);
102			intent.putExtra("longitude", longitude);
103			intents.add(intent);
104		}
105
106		Intent geoIntent = new Intent(Intent.ACTION_VIEW);
107		geoIntent.setData(Uri.parse("geo:" + String.valueOf(latitude) + "," + String.valueOf(longitude) + "?q=" + String.valueOf(latitude) + "," + String.valueOf(longitude) + label));
108		intents.add(geoIntent);
109
110		Intent httpIntent = new Intent(Intent.ACTION_VIEW);
111		httpIntent.setData(Uri.parse("https://maps.google.com/maps?q=loc:"+String.valueOf(latitude) + "," + String.valueOf(longitude) +label));
112		intents.add(httpIntent);
113		return intents;
114	}
115}