示例#1
0
    static AwarenessFence CreateExercisingWithHeadphonesFence()
    {
        // DetectedActivityFence will fire when it detects the user performing the specified
        // activity.  In this case it's walking.
        var walkingFence = DetectedActivityFence.During(DetectedActivityFence.ActivityType.Walking);

        // There are lots of cases where it's handy for the device to know if headphones have been
        // plugged in or unplugged.  For instance, if a music app detected your headphones fell out
        // when you were in a library, it'd be pretty considerate of the app to pause itself before
        // the user got in trouble.
        var headphoneFence = HeadphoneFence.During(HeadphoneState.PluggedIn);

#pragma warning disable 0219
        // Combines multiple fences into a compound fence.  While the first two fences trigger
        // individually, this fence will only trigger its callback when all of its member fences
        // hit a true state.
        var notWalkingWithHeadphones = AwarenessFence.And(AwarenessFence.Not(walkingFence), headphoneFence);
#pragma warning restore 0219

        // We can even nest compound fences.  Using both "and" and "or" compound fences, this
        // compound fence will determine when the user has headphones in and is engaging in at least
        // one form of exercise.
        // The below breaks down to "(headphones plugged in) AND (walking OR running OR bicycling)"
        var exercisingWithHeadphonesFence = AwarenessFence.And(
            headphoneFence,
            AwarenessFence.Or(
                walkingFence,
                DetectedActivityFence.During(DetectedActivityFence.ActivityType.Running),
                DetectedActivityFence.During(DetectedActivityFence.ActivityType.OnBicycle)));

        return(exercisingWithHeadphonesFence);
    }
        private void UpdateRequestBuilder(int activityId, string activityName, ActivityPhase phase, FenceUpdateAction action, ref FenceUpdateRequestBuilder requestBuilder)
        {
            AwarenessFence fence = null;

            if (phase == ActivityPhase.Starting)
            {
                fence = DetectedActivityFence.Starting(activityId);
            }
            else if (phase == ActivityPhase.During)
            {
                fence = DetectedActivityFence.During(activityId);
            }
            else if (phase == ActivityPhase.Stopping)
            {
                fence = DetectedActivityFence.Stopping(activityId);
            }
            else
            {
                SensusException.Report("Unknown activity phase:  " + phase);
                return;
            }

            UpdateRequestBuilder(fence, activityName + "." + phase, action, ref requestBuilder);
        }