[android] #4800 - default location view from start: introduced attributes, update MapboxMapOptions, added tests. updated MockParcel
This commit is contained in:
@@ -140,7 +140,7 @@
|
||||
<activity
|
||||
android:name=".activity.userlocation.MyLocationCustomisationActivity"
|
||||
android:description="@string/description_user_location_customization"
|
||||
android:label="@string/activity_user_tracking_mode">
|
||||
android:label="@string/activity_user_tracking_customization">
|
||||
<meta-data
|
||||
android:name="@string/category"
|
||||
android:value="@string/category_userlocation" />
|
||||
|
||||
@@ -4,17 +4,15 @@ import android.graphics.Color;
|
||||
import android.location.Location;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.design.widget.Snackbar;
|
||||
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 android.view.ViewGroup;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.mapbox.mapboxsdk.camera.CameraPosition;
|
||||
import com.mapbox.mapboxsdk.camera.CameraUpdateFactory;
|
||||
import com.mapbox.mapboxsdk.constants.Style;
|
||||
import com.mapbox.mapboxsdk.geometry.LatLng;
|
||||
import com.mapbox.mapboxsdk.location.LocationListener;
|
||||
@@ -23,16 +21,14 @@ import com.mapbox.mapboxsdk.maps.MapView;
|
||||
import com.mapbox.mapboxsdk.maps.MapboxMap;
|
||||
import com.mapbox.mapboxsdk.maps.MapboxMapOptions;
|
||||
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
|
||||
import com.mapbox.mapboxsdk.maps.widgets.MyLocationViewSettings;
|
||||
import com.mapbox.mapboxsdk.testapp.R;
|
||||
|
||||
public class MyLocationCustomisationActivity extends AppCompatActivity implements LocationListener, OnMapReadyCallback {
|
||||
public class MyLocationCustomisationActivity extends AppCompatActivity implements LocationListener {
|
||||
|
||||
private MapView mMapView;
|
||||
private MapboxMap mMapboxMap;
|
||||
private Location mLocation;
|
||||
|
||||
private Bundle savedState;
|
||||
private MapView mapView;
|
||||
private MapboxMap mapboxMap;
|
||||
private Location location;
|
||||
private boolean firstRun;
|
||||
|
||||
@Override
|
||||
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
||||
@@ -47,114 +43,77 @@ public class MyLocationCustomisationActivity extends AppCompatActivity implement
|
||||
actionBar.setDisplayShowHomeEnabled(true);
|
||||
}
|
||||
|
||||
savedState = savedInstanceState;
|
||||
findViewById(R.id.progress).setVisibility(View.GONE);
|
||||
location = LocationServices.getLocationServices(this).getLastLocation();
|
||||
|
||||
LocationServices locationServices = LocationServices.getLocationServices(this);
|
||||
locationServices.addLocationListener(this);
|
||||
locationServices.toggleGPS(true);
|
||||
MapboxMapOptions mapboxMapOptions = new MapboxMapOptions();
|
||||
mapboxMapOptions.accessToken(getString(R.string.mapbox_access_token));
|
||||
mapboxMapOptions.styleUrl(Style.MAPBOX_STREETS);
|
||||
mapboxMapOptions.locationEnabled(true);
|
||||
mapboxMapOptions.camera(new CameraPosition.Builder()
|
||||
.target(location != null ? new LatLng(location) : new LatLng(0, 0))
|
||||
.zoom(11)
|
||||
.tilt(25)
|
||||
.build());
|
||||
|
||||
mapboxMapOptions.myLocationForegroundTintColor(Color.GREEN);
|
||||
mapboxMapOptions.myLocationBackgroundTintColor(Color.YELLOW);
|
||||
// mapboxMapOptions.myLocationBackgroundPadding(new int[]{32, 32, 0, 0});
|
||||
|
||||
// mapboxMapOptions.myLocationAccuracyTint(Color.RED);
|
||||
// mapboxMapOptions.myLocationAccuracyAlpha(155);
|
||||
|
||||
mapView = new MapView(this, mapboxMapOptions);
|
||||
ViewGroup parent = (ViewGroup) findViewById(R.id.container);
|
||||
parent.addView(mapView);
|
||||
|
||||
mapView.onCreate(savedInstanceState);
|
||||
mapView.getMapAsync(new OnMapReadyCallback() {
|
||||
@Override
|
||||
public void onMapReady(MapboxMap map) {
|
||||
mapboxMap = map;
|
||||
}
|
||||
});
|
||||
|
||||
LocationServices.getLocationServices(this).addLocationListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLocationChanged(Location location) {
|
||||
if (location != null) {
|
||||
if (mMapView == null) {
|
||||
findViewById(R.id.progress).setVisibility(View.GONE);
|
||||
|
||||
MapboxMapOptions mapboxMapOptions = new MapboxMapOptions();
|
||||
mapboxMapOptions.accessToken(getString(R.string.mapbox_access_token));
|
||||
mapboxMapOptions.styleUrl(Style.MAPBOX_STREETS);
|
||||
mapboxMapOptions.locationEnabled(true);
|
||||
mapboxMapOptions.camera(new CameraPosition.Builder()
|
||||
.target(new LatLng(location))
|
||||
.zoom(11)
|
||||
.tilt(25)
|
||||
.build());
|
||||
|
||||
mMapView = new MapView(this, mapboxMapOptions);
|
||||
((ViewGroup) findViewById(R.id.container)).addView(mMapView);
|
||||
|
||||
mMapView.onCreate(savedState);
|
||||
mMapView.getMapAsync(this);
|
||||
mMapView.onResume();
|
||||
mLocation = location;
|
||||
}
|
||||
if (mapboxMap != null && firstRun) {
|
||||
mapboxMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location), 10));
|
||||
firstRun = false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMapReady(MapboxMap mapboxMap) {
|
||||
MyLocationViewSettings locationViewSettings = mapboxMap.getMyLocationViewSettings();
|
||||
|
||||
locationViewSettings.setBackgroundTintColor(Color.YELLOW);
|
||||
locationViewSettings.setBackgroundOffset(32, 32, 0, 0);
|
||||
|
||||
locationViewSettings.setForegroundTintColor(Color.GREEN);
|
||||
|
||||
locationViewSettings.setAccuracyTintColor(Color.RED);
|
||||
locationViewSettings.setAccuracyAlpha(100);
|
||||
}
|
||||
|
||||
private void showSnackBar() {
|
||||
String desc = "Loc Chg: ";
|
||||
boolean noInfo = true;
|
||||
if (mLocation.hasSpeed()) {
|
||||
desc += String.format("Spd = %.1f km/h ", mLocation.getSpeed() * 3.6f);
|
||||
noInfo = false;
|
||||
}
|
||||
if (mLocation.hasAltitude()) {
|
||||
desc += String.format("Alt = %.0f m ", mLocation.getAltitude());
|
||||
noInfo = false;
|
||||
}
|
||||
if (noInfo) {
|
||||
desc += "No extra info";
|
||||
}
|
||||
Snackbar.make(findViewById(android.R.id.content), desc, Snackbar.LENGTH_SHORT).show();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
if (mMapView != null) {
|
||||
mMapView.onResume();
|
||||
}
|
||||
mapView.onResume();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPause() {
|
||||
super.onPause();
|
||||
if (mMapView != null) {
|
||||
mMapView.onPause();
|
||||
}
|
||||
mapView.onPause();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onSaveInstanceState(Bundle outState) {
|
||||
super.onSaveInstanceState(outState);
|
||||
if (mMapView != null) {
|
||||
mMapView.onSaveInstanceState(outState);
|
||||
}
|
||||
mapView.onSaveInstanceState(outState);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
super.onDestroy();
|
||||
if (mMapView != null) {
|
||||
mMapView.onDestroy();
|
||||
}
|
||||
mapView.onDestroy();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLowMemory() {
|
||||
super.onLowMemory();
|
||||
if (mMapView != null) {
|
||||
mMapView.onLowMemory();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCreateOptionsMenu(Menu menu) {
|
||||
getMenuInflater().inflate(R.menu.menu_tracking, menu);
|
||||
return true;
|
||||
mapView.onLowMemory();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -163,12 +122,6 @@ public class MyLocationCustomisationActivity extends AppCompatActivity implement
|
||||
case android.R.id.home:
|
||||
onBackPressed();
|
||||
return true;
|
||||
case R.id.action_toggle_dismissible_tracking:
|
||||
boolean state = !item.isChecked();
|
||||
mMapboxMap.getTrackingSettings().setDismissTrackingOnGesture(state);
|
||||
Toast.makeText(this, "Dismiss tracking mode on gesture = " + state, Toast.LENGTH_SHORT).show();
|
||||
item.setChecked(state);
|
||||
return true;
|
||||
default:
|
||||
return super.onOptionsItemSelected(item);
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@ import com.mapbox.mapboxsdk.camera.CameraUpdateFactory;
|
||||
import com.mapbox.mapboxsdk.constants.MyBearingTracking;
|
||||
import com.mapbox.mapboxsdk.constants.MyLocationTracking;
|
||||
import com.mapbox.mapboxsdk.geometry.LatLng;
|
||||
import com.mapbox.mapboxsdk.location.LocationServices;
|
||||
import com.mapbox.mapboxsdk.maps.MapView;
|
||||
import com.mapbox.mapboxsdk.maps.MapboxMap;
|
||||
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
|
||||
@@ -28,6 +29,8 @@ import com.mapbox.mapboxsdk.maps.TrackingSettings;
|
||||
import com.mapbox.mapboxsdk.maps.widgets.MyLocationViewSettings;
|
||||
import com.mapbox.mapboxsdk.testapp.R;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
public class MyLocationTrackingModeActivity extends AppCompatActivity implements MapboxMap.OnMyLocationChangeListener, AdapterView.OnItemSelectedListener {
|
||||
|
||||
private MapView mMapView;
|
||||
@@ -58,7 +61,7 @@ public class MyLocationTrackingModeActivity extends AppCompatActivity implements
|
||||
mMapboxMap = mapboxMap;
|
||||
|
||||
// disable dismissal when a gesture occurs
|
||||
mMapboxMap.getTrackingSettings().setDismissTrackingOnGesture(false);
|
||||
mMapboxMap.getTrackingSettings().setDismissLocationTrackingOnGesture(false);
|
||||
mMapboxMap.getTrackingSettings().setDismissBearingTrackingOnGesture(false);
|
||||
|
||||
mapboxMap.setOnMyLocationChangeListener(MyLocationTrackingModeActivity.this);
|
||||
@@ -75,7 +78,6 @@ public class MyLocationTrackingModeActivity extends AppCompatActivity implements
|
||||
mBearingSpinner.setAdapter(bearingTrackingAdapter);
|
||||
mBearingSpinner.setOnItemSelectedListener(MyLocationTrackingModeActivity.this);
|
||||
|
||||
|
||||
mapboxMap.setOnMyLocationTrackingModeChangeListener(new MapboxMap.OnMyLocationTrackingModeChangeListener() {
|
||||
@Override
|
||||
public void onMyLocationTrackingModeChange(@MyLocationTracking.Mode int myLocationTrackingMode) {
|
||||
@@ -97,19 +99,29 @@ public class MyLocationTrackingModeActivity extends AppCompatActivity implements
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
mLocation = LocationServices.getLocationServices(MyLocationTrackingModeActivity.this).getLastLocation();
|
||||
if(mLocation!=null){
|
||||
setInitialPosition(new LatLng(mLocation));
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void setInitialPosition(LatLng latLng){
|
||||
mMapboxMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 14));
|
||||
mMapboxMap.setMyLocationEnabled(true);
|
||||
mLocationSpinner.setEnabled(true);
|
||||
mBearingSpinner.setEnabled(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMyLocationChange(@Nullable Location location) {
|
||||
if (location != null) {
|
||||
if (mLocation == null) {
|
||||
// initial location to reposition map
|
||||
mMapboxMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), 14));
|
||||
mMapboxMap.setMyLocationEnabled(true);
|
||||
mLocationSpinner.setEnabled(true);
|
||||
mBearingSpinner.setEnabled(true);
|
||||
setInitialPosition(new LatLng(location));
|
||||
}
|
||||
mLocation = location;
|
||||
showSnackBar();
|
||||
@@ -120,15 +132,15 @@ public class MyLocationTrackingModeActivity extends AppCompatActivity implements
|
||||
String desc = "Loc Chg: ";
|
||||
boolean noInfo = true;
|
||||
if (mLocation.hasSpeed()) {
|
||||
desc += String.format("Spd = %.1f km/h ", mLocation.getSpeed() * 3.6f);
|
||||
desc += String.format(Locale.getDefault(), "Spd = %.1f km/h ", mLocation.getSpeed() * 3.6f);
|
||||
noInfo = false;
|
||||
}
|
||||
if (mLocation.hasAltitude()) {
|
||||
desc += String.format("Alt = %.0f m ", mLocation.getAltitude());
|
||||
desc += String.format(Locale.getDefault(), "Alt = %.0f m ", mLocation.getAltitude());
|
||||
noInfo = false;
|
||||
}
|
||||
if(mLocation.hasAccuracy()){
|
||||
desc += String.format("Acc = %.0f m",mLocation.getAccuracy());
|
||||
if (mLocation.hasAccuracy()) {
|
||||
desc += String.format(Locale.getDefault(), "Acc = %.0f m", mLocation.getAccuracy());
|
||||
}
|
||||
|
||||
if (noInfo) {
|
||||
|
||||
@@ -42,6 +42,9 @@
|
||||
android:id="@+id/mapView"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
app:my_location_foreground_tint="@color/primary"
|
||||
app:my_location_background_tint="@android:color/white"
|
||||
app:my_location_accuracy_tint="@color/primary"
|
||||
app:access_token="@string/mapbox_access_token"
|
||||
app:style_url="@string/style_mapbox_streets"
|
||||
app:zoom="15" />
|
||||
|
||||
@@ -7,6 +7,7 @@ import com.mapbox.mapboxsdk.camera.CameraPosition;
|
||||
import com.mapbox.mapboxsdk.constants.MapboxConstants;
|
||||
import com.mapbox.mapboxsdk.constants.Style;
|
||||
import com.mapbox.mapboxsdk.geometry.LatLng;
|
||||
import com.mapbox.mapboxsdk.utils.MockParcel;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
@@ -167,6 +168,23 @@ public class MapboxMapOptionsTest {
|
||||
assertNull(new MapboxMapOptions().getCamera());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMyLocationForegroundTint() {
|
||||
assertEquals(Color.BLUE, new MapboxMapOptions().myLocationForegroundTintColor(Color.BLUE).getMyLocationForegroundTintColor());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMyLocationBackgroundTint() {
|
||||
assertEquals(Color.BLUE, new MapboxMapOptions().myLocationBackgroundTintColor(Color.BLUE).getMyLocationBackgroundTintColor());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParceable() {
|
||||
MapboxMapOptions options = new MapboxMapOptions().camera(new CameraPosition.Builder().build()).styleUrl("s").accessToken("a").debugActive(true).compassMargins(new int[]{0, 1, 2, 3});
|
||||
MapboxMapOptions parceled = (MapboxMapOptions) MockParcel.obtain(options);
|
||||
assertEquals(options, parceled);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAccessToken() {
|
||||
assertNull(new MapboxMapOptions().getAccessToken());
|
||||
|
||||
@@ -23,13 +23,13 @@ public class MyLocationViewSettingsTest {
|
||||
MapView mMapView = mock(MapView.class);
|
||||
|
||||
@InjectMocks
|
||||
UserLocationView userLocationView = mock(UserLocationView.class);
|
||||
MyLocationView myLocationView = mock(MyLocationView.class);
|
||||
|
||||
MyLocationViewSettings locationViewSettings;
|
||||
|
||||
@Before
|
||||
public void beforeTest() {
|
||||
locationViewSettings = new MyLocationViewSettings(mMapView, userLocationView);
|
||||
locationViewSettings = new MyLocationViewSettings(mMapView, myLocationView);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -18,6 +18,7 @@ import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Matchers.anyByte;
|
||||
import static org.mockito.Matchers.anyDouble;
|
||||
import static org.mockito.Matchers.anyFloat;
|
||||
import static org.mockito.Matchers.anyInt;
|
||||
import static org.mockito.Matchers.anyLong;
|
||||
import static org.mockito.Matchers.anyString;
|
||||
@@ -51,9 +52,8 @@ public class MockParcel {
|
||||
Method myMethod = creatorClass.getDeclaredMethod("createFromParcel", Parcel.class);
|
||||
return (Parcelable) myMethod.invoke(fieldValue, parcel);
|
||||
} catch (Exception e) {
|
||||
assertNotNull("Exception occurred:" + e.getLocalizedMessage(), null);
|
||||
return null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void testParcelableArray(@NonNull Parcelable object) {
|
||||
@@ -139,15 +139,39 @@ public class MockParcel {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
Answer<Void> writeIntArrayAnswer = new Answer<Void>() {
|
||||
@Override
|
||||
public Void answer(InvocationOnMock invocation) throws Throwable {
|
||||
int[] parameters = (int[]) invocation.getArguments()[0];
|
||||
if (parameters != null) {
|
||||
objects.add(parameters.length);
|
||||
for (Object o : parameters) {
|
||||
objects.add(o);
|
||||
}
|
||||
} else {
|
||||
objects.add(-1);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
};
|
||||
doAnswer(writeValueAnswer).when(mockedParcel).writeByte(anyByte());
|
||||
doAnswer(writeValueAnswer).when(mockedParcel).writeLong(anyLong());
|
||||
doAnswer(writeValueAnswer).when(mockedParcel).writeString(anyString());
|
||||
doAnswer(writeValueAnswer).when(mockedParcel).writeInt(anyInt());
|
||||
doAnswer(writeIntArrayAnswer).when(mockedParcel).writeIntArray(any(int[].class));
|
||||
doAnswer(writeValueAnswer).when(mockedParcel).writeDouble(anyDouble());
|
||||
doAnswer(writeValueAnswer).when(mockedParcel).writeFloat(anyFloat());
|
||||
doAnswer(writeValueAnswer).when(mockedParcel).writeParcelable(any(Parcelable.class), eq(0));
|
||||
doAnswer(writeArrayAnswer).when(mockedParcel).writeParcelableArray(any(Parcelable[].class), eq(0));
|
||||
}
|
||||
|
||||
private void setupReads() {
|
||||
when(mockedParcel.readInt()).then(new Answer<Integer>() {
|
||||
@Override
|
||||
public Integer answer(InvocationOnMock invocation) throws Throwable {
|
||||
return (Integer) objects.get(position++);
|
||||
}
|
||||
});
|
||||
when(mockedParcel.readByte()).thenAnswer(new Answer<Byte>() {
|
||||
@Override
|
||||
public Byte answer(InvocationOnMock invocation) throws Throwable {
|
||||
@@ -172,6 +196,12 @@ public class MockParcel {
|
||||
return (Double) objects.get(position++);
|
||||
}
|
||||
});
|
||||
when(mockedParcel.readFloat()).thenAnswer(new Answer<Float>() {
|
||||
@Override
|
||||
public Float answer(InvocationOnMock invocation) throws Throwable {
|
||||
return (Float) objects.get(position++);
|
||||
}
|
||||
});
|
||||
when(mockedParcel.readParcelable(Parcelable.class.getClassLoader())).thenAnswer(new Answer<Parcelable>() {
|
||||
@Override
|
||||
public Parcelable answer(InvocationOnMock invocation) throws Throwable {
|
||||
@@ -194,6 +224,22 @@ public class MockParcel {
|
||||
return array;
|
||||
}
|
||||
});
|
||||
when(mockedParcel.createIntArray()).then(new Answer<int[]>() {
|
||||
@Override
|
||||
public int[] answer(InvocationOnMock invocation) throws Throwable {
|
||||
int size = (Integer) objects.get(position++);
|
||||
if (size == -1) {
|
||||
return null;
|
||||
}
|
||||
|
||||
int[] array = new int[size];
|
||||
for (int i = 0; i < size; i++) {
|
||||
array[i] = (Integer) objects.get(position++);
|
||||
}
|
||||
|
||||
return array;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void setupOthers() {
|
||||
|
||||
Reference in New Issue
Block a user