private void OnAddressSearch(string src, bool useGeolocation, int?addressCount)
 {
     try
     {
         if (!_isWaiting)
         {
             ShowProgressbar(true);
             var intent =
                 AndroidUtil.CreateExplicitFromImplicitIntent(this,
                                                              new Intent(LocationBoundService.ActionGetAddress));
             if (!string.IsNullOrEmpty(src))
             {
                 intent.PutExtra(LocationBoundService.ExtraAddressValue, src);
             }
             if (addressCount.HasValue)
             {
                 intent.PutExtra(LocationBoundService.ExtraAddressCount, addressCount.Value);
             }
             intent.PutExtra(LocationBoundService.ExtraUseGeolocation, useGeolocation);
             StartService(intent);
         }
     }
     catch (Exception ex)
     {
         Log.Error(ex);
         ShowProgressbar(false);
     }
 }
예제 #2
0
        public string GetMd5Code()
        {
            if (_contractResolver == null)
            {
                _contractResolver = new LocationAddressDynamicContractResolver(new[] { nameof(DisplayLocality) });
            }

            var result = JsonConvert.SerializeObject(this, GetJsonSerializerSettings(_contractResolver));

            return(AndroidUtil.GetMd5Code(result, false));
        }
예제 #3
0
        protected override void OnResume()
        {
            base.OnResume();

            try
            {
                // если изменился settings
                GeoTrackingServiceStartSafeAsync();

                OnUpdateSettings();
                UpdateLocationAsync();
                _menuFragment?.UpdateMenu();
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }

            SubscribeEvents();

            ValidateServiceReceiver();
            var intentFilter = new IntentFilter(WeatherBoundService.ActionGetWeatherData)
            {
                Priority = (int)IntentFilterPriority.HighPriority
            };

            RegisterReceiver(_serviceReceiver, intentFilter);

            _serviceConnection?.Dispose();
            _serviceConnection = new ServiceConnection(this);
            var serviceIntent = AndroidUtil.CreateExplicitFromImplicitIntent(this,
                                                                             new Intent(WeatherBoundService.ActionGetWeatherData));

            serviceIntent.SetAction(WeatherBoundService.ActionGetWeatherData);
            BindService(serviceIntent, _serviceConnection, Bind.AutoCreate);

            if (_needRefresh)
            {
                _needRefresh = false;
                OnRefresh(showProgressbar: true, isOnlyDaily: false, infoReason: "_needRefresh = true");
                return;
            }

            if (_needWeatherUpdate)
            {
                _needWeatherUpdate = false;
                UpdateWeatherContentFragmentAsync(_cancellationTokenSource?.Token);
            }
        }
        protected override void OnResume()
        {
            base.OnResume();

            SubscribeEvents();

            ValidateServiceReceiver();
            var intentFilter = new IntentFilter(LocationBoundService.ActionGetAddress)
            {
                Priority = (int)IntentFilterPriority.HighPriority
            };

            RegisterReceiver(_serviceReceiver, intentFilter);

            _serviceConnection?.Dispose();
            _serviceConnection = new ServiceConnection(this);
            var serviceIntent =
                AndroidUtil.CreateExplicitFromImplicitIntent(this, new Intent(LocationBoundService.ActionGetAddress));

            BindService(serviceIntent, _serviceConnection, Bind.AutoCreate);
        }
예제 #5
0
        private void OnRefresh(bool showProgressbar, bool isOnlyDaily, string infoReason)
        {
            try
            {
                _needWeatherUpdate = false;

                if (showProgressbar)
                {
                    if (_isWaiting)
                    {
                        return;
                    }

                    ShowProgressbar(true);
                }

                var intent =
                    AndroidUtil.CreateExplicitFromImplicitIntent(this,
                                                                 new Intent(WeatherBoundService.ActionGetWeatherData));
                if (isOnlyDaily)
                {
                    intent.PutExtra(WeatherBoundService.ExtraIsOnlyDaily, true);
                }
                else
                {
                    Log.InfoFormat("OnRefresh ('{0}').", infoReason);
                    UpdateDisplayLocation(null);
                    _contentFragment?.UpdateView(null, null, null, null, null, null, out bool _);
                }
                StartService(intent);
            }
            catch (Exception ex)
            {
                Log.Error(ex);
                ShowProgressbar(false);
            }
        }
예제 #6
0
        public void StopService(Context context, Intent intent)
        {
            var explicitintent = AndroidUtil.CreateExplicitFromImplicitIntent(context, intent);

            context.StopService(explicitintent);
        }
예제 #7
0
        private WeatherForecast GetWeatherData(Context context, ISb49SecureString apiKey, IWeatherProviderService weatherProvider,
                                               IWeatherLocationAddress address, string languageCode, CancellationToken token)
        {
            if (weatherProvider == null)
            {
                throw new WeatherProviderException();
            }

            if (apiKey == null || !apiKey.Validate())
            {
                throw new WeatherApiKeyException();
            }

            if (address == null || !address.IsValid())
            {
                throw new LocationException();
            }

            var permissionChecker = new Sb49PermissionChecker();
            var permissions       = new[] { Manifest.Permission.Internet, Manifest.Permission.AccessNetworkState };
            var status            = permissionChecker.CheckPermissionAsync(context, permissions).Result;

            if (status != PermissionStatus.Granted)
            {
                throw new Java.Lang.SecurityException(string.Format("Permission denied. Missing {0} permissions.",
                                                                    string.Join(", ", permissions.Select(p => string.Format("'{0}'", p)))));
            }

            weatherProvider.ApiKey = apiKey;
            if (weatherProvider.OptionalParameters == null)
            {
                weatherProvider.OptionalParameters = new OptionalParameters();
            }
            weatherProvider.OptionalParameters.LanguageCode = languageCode;

            using (var connectivityManager = (ConnectivityManager)context.GetSystemService(Context.ConnectivityService))
            {
                if (!AndroidUtil.IsNetworkAvailable(connectivityManager))
                {
                    throw new Sb49HttpRequestException();
                }

                Log.DebugFormat(
                    "Weather provider: '{0}', Uri: '{1}', Lan: '{2}', Count: '{3}', Connectivity type: '{4}', Address: '{5}'.",
                    weatherProvider.ProviderId, weatherProvider.BaseUri, languageCode,
                    weatherProvider.RequestCounter?.Count,
                    connectivityManager.ActiveNetworkInfo?.Type,
                    address);
            }

            // TwilightCache
            var twilightCacheManager = new TwilightCacheManager();

            try
            {
                var twilightCache = twilightCacheManager.RetrieveData(context, AppSettings.TwilightCacheKey);
                weatherProvider.SetSunInfoCache(twilightCache);
            }
            catch (Newtonsoft.Json.JsonSerializationException ex)
            {
                Log.Error(ex);
                twilightCacheManager.Remove(context, AppSettings.TwilightCacheKey);
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }

            var result = weatherProvider.GetForecast(address, token: token);

            if (result != null)
            {
                if (!address.Latitude.HasValue)
                {
                    address.Latitude = weatherProvider.Latitude;
                }
                if (!address.Longitude.HasValue)
                {
                    address.Longitude = weatherProvider.Longitude;
                }

                result.AddressHashCode = address.GetMd5Code();

                // TwilightCache
                try
                {
                    var cache = weatherProvider.GetSunInfoCache(TwilightCacheManager.MaxCount);
                    twilightCacheManager.CacheData(context, AppSettings.TwilightCacheKey, cache);
                }
                catch (Exception ex)
                {
                    Log.Error(ex);
                }
            }

            Log.Debug("Weather data updated.");

            return(result);
        }