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); }
static AwarenessFence CreateSunriseOrSunsetFence() { return(AwarenessFence.Or( TimeFence.AroundTimeInstant(TimeFence.TimeInstant.Sunrise, -HourInMillis, HourInMillis), TimeFence.AroundTimeInstant(TimeFence.TimeInstant.Sunset, -HourInMillis, HourInMillis) )); }
static AwarenessFence CreateLocationFence() { return(AwarenessFence.Or( LocationFence.Entering(0, 0, 1000), LocationFence.Exiting(0, 0, 1000), LocationFence.In(0, 0, 1000, 100) )); }
protected void UpdateRequestBuilder(AwarenessFence fence, string fenceKey, FenceUpdateAction action, ref FenceUpdateRequestBuilder requestBuilder) { if (action == FenceUpdateAction.Add) { requestBuilder.AddFence(fenceKey, fence, _fencePendingIntent); } else if (action == FenceUpdateAction.Remove) { requestBuilder.RemoveFence(fenceKey); } }
static AwarenessFence CreateBeaconFence() { var beaconTypes = new List <BeaconState.TypeFilter> { BeaconState.TypeFilter.With("awareness-api-1534415879510", "string") }; var found = BeaconFence.Found(beaconTypes); var near = BeaconFence.Near(beaconTypes); var lost = BeaconFence.Lost(beaconTypes); return(AwarenessFence.Or(found, near, lost)); }
static AwarenessFence CreateAnyTimeIntervalFence() { return(AwarenessFence.Or( TimeFence.InTimeInterval(TimeFence.TimeInterval.Weekday), TimeFence.InTimeInterval(TimeFence.TimeInterval.Weekend), TimeFence.InTimeInterval(TimeFence.TimeInterval.Holiday), TimeFence.InTimeInterval(TimeFence.TimeInterval.Morning), TimeFence.InTimeInterval(TimeFence.TimeInterval.Afternoon), TimeFence.InTimeInterval(TimeFence.TimeInterval.Evening), TimeFence.InTimeInterval(TimeFence.TimeInterval.Night) )); }
private async Task RequestLocationSnapshotAsync() { if (await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Location) == PermissionStatus.Granted) { // store current location ILocationResult locationResult = await Awareness.SnapshotApi.GetLocationAsync(AwarenessApiClient); global::Android.Locations.Location location = locationResult.Location; DateTimeOffset timestamp = new DateTimeOffset(1970, 1, 1, 0, 0, 0, new TimeSpan()).AddMilliseconds(location.Time); await StoreDatumAsync(new LocationDatum(timestamp, location.HasAccuracy ? location.Accuracy : -1, location.Latitude, location.Longitude)); // replace the previous location fence with one around the current location. additions and removals are handled // in the order specified below. FenceUpdateRequestBuilder locationFenceRequestBuilder = new FenceUpdateRequestBuilder(); UpdateRequestBuilder(null, AWARENESS_EXITING_LOCATION_FENCE_KEY, FenceUpdateAction.Remove, ref locationFenceRequestBuilder); AwarenessFence locationFence = LocationFence.Exiting(location.Latitude, location.Longitude, _locationChangeRadiusMeters); UpdateRequestBuilder(locationFence, AWARENESS_EXITING_LOCATION_FENCE_KEY, FenceUpdateAction.Add, ref locationFenceRequestBuilder); await UpdateFencesAsync(locationFenceRequestBuilder.Build()); } }
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); }
static AwarenessFence CreateHeadphonesFence() { // Will trigger when headphones are connected or disconnected return(AwarenessFence.Or(HeadphoneFence.During(HeadphoneState.PluggedIn), HeadphoneFence.PluggingIn(), HeadphoneFence.Unplugging())); }