[android] #2809 - addPolylines and removeAnnotations in JNI, added feature activity + sanity test case

This commit is contained in:
Tobrun
2015-10-27 11:16:55 +01:00
parent fd33dfcd73
commit f3ffb45cf6
26 changed files with 364 additions and 7 deletions

View File

@@ -1757,12 +1757,20 @@ public final class MapView extends FrameLayout {
throw new NullPointerException("polylineOptionsList is null");
}
// TODO make faster in JNI
int count = polylineOptionsList.size();
List<Polyline> polylines = new ArrayList<>(count);
for (PolylineOptions options : polylineOptionsList) {
polylines.add(options.getPolyline());
}
long[] ids = mNativeMapView.addPolylines(polylines);
Polyline p;
for (int i = 0; i < count; i++) {
PolylineOptions polylineOptions = polylineOptionsList.get(i);
polylines.add(addPolyline(polylineOptions));
p = polylines.get(i);
p.setId(ids[i]);
p.setMapView(this);
mAnnotations.add(p);
}
return new ArrayList<>(polylines);
@@ -1847,17 +1855,17 @@ public final class MapView extends FrameLayout {
* @param annotationList A list of annotation objects to remove.
*/
@UiThread
public void removeAnnotations(@NonNull List<Annotation> annotationList) {
public void removeAnnotations(@NonNull List<? extends Annotation> annotationList) {
if (annotationList == null) {
throw new NullPointerException("annotationList is null");
}
// TODO make faster in JNI
int count = annotationList.size();
long[] ids = new long[count];
for (int i = 0; i < count; i++) {
Annotation annotation = annotationList.get(i);
removeAnnotation(annotation);
ids[i] = annotationList.get(i).getId();
}
mNativeMapView.removeAnnotations(ids);
}
/**

View File

@@ -361,6 +361,10 @@ final class NativeMapView {
return nativeAddPolyline(mNativeMapViewPtr, polyline);
}
public long[] addPolylines(List<Polyline> polylines) {
return nativeAddPolylines(mNativeMapViewPtr, polylines);
}
public long addPolygon(Polygon polygon) {
return nativeAddPolygon(mNativeMapViewPtr, polygon);
}
@@ -584,6 +588,8 @@ final class NativeMapView {
private native long nativeAddPolyline(long nativeMapViewPtr, Polyline polyline);
private native long[] nativeAddPolylines(long mNativeMapViewPtr, List<Polyline> polygon);
private native long nativeAddPolygon(long mNativeMapViewPtr, Polygon polygon);
private native long[] nativeAddPolygons(long mNativeMapViewPtr, List<Polygon> polygon);

View File

@@ -0,0 +1,34 @@
package com.mapbox.mapboxsdk.testapp;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.test.suitebuilder.annotation.LargeTest;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
/**
* Tests on PolyLineActivity
*/
@RunWith(AndroidJUnit4.class)
@LargeTest
public class PolylineActivityTest extends BaseTest {
@Rule
public ActivityTestRule<PolylineActivity> mActivityRule = new ActivityTestRule<>(
PolylineActivity.class);
private PolylineActivity mActivity = null;
@Before
public void setActivity() {
mActivity = mActivityRule.getActivity();
}
@Test
public void testSanity() {
checkViewIsDisplayed(R.id.mapView);
}
}

View File

@@ -52,6 +52,10 @@
android:name=".MyLocationTrackingModeActivity"
android:label="@string/activity_user_tracking_mode" />
<activity
android:name=".PolylineActivity"
android:label="@string/activity_polyline" />
<meta-data
android:name="io.fabric.ApiKey"
android:value="9724157045ff7d083492c6d9ae03e60e8609d461" />

View File

@@ -343,6 +343,10 @@ public class MainActivity extends AppCompatActivity {
startActivity(new Intent(getApplicationContext(), MyLocationTrackingModeActivity.class));
return true;
case R.id.action_polyline:
startActivity(new Intent(getApplicationContext(), PolylineActivity.class));
return true;
default:
return changeMapStyle(menuItem.getItemId());
}

View File

@@ -0,0 +1,168 @@
package com.mapbox.mapboxsdk.testapp;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import com.mapbox.mapboxsdk.annotations.Polyline;
import com.mapbox.mapboxsdk.annotations.PolylineOptions;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.utils.ApiAccess;
import com.mapbox.mapboxsdk.views.MapView;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class PolylineActivity extends AppCompatActivity {
private List<Polyline> mPolyLines;
private MapView mMapView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_polyline);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
}
mMapView = (MapView) findViewById(R.id.mapView);
mMapView.setAccessToken(ApiAccess.getToken(this));
mMapView.onCreate(savedInstanceState);
mPolyLines = mMapView.addPolylines(PolylineProvider.getAll());
findViewById(R.id.fab).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mPolyLines != null && mPolyLines.size() > 0) {
if (mPolyLines.size() == 1) {
// test for removing annotation
mMapView.removeAnnotation(mPolyLines.get(0));
} else {
// test for removing annotations
mMapView.removeAnnotations(mPolyLines);
}
}
mPolyLines = mMapView.addPolylines(PolylineProvider.getRandomLine());
}
});
}
@Override
protected void onStart() {
super.onStart();
mMapView.onStart();
}
@Override
public void onResume() {
super.onResume();
mMapView.onResume();
}
@Override
public void onPause() {
super.onPause();
mMapView.onPause();
}
@Override
protected void onStop() {
super.onStop();
mMapView.onStop();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mMapView.onSaveInstanceState(outState);
}
@Override
protected void onDestroy() {
super.onDestroy();
mMapView.onDestroy();
}
@Override
public void onLowMemory() {
super.onLowMemory();
mMapView.onLowMemory();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_polyline, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_id_remove:
// test to remove all annotations
mMapView.removeAllAnnotations();
return true;
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private static class PolylineProvider {
private static final LatLng ANDORRA = new LatLng(42.505777, 1.52529);
private static final LatLng LUXEMBOURG = new LatLng(49.815273, 6.129583);
private static final LatLng MONACO = new LatLng(43.738418, 7.424616);
private static final LatLng VATICAN_CITY = new LatLng(41.902916, 12.453389);
private static final LatLng SAN_MARINO = new LatLng(43.942360, 12.457777);
private static final LatLng LIECHTENSTEIN = new LatLng(47.166000, 9.555373);
private static final List<PolylineOptions> POLYLINES = new ArrayList<PolylineOptions>() {{
add(generatePolyline(ANDORRA, LUXEMBOURG, "#F44336"));
add(generatePolyline(ANDORRA, MONACO, "#FF5722"));
add(generatePolyline(MONACO, VATICAN_CITY, "#673AB7"));
add(generatePolyline(VATICAN_CITY, SAN_MARINO, "#009688"));
add(generatePolyline(SAN_MARINO, LIECHTENSTEIN, "#795548"));
add(generatePolyline(LIECHTENSTEIN, LUXEMBOURG, "#3F51B5"));
}};
private static PolylineOptions generatePolyline(LatLng start, LatLng end, String color) {
PolylineOptions line = new PolylineOptions();
line.add(start);
line.add(end);
line.color(Color.parseColor(color));
return line;
}
public static List<PolylineOptions> getAll() {
return POLYLINES;
}
public static List<PolylineOptions> getRandomLine() {
List<PolylineOptions> randomLines = new ArrayList<>();
Collections.shuffle(POLYLINES);
randomLines.add(POLYLINES.get(0));
return randomLines;
}
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 124 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 161 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 227 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 86 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 115 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 179 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 108 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 151 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 243 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 114 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 194 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 367 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 119 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 243 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 408 B

View File

@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/primary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
<com.mapbox.mapboxsdk.views.MapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/toolbar"
app:center_latitude="47.798202"
app:center_longitude="7.573781"
app:style_url="@string/style_mapbox_streets"
app:zoom_level="4" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_gravity="end|bottom"
android:layout_margin="@dimen/fab_margin"
android:src="@drawable/ic_add_black_24dp"
app:backgroundTint="@android:color/white" />
</RelativeLayout>

View File

@@ -108,6 +108,12 @@
android:icon="@drawable/ic_gps_fixed_white_24dp"
android:title="@string/activity_user_tracking_mode" />
<item
android:id="@+id/action_polyline"
android:checkable="false"
android:icon="@drawable/ic_trending_up_white_24dp"
android:title="@string/activity_polyline"/>
</menu>
</item>

View File

@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:mapbox="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_id_remove"
android:icon="@drawable/ic_delete_white_24dp"
android:title="@string/action_remove_polylines"
mapbox:showAsAction="ifRoom" />
</menu>

View File

@@ -11,6 +11,7 @@
<string name="activity_info_window">InfoWindow Activity</string>
<string name="activity_visible_coordinate_bounds">Visible Coordinate Bounds</string>
<string name="activity_user_tracking_mode">User tracking mode</string>
<string name="activity_polyline">Polyline Activity</string>
<string name="navdrawer_menu_title_mainactivity_controls">Main Activity Controls</string>
<string name="navdrawer_menu_title_mainactivity_styles">Main Activity Styles</string>
@@ -27,6 +28,7 @@
<string name="action_add_bulk_markers">Add Markers in bulk</string>
<string name="action_visible_bounds">Set Visible Bounds</string>
<string name="action_visible_bounds_explanation">Center map around 2 markers</string>
<string name="action_remove_polylines">Remove polylines</string>
<string name="label_fps">FPS:</string>

View File

@@ -959,6 +959,83 @@ jlong JNICALL nativeAddPolyline(JNIEnv *env, jobject obj, jlong nativeMapViewPtr
return id;
}
jlongArray JNICALL nativeAddPolylines(JNIEnv *env, jobject obj, jlong nativeMapViewPtr, jobject jlist) {
mbgl::Log::Debug(mbgl::Event::JNI, "nativeAddPolylines");
assert(nativeMapViewPtr != 0);
NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr);
std::vector<mbgl::ShapeAnnotation> shapes;
if (jlist == nullptr) {
if (env->ThrowNew(nullPointerExceptionClass, "List cannot be null.") < 0) {
env->ExceptionDescribe();
return nullptr;
}
return nullptr;
}
jobjectArray jarray =
reinterpret_cast<jobjectArray>(env->CallObjectMethod(jlist, listToArrayId));
if (env->ExceptionCheck() || (jarray == nullptr)) {
env->ExceptionDescribe();
return nullptr;
}
jsize len = env->GetArrayLength(jarray);
if (len < 0) {
env->ExceptionDescribe();
return nullptr;
}
shapes.reserve(len);
for (jsize i = 0; i < len; i++) {
jobject polyline = reinterpret_cast<jobject>(env->GetObjectArrayElement(jarray, i));
jfloat alpha = env->GetFloatField(polyline, polylineAlphaId);
if (env->ExceptionCheck()) {
env->ExceptionDescribe();
return nullptr;
}
jint color = env->GetIntField(polyline, polylineColorId);
if (env->ExceptionCheck()) {
env->ExceptionDescribe();
return nullptr;
}
int r = (color >> 16) & 0xFF;
int g = (color >> 8) & 0xFF;
int b = (color) & 0xFF;
int a = (color >> 24) & 0xFF;
jfloat width = env->GetFloatField(polyline, polylineWidthId);
if (env->ExceptionCheck()) {
env->ExceptionDescribe();
return nullptr;
}
mbgl::ShapeAnnotation::Properties shapeProperties;
mbgl::LinePaintProperties lineProperties;
lineProperties.opacity = alpha;
lineProperties.color = {{ static_cast<float>(r) / 255.0f, static_cast<float>(g) / 255.0f, static_cast<float>(b) / 255.0f, static_cast<float>(a) / 255.0f }};
lineProperties.width = width;
shapeProperties.set<mbgl::LinePaintProperties>(lineProperties);
jobject points = env->GetObjectField(polyline, polylinePointsId);
mbgl::AnnotationSegment segment = annotation_segment_from_latlng_jlist(env, points);
shapes.emplace_back(mbgl::AnnotationSegments { segment }, shapeProperties);
env->DeleteLocalRef(polyline);
}
env->DeleteLocalRef(jarray);
std::vector<uint32_t> shapeAnnotationIDs = nativeMapView->getMap().addShapeAnnotations(shapes);
return std_vector_uint_to_jobject(env, shapeAnnotationIDs);
}
jlong JNICALL nativeAddPolygon(JNIEnv *env, jobject obj, jlong nativeMapViewPtr, jobject polygon) {
mbgl::Log::Debug(mbgl::Event::JNI, "nativeAddPolygon");
assert(nativeMapViewPtr != 0);
@@ -1852,6 +1929,8 @@ extern "C" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
reinterpret_cast<void *>(&nativeAddMarkers)},
{"nativeAddPolyline", "(JLcom/mapbox/mapboxsdk/annotations/Polyline;)J",
reinterpret_cast<void *>(&nativeAddPolyline)},
{"nativeAddPolylines", "(JLjava/util/List;)[J",
reinterpret_cast<void *>(&nativeAddPolylines)},
{"nativeAddPolygon", "(JLcom/mapbox/mapboxsdk/annotations/Polygon;)J",
reinterpret_cast<void *>(&nativeAddPolygon)},
{"nativeAddPolygons", "(JLjava/util/List;)[J",