Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to open location in another app #1396

Merged
merged 6 commits into from
May 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="geo" />
</intent-filter>
</activity>
<activity
android:name=".settings.SettingsActivity"
Expand Down
72 changes: 64 additions & 8 deletions app/src/main/java/de/westnordost/streetcomplete/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import android.graphics.Rect;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Bundle;
import android.os.IBinder;
import androidx.annotation.AnyThread;
Expand Down Expand Up @@ -46,10 +47,14 @@
import android.widget.TextView;
import android.widget.Toast;

import com.mapzen.tangram.LngLat;

import java.util.Collection;
import java.util.List;
import java.util.Random;
import java.util.concurrent.FutureTask;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.inject.Inject;

Expand Down Expand Up @@ -91,6 +96,7 @@
import de.westnordost.streetcomplete.tangram.MapControlsFragment;
import de.westnordost.streetcomplete.tangram.MapFragment;
import de.westnordost.streetcomplete.tangram.QuestsMapFragment;
import de.westnordost.streetcomplete.tangram.TangramConst;
import de.westnordost.streetcomplete.tools.CrashReportExceptionHandler;
import de.westnordost.streetcomplete.util.DpUtil;
import de.westnordost.streetcomplete.util.SlippyMapMath;
Expand Down Expand Up @@ -247,6 +253,39 @@ public void onServiceDisconnected(ComponentName className)
{
questController.deleteOld();
}

Intent intent = getIntent();
Uri data = intent.getData();
if (Intent.ACTION_VIEW.equals(intent.getAction()) && data != null)
{
if ("geo".equals(data.getScheme()))
{
String geoUriRegex = "geo:(-?[0-9]*\\.?[0-9]+),(-?[0-9]*\\.?[0-9]+).*?(?:\\?z=([0-9]*\\.?[0-9]+))?";
westnordost marked this conversation as resolved.
Show resolved Hide resolved
Pattern pattern = Pattern.compile(geoUriRegex);
Matcher matcher = pattern.matcher(data.toString());
if (matcher.matches())
{
double latitude = Double.parseDouble(matcher.group(1));
double longitude = Double.parseDouble(matcher.group(2));

float zoom = -1;
if (matcher.group(3) != null) {
zoom = Float.valueOf(matcher.group(3));
}
westnordost marked this conversation as resolved.
Show resolved Hide resolved

if (longitude >= -180 && longitude <= +180
&& latitude >= -90 && latitude <= +90)
{
mapFragment.setPosition(new LngLat(longitude, latitude));
}

if (zoom != -1 && zoom > 0)
{
mapFragment.setZoom(zoom);
}
}
}
}
}

@Override public void onStart()
Expand Down Expand Up @@ -300,10 +339,10 @@ public void onServiceDisconnected(ComponentName className)
super.onPause();
questAutoSyncer.onPause();

LatLon pos = mapFragment.getPosition();
LngLat pos = mapFragment.getPosition();
prefs.edit()
.putLong(Prefs.MAP_LATITUDE, Double.doubleToRawLongBits(pos.getLatitude()))
.putLong(Prefs.MAP_LONGITUDE, Double.doubleToRawLongBits(pos.getLongitude()))
.putLong(Prefs.MAP_LATITUDE, Double.doubleToRawLongBits(pos.latitude))
.putLong(Prefs.MAP_LONGITUDE, Double.doubleToRawLongBits(pos.longitude))
.apply();
}

Expand Down Expand Up @@ -416,6 +455,23 @@ private void confirmUndo(final OsmQuest quest)
if(isConnected()) downloadDisplayedArea();
else Toast.makeText(this, R.string.offline, Toast.LENGTH_SHORT).show();
return true;
case R.id.action_open_location:
LngLat position = mapFragment.getPosition();
float zoom = mapFragment.getZoom();

Uri uri = Uri.parse(String.format("geo:%f,%f?z=%f",
position.latitude,
position.longitude,
zoom
));

intent = new Intent(Intent.ACTION_VIEW, uri);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
westnordost marked this conversation as resolved.
Show resolved Hide resolved
} else {
Toast.makeText(this, R.string.map_application_missing, Toast.LENGTH_LONG).show();
}
return true;
}

return super.onOptionsItemSelected(item);
Expand Down Expand Up @@ -496,10 +552,10 @@ private void downloadAreaConfirmed(BoundingBox bbox)
// below a certain threshold, it does not make sense to download, so let's enlarge it
if (areaInSqKm < ApplicationConstants.MIN_DOWNLOADABLE_AREA_IN_SQKM)
{
LatLon pos = mapFragment.getPosition();
LngLat pos = mapFragment.getPosition();
if (pos != null)
{
bbox = SphericalEarthMath.enclosingBoundingBox(pos,
bbox = SphericalEarthMath.enclosingBoundingBox(TangramConst.toLatLon(pos),
ApplicationConstants.MIN_DOWNLOADABLE_RADIUS_IN_METERS);
}
}
Expand Down Expand Up @@ -762,7 +818,7 @@ private void showQuestSolvedAnimation(Quest quest, String source)
int size = (int) DpUtil.toPx(42, this);
int[] offset = new int[2];
mapFragment.getView().getLocationOnScreen(offset);
PointF startPos = mapFragment.getPointOf(quest.getCenter());
PointF startPos = mapFragment.getPointOf(TangramConst.toLngLat(quest.getCenter()));
startPos.x += offset[0] - size/2;
startPos.y += offset[1] - size*1.5;
showMarkerSolvedAnimation(quest.getType().getIcon(), startPos, source);
Expand Down Expand Up @@ -833,9 +889,9 @@ private void composeNote()
PointF notePosition = new PointF(screenPosition);
notePosition.offset(-mapPosition[0], -mapPosition[1]);

LatLon position = mapFragment.getPositionAt(notePosition);
LngLat position = mapFragment.getPositionAt(notePosition);
if(position == null) throw new NullPointerException();
questController.createNote(note, imagePaths, position);
questController.createNote(note, imagePaths, TangramConst.toLatLon(position));
triggerAutoUploadByUserInteraction();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -654,16 +654,44 @@ public void setMapOrientation(float rotation, float tilt)
onMapOrientation(rotation, tilt);
}

public float getRotation()
public LngLat getPositionAt(PointF pointF)
{
return controller != null ? controller.getRotation() : 0;
LngLat pos = controller.screenPositionToLngLat(pointF);
if(pos == null) return null;
return pos;
}

public PointF getPointOf(LngLat pos)
{
return controller.lngLatToScreenPosition(pos);
}

public LngLat getPosition()
{
if(controller == null) return null;
return controller.getPosition();
}

public void setPosition(LngLat position)
westnordost marked this conversation as resolved.
Show resolved Hide resolved
{
controller.setPosition(position);
}

public float getZoom()
{
return controller.getZoom();
}

public void setZoom(float zoom)
{
controller.setZoom(zoom);
}

public float getRotation()
{
return controller != null ? controller.getRotation() : 0;
}

public void showMapControls()
{
if(mapControls != null) mapControls.showControls();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ public BoundingBox getDisplayedArea(Rect offset)
// dealing with tilt: this method is just not defined if the tilt is above a certain limit
if(controller.getTilt() > Math.PI / 4f) return null; // 45°

LatLon[] positions = new LatLon[4];
LngLat[] positions = new LngLat[4];
positions[0] = getPositionAt(new PointF(offset.left, offset.top));
positions[1] = getPositionAt(new PointF(offset.left + size.x ,offset.top));
positions[2] = getPositionAt(new PointF(offset.left, offset.top + size.y));
Expand All @@ -492,11 +492,11 @@ public BoundingBox getDisplayedArea(Rect offset)
// be our bounding box

Double latMin = null, lonMin = null, latMax = null, lonMax = null;
for (LatLon position : positions)
for (LngLat position : positions)
{
if(position == null) return null;
double lat = position.getLatitude();
double lon = position.getLongitude();
double lat = position.latitude;
double lon = position.longitude;

if (latMin == null || latMin > lat) latMin = lat;
if (latMax == null || latMax < lat) latMax = lat;
Expand All @@ -506,22 +506,4 @@ public BoundingBox getDisplayedArea(Rect offset)

return new BoundingBox(latMin, lonMin, latMax, lonMax);
}

public LatLon getPositionAt(PointF pointF)
{
LngLat pos = controller.screenPositionToLngLat(pointF);
if(pos == null) return null;
return TangramConst.toLatLon(pos);
}

public LatLon getPosition()
{
if(controller == null) return null;
return TangramConst.toLatLon(controller.getPosition());
}

public PointF getPointOf(LatLon pos)
{
return controller.lngLatToScreenPosition(TangramConst.toLngLat(pos));
}
}
6 changes: 4 additions & 2 deletions app/src/main/res/menu/menu_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
android:icon="@drawable/ic_undo_white_24dp" android:orderInCategory="1" app:showAsAction="ifRoom" />
<item android:id="@+id/action_download" android:title="@string/action_download"
android:orderInCategory="2" app:showAsAction="never" />
<item android:id="@+id/action_open_location" android:title="@string/action_open_location"
android:orderInCategory="3" app:showAsAction="never" />
<item android:id="@+id/action_settings" android:title="@string/action_settings"
android:orderInCategory="3" app:showAsAction="never" />
<item android:id="@+id/action_about" android:title="@string/action_about"
android:orderInCategory="4" app:showAsAction="never" />
<item android:id="@+id/action_about" android:title="@string/action_about"
android:orderInCategory="5" app:showAsAction="never" />

</menu>
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -650,4 +650,6 @@ Otherwise, you can download another keyboard in the app store. Popular keyboards
<string name="theme_dark">Dark</string>
<string name="theme_system_default">System default</string>
<string name="quest_accessible_for_pedestrians_separate_sidewalk_explanation">This street was tagged as having no sidewalk on either side. In the case that there is a sidewalk after all but it is displayed as a separate way, please answer \"sidewalk\".</string>
<string name="action_open_location">Open location in another app</string>
<string name="map_application_missing">No other map application installed</string>
</resources>