예제 #1
0
        public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
        {
            var result = StartCommandResult.NotSticky;

            try
            {
                var permissionChecker = new Sb49PermissionChecker();
                var status            = permissionChecker.CheckPermissionAsync(this, permissionChecker.PermissionLocation).Result;
                if (status != PermissionStatus.Granted)
                {
                    _log.Error("Location permission denied.");
                    StopSelf();
                    return(result);
                }

                if (!AppSettings.Default.CheckIsGooglePlayServicesInstalled())
                {
                    StopSelf();
                    return(result);
                }

                if (_apiClient == null)
                {
                    _apiClient = new GoogleApiClient.Builder(this, this, this)
                                 .AddApi(LocationServices.API).Build();
                }

                if (_locRequest == null)
                {
                    _locRequest = new LocationRequest();
                }
                _apiClient.Connect();

                result           = StartCommandResult.Sticky;
                IsServiceRunning = true;
            }
            catch (Exception ex)
            {
                _log.Error(ex);
            }

            return(result);
        }
예제 #2
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);
        }