Skip to content

Commit

Permalink
Added locale distance formatter (#668)
Browse files Browse the repository at this point in the history
* Refactored formatting method name

* Added DistanceUtilsTest

* Added tests to DistanceUtilsTest

* Set spanEnabled to true in only place that it was set to false. This way, the special handling for returning the unspanned string can be removed

* Changed SpannableStringBuilder to SpannableString

* Removed decimalFormat and spanEnabled and added locale to DistanceUtils

* Did refactoring and simlified code, and added tests for locale

* Updated all usages of DistanceUtils to use locale

* Fixed checkstyle issues

* Moved toString so that SummaryModel.getDistanceRemaining returns a string

* Made fields final

* Removed whitespace

* Rearranged fields

* Made fields final

* Removed whitespace

* Formatted tests

* Added unit abbreviations to strings.xml for future localization. Also made DistanceUtils instantiable so that the user doesn't have to pass in fields every time

* Added context to all calls to DistanceUtils

* Used only context as parameter of DistanceUtils

* Added special handling for locale and unit types and added tests for special cases

* Fixed checkstyle issues

* Removed unused locales

* Added LocaleUtils

* Fixed typo

* Added special handling for deprecated locale

* Updated test for use of LocaleList for sdk>=N

* Removed directionsLanguage from NavigationViewOptions

* Added setLocale helper method to LocaleUtils

* Stopped storing context in InstructionListAdapter to prevent memory leaks
  • Loading branch information
Devota Aabel authored Feb 7, 2018
1 parent ebf66b4 commit 33ff396
Show file tree
Hide file tree
Showing 19 changed files with 420 additions and 225 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import com.mapbox.services.android.navigation.ui.v5.listeners.NavigationListener;
import com.mapbox.services.android.navigation.v5.navigation.MapboxNavigationOptions;
import com.mapbox.services.android.navigation.v5.navigation.NavigationConstants;
import com.mapbox.services.android.navigation.v5.navigation.NavigationUnitType;

import java.util.HashMap;

Expand Down Expand Up @@ -116,8 +115,6 @@ private void extractConfiguration(NavigationViewOptions.Builder options) {
.getBoolean(NavigationConstants.NAVIGATION_VIEW_SIMULATE_ROUTE, false));

MapboxNavigationOptions navigationOptions = MapboxNavigationOptions.builder()
.unitType(preferences.getInt(NavigationConstants.NAVIGATION_VIEW_UNIT_TYPE,
NavigationUnitType.TYPE_IMPERIAL))
.build();
options.navigationOptions(navigationOptions);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,9 @@
import com.mapbox.services.android.navigation.v5.route.FasterRouteListener;
import com.mapbox.services.android.navigation.v5.routeprogress.ProgressChangeListener;
import com.mapbox.services.android.navigation.v5.routeprogress.RouteProgress;
import com.mapbox.services.android.navigation.v5.utils.LocaleUtils;
import com.mapbox.services.android.telemetry.location.LocationEngine;

import java.text.DecimalFormat;

public class NavigationViewModel extends AndroidViewModel implements ProgressChangeListener,
OffRouteListener, MilestoneEventListener, NavigationEventListener, FasterRouteListener {

Expand All @@ -52,12 +51,10 @@ public class NavigationViewModel extends AndroidViewModel implements ProgressCha
final MutableLiveData<Boolean> isRunning = new MutableLiveData<>();
final MutableLiveData<Boolean> shouldRecordScreenshot = new MutableLiveData<>();

private final SharedPreferences preferences;
private MapboxNavigation navigation;
private NavigationInstructionPlayer instructionPlayer;
private ConnectivityManager connectivityManager;
private SharedPreferences preferences;
private DecimalFormat decimalFormat;
private int unitType;
private String feedbackId;
private String screenshot;

Expand All @@ -66,7 +63,6 @@ public NavigationViewModel(Application application) {
preferences = PreferenceManager.getDefaultSharedPreferences(application);
initVoiceInstructions(application);
initConnectivityManager(application);
initDecimalFormat();
}

public void onDestroy() {
Expand All @@ -86,8 +82,8 @@ public void onDestroy() {
*/
@Override
public void onProgressChange(Location location, RouteProgress routeProgress) {
instructionModel.setValue(new InstructionModel(routeProgress, decimalFormat, unitType));
summaryModel.setValue(new SummaryModel(routeProgress, decimalFormat, unitType));
instructionModel.setValue(new InstructionModel(getApplication(), routeProgress));
summaryModel.setValue(new SummaryModel(getApplication(), routeProgress));
navigationLocation.setValue(location);
}

Expand Down Expand Up @@ -219,8 +215,9 @@ public MapboxNavigation getNavigation() {
* @param options to init MapboxNavigation
*/
void initializeNavigationOptions(Context context, MapboxNavigationOptions options) {
LocaleUtils.setLocale(context, options.locale(), options.unitType());

initNavigation(context, options);
this.unitType = options.unitType();
}

void updateRoute(DirectionsRoute route) {
Expand Down Expand Up @@ -262,14 +259,6 @@ private void initConnectivityManager(Application application) {
connectivityManager = (ConnectivityManager) application.getSystemService(Context.CONNECTIVITY_SERVICE);
}

/**
* Initializes decimal format to be used to populate views with
* distance remaining.
*/
private void initDecimalFormat() {
decimalFormat = new DecimalFormat(NavigationConstants.DECIMAL_FORMAT);
}

/**
* Adds this class as a listener for progress,
* milestones, and off route events.
Expand Down Expand Up @@ -332,8 +321,8 @@ private boolean hasNetworkConnection() {

private void updateBannerInstruction(RouteProgress routeProgress, Milestone milestone) {
if (milestone instanceof BannerInstructionMilestone) {
bannerInstructionModel.setValue(new BannerInstructionModel((BannerInstructionMilestone) milestone,
routeProgress, decimalFormat, unitType));
bannerInstructionModel.setValue(
new BannerInstructionModel(getApplication(), (BannerInstructionMilestone) milestone, routeProgress));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
import com.mapbox.services.android.navigation.v5.navigation.MapboxNavigationOptions;
import com.mapbox.services.android.navigation.v5.routeprogress.ProgressChangeListener;

import java.util.Locale;

@AutoValue
public abstract class NavigationViewOptions {

Expand All @@ -25,9 +23,6 @@ public abstract class NavigationViewOptions {
@Nullable
public abstract String directionsProfile();

@Nullable
public abstract Locale directionsLanguage();

@Nullable
public abstract Point origin();

Expand Down Expand Up @@ -72,8 +67,6 @@ public abstract static class Builder {

public abstract Builder directionsProfile(@DirectionsCriteria.ProfileCriteria String directionsProfile);

public abstract Builder directionsLanguage(Locale directionsLanguage);

public abstract Builder origin(Point origin);

public abstract Builder destination(Point destination);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package com.mapbox.services.android.navigation.ui.v5.instruction;

import android.content.Context;

import com.mapbox.api.directions.v5.models.BannerText;
import com.mapbox.services.android.navigation.v5.milestone.BannerInstructionMilestone;
import com.mapbox.services.android.navigation.v5.routeprogress.RouteProgress;

import java.text.DecimalFormat;

public class BannerInstructionModel extends InstructionModel {

private BannerInstructionMilestone milestone;

public BannerInstructionModel(BannerInstructionMilestone milestone, RouteProgress progress,
DecimalFormat decimalFormat, int unitType) {
super(progress, decimalFormat, unitType);
public BannerInstructionModel(Context context, BannerInstructionMilestone milestone, RouteProgress progress) {
super(context, progress);
this.milestone = milestone;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.mapbox.services.android.navigation.ui.v5.instruction;

import android.content.Context;

import com.mapbox.api.directions.v5.models.BannerInstructions;
import com.mapbox.api.directions.v5.models.BannerText;
import com.mapbox.services.android.navigation.v5.navigation.NavigationUnitType;
import com.mapbox.services.android.navigation.v5.routeprogress.RouteProgress;

import java.text.DecimalFormat;
import java.util.List;

public class InstructionModel {
Expand All @@ -15,13 +15,10 @@ public class InstructionModel {
private BannerText thenBannerText;
private InstructionStepResources stepResources;
private RouteProgress progress;
private int unitType;

public InstructionModel(RouteProgress progress, DecimalFormat decimalFormat,
@NavigationUnitType.UnitType int unitType) {
public InstructionModel(Context context, RouteProgress progress) {
this.progress = progress;
this.unitType = unitType;
buildInstructionModel(progress, decimalFormat, unitType);
buildInstructionModel(context, progress);
}

BannerText getPrimaryBannerText() {
Expand All @@ -44,12 +41,8 @@ RouteProgress getProgress() {
return progress;
}

int getUnitType() {
return unitType;
}

private void buildInstructionModel(RouteProgress progress, DecimalFormat decimalFormat, int unitType) {
stepResources = new InstructionStepResources(progress, decimalFormat, unitType);
private void buildInstructionModel(Context context, RouteProgress progress) {
stepResources = new InstructionStepResources(context, progress);
extractStepInstructions(progress);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
package com.mapbox.services.android.navigation.ui.v5.instruction;

import android.text.SpannableStringBuilder;
import android.content.Context;
import android.text.SpannableString;

import com.mapbox.api.directions.v5.models.IntersectionLanes;
import com.mapbox.api.directions.v5.models.LegStep;
import com.mapbox.api.directions.v5.models.StepIntersection;
import com.mapbox.services.android.navigation.v5.routeprogress.RouteProgress;
import com.mapbox.services.android.navigation.v5.utils.DistanceUtils;

import java.text.DecimalFormat;
import java.util.List;

class InstructionStepResources {

private SpannableStringBuilder stepDistanceRemaining;
private SpannableString stepDistanceRemaining;
private String maneuverViewModifier;
private String maneuverViewType;
private String thenStepManeuverModifier;
private String thenStepManeuverType;
private boolean shouldShowThenStep;
private List<IntersectionLanes> turnLanes;

InstructionStepResources(RouteProgress progress, DecimalFormat decimalFormat, int unitType) {
formatStepDistance(progress, decimalFormat, unitType);
InstructionStepResources(Context context, RouteProgress progress) {
formatStepDistance(context, progress);
extractStepResources(progress);
}

SpannableStringBuilder getStepDistanceRemaining() {
SpannableString getStepDistanceRemaining() {
return stepDistanceRemaining;
}

Expand Down Expand Up @@ -79,9 +79,9 @@ private void extractStepResources(RouteProgress progress) {
}
}

private void formatStepDistance(RouteProgress progress, DecimalFormat decimalFormat, int unitType) {
stepDistanceRemaining = DistanceUtils.distanceFormatter(progress.currentLegProgress()
.currentStepProgress().distanceRemaining(), decimalFormat, true, unitType);
private void formatStepDistance(Context context, RouteProgress progress) {
stepDistanceRemaining = new DistanceUtils(context)
.formatDistance(progress.currentLegProgress().currentStepProgress().distanceRemaining());
}

private void intersectionTurnLanes(LegStep step) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,11 @@
import com.mapbox.services.android.navigation.ui.v5.instruction.turnlane.TurnLaneAdapter;
import com.mapbox.services.android.navigation.ui.v5.summary.list.InstructionListAdapter;
import com.mapbox.services.android.navigation.v5.navigation.NavigationConstants;
import com.mapbox.services.android.navigation.v5.navigation.NavigationUnitType;
import com.mapbox.services.android.navigation.v5.navigation.metrics.FeedbackEvent;
import com.mapbox.services.android.navigation.v5.offroute.OffRouteListener;
import com.mapbox.services.android.navigation.v5.routeprogress.ProgressChangeListener;
import com.mapbox.services.android.navigation.v5.routeprogress.RouteProgress;

import java.text.DecimalFormat;

/**
* A view that can be used to display upcoming maneuver information and control
* voice instruction mute / unmute.
Expand Down Expand Up @@ -93,7 +90,6 @@ public class InstructionView extends RelativeLayout implements FeedbackBottomShe
private Animation rerouteSlideUpTop;
private Animation rerouteSlideDownTop;
private AnimationSet fadeInSlowOut;
private DecimalFormat decimalFormat;
private LegStep currentStep;
private NavigationViewModel navigationViewModel;
private boolean isRerouting;
Expand Down Expand Up @@ -124,7 +120,6 @@ protected void onFinishInflate() {
initBackground();
initTurnLaneRecyclerView();
initDirectionsRecyclerView();
initDecimalFormat();
initAnimations();
}

Expand Down Expand Up @@ -192,13 +187,12 @@ public void onChanged(@Nullable Boolean isOffRoute) {
* uses it to update the views.
*
* @param routeProgress used to provide navigation / routeProgress data
* @param unitType either imperial or metric
* @since 0.6.2
*/
@SuppressWarnings("UnusedDeclaration")
public void update(RouteProgress routeProgress, @NavigationUnitType.UnitType int unitType) {
public void update(RouteProgress routeProgress) {
if (routeProgress != null && !isRerouting) {
InstructionModel model = new InstructionModel(routeProgress, decimalFormat, unitType);
InstructionModel model = new InstructionModel(getContext(), routeProgress);
updateViews(model);
updateTextInstruction(model);
}
Expand Down Expand Up @@ -487,22 +481,14 @@ private void initTurnLaneRecyclerView() {
* Sets up the {@link RecyclerView} that is used to display the list of instructions.
*/
private void initDirectionsRecyclerView() {
instructionListAdapter = new InstructionListAdapter();
instructionListAdapter = new InstructionListAdapter(getContext());
rvInstructions.setAdapter(instructionListAdapter);
rvInstructions.setHasFixedSize(true);
rvInstructions.setNestedScrollingEnabled(true);
rvInstructions.setItemAnimator(new DefaultItemAnimator());
rvInstructions.setLayoutManager(new LinearLayoutManager(getContext()));
}

/**
* Initializes decimal format to be used to populate views with
* distance remaining.
*/
private void initDecimalFormat() {
decimalFormat = new DecimalFormat(NavigationConstants.DECIMAL_FORMAT);
}

/**
* Initializes all animations needed to show / hide views.
*/
Expand Down Expand Up @@ -792,6 +778,6 @@ private void adjustBannerTextVerticalBias(float percentBias) {
* @param model to provide the current steps and unit type
*/
private void updateInstructionList(InstructionModel model) {
instructionListAdapter.updateSteps(model.getProgress(), model.getUnitType());
instructionListAdapter.updateSteps(model.getProgress());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,8 @@ private void cacheRouteProfile(NavigationViewOptions options, DirectionsRoute ro
* @param route as backup if view options language not found
*/
private void cacheRouteLanguage(NavigationViewOptions options, DirectionsRoute route) {
if (options.directionsLanguage() != null) {
language = options.directionsLanguage();
if (options.navigationOptions().locale() != null) {
language = options.navigationOptions().locale();
} else if (!TextUtils.isEmpty(route.routeOptions().language())) {
language = new Locale(route.routeOptions().language());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@

import com.mapbox.services.android.navigation.ui.v5.NavigationViewModel;
import com.mapbox.services.android.navigation.ui.v5.R;
import com.mapbox.services.android.navigation.v5.navigation.NavigationConstants;
import com.mapbox.services.android.navigation.v5.navigation.NavigationUnitType;
import com.mapbox.services.android.navigation.v5.routeprogress.ProgressChangeListener;
import com.mapbox.services.android.navigation.v5.routeprogress.RouteProgress;

Expand All @@ -32,13 +30,10 @@
public class SummaryBottomSheet extends FrameLayout {

private static final String EMPTY_STRING = "";

private TextView distanceRemainingText;
private TextView timeRemainingText;
private TextView arrivalTimeText;
private ProgressBar rerouteProgressBar;

private DecimalFormat decimalFormat;
private boolean isRerouting;

public SummaryBottomSheet(Context context) {
Expand All @@ -63,7 +58,6 @@ public SummaryBottomSheet(Context context, AttributeSet attrs, int defStyleAttr)
protected void onFinishInflate() {
super.onFinishInflate();
bind();
initDecimalFormat();
}

public void subscribe(NavigationViewModel navigationViewModel) {
Expand Down Expand Up @@ -97,13 +91,12 @@ public void onChanged(@Nullable Boolean isOffRoute) {
* uses it to update the views.
*
* @param routeProgress used to provide navigation / routeProgress data
* @param unitType either imperial or metric
* @since 0.6.2
*/
@SuppressWarnings("UnusedDeclaration")
public void update(RouteProgress routeProgress, @NavigationUnitType.UnitType int unitType) {
public void update(RouteProgress routeProgress) {
if (routeProgress != null && !isRerouting) {
SummaryModel model = new SummaryModel(routeProgress, decimalFormat, unitType);
SummaryModel model = new SummaryModel(getContext(), routeProgress);
arrivalTimeText.setText(model.getArrivalTime());
timeRemainingText.setText(model.getTimeRemaining());
distanceRemainingText.setText(model.getDistanceRemaining());
Expand Down Expand Up @@ -149,14 +142,6 @@ private void bind() {
rerouteProgressBar = findViewById(R.id.rerouteProgressBar);
}

/**
* Initializes decimal format to be used to populate views with
* distance remaining.
*/
private void initDecimalFormat() {
decimalFormat = new DecimalFormat(NavigationConstants.DECIMAL_FORMAT);
}

/**
* Clears all {@link View}s.
*/
Expand Down
Loading

0 comments on commit 33ff396

Please sign in to comment.