public void OnLocationChanged(Location location) { logDebug(LogLevel.Verbose, $"new loc {locationToString(location)}"); if (!statistics.CanUpdate()) { // don't alarm because we have already computing distance and it will sends the proper info // about if GPS-on alarm is OK this.signalTimer.Value.Update(canAlarm: false); return; } double dist = 0; try { dist = updateLocation(location); } finally { // alarm about GPS only if there is no off-track alarm this.signalTimer.Value.Update(canAlarm: dist <= 0); statistics.UpdateCompleted(dist, location.Accuracy); MainReceiver.SendDistance(this, statistics.SignedDistance); } }
public static MainReceiver Create(Context context) { var receiver = new MainReceiver(); var filter = new IntentFilter(); filter.AddAction(Message.Dbg); filter.AddAction(Message.Dist); filter.AddAction(Message.Alarm); context.RegisterReceiver(receiver, filter); return(receiver); }
public override StartCommandResult OnStartCommand(Intent intent, [GeneratedEnum] StartCommandFlags flags, int startId) { this.serviceLog.Value = new LogFile(this, "service.log", DateTime.UtcNow.AddDays(-2)); this.handler = new HandlerThread("GPSHandler"); this.handler.Start(); this.trackSegments.Value = readGpx(Preferences.LoadTrackFileName(this)); logDebug(LogLevel.Verbose, trackSegments.Value.Count.ToString() + " segs, with " + trackSegments.Value.Select(it => it.TrackPoints.Count()).Sum() + " points"); #if MOCK this.locationManager = new LocationManagerMock(trackSegments.Value.First().TrackPoints.EndPoint); #else this.locationManager = (LocationManager)GetSystemService(Context.LocationService); #endif loadPreferences(); { // start tracking this.lastAlarmAt = 0; this.statistics.Reset(); locationManager.RequestLocationUpdates(LocationManager.GpsProvider, 0, 0, this, this.handler.Looper); } // push Android to update us with gps positions getLastKnownPosition(); this.signalTimer.Value = new SignalTimer(logDebug, () => prefs.Value.NoGpsAlarmFirstTimeout, () => prefs.Value.NoGpsAlarmAgainInterval, () => alarms.Go(Alarm.PositiveAcknowledgement), () => { logDebug(LogLevel.Verbose, "gps off"); MainReceiver.SendAlarm(this, Message.NoSignalText); alarms.Go(Alarm.GpsLost); // weird, but RequestLocationUpdates does not force GPS provider to actually start providing updates // thus such try -- we will see if requesting single update will start it getLastKnownPosition(); // this gets OK location but is to weak to force GPS to start updating, if above will not work // we would have to manually request update one after another and rework alarms //this.locationManager.RequestSingleUpdate(LocationManager.GpsProvider, this, this.handler.Looper); }); this.receiver = ServiceReceiver.Create(this); receiver.UpdatePrefs += Receiver_UpdatePrefs; receiver.InfoRequest += Receiver_InfoRequest; logDebug(LogLevel.Info, "service started (+testing log)"); return(StartCommandResult.Sticky); }
private void logDebug(LogLevel level, string message) { try { logLocal(level, message); MainReceiver.SendDebug(this, message); } catch (Exception ex) { Common.Log($"CRASH logDebug {ex}"); } }
private void Receiver_InfoRequest(object sender, EventArgs e) { logLocal(LogLevel.Verbose, "Received info request"); if (this.signalTimer.Value.HasGpsSignal) { MainReceiver.SendDistance(this, statistics.SignedDistance); } else { MainReceiver.SendAlarm(this, Message.NoSignalText); } }
protected override void OnCreate(Bundle bundle) { try { // https://stackoverflow.com/questions/704311/android-how-do-i-investigate-an-anr // https://stackoverflow.com/questions/5513457/anr-keydispatchingtimedout-error/5513623#5513623 StrictMode.SetThreadPolicy(new StrictMode.ThreadPolicy.Builder() .DetectAll() .PenaltyLog() .Build()); StrictMode.SetVmPolicy(new StrictMode.VmPolicy.Builder() .DetectAll() .PenaltyLog() .Build()); base.OnCreate(bundle); this.debugMode = Common.IsDebugMode(this); this.log_writer = new LogFile(this, "app.log", DateTime.UtcNow.AddDays(-2)); logDebug(LogLevel.Info, "app started (+testing log)"); this.radarIntent = new Intent(this, typeof(RadarService)); SetContentView(Resource.Layout.Main); this.enableButton = FindViewById <Button>(Resource.Id.EnableButton); this.trackFileNameTextView = FindViewById <TextView>(Resource.Id.TrackFileNameTextView); this.gpsInfoTextView = FindViewById <TextView>(Resource.Id.GpsInfoTextView); this.trackInfoTextView = FindViewById <TextView>(Resource.Id.TrackInfoTextView); this.alarmInfoTextView = FindViewById <TextView>(Resource.Id.AlarmInfoTextView); this.infoTextView = FindViewById <TextView>(Resource.Id.InfoTextView); this.adapter = new ArrayAdapter <string>(this, Resource.Layout.ListViewItem, new List <string>()); ListAdapter = this.adapter; this.trackButton = FindViewById <Button>(Resource.Id.TrackButton); this.enableButton.Click += EnableButtonClicked; this.trackButton.Click += trackSelectionClicked; this.receiver = MainReceiver.Create(this); this.receiver.DistanceUpdate += Receiver_DistanceUpdate; this.receiver.AlarmUpdate += Receiver_AlarmUpdate; this.receiver.DebugUpdate += Receiver_DebugUpdate; this.logDebug(LogLevel.Verbose, "Done OnCreate"); } catch (Exception ex) { this.logDebug(LogLevel.Error, "OnCreate " + ex.ToString()); } }