Пример #1
0
        private void OnAppStart(ServiceApi api)
        {
            // стартуем GeoTrackingService
            GeoTrackingServiceStart(api);

            // стартуем GeoTrackingService
            WeatherServiceStart(api);

            OnArrangeAppWidgetSettings(api);

            // пытаемся остановить GeoTrackingService
            GeoTrackingServiceStop(api);

            // пытаемся остановить WeatherService
            WeatherServiceStop(api);
        }
Пример #2
0
        protected override void OnHandleIntent(Intent intent)
        {
            var action = intent?.Action;

            if (string.IsNullOrEmpty(action))
            {
                return;
            }

            try
            {
                var api = new ServiceApi();

                switch (action)
                {
                case ActionAppStart:
                    OnAppStart(api);
                    break;

                case ActionGeoTrackingServiceStart:
                    OnGeoTrackingServiceStart(api);
                    break;

                case ActionWeatherServiceStart:
                    WeatherServiceStart(api);
                    break;

                case ActionAppServiceStart:
                    OnAppServiceStart(api);
                    break;

                case ActionAppServiceStop:
                    OnAppServiceStop(api);
                    break;
                }
            }
            catch (OperationCanceledException ex)
            {
                _log.Debug(ex);
            }
            catch (Exception ex)
            {
                _log.ErrorFormat("Action: '{0}'. {1}", action, ex);
            }
        }
Пример #3
0
        private void OnWeatherDataUpdate(int[] widgetsIds, PendingIntent pendingIntent)
        {
            if (widgetsIds == null || widgetsIds.Length == 0)
            {
                return;
            }

            try
            {
                var api = new ServiceApi();
                api.UpdateWeatherData(this, widgetsIds, CancellationToken.None);
                api.AppWidgetUpdate(this, widgetsIds);
            }
            finally
            {
                pendingIntent?.Send(this, Result.Ok, null);
            }
        }
Пример #4
0
        private void OnGetWeatherData(CancellationToken token)
        {
            if (IfCancellationRequested(token))
            {
                return;
            }

            WeatherForecast weather;

            if (IsOnlyDaily)
            {
                weather = AppSettings.Default.Weather;
            }
            else
            {
                var api = new ServiceApi();
                weather = api.GetWeatherData(this, AppSettings.Default, token);
                AppSettings.Default.Weather = weather;
            }
            OnGetWeatherDaily(weather, token);
        }
        private void OnGetAddress(Intent serviceIntent, CancellationToken token)
        {
            GeoAddresses = null;
            if (IfCancellationRequested(token))
            {
                return;
            }
            var addresses = new List <Address>();

            var useGeolocation = serviceIntent.GetBooleanExtra(ExtraUseGeolocation, false);
            var address        = serviceIntent.GetStringExtra(ExtraAddressValue);

            if (!useGeolocation)
            {
                var result = LocationAddress.Parse(address);
                GeoAddresses = new[] { result };
                return;
            }

            var api   = new ServiceApi();
            var count = serviceIntent.GetIntExtra(ExtraAddressCount, 10);

            if (string.IsNullOrEmpty(address))
            {
                if (IfCancellationRequested(token))
                {
                    return;
                }

                var addrs = api.GetCurrentLocations(this, token: token, geoMaxResult: count);
                if (addrs != null && addrs.Length > 0)
                {
                    addresses.AddRange(addrs);
                }
            }
            else
            {
                if (IfCancellationRequested(token))
                {
                    return;
                }

                var locationAddress = LocationAddress.Parse(address);

                var geo = new Sb49Geocoder();
                // ReSharper disable PossibleInvalidOperationException
                var task = locationAddress.HasCoordinatesOnly
                    ? geo.GetFromLocationAsync(this, locationAddress.Latitude.Value, locationAddress.Longitude.Value,
                                               count, token)
                    : geo.GetFromLocationNameAsync(this, address, count, token);
                // ReSharper restore PossibleInvalidOperationException
                api.TaskWait(task, api.WaitGeoMsec, token);

                var addrs = task.Result?.ToArray();
                if (addrs != null && addrs.Length > 0)
                {
                    addresses.AddRange(addrs);
                }
            }

            if (addresses.Count > 0)
            {
                if (IfCancellationRequested(token))
                {
                    return;
                }

                var locationAddress = addresses
                                      .Where(p => !string.IsNullOrEmpty(p.Locality) || p.HasLatitude && p.HasLongitude)
                                      .Select(p => new LocationAddress(p))
                                      .Distinct(new LocationAddressEqualityComparer())
                                      .ToArray();
                if (IfCancellationRequested(token))
                {
                    return;
                }

                if (locationAddress.Length > 0)
                {
                    if (IfCancellationRequested(token))
                    {
                        return;
                    }

                    var notEmptyLocalityAddress =
                        locationAddress.Where(p => !string.IsNullOrEmpty(p.Locality)).ToArray();
                    if (notEmptyLocalityAddress.Length > 0)
                    {
                        locationAddress = notEmptyLocalityAddress;
                    }

                    if (IfCancellationRequested(token))
                    {
                        return;
                    }

                    locationAddress = locationAddress
                                      .OrderBy(p => p.CountryCode)
                                      .ThenBy(p => p.PostalCode)
                                      .ThenBy(p => p.AdminArea)
                                      .ThenBy(p => p.Locality)
                                      .ToArray();
                    GeoAddresses = locationAddress;
                }
            }
        }
Пример #6
0
 private void OnAppServiceStart(ServiceApi api)
 {
     OnGeoTrackingServiceStart(api);
     WeatherServiceStart(api);
 }
Пример #7
0
 private void OnGeoTrackingServiceStart(ServiceApi api)
 {
     OnArrangeAppWidgetSettings(api);
     GeoTrackingServiceStartSafe(api);
 }
Пример #8
0
 private void OnArrangeAppWidgetSettings(ServiceApi api)
 {
     api.ArrangeAppWidgetSettings(this);
 }
Пример #9
0
 private void OnAppServiceStop(ServiceApi api)
 {
     GeoTrackingServiceStop(api);
     WeatherServiceStop(api);
     OnArrangeAppWidgetSettings(api);
 }
Пример #10
0
        private void OnAppWidgetUpdate(int[] widgetsIds)
        {
            var api = new ServiceApi();

            api.AppWidgetUpdate(this, widgetsIds);
        }