Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[android] refactoring LocationComponentActivationOptions based on cod…
Browse files Browse the repository at this point in the history
…e review
  • Loading branch information
langsmith committed Feb 19, 2019
1 parent 3f5fbc7 commit d378b2a
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -459,16 +459,15 @@ public void activateLocationComponent(@NonNull Context context, @NonNull Style s
applyStyle(options);
}

/**
* This method initializes the component and needs to be called before any other operations are performed.
* Afterwards, you can manage component's visibility by {@link #setLocationComponentEnabled(boolean)}.
*
* @param locationComponentActivationOptions a fully built {@link LocationComponentActivationOptions} object
*/
public void activateLocationComponent(@NonNull LocationComponentActivationOptions
locationComponentActivationOptions) {

Log.d(TAG, "locationComponentActivationOptions context packageName = " + locationComponentActivationOptions.context().getPackageName());
Log.d(TAG, "locationComponentActivationOptions style URL = " + locationComponentActivationOptions.style().getUrl());
Log.d(TAG, "locationComponentActivationOptions styleRes = " + locationComponentActivationOptions.styleRes());

if (locationComponentActivationOptions.context() != null
&& locationComponentActivationOptions.style() != null
&& locationComponentActivationOptions.locationComponentOptions() != null) {
if (locationComponentActivationOptions.locationComponentOptions() != null) {
initialize(locationComponentActivationOptions.context(), locationComponentActivationOptions.style(),
locationComponentActivationOptions.locationComponentOptions());
}
Expand All @@ -485,24 +484,14 @@ public void activateLocationComponent(@NonNull LocationComponentActivationOption
setLocationEngine(null);
}

// TODO: How to do some sort of null check on an int for locationComponentActivationOptions.styleRes() ?
if (locationComponentActivationOptions.styleRes() <0 &&
locationComponentActivationOptions.locationComponentOptions() == null
&& locationComponentActivationOptions.context() != null) {

applyStyle(LocationComponentOptions.createFromAttributes(locationComponentActivationOptions.context(),
R.style.mapbox_LocationComponent));

} else if (locationComponentActivationOptions.styleRes() <0
&& locationComponentActivationOptions.locationComponentOptions() == null
&& locationComponentActivationOptions.context() != null) {

if (locationComponentActivationOptions.styleRes() == 0 &&
locationComponentActivationOptions.locationComponentOptions() == null) {
applyStyle(LocationComponentOptions.createFromAttributes(
locationComponentActivationOptions.context(), R.style.mapbox_LocationComponent));
} else if (locationComponentActivationOptions.styleRes() != 0) {
applyStyle(LocationComponentOptions.createFromAttributes(locationComponentActivationOptions.context(),
locationComponentActivationOptions.styleRes()));
} else if (locationComponentActivationOptions.styleRes() < 0
&& locationComponentActivationOptions.locationComponentOptions() != null
&& locationComponentActivationOptions.context() != null) {

} else if (locationComponentActivationOptions.locationComponentOptions() != null) {
applyStyle(locationComponentActivationOptions.locationComponentOptions());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,20 @@
import com.mapbox.mapboxsdk.maps.Style;

/**
* A class which holds the various options for activating the Maps SDK's {@link LocationComponent} to eventually
* show the device location on the map.
*
* since 7.2.0
*/
public class LocationComponentActivationOptions {

private Context context;
private Style style;
private LocationEngine locationEngine;
private LocationEngineRequest locationEngineRequest;
private LocationComponentOptions locationComponentOptions;
private int styleRes;
private boolean useDefaultLocationEngine;
private final Context context;
private final Style style;
private final LocationEngine locationEngine;
private final LocationEngineRequest locationEngineRequest;
private final LocationComponentOptions locationComponentOptions;
private final int styleRes;
private final boolean useDefaultLocationEngine;

LocationComponentActivationOptions(@NonNull Context context, @NonNull Style style,
@Nullable LocationEngine locationEngine,
Expand Down Expand Up @@ -51,7 +54,7 @@ public static Builder builder(@NonNull Context context, @NonNull Style fullyLoad
*
* @return the application's current context
*/
@Nullable
@NonNull
public Context context() {
return context;
}
Expand All @@ -61,7 +64,7 @@ public Context context() {
*
* @return the map's fully loaded Style object
*/
@Nullable
@NonNull
public Style style() {
return style;
}
Expand Down Expand Up @@ -95,17 +98,17 @@ public LocationComponentOptions locationComponentOptions() {
}

/**
* the LocationComponent style res
* the LocationComponent style resource (e.g. R.style.style_name)
*
* @return
* @return the style resource.
*/
@Nullable
public int styleRes() {
return styleRes;
}

/**
* true if you want to initialize and use the built-in location engine or false if there should be no
* True if you want to initialize and use the built-in location engine or false if there should be no
* location engine initialized
* @return whether the default LocationEngine is used
*/
Expand Down Expand Up @@ -133,10 +136,6 @@ public Builder(Context context, Style style) {
this.style = style;
}

public Builder() {

}

public Builder locationEngine(LocationEngine locationEngine) {
this.locationEngine = locationEngine;
return this;
Expand All @@ -163,6 +162,21 @@ public Builder useDefaultLocationEngine(boolean useDefaultLocationEngine) {
}

public LocationComponentActivationOptions build() {
if (styleRes != 0 && locationComponentOptions != null) {
throw new IllegalArgumentException(
"You've provided both a style resource and a LocationComponentOptions object to the " +
"LocationComponentActivationOptions builder. You can't use both and " +
"you must choose one of the two to style the LocationComponent.");
}
if (context == null) {
throw new NullPointerException(
"Context in LocationComponentActivationOptions is null.");
}
if (style == null) {
throw new NullPointerException(
"Style in LocationComponentActivationOptions is null. Wait for the map to fully load before " +
"passing the Style object to LocationComponentActivationOptions.");
}
return new LocationComponentActivationOptions(context, style, locationEngine, locationEngineRequest,
locationComponentOptions, styleRes, useDefaultLocationEngine);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.mapbox.mapboxsdk.location.LocationComponent;
import com.mapbox.mapboxsdk.location.LocationComponentActivationOptions;
import com.mapbox.mapboxsdk.location.LocationComponentOptions;
import com.mapbox.mapboxsdk.location.modes.CameraMode;
import com.mapbox.mapboxsdk.location.modes.RenderMode;
import com.mapbox.mapboxsdk.maps.MapView;
import com.mapbox.mapboxsdk.maps.MapboxMap;
Expand Down Expand Up @@ -81,18 +82,18 @@ private void activateLocationComponent(@NonNull Style style) {
.elevation(5)
.accuracyAlpha(.6f)
.accuracyColor(Color.GREEN)
.foregroundDrawable(R.drawable.mapbox_marker_icon_default)
.foregroundDrawable(R.drawable.mapbox_logo_helmet)
.build();

LocationComponentActivationOptions locationComponentActivationOptions = LocationComponentActivationOptions
.builder(this, style)
.locationComponentOptions(locationComponentOptions)
.useDefaultLocationEngine(true)
.build();

locationComponent.activateLocationComponent(locationComponentActivationOptions);
locationComponent.setLocationComponentEnabled(true);
locationComponent.setRenderMode(RenderMode.COMPASS);
locationComponent.setRenderMode(RenderMode.NORMAL);
locationComponent.setCameraMode(CameraMode.TRACKING);
}

@Override
Expand Down

0 comments on commit d378b2a

Please sign in to comment.