2015-03-07 13:15:38 +00:00
|
|
|
package eu.siacs.conversations.utils;
|
|
|
|
|
2018-05-01 10:03:35 +00:00
|
|
|
import android.app.Activity;
|
2018-05-01 10:55:11 +00:00
|
|
|
import android.content.Context;
|
2015-03-07 13:15:38 +00:00
|
|
|
import android.content.Intent;
|
2018-05-01 10:03:35 +00:00
|
|
|
import android.content.SharedPreferences;
|
2015-03-07 13:15:38 +00:00
|
|
|
import android.net.Uri;
|
2018-05-01 10:03:35 +00:00
|
|
|
import android.preference.PreferenceManager;
|
2015-03-07 13:15:38 +00:00
|
|
|
|
|
|
|
import java.io.UnsupportedEncodingException;
|
|
|
|
import java.net.URLEncoder;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.regex.Matcher;
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
2018-05-01 10:03:35 +00:00
|
|
|
import eu.siacs.conversations.R;
|
2016-05-15 09:08:00 +00:00
|
|
|
import eu.siacs.conversations.entities.Contact;
|
2015-03-07 13:15:38 +00:00
|
|
|
import eu.siacs.conversations.entities.Conversation;
|
2018-04-26 11:22:31 +00:00
|
|
|
import eu.siacs.conversations.entities.Conversational;
|
2015-03-07 13:15:38 +00:00
|
|
|
import eu.siacs.conversations.entities.Message;
|
2018-05-01 10:03:35 +00:00
|
|
|
import eu.siacs.conversations.ui.ShareLocationActivity;
|
2018-05-01 10:55:11 +00:00
|
|
|
import eu.siacs.conversations.ui.ShowLocationActivity;
|
2015-03-07 13:15:38 +00:00
|
|
|
|
|
|
|
public class GeoHelper {
|
2018-05-01 10:03:35 +00:00
|
|
|
|
|
|
|
private static final String SHARE_LOCATION_PACKAGE_NAME = "eu.siacs.conversations.location.request";
|
2018-05-01 10:55:11 +00:00
|
|
|
private static final String SHOW_LOCATION_PACKAGE_NAME = "eu.siacs.conversations.location.show";
|
2018-05-01 10:03:35 +00:00
|
|
|
|
2016-05-26 21:26:38 +00:00
|
|
|
public static Pattern GEO_URI = Pattern.compile("geo:([\\-0-9.]+),([\\-0-9.]+)(?:,([\\-0-9.]+))?(?:\\?(.*))?", Pattern.CASE_INSENSITIVE);
|
2015-03-07 13:15:38 +00:00
|
|
|
|
2018-05-01 10:55:11 +00:00
|
|
|
public static boolean isLocationPluginInstalled(Context context) {
|
|
|
|
return new Intent(SHARE_LOCATION_PACKAGE_NAME).resolveActivity(context.getPackageManager()) != null;
|
2018-05-01 10:03:35 +00:00
|
|
|
}
|
2017-08-03 11:24:41 +00:00
|
|
|
|
2018-05-01 10:55:11 +00:00
|
|
|
public static boolean isLocationPluginInstalledAndDesired(Context context) {
|
|
|
|
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
|
|
|
|
final boolean configured = preferences.getBoolean("use_share_location_plugin", context.getResources().getBoolean(R.bool.use_share_location_plugin));
|
|
|
|
return configured && isLocationPluginInstalled(context);
|
2018-05-01 10:03:35 +00:00
|
|
|
}
|
|
|
|
|
2018-05-01 10:55:11 +00:00
|
|
|
public static Intent getFetchIntent(Context context) {
|
|
|
|
if (isLocationPluginInstalledAndDesired(context)) {
|
2018-05-01 10:03:35 +00:00
|
|
|
return new Intent(SHARE_LOCATION_PACKAGE_NAME);
|
|
|
|
} else {
|
2018-05-01 10:55:11 +00:00
|
|
|
return new Intent(context, ShareLocationActivity.class);
|
2018-05-01 10:03:35 +00:00
|
|
|
}
|
|
|
|
}
|
2015-03-07 13:15:38 +00:00
|
|
|
|
2018-05-01 10:55:11 +00:00
|
|
|
public static ArrayList<Intent> createGeoIntentsFromMessage(Context context, Message message) {
|
2015-04-13 16:18:25 +00:00
|
|
|
final ArrayList<Intent> intents = new ArrayList<>();
|
2015-03-07 13:15:38 +00:00
|
|
|
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;
|
|
|
|
}
|
2018-04-26 11:22:31 +00:00
|
|
|
final Conversational conversation = message.getConversation();
|
2015-03-07 13:15:38 +00:00
|
|
|
String label;
|
2018-04-26 11:22:31 +00:00
|
|
|
if (conversation instanceof Conversation && conversation.getMode() == Conversation.MODE_SINGLE && message.getStatus() == Message.STATUS_RECEIVED) {
|
2015-03-07 13:15:38 +00:00
|
|
|
try {
|
2018-04-26 11:22:31 +00:00
|
|
|
label = "(" + URLEncoder.encode(((Conversation)conversation).getName().toString(), "UTF-8") + ")";
|
2015-03-07 13:15:38 +00:00
|
|
|
} catch (UnsupportedEncodingException e) {
|
|
|
|
label = "";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
label = "";
|
|
|
|
}
|
|
|
|
|
2018-05-01 10:55:11 +00:00
|
|
|
if (isLocationPluginInstalledAndDesired(context)) {
|
|
|
|
Intent locationPluginIntent = new Intent(SHOW_LOCATION_PACKAGE_NAME);
|
|
|
|
locationPluginIntent.putExtra("latitude", latitude);
|
|
|
|
locationPluginIntent.putExtra("longitude", longitude);
|
|
|
|
if (message.getStatus() != Message.STATUS_RECEIVED) {
|
|
|
|
locationPluginIntent.putExtra("jid", conversation.getAccount().getJid().toString());
|
|
|
|
locationPluginIntent.putExtra("name", conversation.getAccount().getJid().getLocal());
|
2016-05-15 09:08:00 +00:00
|
|
|
} else {
|
2018-05-01 10:55:11 +00:00
|
|
|
Contact contact = message.getContact();
|
|
|
|
if (contact != null) {
|
|
|
|
locationPluginIntent.putExtra("name", contact.getDisplayName());
|
|
|
|
locationPluginIntent.putExtra("jid", contact.getJid().toString());
|
|
|
|
} else {
|
|
|
|
locationPluginIntent.putExtra("name", UIHelper.getDisplayedMucCounterpart(message.getCounterpart()));
|
|
|
|
}
|
2015-05-20 13:43:45 +00:00
|
|
|
}
|
2018-05-01 10:55:11 +00:00
|
|
|
intents.add(locationPluginIntent);
|
|
|
|
} else {
|
|
|
|
Intent intent = new Intent(context, ShowLocationActivity.class);
|
2018-05-01 12:32:55 +00:00
|
|
|
intent.setAction(SHOW_LOCATION_PACKAGE_NAME);
|
2018-05-01 10:55:11 +00:00
|
|
|
intent.putExtra("latitude", latitude);
|
|
|
|
intent.putExtra("longitude", longitude);
|
|
|
|
intents.add(intent);
|
2015-03-07 13:15:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|