Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Locale distance formatter #668

Merged
merged 32 commits into from
Feb 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
6d1a6e1
Refactored formatting method name
Jan 24, 2018
441759c
Added DistanceUtilsTest
Jan 24, 2018
9cd072f
Added tests to DistanceUtilsTest
Jan 24, 2018
4284cab
Set spanEnabled to true in only place that it was set to false. This …
Jan 24, 2018
d6dde29
Changed SpannableStringBuilder to SpannableString
Jan 24, 2018
5728d4d
Removed decimalFormat and spanEnabled and added locale to DistanceUtils
Jan 24, 2018
c86ac35
Did refactoring and simlified code, and added tests for locale
Jan 24, 2018
26ba2b5
Updated all usages of DistanceUtils to use locale
Jan 24, 2018
bb93d6a
Fixed checkstyle issues
Jan 24, 2018
62e1fbd
Moved toString so that SummaryModel.getDistanceRemaining returns a st…
Jan 25, 2018
2feb26b
Merge remote-tracking branch 'origin/master' into devota-locale-dista…
Jan 25, 2018
1c116c5
Made fields final
Jan 25, 2018
dd18e3d
Removed whitespace
Jan 25, 2018
8d47efb
Rearranged fields
Jan 25, 2018
701a226
Made fields final
Jan 25, 2018
1473409
Removed whitespace
Jan 25, 2018
4e103e1
Formatted tests
Jan 25, 2018
db1380b
Added unit abbreviations to strings.xml for future localization. Also…
Jan 25, 2018
21a5fbe
Added context to all calls to DistanceUtils
Jan 25, 2018
9d85ef5
Used only context as parameter of DistanceUtils
Jan 25, 2018
95ab03b
Added special handling for locale and unit types and added tests for …
Jan 26, 2018
98970e5
Fixed checkstyle issues
Jan 26, 2018
dbb24e4
Removed unused locales
Jan 26, 2018
60daf6c
Merge branch 'master' into devota-locale-distance-formatter
Jan 29, 2018
80d9f36
Added LocaleUtils
Jan 29, 2018
1f603fa
Fixed typo
Jan 29, 2018
d5f80b1
Added special handling for deprecated locale
Jan 29, 2018
06ad29e
Merge remote-tracking branch 'origin/master' into devota-locale-dista…
Feb 5, 2018
4af1c51
Updated test for use of LocaleList for sdk>=N
Feb 5, 2018
35d8bfa
Removed directionsLanguage from NavigationViewOptions
Feb 7, 2018
a706541
Added setLocale helper method to LocaleUtils
Feb 7, 2018
c5bd115
Stopped storing context in InstructionListAdapter to prevent memory l…
Feb 7, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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