summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLangston Smith <langston.smith@mapbox.com>2019-09-25 09:41:02 -0700
committerGitHub <noreply@github.com>2019-09-25 09:41:02 -0700
commit60c44d8fa88bc6e9ca057e77ee36f88483e34f14 (patch)
tree44399fdeb13fd6ddef443f8d35ebb14647aeab96
parentc11fbb1786a298870898123ef9248444123e6763 (diff)
downloadqtlocation-mapboxgl-60c44d8fa88bc6e9ca057e77ee36f88483e34f14.tar.gz
[android] cherry pick of snapshot padding (15623) and feedback form (15699) to ristretto (#15714)
* [android] Migrate Android SDK to GL JS–powered feedback form (#15623) * [android] Fix the crash when CameraPosition padding is set. (#15699)
-rw-r--r--platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/AttributionDialogManager.java62
-rw-r--r--platform/android/MapboxGLAndroidSDK/src/test/java/com/mapbox/mapboxsdk/maps/AttributionDialogManagerTest.java99
-rw-r--r--platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/snapshot/MapSnapshotterActivity.java1
-rw-r--r--platform/android/src/map/camera_position.cpp21
4 files changed, 160 insertions, 23 deletions
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/AttributionDialogManager.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/AttributionDialogManager.java
index 4f523948da..5384a29fca 100644
--- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/AttributionDialogManager.java
+++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/AttributionDialogManager.java
@@ -26,6 +26,8 @@ import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
/**
* Responsible for managing attribution interactions on the map.
@@ -36,9 +38,10 @@ import java.util.Set;
* </p>
*/
public class AttributionDialogManager implements View.OnClickListener, DialogInterface.OnClickListener {
-
private static final String MAP_FEEDBACK_URL = "https://apps.mapbox.com/feedback";
- private static final String MAP_FEEDBACK_LOCATION_FORMAT = MAP_FEEDBACK_URL + "/#/%f/%f/%d";
+ private static final String MAP_FEEDBACK_URL_OLD = "https://www.mapbox.com/map-feedback";
+ private static final String MAP_FEEDBACK_URL_LOCATION_FRAGMENT_FORMAT = "/%f/%f/%f/%f/%d";
+ private static final String MAP_FEEDBACK_STYLE_URI_REGEX = "^(.*://[^:^/]*)/(.*)/(.*)";
@NonNull
private final Context context;
@@ -90,7 +93,7 @@ public class AttributionDialogManager implements View.OnClickListener, DialogInt
if (isLatestEntry(which)) {
showTelemetryDialog();
} else {
- showMapFeedbackWebPage(which);
+ showMapAttributionWebPage(which);
}
}
@@ -138,21 +141,54 @@ public class AttributionDialogManager implements View.OnClickListener, DialogInt
builder.show();
}
- private void showMapFeedbackWebPage(int which) {
+ private void showMapAttributionWebPage(int which) {
Attribution[] attributions = attributionSet.toArray(new Attribution[attributionSet.size()]);
String url = attributions[which].getUrl();
- if (url.contains(MAP_FEEDBACK_URL)) {
- url = buildMapFeedbackMapUrl(mapboxMap.getCameraPosition());
+ if (url.contains(MAP_FEEDBACK_URL_OLD) || url.contains(MAP_FEEDBACK_URL)) {
+ url = buildMapFeedbackMapUrl(Mapbox.getAccessToken());
}
showWebPage(url);
}
@NonNull
- private String buildMapFeedbackMapUrl(@Nullable CameraPosition cameraPosition) {
- // appends current location to the map feedback url if available
- return cameraPosition != null ? String.format(Locale.getDefault(),
- MAP_FEEDBACK_LOCATION_FORMAT, cameraPosition.target.getLongitude(), cameraPosition.target.getLatitude(),
- (int) cameraPosition.zoom) : MAP_FEEDBACK_URL;
+ String buildMapFeedbackMapUrl(@Nullable String accessToken) {
+ // TODO Add Android Maps SDK version to the query parameter, currently the version API is not available.
+ // TODO Keep track of this issue at [#15632](https://github.com/mapbox/mapbox-gl-native/issues/15632)
+
+ Uri.Builder builder = Uri.parse(MAP_FEEDBACK_URL).buildUpon();
+
+ CameraPosition cameraPosition = mapboxMap.getCameraPosition();
+ if (cameraPosition != null) {
+ builder.encodedFragment(String.format(Locale.getDefault(), MAP_FEEDBACK_URL_LOCATION_FRAGMENT_FORMAT,
+ cameraPosition.target.getLongitude(), cameraPosition.target.getLatitude(),
+ cameraPosition.zoom, cameraPosition.bearing, (int) cameraPosition.tilt));
+ }
+
+ String packageName = context.getApplicationContext().getPackageName();
+ if (packageName != null) {
+ builder.appendQueryParameter("referrer", packageName);
+ }
+
+ if (accessToken != null) {
+ builder.appendQueryParameter("access_token", accessToken);
+ }
+
+ Style style = mapboxMap.getStyle();
+ if (style != null) {
+ String styleUri = style.getUri();
+ Pattern pattern = Pattern.compile(MAP_FEEDBACK_STYLE_URI_REGEX);
+ Matcher matcher = pattern.matcher(styleUri);
+
+ if (matcher.find()) {
+ String styleOwner = matcher.group(2);
+ String styleId = matcher.group(3);
+
+ builder.appendQueryParameter("owner", styleOwner)
+ .appendQueryParameter("id", styleId);
+ }
+ }
+
+ return builder.build().toString();
}
private void showWebPage(@NonNull String url) {
@@ -189,10 +225,10 @@ public class AttributionDialogManager implements View.OnClickListener, DialogInt
Style style = mapboxMap.getStyle();
if (style != null) {
- for (Source source : mapboxMap.getStyle().getSources()) {
+ for (Source source : style.getSources()) {
attribution = source.getAttribution();
if (!attribution.isEmpty()) {
- attributions.add(source.getAttribution());
+ attributions.add(attribution);
}
}
}
diff --git a/platform/android/MapboxGLAndroidSDK/src/test/java/com/mapbox/mapboxsdk/maps/AttributionDialogManagerTest.java b/platform/android/MapboxGLAndroidSDK/src/test/java/com/mapbox/mapboxsdk/maps/AttributionDialogManagerTest.java
new file mode 100644
index 0000000000..9b4eeb27aa
--- /dev/null
+++ b/platform/android/MapboxGLAndroidSDK/src/test/java/com/mapbox/mapboxsdk/maps/AttributionDialogManagerTest.java
@@ -0,0 +1,99 @@
+package com.mapbox.mapboxsdk.maps;
+
+import android.content.Context;
+
+import com.mapbox.mapboxsdk.camera.CameraPosition;
+import com.mapbox.mapboxsdk.geometry.LatLng;
+
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.InjectMocks;
+import org.robolectric.RobolectricTestRunner;
+
+import static org.junit.Assert.assertNotNull;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+@RunWith(RobolectricTestRunner.class)
+public class AttributionDialogManagerTest {
+ @InjectMocks
+ Context context = mock(Context.class);
+
+ @InjectMocks
+ MapboxMap mapboxMap = mock(MapboxMap.class);
+
+ @InjectMocks
+ Style style = mock(Style.class);
+
+ private AttributionDialogManager attributionDialogManager;
+ private CameraPosition cameraPosition;
+
+ private static final String ASSERT_MAPBOX_TOKEN = "TestAccessToken";
+
+ private static final String ASSERT_MAPBOX_STYLE_URI = "mapbox://styles/mapbox/streets-v11";
+ private static final String ASSERT_MAPBOX_LOCAL_STYLE_URI = "asset://style.json";
+
+ private static final String ASSERT_MAPBOX_PACKAGE_NAME = "com.mapbox.attributionmanagertest";
+
+ private static final String ASSERT_MAPBOX_FEEDBACK_FINAL_URL =
+ "https://apps.mapbox.com/feedback?referrer=com.mapbox.attributionmanagertest&"
+ + "access_token=TestAccessToken&owner=mapbox&id=streets-v11"
+ + "#/22.200001/11.100000/12.000000/24.000000/5";
+ private static final String ASSERT_MAPBOX_FEEDHACK_FINAL_URL_LOCAL_STYLE =
+ "https://apps.mapbox.com/feedback?referrer=com.mapbox.attributionmanagertest&"
+ + "access_token=TestAccessToken#/22.200001/11.100000/12.000000/24.000000/5";
+ private static final String ASSERT_MAPBOX_FEEDBACL_FINAL_URL_NULL_CAMERA_POSITION =
+ "https://apps.mapbox.com/feedback?referrer=com.mapbox.attributionmanagertest&access_token=TestAccessToken";
+
+ @Before
+ public void beforeTest() {
+ attributionDialogManager = new AttributionDialogManager(context, mapboxMap);
+ cameraPosition = new CameraPosition.Builder(CameraPosition.DEFAULT)
+ .tilt(5.0f).zoom(12).bearing(24.0f).target(new LatLng(11.1f, 22.2f)).build();
+ }
+
+ @Test
+ public void testSanity() {
+ assertNotNull("AttributionDialogManager should not be null", attributionDialogManager);
+ }
+
+ @Test
+ public void testBuildMapFeedbackMapUrl() {
+ when(context.getApplicationContext()).thenReturn(context);
+ when(context.getPackageName()).thenReturn(ASSERT_MAPBOX_PACKAGE_NAME);
+ when(style.getUri()).thenReturn(ASSERT_MAPBOX_STYLE_URI);
+ when(mapboxMap.getCameraPosition()).thenReturn(cameraPosition);
+ when(mapboxMap.getStyle()).thenReturn(style);
+
+ Assert.assertEquals(ASSERT_MAPBOX_FEEDBACK_FINAL_URL,
+ attributionDialogManager.buildMapFeedbackMapUrl(ASSERT_MAPBOX_TOKEN));
+ }
+
+ @Test
+ public void testBuildMapFeedbackMapUrlWithLocalStyleJson() {
+ when(context.getApplicationContext()).thenReturn(context);
+ when(context.getPackageName()).thenReturn(ASSERT_MAPBOX_PACKAGE_NAME);
+ when(style.getUri()).thenReturn(ASSERT_MAPBOX_LOCAL_STYLE_URI);
+ when(mapboxMap.getCameraPosition()).thenReturn(cameraPosition);
+ when(mapboxMap.getStyle()).thenReturn(style);
+
+ Assert.assertEquals(ASSERT_MAPBOX_FEEDHACK_FINAL_URL_LOCAL_STYLE,
+ attributionDialogManager.buildMapFeedbackMapUrl(ASSERT_MAPBOX_TOKEN));
+ }
+
+ @Test
+ public void testBuildMapFeedbackMapUrlWithNullCameraPosition() {
+ when(context.getApplicationContext()).thenReturn(context);
+ when(context.getPackageName()).thenReturn(ASSERT_MAPBOX_PACKAGE_NAME);
+ when(style.getUri()).thenReturn(ASSERT_MAPBOX_LOCAL_STYLE_URI);
+ when(mapboxMap.getCameraPosition()).thenReturn(null);
+ when(mapboxMap.getStyle()).thenReturn(style);
+
+ Assert.assertEquals(ASSERT_MAPBOX_FEEDBACL_FINAL_URL_NULL_CAMERA_POSITION,
+ attributionDialogManager.buildMapFeedbackMapUrl(ASSERT_MAPBOX_TOKEN));
+ }
+
+
+}
diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/snapshot/MapSnapshotterActivity.java b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/snapshot/MapSnapshotterActivity.java
index 59c708266e..ab7ffed83e 100644
--- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/snapshot/MapSnapshotterActivity.java
+++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/snapshot/MapSnapshotterActivity.java
@@ -89,6 +89,7 @@ public class MapSnapshotterActivity extends AppCompatActivity {
.bearing(randomInRange(0, 360))
.tilt(randomInRange(0, 60))
.zoom(randomInRange(0, 20))
+ .padding(1, 1, 1, 1)
.build()
);
}
diff --git a/platform/android/src/map/camera_position.cpp b/platform/android/src/map/camera_position.cpp
index cbf5f2e408..4266b64fdf 100644
--- a/platform/android/src/map/camera_position.cpp
+++ b/platform/android/src/map/camera_position.cpp
@@ -44,19 +44,20 @@ mbgl::CameraOptions CameraPosition::getCameraOptions(jni::JNIEnv& env, const jni
static auto tilt = javaClass.GetField<jni::jdouble>(env, "tilt");
static auto zoom = javaClass.GetField<jni::jdouble>(env, "zoom");
static auto paddingField = javaClass.GetField<jni::Array<jni::jdouble>>(env, "padding");
- static auto padding = position.Get(env, paddingField);
+ auto padding = position.Get(env, paddingField);
auto center = LatLng::getLatLng(env, position.Get(env, target));
- return mbgl::CameraOptions {
- center,
- padding ? EdgeInsets {padding.Get(env, 1) * pixelRatio, padding.Get(env, 0) * pixelRatio,
- padding.Get(env, 3) * pixelRatio, padding.Get(env, 2) * pixelRatio} : (EdgeInsets) {},
- {},
- position.Get(env, zoom),
- position.Get(env, bearing),
- position.Get(env, tilt)
- };
+ return mbgl::CameraOptions{center,
+ padding && padding.Length(env) == 4 ? EdgeInsets{padding.Get(env, 1) * pixelRatio,
+ padding.Get(env, 0) * pixelRatio,
+ padding.Get(env, 3) * pixelRatio,
+ padding.Get(env, 2) * pixelRatio}
+ : (EdgeInsets){},
+ {},
+ position.Get(env, zoom),
+ position.Get(env, bearing),
+ position.Get(env, tilt)};
}
void CameraPosition::registerNative(jni::JNIEnv &env) {