GeoHelper.java

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