예제 #1
0
        protected override void OnResume()
        {
            try
            {
                this.logDebug(LogLevel.Verbose, "Entering OnResume");

                lastGpsEvent_debug = GpsEvent.Stopped;

                base.OnResume();

                logDebug(LogLevel.Verbose, "RESUMED");
                LocationManager lm = (LocationManager)GetSystemService(Context.LocationService);
                lm.AddGpsStatusListener(this);

                if (updateReadiness()) // gps could be switched meanwhile
                {
                    showAlarm("running", Android.Graphics.Color.GreenYellow);
                    ServiceReceiver.SendInfoRequest(this);
                }

                this.logDebug(LogLevel.Verbose, "Done OnResume");
            }
            catch (Exception ex)
            {
                this.logDebug(LogLevel.Error, "OnResume " + ex.ToString());
            }
        }
예제 #2
0
        public override void OnBackPressed()
        {
            Preferences.Save(this, new Preferences()
            {
                UseVibration            = vibrateCheckBox.Checked,
                OffTrackAlarmDistance   = int.Parse(distanceEditText.Text),
                OffTrackAlarmInterval   = TimeSpan.FromSeconds(int.Parse(offTrackIntervalEditText.Text)),
                NoGpsAlarmAgainInterval = TimeSpan.FromMinutes(int.Parse(this.noGpsIntervalEditText.Text)),
                NoGpsAlarmFirstTimeout  = TimeSpan.FromSeconds(int.Parse(this.noGpsTimeoutEditText.Text)),
                DistanceAudioVolume     = this.distanceSettings.Volume,
                DistanceAudioFileName   = this.distanceSettings.AudioFileName,
                GpsLostAudioVolume      = this.gpsLostSettings.Volume,
                GpsLostAudioFileName    = this.gpsLostSettings.AudioFileName,
                GpsOnAudioVolume        = this.gpsOnSettings.Volume,
                GpsOnAudioFileName      = this.gpsOnSettings.AudioFileName,
            });

            this.gpsLostSettings.Destroy();
            this.distanceSettings.Destroy();
            this.gpsOnSettings.Destroy();

            ServiceReceiver.SendUpdatePrefs(this);

            base.OnBackPressed();
        }
예제 #3
0
        public static ServiceReceiver Create(Context context)
        {
            var receiver = new ServiceReceiver();
            var filter   = new IntentFilter();

            filter.AddAction(Message.Prefs);
            filter.AddAction(Message.Req);
            context.RegisterReceiver(receiver, filter);
            return(receiver);
        }
예제 #4
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);
        }
예제 #5
0
        public override void OnDestroy()
        {
            try
            {
                logDebug(LogLevel.Info, "destroying service");

                this.signalTimer.Value.Dispose();

                logDebug(LogLevel.Verbose, "removing events handlers");

                this.receiver.UpdatePrefs -= Receiver_UpdatePrefs;
                this.receiver.InfoRequest -= Receiver_InfoRequest;

                logDebug(LogLevel.Verbose, "unregistering receiver");

                UnregisterReceiver(this.receiver);
                this.receiver = null;

                logDebug(LogLevel.Verbose, "removing GPS updates");

                locationManager.RemoveUpdates(this);

                logDebug(LogLevel.Verbose, "disposing alarms");

                this.alarms.Dispose();

                logDebug(LogLevel.Verbose, "disposing handler");

                this.handler.Dispose();

                logDebug(LogLevel.Verbose, "service destroyed " + statistics.ToString());

                this.serviceLog.Value.Dispose();

                base.OnDestroy();
            }
            catch (Exception ex)
            {
                logDebug(LogLevel.Error, $"exception during destroying service {ex}");
            }
        }