internal void FinishTrip() { TripDebugLog.EndBikeTrip(); SetLocationUpdateEnabled(false); // Do we have a trip? if (lastLocation != null && currentDistance > MinimumDistanceForTrip && currentFix > startFix) { var trip = new BikeTrip { Distance = currentDistance, StartTime = Epoch + TimeSpan.FromMilliseconds(startFix), EndTime = Epoch + TimeSpan.FromMilliseconds(currentFix) }; var dataApi = DataApi.Obtain(this); dataApi.AddTrip(trip).Wait(); TripDebugLog.LogNewTrip(trip.EndTime - trip.StartTime, trip.Distance); } lastLocation = null; currentDistance = startFix = currentFix = 0; var oldState = currentBikingState; currentBikingState = BikingState.NotBiking; OnBikingStateChanged(oldState, currentBikingState); TripDebugLog.CommitTripLog(); StopSelf(); }
void HandleLocationUpdate(Intent intent) { var newLocation = intent.GetParcelableExtra(FusedLocationProviderApi.KeyLocationChanged).JavaCast <Location> (); if (lastLocation != null) { if (currentBikingState == BikingState.Biking || currentBikingState == BikingState.InGrace) { currentDistance += lastLocation.DistanceTo(newLocation); } currentFix = newLocation.Time; } else { startFix = newLocation.Time; lastLocation = prevStoredLocation; if (lastLocation != null) { currentDistance += lastLocation.DistanceTo(newLocation); } prevStoredLocation = null; } TripDebugLog.LogPositionEvent(newLocation.Latitude, newLocation.Longitude, lastLocation == null ? 0 : lastLocation.DistanceTo(newLocation), currentDistance); lastLocation = newLocation; }
void HandleActivityRecognition(Intent intent) { var result = ActivityRecognitionResult.ExtractResult(intent); var activity = result.MostProbableActivity; TripDebugLog.LogActivityEvent(activity.Type, activity.Confidence); // We don't care about tilting activity if (activity.Type == DetectedActivity.Tilting) { return; } var prevBikingState = currentBikingState; switch (currentBikingState) { case BikingState.NotBiking: if (activity.Type == DetectedActivity.OnBicycle && activity.Confidence >= StrongConfidence) { SetLocationUpdateEnabled(true); currentBikingState = BikingState.Biking; } break; case BikingState.Biking: CheckDelayedFinishTripConditions(activity); break; case BikingState.MovingNotOnBike: if (activity.Type == DetectedActivity.OnBicycle && activity.Confidence >= StrongConfidence) { currentBikingState = BikingState.Biking; Interlocked.Increment(ref graceID); } break; case BikingState.InGrace: if (activity.Type == DetectedActivity.OnBicycle && activity.Confidence >= WeakConfidence) { currentBikingState = BikingState.Biking; Interlocked.Increment(ref graceID); } else { CheckDelayedFinishTripConditions(activity); } break; } Log.Debug("ActivityHandler", "Activity ({0} w/ {1}%): {2} -> {3}", activity.Type, activity.Confidence, prevBikingState, currentBikingState); if (prevBikingState != currentBikingState) { OnBikingStateChanged(prevBikingState, currentBikingState); } }
void StartDelayedFinishTrip(int id, long timeout) { TripDebugLog.DeferredBikeTripEnd(); var handler = new Handler(); handler.PostDelayed(() => { if ((currentBikingState == BikingState.MovingNotOnBike || currentBikingState == BikingState.InGrace) && id == graceID) { FinishTrip(); } }, timeout); }
void SetLocationUpdateEnabled(bool enabled) { if (currentRequest != ConnectionUpdateRequest.None) { return; } currentRequest = enabled ? ConnectionUpdateRequest.Start : ConnectionUpdateRequest.Stop; if (client == null) { client = CreateGoogleClient(); } if (!(client.IsConnected || client.IsConnecting)) { client.Connect(); } if (enabled) { TripDebugLog.StartBikeTrip(); } }