示例#1
0
 void OnBikingStateChanged(BikingState previousState, BikingState newState)
 {
     if (previousState == BikingState.NotBiking && newState == BikingState.Biking)
     {
         // A biking trip was started, decrease activity recognition delay and show notification
         ShowNotification();
         if (actRecognitionHandler == null)
         {
             actRecognitionHandler = new ActivityRecognitionHandler(this);
         }
         actRecognitionHandler.SetTrackingEnabled(true, desiredDelay: TrackingDelay.Short);
     }
     else if (previousState != BikingState.NotBiking && newState == BikingState.NotBiking)
     {
         // A biking trip finished, hide notification and restore old activity recognition delay
         HideNotification();
         if (actRecognitionHandler == null)
         {
             actRecognitionHandler = new ActivityRecognitionHandler(this);
         }
         actRecognitionHandler.SetTrackingEnabled(true, desiredDelay: TrackingDelay.Long);
     }
     else if (previousState == BikingState.Biking &&
              (newState == BikingState.InGrace || newState == BikingState.MovingNotOnBike))
     {
         // We were put in grace, update the currently shown notification
         UpdateNotification(inGrace: true);
     }
     else if ((previousState == BikingState.InGrace || previousState == BikingState.MovingNotOnBike) &&
              newState == BikingState.Biking)
     {
         // We were put out of grace, revert the notification to its old style
         UpdateNotification(inGrace: false);
     }
 }
示例#2
0
        void CheckDelayedFinishTripConditions(DetectedActivity activity)
        {
            // Only increment the grace ID (thus invalidating existing delayed finish transactions
            // if the previous state was an active one.
            bool shouldIncrementGraceID =
                currentBikingState != BikingState.InGrace && currentBikingState != BikingState.MovingNotOnBike;

            // The service mistakenly thinks fast biking is a vehicule activity so we require an even stronger confidence
            if ((activity.Type == DetectedActivity.OnFoot && activity.Confidence > StrongConfidence) ||
                (activity.Type == DetectedActivity.InVehicle && activity.Confidence > ExtremeConfidence))
            {
                var id = graceID;
                if (shouldIncrementGraceID)
                {
                    id = Interlocked.Increment(ref graceID);
                }
                currentBikingState = BikingState.MovingNotOnBike;
                StartDelayedFinishTrip(id, (long)MovingNotBikePeriod.TotalMilliseconds);
            }
            else if ((activity.Type == DetectedActivity.OnBicycle && activity.Confidence < WeakConfidence) ||
                     (activity.Type != DetectedActivity.OnBicycle && activity.Confidence >= StrongConfidence))
            {
                var id = graceID;
                if (shouldIncrementGraceID)
                {
                    id = Interlocked.Increment(ref graceID);
                }
                if (currentBikingState != BikingState.InGrace)
                {
                    currentBikingState = BikingState.InGrace;
                    StartDelayedFinishTrip(id, (long)GracePeriod.TotalMilliseconds);
                }
            }
        }
示例#3
0
        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();
        }
示例#4
0
        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);
            }
        }
示例#5
0
 void OnBikingStateChanged(BikingState previousState, BikingState newState)
 {
     if (previousState == BikingState.NotBiking && newState == BikingState.Biking) {
         // A biking trip was started, decrease activity recognition delay and show notification
         ShowNotification ();
         if (actRecognitionHandler == null)
             actRecognitionHandler = new ActivityRecognitionHandler (this);
         actRecognitionHandler.SetTrackingEnabled (true, desiredDelay: TrackingDelay.Short);
     } else if (previousState != BikingState.NotBiking && newState == BikingState.NotBiking) {
         // A biking trip finished, hide notification and restore old activity recognition delay
         HideNotification ();
         if (actRecognitionHandler == null)
             actRecognitionHandler = new ActivityRecognitionHandler (this);
         actRecognitionHandler.SetTrackingEnabled (true, desiredDelay: TrackingDelay.Long);
     } else if (previousState == BikingState.Biking
                && (newState == BikingState.InGrace || newState == BikingState.MovingNotOnBike)) {
         // We were put in grace, update the currently shown notification
         UpdateNotification (inGrace: true);
     } else if ((previousState == BikingState.InGrace || previousState == BikingState.MovingNotOnBike)
                && newState == BikingState.Biking) {
         // We were put out of grace, revert the notification to its old style
         UpdateNotification (inGrace: false);
     }
 }
示例#6
0
        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);
        }
示例#7
0
        void CheckDelayedFinishTripConditions(DetectedActivity activity)
        {
            // Only increment the grace ID (thus invalidating existing delayed finish transactions
            // if the previous state was an active one.
            bool shouldIncrementGraceID =
                currentBikingState != BikingState.InGrace && currentBikingState != BikingState.MovingNotOnBike;

            // The service mistakenly thinks fast biking is a vehicule activity so we require an even stronger confidence
            if ((activity.Type == DetectedActivity.OnFoot && activity.Confidence > StrongConfidence)
                || (activity.Type == DetectedActivity.InVehicle && activity.Confidence > ExtremeConfidence)) {
                var id = graceID;
                if (shouldIncrementGraceID)
                    id = Interlocked.Increment (ref graceID);
                currentBikingState = BikingState.MovingNotOnBike;
                StartDelayedFinishTrip (id, (long)MovingNotBikePeriod.TotalMilliseconds);
            } else if ((activity.Type == DetectedActivity.OnBicycle && activity.Confidence < WeakConfidence)
                       || (activity.Type != DetectedActivity.OnBicycle && activity.Confidence >= StrongConfidence)) {
                var id = graceID;
                if (shouldIncrementGraceID)
                    id = Interlocked.Increment (ref graceID);
                if (currentBikingState != BikingState.InGrace) {
                    currentBikingState = BikingState.InGrace;
                    StartDelayedFinishTrip (id, (long)GracePeriod.TotalMilliseconds);
                }
            }
        }
示例#8
0
        internal void FinishTrip()
        {
            TripDebugLog.EndBikeTrip ();

            SetLocationUpdateEnabled (false);

            // Do we have a trip?
            if (lastLocation != null && currentDistance > 0 && 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 ();
        }