/// <summary> /// This registers GPS services with the Shiny container as well as the delegate - you can also auto-start the listener when necessary background permissions are received /// </summary> /// <typeparam name="T">The IGpsDelegate to call</typeparam> /// <param name="builder">The servicecollection to configure</param> /// <param name="requestIfPermissionGranted">This will be called when permission is given to use GPS functionality (background permission is assumed when calling this - setting your GPS request to not use background is ignored)</param> /// <returns></returns> public static bool UseGps <T>(this IServiceCollection builder, Action <GpsRequest> requestIfPermissionGranted = null) where T : class, IGpsDelegate { if (!builder.UseGps()) { return(false); } builder.AddSingleton <IGpsDelegate, T>(); if (requestIfPermissionGranted != null) { builder.RegisterPostBuildAction(async sp => { var mgr = sp.GetService <IGpsManager>(); var access = await mgr.RequestAccess(true); if (access == AccessState.Available) { var request = new GpsRequest(); requestIfPermissionGranted(request); request.UseBackground = true; await mgr.StartListener(request); } }); } return(true); }
async Task SetGps() { this.IsGpsSyncEnabled = await this.syncManager.IsMonitoring(LocationSyncType.GPS); this.WhenAnyValue(x => x.IsGeofenceSyncEnabled) .Skip(1) .Subscribe(async enabled => { try { if (enabled) { await this.syncManager.StartGpsMonitoring(GpsRequest.Realtime(true)); } else { await this.syncManager.StopMonitoring(LocationSyncType.Geofence); } } catch (Exception ex) { await this.dialogs.Alert(ex.ToString()); } }) .DisposeWith(this.DeactivateWith); }
public AccessViewModel(IJobManager jobs, IDialogs dialogs, INotificationManager?notifications = null, ISpeechRecognizer?speech = null, IGeofenceManager?geofences = null, IGpsManager?gps = null, IMotionActivityManager?activityManager = null, IBleManager?bluetooth = null, IBeaconRangingManager?beaconRanging = null, IBeaconMonitoringManager?beaconMonitoring = null, IPushManager?push = null, INfcManager?nfc = null) { this.dialogs = dialogs; this.Append("Jobs", jobs, () => jobs.RequestAccess()); this.Append("Notifications", notifications, () => notifications !.RequestAccess()); this.Append("Speech", speech, () => speech !.RequestAccess()); this.Append("Motion Activity", activityManager, () => activityManager !.RequestAccess()); this.Append("GPS (Background)", gps, () => gps !.RequestAccess(GpsRequest.Realtime(true))); this.Append("Geofences", geofences, () => geofences !.RequestAccess()); this.Append("BluetoothLE Central", bluetooth, () => bluetooth !.RequestAccess().ToTask(CancellationToken.None)); this.Append("iBeacons (Ranging)", beaconRanging, () => beaconRanging !.RequestAccess()); this.Append("iBeacons (Monitoring)", beaconMonitoring, () => beaconMonitoring !.RequestAccess()); this.Append("Push", push, async() => { var status = await push !.RequestAccess(); return(status.Status); });
public async Task Run(CancellationToken token) { if (_gpsManager.IsListening) { return; } #region Subscribe GPS reading event //購読重複を避けるために購読を解除する _gpsObserver?.Dispose(); //位置情報取得イベントを購読 _gpsObserver = _gpsManager .WhenReading() .Subscribe(async x => { if (token.IsCancellationRequested) { await _gpsManager.StopListener(); _gpsObserver?.Dispose(); return; } MainThread.BeginInvokeOnMainThread(() => { var message = new LocationReadMessage { GpsInfo = x, }; MessagingCenter.Send(message, nameof(LocationReadMessage)); }); await _notificationManager.ScheduleNotification($"{x.Position.Latitude}, {x.Position.Longitude}", "test"); }); #endregion #region GPS start listen. //位置情報取得許可を得る var checkResult = await Permissions.CheckStatusAsync <Permissions.LocationAlways>(); if (checkResult != PermissionStatus.Granted) { var requestResult = await Permissions.RequestAsync <Permissions.LocationAlways>(); if (requestResult != PermissionStatus.Granted) { return; } } var request = new GpsRequest { Interval = TimeSpan.FromSeconds(5), UseBackground = false, }; await _gpsManager.StartListener(request); #endregion }
public AccessViewModel(IJobManager jobs, INotificationManager?notifications = null, ISpeechRecognizer?speech = null, IGeofenceManager?geofences = null, IGpsManager?gps = null, IMotionActivityManager?activityManager = null, IBleManager?bluetooth = null, IPushManager?push = null) { this.Append("Jobs", AccessState.Unknown, () => jobs.RequestAccess()); if (notifications != null) { this.Append("Notifications", AccessState.Unknown, () => notifications.RequestAccess()); } if (speech != null) { this.Append("Speech", AccessState.Unknown, () => speech.RequestAccess()); } if (activityManager != null) { this.Append("Motion Activity", AccessState.Unknown, () => activityManager.RequestPermission()); } if (gps != null) { var request = new GpsRequest { UseBackground = true }; this.Append("GPS (Background)", gps.GetCurrentStatus(request), () => gps.RequestAccess(request)); } if (geofences != null) { this.Append("Geofences", geofences.Status, () => geofences.RequestAccess()); } if (bluetooth != null) { this.Append("BluetoothLE Central", bluetooth.Status, () => bluetooth.RequestAccess().ToTask(CancellationToken.None)); } if (push != null) { this.Append("Push", AccessState.Unknown, async() => { var status = await push.RequestAccess(); return(status.Status); }); } }
public GpsViewModel(IGpsManager gps) { this.Start = ReactiveCommand.CreateFromTask( async() => { this.foreground = gps .WhenReading() .Subscribe(reading => { this.Latitude = reading.Position.Latitude; this.Longitude = reading.Position.Longitude; }); await gps.StartListener(GpsRequest.Realtime(true)); } ); this.Stop = ReactiveCommand.CreateFromTask(async() => { await gps.StopListener(); this.foreground?.Dispose(); }); }
public GpsViewModel(IGpsManager manager, IUserDialogs dialogs) { this.manager = manager; this.IsUpdating = this.manager.IsListening; this.WhenAnyValue(x => x.UseBackground) .Subscribe(x => this.Access = this.manager.GetCurrentStatus(this.UseBackground).ToString()); this.WhenAnyValue(x => x.IsUpdating) .Select(x => x ? "Stop Listening" : "Start Updating") .ToPropertyEx(this, x => x.ListenerText); this.GetCurrentPosition = ReactiveCommand.CreateFromTask(async _ => { var result = await dialogs.RequestAccess(() => this.manager.RequestAccess(true)); if (!result) { return; } var reading = await this.manager.GetLastReading(); if (reading == null) { await dialogs.Alert("Could not getting GPS coordinates"); } else { this.SetValues(reading); } }); this.BindBusyCommand(this.GetCurrentPosition); this.SelectPriority = ReactiveCommand.Create(() => dialogs.ActionSheet( new ActionSheetConfig() .SetTitle("Select Priority/Desired Accuracy") .Add("Highest", () => this.Priority = GpsPriority.Highest) .Add("Normal", () => this.Priority = GpsPriority.Normal) .Add("Low", () => this.Priority = GpsPriority.Low) .AddCancel() )); this.ToggleUpdates = ReactiveCommand.CreateFromTask( async() => { if (this.manager.IsListening) { await this.manager.StopListener(); this.gpsListener?.Dispose(); } else { var result = await dialogs.RequestAccess(() => this.manager.RequestAccess(this.UseBackground)); if (!result) { await dialogs.Alert("Insufficient permissions"); return; } var request = new GpsRequest { UseBackground = this.UseBackground, Priority = this.Priority, }; if (IsInterval(this.DesiredInterval)) { request.Interval = ToInterval(this.DesiredInterval); } if (IsInterval(this.ThrottledInterval)) { request.ThrottledInterval = ToInterval(this.ThrottledInterval); } await this.manager.StartListener(request); } this.IsUpdating = this.manager.IsListening; }, this.WhenAny( x => x.IsUpdating, x => x.DesiredInterval, x => x.ThrottledInterval, (u, i, t) => { if (u.GetValue()) { return(true); } var isdesired = IsInterval(i.GetValue()); var isthrottled = IsInterval(t.GetValue()); if (isdesired && isthrottled) { var desired = ToInterval(i.GetValue()); var throttle = ToInterval(t.GetValue()); if (throttle.TotalSeconds >= desired.TotalSeconds) { return(false); } } return(true); } ) ); this.UseRealtime = ReactiveCommand.Create(() => { var rt = GpsRequest.Realtime(false); this.ThrottledInterval = String.Empty; this.DesiredInterval = rt.Interval.TotalSeconds.ToString(); this.Priority = rt.Priority; }); this.RequestAccess = ReactiveCommand.CreateFromTask(async() => { var access = await this.manager.RequestAccess(this.UseBackground); this.Access = access.ToString(); }); this.BindBusyCommand(this.RequestAccess); }
public GpsViewModel(IGpsManager manager, IUserDialogs dialogs) { this.manager = manager; this.IsUpdating = this.manager.IsListening; this.WhenAnyValue(x => x.UseBackground) .Subscribe(x => this.Access = this.manager.GetCurrentStatus(this.UseBackground).ToString()); this.WhenAnyValue(x => x.IsUpdating) .Select(x => x ? "Stop Listening" : "Start Updating") .ToPropertyEx(this, x => x.ListenerText); this.GetCurrentPosition = ReactiveCommand.CreateFromTask(async _ => { var result = await dialogs.RequestAccess(() => this.manager.RequestAccess(true)); if (!result) { return; } var reading = await this.manager.GetLastReading(); if (reading == null) { dialogs.Alert("Could not getting GPS coordinates"); } else { this.SetValues(reading); } }); this.BindBusyCommand(this.GetCurrentPosition); this.SelectPriority = ReactiveCommand.Create(() => dialogs.ActionSheet( new ActionSheetConfig() .SetTitle("Select Priority/Desired Accuracy") .Add("Highest", () => this.Priority = GpsPriority.Highest) .Add("Normal", () => this.Priority = GpsPriority.Normal) .Add("Low", () => this.Priority = GpsPriority.Low) .SetCancel() )); this.ToggleUpdates = ReactiveCommand.CreateFromTask( async() => { if (this.manager.IsListening) { await this.manager.StopListener(); this.gpsListener?.Dispose(); } else { var result = await dialogs.RequestAccess(() => this.manager.RequestAccess(this.UseBackground)); if (!result) { dialogs.Alert("Insufficient permissions"); return; } var request = new GpsRequest { UseBackground = this.UseBackground, Priority = this.Priority }; var meters = ToDeferred(this.DeferredMeters); if (meters > 0) { request.DeferredDistance = Distance.FromMeters(meters); } var secs = ToDeferred(this.DeferredSeconds); if (secs > 0) { request.DeferredTime = TimeSpan.FromSeconds(secs); } await this.manager.StartListener(request); } this.IsUpdating = this.manager.IsListening; }, this.WhenAny( x => x.IsUpdating, x => x.DeferredMeters, x => x.DeferredSeconds, (u, m, s) => u.GetValue() || ( ToDeferred(m.GetValue()) >= 0 && ToDeferred(s.GetValue()) >= 0 ) ) ); this.RequestAccess = ReactiveCommand.CreateFromTask(async() => { var access = await this.manager.RequestAccess(this.UseBackground); this.Access = access.ToString(); }); this.BindBusyCommand(this.RequestAccess); }
public GpsViewModel(IGpsManager manager, IDialogs dialogs) { this.manager = manager; var l = this.manager.CurrentListener; this.IsUpdating = l != null; this.UseBackground = l?.UseBackground ?? true; this.Priority = l?.Priority ?? GpsPriority.Normal; this.DesiredInterval = l?.Interval.TotalSeconds.ToString() ?? String.Empty; this.ThrottledInterval = l?.ThrottledInterval?.TotalSeconds.ToString() ?? String.Empty; this.MinimumDistanceMeters = l?.MinimumDistance?.TotalMeters.ToString() ?? String.Empty; this.NotificationTitle = manager.Title; this.NotificationMessage = manager.Message; this.WhenAnyValue(x => x.IsUpdating) .Select(x => x ? "Stop Listening" : "Start Updating") .ToPropertyEx(this, x => x.ListenerText); this.WhenAnyValue(x => x.NotificationTitle) .Skip(1) .Subscribe(x => this.manager.Title = x) .DisposedBy(this.DestroyWith); this.WhenAnyValue(x => x.NotificationMessage) .Skip(1) .Subscribe(x => this.manager.Message = x) .DisposedBy(this.DestroyWith); this.GetCurrentPosition = this.CreateOneReading(dialogs, LocationRetrieve.Current); this.GetLastReading = this.CreateOneReading(dialogs, LocationRetrieve.Last); this.GetLastOrCurrent = this.CreateOneReading(dialogs, LocationRetrieve.LastOrCurrent); ReactiveCommand.Create(() => dialogs.ActionSheet( "Select Priority", false, ("Highest", () => this.Priority = GpsPriority.Highest), ("Normal", () => this.Priority = GpsPriority.Normal), ("Low", () => this.Priority = GpsPriority.Low) )); this.ToggleUpdates = ReactiveCommand.CreateFromTask( async() => { if (this.manager.CurrentListener != null) { await this.manager.StopListener(); this.gpsListener?.Dispose(); } else { var result = await dialogs.RequestAccess(async() => { var access = await this.manager.RequestAccess(new GpsRequest { UseBackground = this.UseBackground }); this.Access = access.ToString(); return(access); }); if (!result) { await dialogs.Alert("Insufficient permissions"); return; } var request = new GpsRequest { UseBackground = this.UseBackground, Priority = this.Priority, }; if (IsNumeric(this.DesiredInterval)) { request.Interval = ToInterval(this.DesiredInterval); } if (IsNumeric(this.ThrottledInterval)) { request.ThrottledInterval = ToInterval(this.ThrottledInterval); } if (IsNumeric(this.MinimumDistanceMeters)) { request.MinimumDistance = Distance.FromMeters(Int32.Parse(this.MinimumDistanceMeters)); } await this.manager.StartListener(request); } this.IsUpdating = this.manager.CurrentListener != null; }, this.WhenAny( x => x.IsUpdating, x => x.DesiredInterval, x => x.ThrottledInterval, x => x.MinimumDistanceMeters, (u, i, t, d) => { if (u.GetValue()) { return(true); } var isdesired = IsNumeric(i.GetValue()); var isthrottled = IsNumeric(t.GetValue()); var ismindist = IsNumeric(d.GetValue()); if (isdesired && isthrottled) { var desired = ToInterval(i.GetValue()); var throttle = ToInterval(t.GetValue()); if (throttle.TotalSeconds >= desired.TotalSeconds) { return(false); } } return(true); } ) ); this.UseRealtime = ReactiveCommand.Create(() => { var rt = GpsRequest.Realtime(false); this.ThrottledInterval = String.Empty; this.DesiredInterval = rt.Interval.TotalSeconds.ToString(); this.Priority = rt.Priority; }); this.RequestAccess = ReactiveCommand.CreateFromTask(async() => { var access = await this.manager.RequestAccess(new GpsRequest { UseBackground = this.UseBackground }); this.Access = access.ToString(); }); this.BindBusyCommand(this.RequestAccess); }
public Task StartListener(GpsRequest request = null) { this.LastGpsRequest = request; return(Task.CompletedTask); }
public static Task StartListener(GpsRequest request) => Current.StartListener(request);
public static IObservable <AccessState> WhenAccessStatusChanged(GpsRequest request) => Current.WhenAccessStatusChanged(request);
public static Task <AccessState> RequestAccess(GpsRequest request) => Current.RequestAccess(request);
public static AccessState GetCurrentStatus(GpsRequest request) => Current.GetCurrentStatus(request);