55import android .arch .lifecycle .MutableLiveData ;
66import android .content .Context ;
77import android .location .Location ;
8- import android .net .ConnectivityManager ;
9- import android .net .NetworkInfo ;
108import android .support .annotation .NonNull ;
119import android .support .annotation .Nullable ;
1210import android .text .TextUtils ;
2119import com .mapbox .services .android .navigation .ui .v5 .feedback .FeedbackItem ;
2220import com .mapbox .services .android .navigation .ui .v5 .instruction .BannerInstructionModel ;
2321import com .mapbox .services .android .navigation .ui .v5 .instruction .InstructionModel ;
24- import com .mapbox .services .android .navigation .ui .v5 .route .OffRouteEvent ;
25- import com .mapbox .services .android .navigation .ui .v5 .route .ViewRouteFetcher ;
26- import com .mapbox .services .android .navigation .ui .v5 .route .ViewRouteListener ;
2722import com .mapbox .services .android .navigation .ui .v5 .summary .SummaryModel ;
2823import com .mapbox .services .android .navigation .ui .v5 .voice .NavigationSpeechPlayer ;
2924import com .mapbox .services .android .navigation .ui .v5 .voice .SpeechAnnouncement ;
4237import com .mapbox .services .android .navigation .v5 .navigation .metrics .FeedbackEvent ;
4338import com .mapbox .services .android .navigation .v5 .offroute .OffRouteListener ;
4439import com .mapbox .services .android .navigation .v5 .route .FasterRouteListener ;
40+ import com .mapbox .services .android .navigation .v5 .route .RouteFetcher ;
4541import com .mapbox .services .android .navigation .v5 .routeprogress .ProgressChangeListener ;
4642import com .mapbox .services .android .navigation .v5 .routeprogress .RouteProgress ;
4743import com .mapbox .services .android .navigation .v5 .utils .DistanceFormatter ;
@@ -69,14 +65,13 @@ public class NavigationViewModel extends AndroidViewModel {
6965 private final MutableLiveData <Point > destination = new MutableLiveData <>();
7066
7167 private MapboxNavigation navigation ;
72- private ViewRouteFetcher routeFetcher ;
68+ private NavigationViewRouter router ;
7369 private LocationEngineConductor locationEngineConductor ;
7470 private NavigationViewEventDispatcher navigationViewEventDispatcher ;
7571 private SpeechPlayer speechPlayer ;
7672 private VoiceInstructionLoader voiceInstructionLoader ;
7773 private VoiceInstructionCache voiceInstructionCache ;
7874 private int voiceInstructionsToAnnounce = 0 ;
79- private ConnectivityManager connectivityManager ;
8075 private RouteProgress routeProgress ;
8176 private String feedbackId ;
8277 private String screenshot ;
@@ -93,9 +88,8 @@ public class NavigationViewModel extends AndroidViewModel {
9388 public NavigationViewModel (Application application ) {
9489 super (application );
9590 this .accessToken = Mapbox .getAccessToken ();
96- initializeConnectivityManager (application );
97- initializeNavigationRouteEngine ();
98- initializeNavigationLocationEngine ();
91+ initializeLocationEngine ();
92+ initializeRouter ();
9993 routeUtils = new RouteUtils ();
10094 localeUtils = new LocaleUtils ();
10195 }
@@ -121,7 +115,7 @@ public NavigationViewModel(Application application) {
121115 public void onDestroy (boolean isChangingConfigurations ) {
122116 this .isChangingConfigurations = isChangingConfigurations ;
123117 if (!isChangingConfigurations ) {
124- routeFetcher .onDestroy ();
118+ router .onDestroy ();
125119 endNavigation ();
126120 deactivateInstructionPlayer ();
127121 isRunning = false ;
@@ -208,7 +202,7 @@ MapboxNavigation initialize(NavigationViewOptions options) {
208202 initializeVoiceInstructionCache ();
209203 initializeNavigationSpeechPlayer (options );
210204 }
211- routeFetcher .extractRouteOptions (options );
205+ router .extractRouteOptions (options );
212206 return navigation ;
213207 }
214208
@@ -265,15 +259,13 @@ MutableLiveData<Point> retrieveDestination() {
265259 return destination ;
266260 }
267261
268- private void initializeConnectivityManager (Application application ) {
269- connectivityManager = (ConnectivityManager ) application .getSystemService (Context .CONNECTIVITY_SERVICE );
262+ private void initializeRouter () {
263+ RouteFetcher onlineRouter = new RouteFetcher (getApplication (), accessToken );
264+ ConnectivityStatusProvider connectivityStatus = new ConnectivityStatusProvider (getApplication ());
265+ router = new NavigationViewRouter (onlineRouter , connectivityStatus , routeEngineListener );
270266 }
271267
272- private void initializeNavigationRouteEngine () {
273- routeFetcher = new ViewRouteFetcher (getApplication (), accessToken , routeEngineListener );
274- }
275-
276- private void initializeNavigationLocationEngine () {
268+ private void initializeLocationEngine () {
277269 locationEngineConductor = new LocationEngineConductor ();
278270 }
279271
@@ -366,7 +358,7 @@ private void addMilestones(NavigationViewOptions options) {
366358 @ Override
367359 public void onProgressChange (Location location , RouteProgress routeProgress ) {
368360 NavigationViewModel .this .routeProgress = routeProgress ;
369- routeFetcher .updateLocation (location );
361+ router .updateLocation (location );
370362 instructionModel .setValue (new InstructionModel (distanceFormatter , routeProgress ));
371363 summaryModel .setValue (new SummaryModel (getApplication (), distanceFormatter , routeProgress , timeFormatType ));
372364 navigationLocation .setValue (location );
@@ -377,11 +369,9 @@ public void onProgressChange(Location location, RouteProgress routeProgress) {
377369 private OffRouteListener offRouteListener = new OffRouteListener () {
378370 @ Override
379371 public void userOffRoute (Location location ) {
380- if (hasNetworkConnection ()) {
381- speechPlayer .onOffRoute ();
382- Point newOrigin = Point .fromLngLat (location .getLongitude (), location .getLatitude ());
383- handleOffRouteEvent (newOrigin );
384- }
372+ speechPlayer .onOffRoute ();
373+ Point newOrigin = Point .fromLngLat (location .getLongitude (), location .getLatitude ());
374+ handleOffRouteEvent (newOrigin );
385375 }
386376 };
387377
@@ -470,15 +460,6 @@ private void updateBannerInstruction(RouteProgress routeProgress, Milestone mile
470460 }
471461 }
472462
473- @ SuppressWarnings ( {"MissingPermission" })
474- private boolean hasNetworkConnection () {
475- if (connectivityManager == null ) {
476- return false ;
477- }
478- NetworkInfo activeNetwork = connectivityManager .getActiveNetworkInfo ();
479- return activeNetwork != null && activeNetwork .isConnectedOrConnecting ();
480- }
481-
482463 private void sendEventFeedback (FeedbackItem feedbackItem ) {
483464 if (navigationViewEventDispatcher != null ) {
484465 navigationViewEventDispatcher .onFeedbackSent (feedbackItem );
@@ -494,8 +475,7 @@ private void sendEventArrival(RouteProgress routeProgress) {
494475 private void handleOffRouteEvent (Point newOrigin ) {
495476 if (navigationViewEventDispatcher != null && navigationViewEventDispatcher .allowRerouteFrom (newOrigin )) {
496477 navigationViewEventDispatcher .onOffRoute (newOrigin );
497- OffRouteEvent event = new OffRouteEvent (newOrigin , routeProgress );
498- routeFetcher .fetchRouteFromOffRouteEvent (event );
478+ router .findRouteFrom (routeProgress );
499479 isOffRoute .setValue (true );
500480 }
501481 }
0 commit comments