/
NavigationViewActivity.java
409 lines (358 loc) · 13.7 KB
/
NavigationViewActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
package com.mapbox.services.android.navigation.testapp.activity.navigationui;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.Location;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ProgressBar;
import android.widget.Toast;
import com.mapbox.android.core.location.LocationEngine;
import com.mapbox.android.core.location.LocationEngineListener;
import com.mapbox.android.core.location.LocationEngineProvider;
import com.mapbox.api.directions.v5.DirectionsCriteria;
import com.mapbox.api.directions.v5.models.DirectionsResponse;
import com.mapbox.api.directions.v5.models.DirectionsRoute;
import com.mapbox.core.constants.Constants;
import com.mapbox.geojson.LineString;
import com.mapbox.geojson.Point;
import com.mapbox.mapboxsdk.Mapbox;
import com.mapbox.mapboxsdk.annotations.Marker;
import com.mapbox.mapboxsdk.annotations.MarkerViewOptions;
import com.mapbox.mapboxsdk.camera.CameraPosition;
import com.mapbox.mapboxsdk.camera.CameraUpdateFactory;
import com.mapbox.mapboxsdk.exceptions.InvalidLatLngBoundsException;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.geometry.LatLngBounds;
import com.mapbox.mapboxsdk.maps.MapView;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
import com.mapbox.mapboxsdk.plugins.locationlayer.LocationLayerPlugin;
import com.mapbox.mapboxsdk.plugins.locationlayer.modes.RenderMode;
import com.mapbox.services.android.navigation.testapp.R;
import com.mapbox.services.android.navigation.ui.v5.NavigationLauncher;
import com.mapbox.services.android.navigation.ui.v5.NavigationLauncherOptions;
import com.mapbox.services.android.navigation.ui.v5.route.NavigationMapRoute;
import com.mapbox.services.android.navigation.ui.v5.route.OnRouteSelectionChangeListener;
import com.mapbox.services.android.navigation.v5.navigation.NavigationRoute;
import com.mapbox.services.android.navigation.v5.utils.LocaleUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Response;
import static com.mapbox.android.core.location.LocationEnginePriority.HIGH_ACCURACY;
public class NavigationViewActivity extends AppCompatActivity implements OnMapReadyCallback,
MapboxMap.OnMapLongClickListener, LocationEngineListener, OnRouteSelectionChangeListener {
private static final int CAMERA_ANIMATION_DURATION = 1000;
private static final int DEFAULT_CAMERA_ZOOM = 16;
private static final int CHANGE_SETTING_REQUEST_CODE = 1;
private LocationLayerPlugin locationLayer;
private LocationEngine locationEngine;
private NavigationMapRoute mapRoute;
private MapboxMap mapboxMap;
@BindView(R.id.mapView)
MapView mapView;
@BindView(R.id.launch_route_btn)
Button launchRouteBtn;
@BindView(R.id.loading)
ProgressBar loading;
@BindView(R.id.launch_btn_frame)
FrameLayout launchBtnFrame;
private Marker currentMarker;
private Point currentLocation;
private Point destination;
private DirectionsRoute route;
private LocaleUtils localeUtils;
private boolean locationFound;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation_view);
ButterKnife.bind(this);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(this);
localeUtils = new LocaleUtils();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.navigation_view_activity_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.settings:
showSettings();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void showSettings() {
startActivityForResult(new Intent(this, NavigationViewSettingsActivity.class), CHANGE_SETTING_REQUEST_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CHANGE_SETTING_REQUEST_CODE && resultCode == RESULT_OK) {
boolean shouldRefetch = data.getBooleanExtra(NavigationViewSettingsActivity.UNIT_TYPE_CHANGED, false)
|| data.getBooleanExtra(NavigationViewSettingsActivity.LANGUAGE_CHANGED, false);
if (destination != null && shouldRefetch) {
fetchRoute();
}
}
}
@SuppressWarnings( {"MissingPermission"})
@Override
protected void onStart() {
super.onStart();
mapView.onStart();
if (locationLayer != null) {
locationLayer.onStart();
}
}
@SuppressWarnings( {"MissingPermission"})
@Override
public void onResume() {
super.onResume();
mapView.onResume();
if (locationEngine != null) {
locationEngine.addLocationEngineListener(this);
if (!locationEngine.isConnected()) {
locationEngine.activate();
}
}
}
@Override
public void onPause() {
super.onPause();
mapView.onPause();
if (locationEngine != null) {
locationEngine.removeLocationEngineListener(this);
}
}
@Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}
@Override
protected void onStop() {
super.onStop();
mapView.onStop();
if (locationLayer != null) {
locationLayer.onStop();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
mapView.onDestroy();
if (locationEngine != null) {
locationEngine.removeLocationUpdates();
locationEngine.deactivate();
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}
@OnClick(R.id.launch_route_btn)
public void onRouteLaunchClick() {
launchNavigationWithRoute();
}
@Override
public void onMapReady(MapboxMap mapboxMap) {
this.mapboxMap = mapboxMap;
this.mapboxMap.setOnMapLongClickListener(this);
initLocationEngine();
initLocationLayer();
initMapRoute();
}
@Override
public void onMapLongClick(@NonNull LatLng point) {
destination = Point.fromLngLat(point.getLongitude(), point.getLatitude());
launchRouteBtn.setEnabled(false);
loading.setVisibility(View.VISIBLE);
setCurrentMarkerPosition(point);
if (currentLocation != null) {
fetchRoute();
}
}
@SuppressWarnings( {"MissingPermission"})
@Override
public void onConnected() {
locationEngine.requestLocationUpdates();
}
@Override
public void onLocationChanged(Location location) {
currentLocation = Point.fromLngLat(location.getLongitude(), location.getLatitude());
onLocationFound(location);
}
@Override
public void onNewPrimaryRouteSelected(DirectionsRoute directionsRoute) {
route = directionsRoute;
}
@SuppressWarnings( {"MissingPermission"})
private void initLocationEngine() {
locationEngine = new LocationEngineProvider(this).obtainBestLocationEngineAvailable();
locationEngine.setPriority(HIGH_ACCURACY);
locationEngine.setInterval(0);
locationEngine.setFastestInterval(1000);
locationEngine.addLocationEngineListener(this);
locationEngine.activate();
if (locationEngine.getLastLocation() != null) {
Location lastLocation = locationEngine.getLastLocation();
onLocationChanged(lastLocation);
currentLocation = Point.fromLngLat(lastLocation.getLongitude(), lastLocation.getLatitude());
}
}
@SuppressWarnings( {"MissingPermission"})
private void initLocationLayer() {
locationLayer = new LocationLayerPlugin(mapView, mapboxMap, locationEngine);
locationLayer.setRenderMode(RenderMode.COMPASS);
}
private void initMapRoute() {
mapRoute = new NavigationMapRoute(mapView, mapboxMap);
mapRoute.setOnRouteSelectionChangeListener(this);
}
private void fetchRoute() {
NavigationRoute.Builder builder = NavigationRoute.builder(this)
.accessToken(Mapbox.getAccessToken())
.origin(currentLocation)
.destination(destination)
.alternatives(true);
setFieldsFromSharedPreferences(builder);
builder.build()
.getRoute(new SimplifiedCallback() {
@Override
public void onResponse(Call<DirectionsResponse> call, Response<DirectionsResponse> response) {
if (validRouteResponse(response)) {
hideLoading();
route = response.body().routes().get(0);
if (route.distance() > 25d) {
launchRouteBtn.setEnabled(true);
mapRoute.addRoutes(response.body().routes());
boundCameraToRoute();
} else {
Snackbar.make(mapView, R.string.error_select_longer_route, Snackbar.LENGTH_SHORT).show();
}
}
}
});
loading.setVisibility(View.VISIBLE);
}
private void setFieldsFromSharedPreferences(NavigationRoute.Builder builder) {
builder
.language(getLanguageFromSharedPreferences())
.voiceUnits(getUnitTypeFromSharedPreferences());
}
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);
if (unitType.equals(defaultUnitType)) {
unitType = localeUtils.getUnitTypeForDeviceLocale(this);
}
return unitType;
}
private Locale 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)) {
return localeUtils.inferDeviceLocale(this);
} else {
return new Locale(language);
}
}
private boolean getShouldSimulateRouteFromSharedPreferences() {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
return sharedPreferences.getBoolean(getString(R.string.simulate_route_key), false);
}
private String getRouteProfileFromSharedPreferences() {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
return sharedPreferences.getString(
getString(R.string.route_profile_key), DirectionsCriteria.PROFILE_DRIVING_TRAFFIC
);
}
private void launchNavigationWithRoute() {
if (route == null) {
Snackbar.make(mapView, R.string.error_route_not_available, Snackbar.LENGTH_SHORT).show();
return;
}
NavigationLauncherOptions.Builder optionsBuilder = NavigationLauncherOptions.builder()
.shouldSimulateRoute(getShouldSimulateRouteFromSharedPreferences())
.directionsProfile(getRouteProfileFromSharedPreferences());
optionsBuilder.directionsRoute(route);
NavigationLauncher.startNavigation(this, optionsBuilder.build());
}
private boolean validRouteResponse(Response<DirectionsResponse> response) {
return response.body() != null && !response.body().routes().isEmpty();
}
private void hideLoading() {
if (loading.getVisibility() == View.VISIBLE) {
loading.setVisibility(View.INVISIBLE);
}
}
private void onLocationFound(Location location) {
if (!locationFound) {
animateCamera(new LatLng(location.getLatitude(), location.getLongitude()));
Snackbar.make(mapView, R.string.explanation_long_press_waypoint, Snackbar.LENGTH_LONG).show();
locationFound = true;
hideLoading();
}
}
public void boundCameraToRoute() {
if (route != null) {
List<Point> routeCoords = LineString.fromPolyline(route.geometry(),
Constants.PRECISION_6).coordinates();
List<LatLng> bboxPoints = new ArrayList<>();
for (Point point : routeCoords) {
bboxPoints.add(new LatLng(point.latitude(), point.longitude()));
}
if (bboxPoints.size() > 1) {
try {
LatLngBounds bounds = new LatLngBounds.Builder().includes(bboxPoints).build();
// left, top, right, bottom
int topPadding = launchBtnFrame.getHeight() * 2;
animateCameraBbox(bounds, CAMERA_ANIMATION_DURATION, new int[] {50, topPadding, 50, 100});
} catch (InvalidLatLngBoundsException exception) {
Toast.makeText(this, R.string.error_valid_route_not_found, Toast.LENGTH_SHORT).show();
}
}
}
}
private void animateCameraBbox(LatLngBounds bounds, int animationTime, int[] padding) {
CameraPosition position = mapboxMap.getCameraForLatLngBounds(bounds, padding);
mapboxMap.animateCamera(CameraUpdateFactory.newCameraPosition(position), animationTime);
}
private void animateCamera(LatLng point) {
mapboxMap.animateCamera(CameraUpdateFactory.newLatLngZoom(point, DEFAULT_CAMERA_ZOOM), CAMERA_ANIMATION_DURATION);
}
private void setCurrentMarkerPosition(LatLng position) {
if (position != null) {
if (currentMarker == null) {
MarkerViewOptions markerViewOptions = new MarkerViewOptions()
.position(position);
currentMarker = mapboxMap.addMarker(markerViewOptions);
} else {
currentMarker.setPosition(position);
}
}
}
}