Skip to content

Commit

Permalink
Addressed most of PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Devota Aabel committed May 29, 2018
1 parent 0c20a6e commit 2e3c693
Show file tree
Hide file tree
Showing 21 changed files with 119 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ private void fetchRoute() {
.destination(DESTINATION)
.alternatives(true)
.build()
.getRoute(new NavigationRoute.SimplifiedCallback() {
.getRoute(new SimplifiedCallback() {
@Override
public void onResponse(Call<DirectionsResponse> call, Response<DirectionsResponse> response) {
DirectionsRoute directionsRoute = response.body().routes().get(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ private void fetchRoute() {
.alternatives(true);
setFieldsFromSharedPreferences(builder);
builder.build()
.getRoute(new NavigationRoute.SimplifiedCallback() {
.getRoute(new SimplifiedCallback() {
@Override
public void onResponse(Call<DirectionsResponse> call, Response<DirectionsResponse> response) {
if (validRouteResponse(response)) {
Expand All @@ -292,11 +292,11 @@ public void onResponse(Call<DirectionsResponse> call, Response<DirectionsRespons

private void setFieldsFromSharedPreferences(NavigationRoute.Builder builder) {
builder
.language(getLanguage())
.voiceUnits(getUnitType());
.language(getLanguageFromSharedPreferences())
.voiceUnits(getUnitTypeFromSharedPreferences());
}

private String getUnitType() {
private String getUnitTypeFromSharedPreferences() {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
String defaultUnitType = getString(R.string.default_unit_type);
String unitType = sharedPreferences.getString(getString(R.string.unit_type_key), defaultUnitType);
Expand All @@ -307,23 +307,23 @@ private String getUnitType() {
return unitType;
}

private String getLanguage() {
private String getLanguageFromSharedPreferences() {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
String defaultLanguage = getString(R.string.default_locale);
String language = sharedPreferences.getString(getString(R.string.language_key), defaultLanguage);
if (language.equals(defaultLanguage)) {
language = localeUtils.getDeviceLanguage(this);
language = localeUtils.inferDeviceLanguage(this);
}

return language;
}

private boolean getShouldSimulateRoute() {
private boolean getShouldSimulateRouteFromSharedPreferences() {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
return sharedPreferences.getBoolean(getString(R.string.simulate_route_key), false);
}

private String getRouteProfile() {
private String getRouteProfileFromSharedPreferences() {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
return sharedPreferences.getString(
getString(R.string.route_profile_key), DirectionsCriteria.PROFILE_DRIVING_TRAFFIC
Expand All @@ -337,8 +337,8 @@ private void launchNavigationWithRoute() {
}

NavigationLauncherOptions.Builder optionsBuilder = NavigationLauncherOptions.builder()
.shouldSimulateRoute(getShouldSimulateRoute())
.directionsProfile(getRouteProfile());
.shouldSimulateRoute(getShouldSimulateRouteFromSharedPreferences())
.directionsProfile(getRouteProfileFromSharedPreferences());

optionsBuilder.directionsRoute(route);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
import com.mapbox.services.android.navigation.testapp.R;

public class NavigationViewSettingsActivity extends PreferenceActivity {
SharedPreferences.OnSharedPreferenceChangeListener listener;
public static final String UNIT_TYPE_CHANGED = "unit_type_changed";
public static final String LANGUAGE_CHANGED = "language_changed";
private SharedPreferences.OnSharedPreferenceChangeListener listener;
static final String UNIT_TYPE_CHANGED = "unit_type_changed";
static final String LANGUAGE_CHANGED = "language_changed";

@Override
public void onCreate(Bundle savedInstanceState) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.mapbox.services.android.navigation.testapp.activity.navigationui;

import com.mapbox.api.directions.v5.models.DirectionsResponse;

import retrofit2.Call;
import retrofit2.Callback;
import timber.log.Timber;

/**
* Helper class to reduce redundant logging code when no other action is taken in onFailure
*/
public abstract class SimplifiedCallback implements Callback<DirectionsResponse> {
@Override
public void onFailure(Call<DirectionsResponse> call, Throwable throwable) {
Timber.e(throwable, throwable.getMessage());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ private void fetchRoute(Point origin, Point destination) {
.destination(destination)
.alternatives(true)
.build()
.getRoute(new NavigationRoute.SimplifiedCallback() {
.getRoute(new SimplifiedCallback() {
@Override
public void onResponse(Call<DirectionsResponse> call, Response<DirectionsResponse> response) {
startNavigation(response.body().routes().get(0));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.mapbox.geojson.Point;
import com.mapbox.mapboxsdk.Mapbox;
import com.mapbox.services.android.navigation.testapp.R;
import com.mapbox.services.android.navigation.testapp.activity.navigationui.SimplifiedCallback;
import com.mapbox.services.android.navigation.ui.v5.NavigationView;
import com.mapbox.services.android.navigation.ui.v5.NavigationViewOptions;
import com.mapbox.services.android.navigation.ui.v5.OnNavigationReadyCallback;
Expand Down Expand Up @@ -128,7 +129,7 @@ private void fetchRoute(Point origin, Point destination) {
.origin(origin)
.destination(destination)
.build()
.getRoute(new NavigationRoute.SimplifiedCallback() {
.getRoute(new SimplifiedCallback() {
@Override
public void onResponse(Call<DirectionsResponse> call, Response<DirectionsResponse> response) {
DirectionsRoute directionsRoute = response.body().routes().get(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
*/
public class NavigationActivity extends AppCompatActivity implements OnNavigationReadyCallback, NavigationListener {

private static final String EMPTY_STRING = "";

private NavigationView navigationView;
private boolean isRunning;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,8 @@ private void establish(NavigationViewOptions options) {
}

private void establishLanguage(NavigationViewOptions options) {
String language = new LocaleUtils().getNonNullLocale(getContext(), options.directionsRoute().voiceLanguage());
LocaleUtils localeUtils = new LocaleUtils();
String language = localeUtils.getNonEmptyLanguage(getContext(), options.directionsRoute().voiceLanguage());
instructionView.setLanguage(language);
summaryBottomSheet.setLanguage(language);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,9 @@ void initializeEventDispatcher(NavigationViewEventDispatcher navigationViewEvent
void initializeNavigation(NavigationViewOptions options) {
MapboxNavigationOptions navigationOptions = options.navigationOptions();
navigationOptions = navigationOptions.toBuilder().isFromNavigationUi(true).build();
initLocaleInfo(options);
LocaleUtils localeUtils = new LocaleUtils();
initLanguage(options, localeUtils);
initUnitType(options, localeUtils);
initTimeFormat(navigationOptions);
initVoiceInstructions();
if (!isRunning) {
Expand Down Expand Up @@ -197,8 +199,11 @@ private void initNavigationLocationEngine() {
locationEngineConductor = new LocationEngineConductor(locationEngineCallback);
}

private void initLocaleInfo(NavigationUiOptions options) {
language = new LocaleUtils().getNonNullLocale(getApplication(), options.directionsRoute().voiceLanguage());
private void initLanguage(NavigationUiOptions options, LocaleUtils localeUtils) {
language = localeUtils.getNonEmptyLanguage(getApplication(), options.directionsRoute().voiceLanguage());
}

private void initUnitType(NavigationUiOptions options, LocaleUtils localeUtils) {
unitType = options.directionsRoute().routeOptions().voiceUnits();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ public class InstructionModel {
private String language;
private String unitType;

public InstructionModel(Context context, RouteProgress progress,
String language, String unitType) {
public InstructionModel(Context context, RouteProgress progress, String language, String unitType) {
this.progress = progress;
this.language = language;
this.unitType = unitType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ class InstructionStepResources {
private boolean shouldShowThenStep;
private DistanceUtils distanceUtils;
private String language;
private @DirectionsCriteria.VoiceUnitCriteria String unitType;
@DirectionsCriteria.VoiceUnitCriteria
private String unitType;

InstructionStepResources(Context context, RouteProgress progress, String language,
String unitType) {
InstructionStepResources(Context context, RouteProgress progress, String language, String unitType) {
formatStepDistance(context, progress, language, unitType);
extractStepResources(progress);
}
Expand Down Expand Up @@ -101,7 +101,6 @@ private void formatStepDistance(Context context, RouteProgress progress,
private boolean shouldDistanceUtilsBeInitialized(String language,
@DirectionsCriteria.VoiceUnitCriteria String unitType) {
return distanceUtils == null || !this.language.equals(language) || !this.unitType.equals(unitType);

}

private void intersectionTurnLanes(LegStep step) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ public void showInstructionList() {
}

/**
* Sets the locale to use for languages and default unit type
* Sets the language to use
*
* @param language to use
*/
Expand All @@ -348,7 +348,8 @@ public void setUnitType(@DirectionsCriteria.VoiceUnitCriteria String unitType) {
* Inflates this layout needed for this view and initializes the locale as the device locale.
*/
private void init() {
language = new LocaleUtils().getDeviceLanguage(getContext());
LocaleUtils localeUtils = new LocaleUtils();
language = localeUtils.inferDeviceLanguage(getContext());
inflate(getContext(), R.layout.instruction_view_layout, this);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,32 @@ public void onErrorReceived(Throwable throwable) {
/**
* Checks the options used to launch this {@link com.mapbox.services.android.navigation.ui.v5.NavigationView}.
* <p>
* Will launch with either a {@link DirectionsRoute} or pair of {@link Point}s.
* Will launch with a {@link DirectionsRoute}.
*
* @param options holds either a set of {@link Point} coordinates or a {@link DirectionsRoute}
* @param options holds a {@link DirectionsRoute}
*/
public void extractRouteOptions(NavigationViewOptions options) {
extractRouteFromOptions(options);
}

/**
* Fetches the route from the off-route event
*
* @param context to pass to route builder
* @param event from which the route progress is extracted
*/
public void fetchRouteFromOffRouteEvent(Context context, OffRouteEvent event) {
if (OffRouteEvent.isValid(event)) {
RouteProgress routeProgress = event.getRouteProgress();
findRouteFromRouteProgress(context, rawLocation, routeProgress);
}
}

/**
* Updates this object's awareness of the raw location
*
* @param rawLocation to set
*/
public void updateRawLocation(@NonNull Location rawLocation) {
this.rawLocation = rawLocation;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public void hideRerouteState() {
}

/**
* Sets the locale to use for languages and default unit type
* Sets the language to use for voice language and default unit type
*
* @param language to use
*/
Expand Down Expand Up @@ -165,7 +165,7 @@ public void setTimeFormat(@NavigationTimeFormat.Type int type) {
* Inflates this layout needed for this view and initializes the locale as the device locale.
*/
private void init() {
language = new LocaleUtils().getDeviceLanguage(getContext());
language = new LocaleUtils().inferDeviceLanguage(getContext());
inflate(getContext(), R.layout.summary_bottomsheet_layout, this);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ public class InstructionListAdapter extends RecyclerView.Adapter<InstructionView
private LegStep currentStep;
private DistanceUtils distanceUtils;
private String language;
private @DirectionsCriteria.VoiceUnitCriteria String unitType;
@DirectionsCriteria.VoiceUnitCriteria
private String unitType;

public InstructionListAdapter() {
stepList = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
import java.util.Locale;

/**
* Default player used to play voice instructions when
* {@link com.mapbox.services.android.navigation.ui.v5.NavigationView} is launched without an AWS Cognito Pool ID.
* Default player used to play voice instructions when a connection to Polly is unable to be established.
* <p>
* This instruction player uses {@link TextToSpeech} to play voice instructions.
*
Expand All @@ -27,6 +26,7 @@ public class AndroidSpeechPlayer implements InstructionPlayer {
* Creates an instance of {@link AndroidSpeechPlayer}.
*
* @param context used to create an instance of {@link TextToSpeech}
* @param language to initialize locale to set
* @since 0.6.0
*/
AndroidSpeechPlayer(Context context, final String language) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

import retrofit2.Call;
import retrofit2.Callback;
import timber.log.Timber;


/**
Expand Down Expand Up @@ -65,7 +64,8 @@ public static Builder builder(Context context) {
public static Builder builder(Context context, LocaleUtils localeUtils) {
return new Builder()
.annotations(DirectionsCriteria.ANNOTATION_CONGESTION, DirectionsCriteria.ANNOTATION_DISTANCE)
.languageAndVoiceUnitsFromContext(context, localeUtils)
.language(context, localeUtils)
.voiceUnits(context, localeUtils)
.profile(DirectionsCriteria.PROFILE_DRIVING_TRAFFIC);
}

Expand Down Expand Up @@ -275,7 +275,7 @@ public Builder alternatives(@Nullable Boolean alternatives) {
* select number of languages are currently supported, reference the table provided in the see
* link below.
*
* @param language a Locale value representing the language you'd like the instructions to be
* @param language a string value representing the language you'd like the instructions to be
* written in when returned
* @return this builder for chaining options together
* @see <a href="https://www.mapbox.com/api-documentation/#instructions-languages">Supported
Expand All @@ -287,6 +287,11 @@ public Builder language(String language) {
return this;
}

Builder language(Context context, LocaleUtils localeUtils) {
directionsBuilder.language(localeUtils.inferDeviceLocale(context));
return this;
}

/**
* Whether or not to return additional metadata along the route. Possible values are:
* {@link DirectionsCriteria#ANNOTATION_DISTANCE},
Expand Down Expand Up @@ -380,6 +385,11 @@ public Builder voiceUnits(@VoiceUnitCriteria String voiceUnits) {
return this;
}

Builder voiceUnits(Context context, LocaleUtils localeUtils) {
directionsBuilder.voiceUnits(localeUtils.getUnitTypeForDeviceLocale(context));
return this;
}

/**
* Exclude specific road classes such as highways, tolls, and more.
*
Expand Down Expand Up @@ -430,12 +440,6 @@ public Builder baseUrl(String baseUrl) {
return this;
}

protected Builder languageAndVoiceUnitsFromContext(Context context, LocaleUtils localeUtils) {
directionsBuilder.language(localeUtils.getDeviceLocale(context))
.voiceUnits(localeUtils.getUnitTypeForDeviceLocale(context));
return this;
}

/**
* Optionally create a {@link Builder} based on all variables
* from given {@link RouteOptions}.
Expand Down Expand Up @@ -509,14 +513,4 @@ public NavigationRoute build() {
return new NavigationRoute(directionsBuilder.build());
}
}

/**
* Helper class to reduce redundant logging code when no other action is taken in onFailure
*/
public abstract static class SimplifiedCallback implements Callback<DirectionsResponse> {
@Override
public void onFailure(Call<DirectionsResponse> call, Throwable throwable) {
Timber.e(throwable, throwable.getMessage());
}
}
}
Loading

0 comments on commit 2e3c693

Please sign in to comment.