Mapbox Navigation `offRoute()` called when first `step` finishes - android

So, I'm using version Mapbox Navigation 0.3.1, I'm fetching route from private OSRM server. Snap-to-route and other features are just working fine, however when 1st step finishes navigation wouldn't proceed to the next step and offRoute method is being called. I'm stuck here for like 1-2 months. I would appreciate any help.
Below, my code snippet of Mapbox Navigation implementation.
public class MapActivity implements MapboxMap.OnMarkerClickListener
,OnMapReadyCallback, AlertLevelChangeListener, ProgressChangeListener, OffRouteListener
,NavigationEventListener, LocationEngineListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
fabStartNavigation.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// star Navigation
startNavigation();
}
});
}
#Override
protected void onDestroy() {
super.onDestroy();
// Remove all navigation listeners being used
mNavigation.removeNavigationEventListener(this);
mNavigation.removeAlertLevelChangeListener(this);
mNavigation.removeProgressChangeListener(this);
mNavigation.removeOffRouteListener(this);
// End the navigation session
mNavigation.endNavigation();
mapView.onDestroy();
}
//navigation controller
private void startNavigation(){
mNavigation.addNavigationEventListener(this);
mNavigation.addAlertLevelChangeListener(this);
mNavigation.addOffRouteListener(this);
mNavigation.addProgressChangeListener(this);
mNavigation.setSnapToRoute(true);
/*LocationEngine mLocationEngine = LostLocationEngine.getLocationEngine(this);
// mNavigation.setLocationEngine(mLocationEngine);*/
mLocationEngine = LostLocationEngine.getLocationEngine(this);
mLocationEngine.setPriority(LocationEnginePriority.HIGH_ACCURACY);
mLocationEngine.addLocationEngineListener(this);
mLocationEngine.activate();
mNavigation.startNavigation(mPlannedRoute);
}
// navigation listeners
#Override
public void onAlertLevelChange(int alertLevel, RouteProgress routeProgress) {
switch (alertLevel) {
case HIGH_ALERT_LEVEL:
break;
case MEDIUM_ALERT_LEVEL:
break;
case LOW_ALERT_LEVEL:
break;
case ARRIVE_ALERT_LEVEL:
break;
case NONE_ALERT_LEVEL:
break;
case DEPART_ALERT_LEVEL:
break;
}
}
#Override
public void onProgressChange(Location location, RouteProgress routeProgress) {
}
#Override
public void userOffRoute(Location location) {
}
#Override
public void onRunning(boolean running) {
}
#Override
public void onConnected() {
mNavigation.setLocationEngine(mLocationEngine);
mLocationEngine.requestLocationUpdates();
}
#Override
public void onLocationChanged(Location location) {
}
}

This sounds like your running into a really old bug that has since been fixed. My advice would be to either upgrade to 0.4.0 or the 0.5.0-snapshot.

Related

Map Download by Specifying a Bounding Box implementation

i have read in the premium user guide that we can download a small amount of map with MapDataPrefetcher. i use a Mapfragment to show the map in my app, and it says that the MapEngine is automatically initialized by using MapFragment.
the problem is that i can't understand how to apply the methods and where i need to initialize the request for downloading the bounding box itself. its not so clear in the user guide.
if someone here can help me with that problem or send an example of the implementation i will be very grateful!
this is my init method for the map and the map fragment
private void initialize() {
setContentView(R.layout.activity_main);
// Search for the map fragment to finish setup by calling init().
// mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.mapfragment);
mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.mapfragment);
mLocationInfo = (TextView) findViewById(R.id.textViewLocationInfo);
mapFragment.setRetainInstance(false);
// Set up disk cache path for the map service for this application
// It is recommended to use a path under your application folder for storing the disk cache
boolean success = com.here.android.mpa.common.MapSettings.setIsolatedDiskCacheRootPath(
getApplicationContext().getExternalFilesDir(null)+File.separator + ".here-maps",
"com.example.andrey88.MapService"); /* ATTENTION! Do not forget to update {YOUR_INTENT_NAME} */
if (!success) {
Toast.makeText(getApplicationContext(), "Unable to set isolated disk cache path.", Toast.LENGTH_LONG);
} else {
mapFragment.init(new OnEngineInitListener() {
#Override
public void onEngineInitializationCompleted(OnEngineInitListener.Error error) {
if (error == OnEngineInitListener.Error.NONE) {
// retrieve a reference of the map from the map fragment
map = mapFragment.getMap();
// Set the map center to the Ruppin region (no animation)
map.setCenter(new GeoCoordinate(32.343099, 34.911272, 0.0), Map.Animation.NONE);
// Set the zoom level
map.setZoomLevel(17.8);
map.setMapScheme(Map.Scheme.PEDESTRIAN_DAY);
map.setLandmarksVisible(false);
map.setProjectionMode(Map.Projection.MERCATOR);
map.getPedestrianFeaturesVisible();
// position of user
map.addTransformListener(MainActivity.this);
mPositioningManager = PositioningManager.getInstance();
// check source of getting geo location
mHereLocation = LocationDataSourceHERE.getInstance(
new StatusListener() {
#Override
public void onOfflineModeChanged(boolean offline) {
// called when offline mode changes
}
#Override
public void onAirplaneModeEnabled() {
// called when airplane mode is enabled
}
#Override
public void onWifiScansDisabled() {
// called when Wi-Fi scans are disabled
}
#Override
public void onBluetoothDisabled() {
// called when Bluetooth is disabled
}
#Override
public void onCellDisabled() {
// called when Cell radios are switch off
}
#Override
public void onGnssLocationDisabled() {
// called when GPS positioning is disabled
}
#Override
public void onNetworkLocationDisabled() {
// called when network positioning is disabled
}
#Override
public void onServiceError(StatusListener.ServiceError serviceError) {
// called on HERE service error
}
#Override
public void onPositioningError(PositioningError positioningError) {
// called when positioning fails
}
#Override
public void onWifiIndoorPositioningNotAvailable() {
// called when running on Android 9.0 (Pie) or newer
}
});
if (mHereLocation == null) {
Toast.makeText(MainActivity.this, "LocationDataSourceHERE.getInstance(): failed, exiting", Toast.LENGTH_LONG).show();
finish();
}
mPositioningManager.setDataSource(mHereLocation);
mPositioningManager.addListener(new WeakReference<PositioningManager.OnPositionChangedListener>(MainActivity.this));
// Create a custom marker image
com.here.android.mpa.common.Image myImage = new com.here.android.mpa.common.Image();
try {myImage.setImageResource(R.drawable.marker_round);
} catch (IOException e) {
finish();
}
initMarkers(myImage);
createPolygon();
mapFragment.getMapGesture().addOnGestureListener(new MapGesture.OnGestureListener() {
#Override
public void onPanStart() {
}
#Override
public void onPanEnd() {
}
#Override
public void onMultiFingerManipulationStart() {
}
#Override
public void onMultiFingerManipulationEnd() {
}
#Override
public boolean onMapObjectsSelected(List<ViewObject> list) {
return false;
}
#Override
public boolean onTapEvent(PointF pointF) {
return false;
}
#Override
public boolean onDoubleTapEvent(PointF pointF) {
return false;
}
#Override
public void onPinchLocked() {
}
#Override
public boolean onPinchZoomEvent(float v, PointF pointF) {
return false;
}
#Override
public void onRotateLocked() {
}
#Override
public boolean onRotateEvent(float v) {
return false;
}
#Override
public boolean onTiltEvent(float v) {
return false;
}
#Override
public boolean onLongPressEvent(PointF pointF) {
return false;
}
#Override
public void onLongPressRelease() {
}
#Override
public boolean onTwoFingerTapEvent(PointF pointF) {
return false;
}
}, 0, false);
if (mPositioningManager.start(PositioningManager.LocationMethod.GPS_NETWORK_INDOOR)) {
mapFragment.getPositionIndicator().setVisible(true);
} else {
Toast.makeText(MainActivity.this, "PositioningManager.start: failed, exiting", Toast.LENGTH_LONG).show();
finish();
}
}
else {
Log.d("errorTag",error.name());
onDestroy();
}
}
});
}
}
MapEngine must be initialized before it can be used. It should be done in the main thread. MapEngine is automatically initialized for your application by using AndroidXMapFragment. AndroidXMapFragment is a fragment class applications can use as a UI module in an activity for map display. However, if your application does not use AndroidXMapFragment classes, then the application should initialize the MapEngine directly before using any HERE APIs. You can do this by calling MapEngine.init(ApplicationContext, OnEngineInitListener) as shown below:
MapEngine mapEngine = MapEngine.getInstance(); ApplicationContext appContext = new ApplicationContext(context); mapEngine.init(appContext, new OnEngineInitListener() { #Override public void onEngineInitializationCompleted(Error error) {
if (error == OnEngineInitListener.Error.NONE) {
// Post initialization code goes here
} else {
// handle factory initialization failure
} } });

Mapbox Navigation `onProgressChange()` not fired when 0.4.0-SNAPSHOT used

I've implemented Mapbox Navigation and lately got it working, now I want to update navigation from 0.3.1 to 0.4.0-SNAPSHOT, unfortunately after update onProgressChange() not being fired even though I haven't made any changes to code.
NOTE: onLocationChanged works in both cases.
EDIT
NOTE: 0.3.1 still fires onProgressChange() although its speed is 0.0, but not 0.4.0-SNAPSHOT
public class MapActivity implements MapboxMap.OnMarkerClickListener
,OnMapReadyCallback, AlertLevelChangeListener, ProgressChangeListener, OffRouteListener
,NavigationEventListener, LocationEngineListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
fabStartNavigation.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// star Navigation
startNavigation();
}
});
}
#Override
protected void onDestroy() {
super.onDestroy();
// Remove all navigation listeners being used
mNavigation.removeNavigationEventListener(this);
mNavigation.removeAlertLevelChangeListener(this);
mNavigation.removeProgressChangeListener(this);
mNavigation.removeOffRouteListener(this);
// End the navigation session
mNavigation.endNavigation();
mapView.onDestroy();
}
//navigation controller
private void startNavigation(){
mNavigation.addNavigationEventListener(this);
mNavigation.addAlertLevelChangeListener(this);
mNavigation.addOffRouteListener(this);
mNavigation.addProgressChangeListener(this);
mNavigation.setSnapToRoute(true);
/*LocationEngine mLocationEngine = LostLocationEngine.getLocationEngine(this);
// mNavigation.setLocationEngine(mLocationEngine);*/
mLocationEngine = LostLocationEngine.getLocationEngine(this);
mLocationEngine.setPriority(LocationEnginePriority.HIGH_ACCURACY);
mLocationEngine.addLocationEngineListener(this);
mLocationEngine.activate();
mNavigation.startNavigation(mPlannedRoute);
}
// navigation listeners
#Override
public void onAlertLevelChange(int alertLevel, RouteProgress routeProgress) {
switch (alertLevel) {
case HIGH_ALERT_LEVEL:
break;
case MEDIUM_ALERT_LEVEL:
break;
case LOW_ALERT_LEVEL:
break;
case ARRIVE_ALERT_LEVEL:
break;
case NONE_ALERT_LEVEL:
break;
case DEPART_ALERT_LEVEL:
break;
}
}
#Override
public void onProgressChange(Location location, RouteProgress routeProgress) {
}
#Override
public void userOffRoute(Location location) {
}
#Override
public void onRunning(boolean running) {
}
#Override
public void onConnected() {
mNavigation.setLocationEngine(mLocationEngine);
mLocationEngine.requestLocationUpdates();
}
#Override
public void onLocationChanged(Location location) {
}
}
Is it possible that the location updates you are receiving have no speed values? If the speed value is 0 or less, onProgressChange doesn't get called. If this isn't the issue, any additional information and code snippets will help to troubleshoot this.

Get data from viewpager fragments

I have an Activity which has a Save details button and a Viewpager which contains 4 fragments. The Fragments contains User details form. Once the Save button is clicked I need to get the data from all fragments and save the details. How to get the data entered by the user in all 4 fragments when Save button is clicked in the activity?
I just worked on an app that had the same use case. In addition, I had to save the data on a back navigation as well. The problem was a bit more difficult that I though it should have been. The problems came from the fact that not all the fragments in the ViewPager are guaranteed to be alive. They either may not have been started yet, or destroyed when the user paged off of them.
To solve the problem, I took inspiration from this blog post about handing back-press events. I had to modify it a bit to allow for any fragments that may be running and not just one.
public abstract class BackHandledFragment extends Fragment {
protected BackHandlerInterface backHandlerInterface;
public abstract String getTagText();
public abstract boolean onBackPressed();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(!(getActivity() instanceof BackHandlerInterface)) {
throw new ClassCastException("Hosting activity must implement BackHandlerInterface");
} else {
backHandlerInterface = (BackHandlerInterface) getActivity();
}
}
#Override
public void onResume() {
super.onResume();
backHandlerInterface.addRunningFragment(this);
}
#Override
public void onPause() {
super.onPause();
backHandlerInterface.removeRunningFragment(this);
}
public interface BackHandlerInterface {
public void addRunningFragment(BackHandledFragment backHandledFragment);
public void removeRunningFragment(BackHandledFragment backHandledFragment);
}
}
The Activity implements the interface and tracks the active fragments:
public class EditActivity implements BackHandledFragment.BackHandlerInterface
{
private List<BackHandledFragment> listActiveFragments = new ArrayList<>
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Notify each active fragment that the back was pressed, this will allow
// them to save any data.
for (BackHandledFragment bf : listActiveFragments) {
bf.onBackPressed();
}
}
#Override
public void addRunningFragment(BackHandledFragment backHandledFragment) {
listActiveFragments.add(backHandledFragment);
}
#Override
public void removeRunningFragment(BackHandledFragment backHandledFragment) {
listActiveFragments.remove(backHandledFragment);
}();
}
Each fragment must extend BackHandledFragment:
public class DetailNetworkFragment extends BackHandledFragment {
#Override
public void onPause() {
super.onPause();
EventBus.getDefault().unregister(this);
saveDataFields();
}
#Override
public void onResume() {
super.onResume();
EventBus.getDefault().register(this);
}
#Override
public String getTagText() {
return TAG;
}
#Override
public boolean onBackPressed() {
saveDataFields();
return false;
}
}
The saveDataFields() is not too interesting. It just copies the data out of the UI views and saves them back to an object in the Activity.

Android vungle event listener

I'm having a problem with vungle, I want the dialog fragment to appear after the video ad ends, my code looks good and there is no errors, the video starts and ends but my dialog fragment never appears, here is my code:
private final EventListener vungleListener = new EventListener(){
#Override
public void onVideoView(boolean isCompletedView, int watchedMillis, int videoDurationMillis) {
if (isCompletedView){
frag.show(fm, "");
}
}
#Override
public void onAdStart() {
// Called before playing an ad
}
#Override
public void onAdEnd(boolean wasCallToActionClicked) {
}
#Override
public void onAdPlayableChanged(boolean isAdPlayable) {
}
#Override
public void onAdUnavailable(String reason) {
}
};
According to Vungle the method onVideoView is deprecated and the method onAdEnd should be used instead. You should try moving the fragment being shown to onAdEnd like below:
#Override
public void onAdEnd(boolean wasCallToActionClicked) {
frag.show(fm, "");
}

How does automatic activity tracking work for google analytics android

I've implemented something similar for iOS using a quick swizzle of viewDidAppear to allow my company to track flow around the app in our own internal systems.
Now, ideally I'd like to avoid having to implement appear & disappear tracking in every activity for our internal use, so was hoping someone could shed some light into how the GA library achieves this.
I had a good google around and couldn't find any kind of internal event that's posted when an activity comes into the foreground so am at a bit of a loss at the moment.
Cheers!
I know the following is not a direct answer to your question but why not applying a basic OOP principle? Inheritance.
import android.app.Activity;
public class BaseActivity extends Activity {
#Override
protected void onResume() {
super.onResume();
// hit when activity appears. Tell Appserver!
}
#Override
protected void onPause() {
super.onPause();
// hit when activity hides. Tell Appserver!
}
}
and then have your other Acivities extend this instead of android.app.Activity?
You can use ActivityLifecycleCallback. Example below:
public class MordorApplication extends Application {
#Override
public void onCreate() {
super.onCreate();
registerActivityLifecycleCallbacks(new ActivityLifecycleLogger());
}
}
public class ActivityLifecycleLogger implements Application.ActivityLifecycleCallbacks {
#Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
createActivityStateLog(activity, "created");
}
#Override
public void onActivityStarted(Activity activity) {
createActivityStateLog(activity, "started");
}
#Override
public void onActivityResumed(Activity activity) {
createActivityStateLog(activity, "resumed");
}
#Override
public void onActivityPaused(Activity activity) {
createActivityStateLog(activity, "paused");
}
#Override
public void onActivityStopped(Activity activity) {
createActivityStateLog(activity, "stopped");
}
#Override
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
createActivityStateLog(activity, "savingStateInstance");
}
#Override
public void onActivityDestroyed(Activity activity) {
createActivityStateLog(activity, "destroyed");
}
private void createActivityStateLog(Activity activity, String state) {
String log = String.format("Activity %s - %s", activity.getClass().getName(), state);
LOG.debug(log);
}
private static final Logger LOG = LoggerFactory.getLogger(ActivityLifecycleLogger.class);
}
Google Analytics has automatic Activity tracking feature.
See
https://developers.google.com/analytics/devguides/collection/android/v4/?hl=en#analytics-xml
or do John's answer :)
You can create delegate class (suppose GaUtils) then call that on resume/pause. Just one line, don't you?
#Override
protected void onPause() {
super.onPause();
GaUtils.onPause(screenName); // track pause
}

Categories

Resources