From 9c8d687e11da7788b813c7f8423ff30e9f3279b1 Mon Sep 17 00:00:00 2001 From: Guardiola31337 Date: Thu, 4 Oct 2018 13:30:30 +0200 Subject: [PATCH] add tunnels functionality --- .../testapp/activity/RerouteActivity.java | 16 +++- .../EmbeddedNavigationActivity.java | 1 + .../fragment/NavigationFragment.java | 73 +++++++++++++++++-- app/src/main/res/values/strings.xml | 2 + .../ui/v5/MapboxNavigationActivity.java | 2 - .../ui/v5/NavigationViewEventDispatcher.java | 13 ++++ .../navigation/ui/v5/NavigationViewModel.java | 7 ++ .../ui/v5/NavigationViewOptions.java | 6 ++ .../navigation/ui/v5/ThemeSwitcher.java | 26 +------ .../ui/v5/listeners/TunnelListener.java | 6 ++ .../src/main/res/values/strings.xml | 2 - 11 files changed, 118 insertions(+), 36 deletions(-) create mode 100644 libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/listeners/TunnelListener.java diff --git a/app/src/main/java/com/mapbox/services/android/navigation/testapp/activity/RerouteActivity.java b/app/src/main/java/com/mapbox/services/android/navigation/testapp/activity/RerouteActivity.java index 6fc361651cb..3f72df2ab57 100644 --- a/app/src/main/java/com/mapbox/services/android/navigation/testapp/activity/RerouteActivity.java +++ b/app/src/main/java/com/mapbox/services/android/navigation/testapp/activity/RerouteActivity.java @@ -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; @@ -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) { @@ -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() @@ -252,7 +262,7 @@ public void onResponse(Call call, Response call, Throwable throwable) { - Timber.e("Getting directions failed: ", throwable); + Timber.e(throwable); } private void getRoute(Point origin, Point destination, Float bearing) { diff --git a/app/src/main/java/com/mapbox/services/android/navigation/testapp/activity/navigationui/EmbeddedNavigationActivity.java b/app/src/main/java/com/mapbox/services/android/navigation/testapp/activity/navigationui/EmbeddedNavigationActivity.java index c050b3b1287..580ab979007 100644 --- a/app/src/main/java/com/mapbox/services/android/navigation/testapp/activity/navigationui/EmbeddedNavigationActivity.java +++ b/app/src/main/java/com/mapbox/services/android/navigation/testapp/activity/navigationui/EmbeddedNavigationActivity.java @@ -132,6 +132,7 @@ protected void onDestroy() { navigationView.onDestroy(); if (isFinishing()) { saveNightModeToPreferences(AppCompatDelegate.MODE_NIGHT_AUTO); + AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO); } } diff --git a/app/src/main/java/com/mapbox/services/android/navigation/testapp/activity/navigationui/fragment/NavigationFragment.java b/app/src/main/java/com/mapbox/services/android/navigation/testapp/activity/navigationui/fragment/NavigationFragment.java index 19cfc01e35f..1551843c799 100644 --- a/app/src/main/java/com/mapbox/services/android/navigation/testapp/activity/navigationui/fragment/NavigationFragment.java +++ b/app/src/main/java/com/mapbox/services/android/navigation/testapp/activity/navigationui/fragment/NavigationFragment.java @@ -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; @@ -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; @@ -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); @@ -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()) @@ -145,6 +172,7 @@ private void startNavigation() { .directionsRoute(directionsRoute) .shouldSimulateRoute(true) .navigationListener(NavigationFragment.this) + .tunnelListener(this) .build(); navigationView.startNavigation(options); } @@ -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(); + } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 74806875f20..83c364961b8 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -47,6 +47,8 @@ default_for_device default_for_device current_night_mode + was_in_tunnel + was_navigation_stopped Current route is not available Please select a longer route diff --git a/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/MapboxNavigationActivity.java b/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/MapboxNavigationActivity.java index f5921c7fe0b..6803eb3d322 100644 --- a/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/MapboxNavigationActivity.java +++ b/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/MapboxNavigationActivity.java @@ -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; @@ -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); diff --git a/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/NavigationViewEventDispatcher.java b/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/NavigationViewEventDispatcher.java index 53c3427ef85..e50a11b9752 100644 --- a/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/NavigationViewEventDispatcher.java +++ b/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/NavigationViewEventDispatcher.java @@ -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; @@ -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} @@ -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) { @@ -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) { @@ -206,4 +215,8 @@ private void removeProgressChangeListener(MapboxNavigation navigation) { navigation.removeProgressChangeListener(progressChangeListener); } } + + private void assignTunnelListener(@Nullable TunnelListener tunnelListener) { + this.tunnelListener = tunnelListener; + } } diff --git a/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/NavigationViewModel.java b/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/NavigationViewModel.java index e44195bdad6..0cbe95baa9b 100644 --- a/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/NavigationViewModel.java +++ b/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/NavigationViewModel.java @@ -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); @@ -106,6 +107,7 @@ public void onDestroy(boolean isChangingConfigurations) { deactivateInstructionPlayer(); endNavigation(); isRunning = false; + isInTunnel = false; } clearDynamicCameraMap(); navigationViewEventDispatcher = null; @@ -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); + } } }; diff --git a/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/NavigationViewOptions.java b/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/NavigationViewOptions.java index a48de72b166..6b48e63bf2c 100644 --- a/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/NavigationViewOptions.java +++ b/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/NavigationViewOptions.java @@ -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; @@ -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 { @@ -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(); } diff --git a/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/ThemeSwitcher.java b/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/ThemeSwitcher.java index 4ad29e1ab75..35df13d83ec 100644 --- a/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/ThemeSwitcher.java +++ b/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/ThemeSwitcher.java @@ -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; @@ -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); @@ -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) { @@ -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); diff --git a/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/listeners/TunnelListener.java b/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/listeners/TunnelListener.java new file mode 100644 index 00000000000..e7b61586794 --- /dev/null +++ b/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/listeners/TunnelListener.java @@ -0,0 +1,6 @@ +package com.mapbox.services.android.navigation.ui.v5.listeners; + +public interface TunnelListener { + + void onTunnel(boolean isInTunnel); +} diff --git a/libandroid-navigation-ui/src/main/res/values/strings.xml b/libandroid-navigation-ui/src/main/res/values/strings.xml index 0626294b831..526a5f8ba99 100644 --- a/libandroid-navigation-ui/src/main/res/values/strings.xml +++ b/libandroid-navigation-ui/src/main/res/values/strings.xml @@ -50,8 +50,6 @@ translatable="false">mapbox://styles/mapbox/navigation-guidance-day-v4 mapbox://styles/mapbox/navigation-guidance-night-v4 - dark_theme_enabled navigation_view_instance_state