Esempio n. 1
0
        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);
        }
Esempio n. 2
0
 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);
     }
 }