Skip to content

Commit

Permalink
add tunnels functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Guardiola31337 committed Oct 4, 2018
1 parent c7ae8e9 commit 9c8d687
Show file tree
Hide file tree
Showing 11 changed files with 118 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ public class RerouteActivity extends AppCompatActivity implements OnMapReadyCall
@BindView(R.id.instructionView)
InstructionView instructionView;

private Point origin = Point.fromLngLat(-87.6900, 41.8529);
private Point destination = Point.fromLngLat(-87.8921, 41.9794);
private Point origin = Point.fromLngLat(-0.358764,39.494876);
private Point destination = Point.fromLngLat(-0.383524, 39.497825);
private Polyline polyline;

private LocationLayerPlugin locationLayerPlugin;
Expand All @@ -73,6 +73,7 @@ public class RerouteActivity extends AppCompatActivity implements OnMapReadyCall
private MapboxMap mapboxMap;
private boolean running;
private boolean tracking;
private boolean isInTunnel = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -214,6 +215,15 @@ public void userOffRoute(Location location) {

@Override
public void onProgressChange(Location location, RouteProgress routeProgress) {
boolean inTunnel = routeProgress.inTunnel();
if (!isInTunnel && inTunnel) {
isInTunnel = true;
Snackbar.make(contentLayout, "Enter tunnel!", Snackbar.LENGTH_SHORT).show();
}
if (isInTunnel && !inTunnel) {
isInTunnel = false;
Snackbar.make(contentLayout, "Exit tunnel!", Snackbar.LENGTH_SHORT).show();
}
if (tracking) {
locationLayerPlugin.forceLocationUpdate(location);
CameraPosition cameraPosition = new CameraPosition.Builder()
Expand Down Expand Up @@ -252,7 +262,7 @@ public void onResponse(Call<DirectionsResponse> call, Response<DirectionsRespons

@Override
public void onFailure(Call<DirectionsResponse> call, Throwable throwable) {
Timber.e("Getting directions failed: ", throwable);
Timber.e(throwable);
}

private void getRoute(Point origin, Point destination, Float bearing) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ protected void onDestroy() {
navigationView.onDestroy();
if (isFinishing()) {
saveNightModeToPreferences(AppCompatDelegate.MODE_NIGHT_AUTO);
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package com.mapbox.services.android.navigation.testapp.activity.navigationui.fragment;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AppCompatDelegate;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand All @@ -19,17 +23,19 @@
import com.mapbox.services.android.navigation.ui.v5.NavigationViewOptions;
import com.mapbox.services.android.navigation.ui.v5.OnNavigationReadyCallback;
import com.mapbox.services.android.navigation.ui.v5.listeners.NavigationListener;
import com.mapbox.services.android.navigation.ui.v5.listeners.TunnelListener;
import com.mapbox.services.android.navigation.v5.navigation.NavigationRoute;

import retrofit2.Call;
import retrofit2.Response;

public class NavigationFragment extends Fragment implements OnNavigationReadyCallback, NavigationListener {
public class NavigationFragment extends Fragment implements OnNavigationReadyCallback, NavigationListener,
TunnelListener {

private static final double ORIGIN_LONGITUDE = -77.04012393951416;
private static final double ORIGIN_LATITUDE = 38.9111117447887;
private static final double DESTINATION_LONGITUDE = -77.03847169876099;
private static final double DESTINATION_LATITUDE = 38.91113678979344;
private static final double ORIGIN_LONGITUDE = -3.714873;
private static final double ORIGIN_LATITUDE = 40.397389;
private static final double DESTINATION_LONGITUDE = -3.712331;
private static final double DESTINATION_LATITUDE = 40.401686;

private NavigationView navigationView;
private DirectionsRoute directionsRoute;
Expand All @@ -44,6 +50,11 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
if (wasNavigationStopped()) {
updateWasNavigationStopped(false);
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO);
getActivity().recreate();
}
navigationView = view.findViewById(R.id.navigation_view_fragment);
navigationView.onCreate(savedInstanceState);
navigationView.initialize(this);
Expand Down Expand Up @@ -122,6 +133,22 @@ public void onNavigationRunning() {
// no-op
}

@Override
public void onTunnel(boolean isInTunnel) {
boolean wasInTunnel = wasInTunnel();
if (isInTunnel) {
if (!wasInTunnel) {
updateWasInTunnel(true);
updateCurrentNightMode(AppCompatDelegate.MODE_NIGHT_YES);
}
} else {
if (wasInTunnel) {
updateWasInTunnel(false);
updateCurrentNightMode(AppCompatDelegate.MODE_NIGHT_AUTO);
}
}
}

private void fetchRoute(Point origin, Point destination) {
NavigationRoute.builder(getContext())
.accessToken(Mapbox.getAccessToken())
Expand All @@ -145,6 +172,7 @@ private void startNavigation() {
.directionsRoute(directionsRoute)
.shouldSimulateRoute(true)
.navigationListener(NavigationFragment.this)
.tunnelListener(this)
.build();
navigationView.startNavigation(options);
}
Expand All @@ -155,6 +183,41 @@ private void stopNavigation() {
FragmentNavigationActivity fragmentNavigationActivity = (FragmentNavigationActivity) activity;
fragmentNavigationActivity.showPlaceholderFragment();
fragmentNavigationActivity.showNavigationFab();
updateWasNavigationStopped(true);
updateWasInTunnel(false);
}
}

private boolean wasInTunnel() {
Context context = getActivity();
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
return preferences.getBoolean(context.getString(R.string.was_in_tunnel), false);
}

private void updateWasInTunnel(boolean wasInTunnel) {
Context context = getActivity();
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean(context.getString(R.string.was_in_tunnel), wasInTunnel);
editor.apply();
}

private void updateCurrentNightMode(int nightMode) {
AppCompatDelegate.setDefaultNightMode(nightMode);
getActivity().recreate();
}

private boolean wasNavigationStopped() {
Context context = getActivity();
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
return preferences.getBoolean(getString(R.string.was_navigation_stopped), false);
}

public void updateWasNavigationStopped(boolean wasNavigationStopped) {
Context context = getActivity();
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean(getString(R.string.was_navigation_stopped), wasNavigationStopped);
editor.apply();
}
}
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
<string name="default_locale" translatable="false">default_for_device</string>
<string name="default_unit_type" translatable="false">default_for_device</string>
<string name="current_night_mode" translatable="false">current_night_mode</string>
<string name="was_in_tunnel" translatable="false">was_in_tunnel</string>
<string name="was_navigation_stopped" translatable="false">was_navigation_stopped</string>

<string name="error_route_not_available">Current route is not available</string>
<string name="error_select_longer_route">Please select a longer route</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import android.preference.PreferenceManager;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.AppCompatDelegate;

import com.mapbox.api.directions.v5.DirectionsCriteria;
import com.mapbox.api.directions.v5.models.DirectionsRoute;
Expand All @@ -27,7 +26,6 @@ public class MapboxNavigationActivity extends AppCompatActivity implements OnNav
protected void onCreate(@Nullable Bundle savedInstanceState) {
setTheme(R.style.Theme_AppCompat_NoActionBar);
super.onCreate(savedInstanceState);
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO);
setContentView(R.layout.activity_navigation);
navigationView = findViewById(R.id.navigationView);
navigationView.onCreate(savedInstanceState);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.mapbox.services.android.navigation.ui.v5.listeners.NavigationListener;
import com.mapbox.services.android.navigation.ui.v5.listeners.RouteListener;
import com.mapbox.services.android.navigation.ui.v5.listeners.SpeechAnnouncementListener;
import com.mapbox.services.android.navigation.ui.v5.listeners.TunnelListener;
import com.mapbox.services.android.navigation.ui.v5.voice.SpeechAnnouncement;
import com.mapbox.services.android.navigation.v5.milestone.MilestoneEventListener;
import com.mapbox.services.android.navigation.v5.navigation.MapboxNavigation;
Expand All @@ -36,6 +37,7 @@ class NavigationViewEventDispatcher {
private InstructionListListener instructionListListener;
private SpeechAnnouncementListener speechAnnouncementListener;
private BannerInstructionsListener bannerInstructionsListener;
private TunnelListener tunnelListener;

/**
* Initializes the listeners in the dispatcher, as well as the listeners in the {@link MapboxNavigation}
Expand All @@ -52,6 +54,7 @@ void initializeListeners(NavigationViewOptions navigationViewOptions, MapboxNavi
assignInstructionListListener(navigationViewOptions.instructionListListener());
assignSpeechAnnouncementListener(navigationViewOptions.speechAnnouncementListener());
assignBannerInstructionsListener(navigationViewOptions.bannerInstructionsListener());
assignTunnelListener(navigationViewOptions.tunnelListener());
}

void onDestroy(@Nullable MapboxNavigation navigation) {
Expand Down Expand Up @@ -181,6 +184,12 @@ BannerInstructions onBannerDisplay(BannerInstructions instructions) {
return instructions;
}

void onTunnel(boolean isInTunnel) {
if (tunnelListener != null) {
tunnelListener.onTunnel(isInTunnel);
}
}

private void assignProgressChangeListener(NavigationViewOptions navigationViewOptions, MapboxNavigation navigation) {
this.progressChangeListener = navigationViewOptions.progressChangeListener();
if (progressChangeListener != null) {
Expand All @@ -206,4 +215,8 @@ private void removeProgressChangeListener(MapboxNavigation navigation) {
navigation.removeProgressChangeListener(progressChangeListener);
}
}

private void assignTunnelListener(@Nullable TunnelListener tunnelListener) {
this.tunnelListener = tunnelListener;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public class NavigationViewModel extends AndroidViewModel {
private int timeFormatType;
private boolean isRunning;
private boolean isChangingConfigurations;
private boolean isInTunnel = false;

public NavigationViewModel(Application application) {
super(application);
Expand All @@ -106,6 +107,7 @@ public void onDestroy(boolean isChangingConfigurations) {
deactivateInstructionPlayer();
endNavigation();
isRunning = false;
isInTunnel = false;
}
clearDynamicCameraMap();
navigationViewEventDispatcher = null;
Expand Down Expand Up @@ -300,6 +302,11 @@ public void onProgressChange(Location location, RouteProgress routeProgress) {
instructionModel.setValue(new InstructionModel(distanceFormatter, routeProgress));
summaryModel.setValue(new SummaryModel(getApplication(), distanceFormatter, routeProgress, timeFormatType));
navigationLocation.setValue(location);
boolean isCurrentlyInTunnel = routeProgress.inTunnel();
if (isInTunnel != isCurrentlyInTunnel) {
isInTunnel = isCurrentlyInTunnel;
navigationViewEventDispatcher.onTunnel(isInTunnel);
}
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.mapbox.services.android.navigation.ui.v5.listeners.NavigationListener;
import com.mapbox.services.android.navigation.ui.v5.listeners.RouteListener;
import com.mapbox.services.android.navigation.ui.v5.listeners.SpeechAnnouncementListener;
import com.mapbox.services.android.navigation.ui.v5.listeners.TunnelListener;
import com.mapbox.services.android.navigation.ui.v5.voice.SpeechPlayer;
import com.mapbox.services.android.navigation.v5.milestone.Milestone;
import com.mapbox.services.android.navigation.v5.milestone.MilestoneEventListener;
Expand Down Expand Up @@ -62,6 +63,9 @@ public abstract class NavigationViewOptions extends NavigationUiOptions {
@Nullable
public abstract LocationEngine locationEngine();

@Nullable
public abstract TunnelListener tunnelListener();

@AutoValue.Builder
public abstract static class Builder {

Expand Down Expand Up @@ -103,6 +107,8 @@ public abstract static class Builder {

public abstract Builder locationEngine(LocationEngine locationEngine);

public abstract Builder tunnelListener(TunnelListener tunnelListener);

public abstract NavigationViewOptions build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import android.preference.PreferenceManager;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatDelegate;
import android.support.v7.content.res.AppCompatResources;
import android.util.AttributeSet;
import android.util.TypedValue;
Expand Down Expand Up @@ -86,7 +85,6 @@ public static int retrieveNavigationViewStyle(Context context, int styleResId) {
*/
static void setTheme(Context context, AttributeSet attrs) {
boolean nightModeEnabled = isNightModeEnabled(context);
updatePreferencesDarkEnabled(context, nightModeEnabled);

if (shouldSetThemeFromPreferences(context)) {
int prefLightTheme = retrieveThemeResIdFromPreferences(context, NavigationConstants.NAVIGATION_VIEW_LIGHT_THEME);
Expand Down Expand Up @@ -114,23 +112,10 @@ static String retrieveMapStyle(Context context) {

/**
* Returns true if the current UI_MODE_NIGHT is enabled, false otherwise.
*
* @param context to retrieve the current configuration
*/
private static boolean isNightModeEnabled(Context context) {
if (isNightModeFollowSystem()) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO);
}
int uiMode = retrieveCurrentUiMode(context);
return uiMode == Configuration.UI_MODE_NIGHT_YES;
}

/**
* Returns true if the current UI_MODE_NIGHT is undefined, false otherwise.
*/
private static boolean isNightModeFollowSystem() {
int nightMode = AppCompatDelegate.getDefaultNightMode();
return nightMode == AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM;
int currentNightMode = retrieveCurrentUiMode(context);
return currentNightMode == Configuration.UI_MODE_NIGHT_YES;
}

private static int retrieveCurrentUiMode(Context context) {
Expand All @@ -145,13 +130,6 @@ private static TypedValue resolveAttributeFromId(Context context, int resId) {
return outValue;
}

private static void updatePreferencesDarkEnabled(Context context, boolean darkThemeEnabled) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean(context.getString(R.string.dark_theme_enabled), darkThemeEnabled);
editor.apply();
}

private static boolean shouldSetThemeFromPreferences(Context context) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
return preferences.getBoolean(NavigationConstants.NAVIGATION_VIEW_PREFERENCE_SET_THEME, false);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.mapbox.services.android.navigation.ui.v5.listeners;

public interface TunnelListener {

void onTunnel(boolean isInTunnel);
}
2 changes: 0 additions & 2 deletions libandroid-navigation-ui/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@
translatable="false">mapbox://styles/mapbox/navigation-guidance-day-v4</string>
<string name="navigation_guidance_night"
translatable="false">mapbox://styles/mapbox/navigation-guidance-night-v4</string>
<string name="dark_theme_enabled"
translatable="false">dark_theme_enabled</string>
<string name="navigation_view_instance_state"
translatable="false">navigation_view_instance_state</string>
<string name="navigation_running"
Expand Down

0 comments on commit 9c8d687

Please sign in to comment.