aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/thedevstack/conversationsplus/utils/GeoHelper.java
blob: b4be2d35186c1d4fa38ae58be8090d3a62983fac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package de.thedevstack.conversationsplus.utils;

import android.content.Intent;
import android.net.Uri;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import de.thedevstack.conversationsplus.entities.Conversation;
import de.thedevstack.conversationsplus.entities.Message;

public class GeoHelper {
	private static Pattern GEO_URI = Pattern.compile("geo:([\\-0-9.]+),([\\-0-9.]+)(?:,([\\-0-9.]+))?(?:\\?(.*))?", Pattern.CASE_INSENSITIVE);

	public static boolean isGeoUri(String body) {
		return body != null && GEO_URI.matcher(body).matches();
	}

	public static ArrayList<Intent> createGeoIntentsFromMessage(Message message) {
		final ArrayList<Intent> intents = new ArrayList<>();
		Matcher matcher = GEO_URI.matcher(message.getBody());
		if (!matcher.matches()) {
			return intents;
		}
		double latitude;
		double longitude;
		try {
			latitude = Double.parseDouble(matcher.group(1));
			if (latitude > 90.0 || latitude < -90.0) {
				return intents;
			}
			longitude = Double.parseDouble(matcher.group(2));
			if (longitude > 180.0 || longitude < -180.0) {
				return intents;
			}
		} catch (NumberFormatException nfe) {
			return intents;
		}
		final Conversation conversation = message.getConversation();
		String label;
		if (conversation.getMode() == Conversation.MODE_SINGLE && MessageUtil.isIncomingMessage(message)) {
			try {
				label = "(" + URLEncoder.encode(message.getConversation().getName(), "UTF-8") + ")";
			} catch (UnsupportedEncodingException e) {
				label = "";
			}
		} else {
			label = "";
		}

		Intent locationPluginIntent = new Intent("eu.siacs.conversations.location.show");
		locationPluginIntent.putExtra("latitude",latitude);
		locationPluginIntent.putExtra("longitude",longitude);
		if (conversation.getMode() == Conversation.MODE_SINGLE) {
			if (MessageUtil.isIncomingMessage(message)) {
				locationPluginIntent.putExtra("name",conversation.getName());
				locationPluginIntent.putExtra("jid",message.getCounterpart().toString());
			}
			else {
				locationPluginIntent.putExtra("jid",conversation.getAccount().getJid().toString());
			}
		}
		intents.add(locationPluginIntent);

		Intent geoIntent = new Intent(Intent.ACTION_VIEW);
		geoIntent.setData(Uri.parse("geo:" + String.valueOf(latitude) + "," + String.valueOf(longitude) + "?q=" + String.valueOf(latitude) + "," + String.valueOf(longitude) + label));
		intents.add(geoIntent);

		Intent httpIntent = new Intent(Intent.ACTION_VIEW);
		httpIntent.setData(Uri.parse("https://maps.google.com/maps?q=loc:"+String.valueOf(latitude) + "," + String.valueOf(longitude) +label));
		intents.add(httpIntent);
		return intents;
	}
}