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

Add dynamic offline routing to NavigationView #1829

Merged
merged 1 commit into from
Mar 28, 2019

Conversation

danesfeder
Copy link
Contributor

@danesfeder danesfeder commented Mar 20, 2019

Description

This PR adds two NavigationViewOptions that will allow for initialization of offline routing within the NavigationView. The view will then detect connectivity when it's time to find a route and use either online or offline based on the current state of the device.

What's the goal?

The goal is to encapsulate the offline logic within the NavigationView so RouteListener#allowRerouteFrom is no longer the go-to for offline with the NavigationView.

How is it being implemented?

For this initial implementation, the view will not worry about downloading data. The two view options are Strings: routing tile offline path and the version of that data. This means this data will already be on the device prior to initialization of the view.

When searching for a new route, the logic is as follows:

    if (connectivityStatus.isConnectedFast()) {
      onlineRouter.findRouteWith(builder);
      ...
    } else if (offlineRouter != null) {
      offlineRouter.findRouteWith(builder);
      ...
    } else if (connectivityStatus.isConnected()) {
      onlineRouter.findRouteWith(builder);
      ...
    }

So we will fallback to offline if we don't find a fast connection. Otherwise, we will try the slow connection with online as a last resort.

connectivityStatus.isConnectedFast() is based on the mobile connection subtype (EDGE, LTE for example). WIFI always returns true.

How has this been tested?

  • Unit tests added
  • This absolutely needs field testing.

Checklist

  • I have tested locally / staging (including SNAPSHOT upstream dependencies if needed)
  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have made corresponding changes to the documentation
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • I have added an Activity example in the test app showing the new feature implemented

@danesfeder danesfeder added ✓ ready for review backwards incompatible Requires a SEMVER major version change. labels Mar 20, 2019
@danesfeder danesfeder added this to the v0.34.0 milestone Mar 20, 2019
@danesfeder danesfeder self-assigned this Mar 20, 2019
@codecov-io
Copy link

codecov-io commented Mar 20, 2019

Codecov Report

Merging #1829 into master will increase coverage by 1.46%.
The diff coverage is 71.17%.

@@             Coverage Diff              @@
##             master    #1829      +/-   ##
============================================
+ Coverage     32.14%   33.61%   +1.46%     
- Complexity      964     1015      +51     
============================================
  Files           248      254       +6     
  Lines          8577     8696     +119     
  Branches        651      660       +9     
============================================
+ Hits           2757     2923     +166     
+ Misses         5572     5513      -59     
- Partials        248      260      +12

Copy link
Contributor

@Guardiola31337 Guardiola31337 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This absolutely needs field testing.

Yeah, this definitely needs a bunch of testing.

I've left some comments to discuss / look at before merging here.


import java.util.HashMap;

class ConnectivityStatusMap extends HashMap<Integer, Boolean> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about adding a HashMap as a property / create a wrapper so creating a custom one extending from HashMap is not necessary?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is to be internal, right? Can this be refactored later if/as needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Straightforward refactor, took care of it 👍

}

private boolean isConnectionFast(int type, int subType) {
if (type == ConnectivityManager.TYPE_WIFI) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should be careful here because you could be connected to a Wi-Fi but that doesn't mean to have connectivity e.g. you could be connected to a hotspot and don't have connection until you log in.

I believe we should implement something similar as with mobile connectivity. Maybe 👀 Wi-Fi strength?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is good to think about as an improvement but can possibly be deferred.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a wifi strength "checker" to address this

@danesfeder
Copy link
Contributor Author

Per conversation with @kevinkreiser, our own timeout mechanism may make sense when offline is present with online calls. I.e. wait no longer than 5 seconds before cancelling the online call and looking at offline for the route.

@@ -377,11 +369,9 @@ public void onProgressChange(Location location, RouteProgress routeProgress) {
private OffRouteListener offRouteListener = new OffRouteListener() {
@Override
public void userOffRoute(Location location) {
if (hasNetworkConnection()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the offRouteListener only getting fired now when the device is online? This doesn't cause any issues?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Connectivity checks are now done in NavigationViewRouter with the ConnectivityStatusProvider, so this check was no longer necessary / would block the new logic.


void findRouteWith(NavigationRoute.Builder builder) {
if (!isConfigured) {
return;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we give an error if not configured?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, good catch

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we throw an exception here? As it is, this would be a somewhat silent failure.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We actually won't call this method if this class isn't configured https://github.com/mapbox/mapbox-navigation-android/pull/1829/files#diff-29009b5750e50557cd5e8a7fd79a46f7R80. So we may not need this check at all?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, let's remove it altogether then.

class RouteComparator {

private static final int FIRST_ROUTE = 0;
private static final int ONE_ROUTE = 1;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

adjust naming for consistency

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consistent with what exactly? Sorry not following the comment

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just understood what that meant, sorry I thought they were both referring to indexes. Maybe SIZE_ONE_ROUTE would be more explicit for the second field?

void compare(@NonNull DirectionsResponse response, @Nullable DirectionsRoute chosenRoute) {
if (isValidRoute(response)) {
List<DirectionsRoute> routes = response.routes();
DirectionsRoute bestRoute = routes.get(FIRST_ROUTE);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could move the initialization to the call to obtainMostSimilarRoute

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adjusted the naming in RouterComparator to be more clear 👍

}
}

private DirectionsRoute obtainMostSimilarRoute(List<DirectionsRoute> routes, DirectionsRoute currentBestRoute,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels redundant with findMostSimilarRoute, if you don't pass in the first route you could just get rid of this method and call the other one.

int routeIndex = 0;
String chosenRouteLegDescription = obtainRouteLegDescriptionFrom(chosenRoute);
int minSimilarity = Integer.MAX_VALUE;
for (int index = 0; index < routes.size(); index++) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could make this a for each (for(DirectionsRoute route: routes)) and instead of routeIndex keep track of the route

@danesfeder danesfeder force-pushed the dan-navigationview-offline branch 2 times, most recently from 392d0a7 to 1df0c85 Compare March 25, 2019 15:21

import timber.log.Timber;

class NavigationViewOfflineRouter {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a lot of objects called routers, since this seems to only take care of making sure that the offline router is properly configured, can we try to name it to make that apparent?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure I completely follow regarding sole responsibility of configuration, findRouteWith is also finding new offline routes

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I understand what you are saying. Can we call this a wrapper or something? Since it is acting mostly as a wrapper for the routers.

@devotaaabel
Copy link
Contributor

@danesfeder can you describe the architecture of the Routers/RouteFetchers? That might make it easier to understand some of your choices

@danesfeder
Copy link
Contributor Author

@devotaaabel sure thing!

  • NavigationViewModel holds:
    • NavigationViewRouter holds:
      • RouteFetcher for online routing -> onlineRouter (from libandroid-navigation)
      • NavigationViewOfflineRouter for offline routing -> offlineRouter
      • ConnectivityStatusProvider for deciding which router to use (as described in the original PR post)

Copy link
Contributor

@devotaaabel devotaaabel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, my existing comments are style NITs I guess. Will approve after field testing

Copy link
Contributor

@Guardiola31337 Guardiola31337 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other than a minor comment I think this is good to go. Let's test it out and 👀 how it looks like.

Great work here @danesfeder 💪


void findRouteWith(NavigationRoute.Builder builder) {
if (!isConfigured) {
return;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, let's remove it altogether then.

@danesfeder danesfeder merged commit e7a9e42 into master Mar 28, 2019
@danesfeder danesfeder deleted the dan-navigationview-offline branch March 28, 2019 20:48
@Guardiola31337 Guardiola31337 removed this from the v0.34.0 milestone Apr 2, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backwards incompatible Requires a SEMVER major version change.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants