Esempio n. 1
0
        private async Task SaveWeatherData()
        {
            // Save location query
            weather.query = location.query;

            // Save weather alerts
            await SaveWeatherAlerts();

            await Settings.SaveWeatherData(weather);

#if !__ANDROID_WEAR__ && __ANDROID__
            // Update weather data for Wearables
            WearableDataListenerService.EnqueueWork(Application.Context,
                                                    new Android.Content.Intent(Application.Context, typeof(WearableDataListenerService))
                                                    .SetAction(WearableDataListenerService.ACTION_SENDWEATHERUPDATE));

            // Update cached weather data for widgets
            await Task.Run(() =>
            {
                if (WidgetUtils.Exists(location.query))
                {
                    var ids = WidgetUtils.GetWidgetIds(location.query);
                    foreach (int id in ids)
                    {
                        WidgetUtils.SaveWeatherData(id, weather);
                    }
                }
            });
#elif __ANDROID_WEAR__
            Settings.UpdateTime = weather.update_time.UtcDateTime;
#endif
        }
Esempio n. 2
0
        private async Task LoadWeatherData()
        {
            /*
             * If unable to retrieve saved data, data is old, or units don't match
             * Refresh weather data
             */

            Logger.WriteLine(LoggerLevel.Debug, "{0}: Loading weather data for {1}", TAG, location?.ToString());

            bool gotData = await LoadSavedWeatherData();

            if (!gotData)
            {
                Logger.WriteLine(LoggerLevel.Debug, "{0}: Saved weather data invalid for {1}", TAG, location?.ToString());
                Logger.WriteLine(LoggerLevel.Debug, "{0}: Retrieving data from weather provider", TAG);

                try
                {
                    if ((weather != null && weather.source != Settings.API) ||
                        (weather == null && location != null && location.source != Settings.API))
                    {
                        // Update location query and source for new API
                        string oldKey = location.query;

                        if (weather != null)
                        {
                            location.query = await wm.UpdateLocationQuery(weather);
                        }
                        else
                        {
                            location.query = await wm.UpdateLocationQuery(location);
                        }

                        location.source = Settings.API;

                        // Update database as well
#if !__ANDROID_WEAR__
                        if (location.locationType == LocationType.GPS)
                        {
                            Settings.SaveLastGPSLocData(location);
#if __ANDROID__
                            WearableDataListenerService.EnqueueWork(App.Context,
                                                                    new Android.Content.Intent(App.Context, typeof(WearableDataListenerService))
                                                                    .SetAction(WearableDataListenerService.ACTION_SENDLOCATIONUPDATE));
#endif
                        }
                        else
                        {
                            await Settings.UpdateLocationWithKey(location, oldKey);
                        }
#else
                        Settings.SaveHomeData(location);
#endif

#if WINDOWS_UWP
                        // Update tile id for location
                        if (SecondaryTileUtils.Exists(oldKey))
                        {
                            await SecondaryTileUtils.UpdateTileId(oldKey, location.query);
                        }
#elif __ANDROID__ && !__ANDROID_WEAR__
                        if (WidgetUtils.Exists(oldKey))
                        {
                            WidgetUtils.UpdateWidgetIds(oldKey, location);
                        }
#endif
                    }

                    await GetWeatherData();
                }
                catch (WeatherException wEx)
                {
                    errorCallback?.OnWeatherError(wEx);
                }
            }
        }
        private async Task <bool> UpdateLocation()
        {
            bool locationChanged = false;

            if (Settings.FollowGPS && (location == null || location.locationType == LocationType.GPS))
            {
                if (AppCompatActivity != null && ContextCompat.CheckSelfPermission(AppCompatActivity, Manifest.Permission.AccessFineLocation) != Permission.Granted &&
                    ContextCompat.CheckSelfPermission(AppCompatActivity, Manifest.Permission.AccessCoarseLocation) != Permission.Granted)
                {
                    ActivityCompat.RequestPermissions(AppCompatActivity, new String[] { Manifest.Permission.AccessCoarseLocation, Manifest.Permission.AccessFineLocation },
                                                      PERMISSION_LOCATION_REQUEST_CODE);
                    return(false);
                }

                LocationManager locMan       = AppCompatActivity?.GetSystemService(Context.LocationService) as LocationManager;
                bool            isGPSEnabled = (bool)locMan?.IsProviderEnabled(LocationManager.GpsProvider);
                bool            isNetEnabled = (bool)locMan?.IsProviderEnabled(LocationManager.NetworkProvider);

                Android.Locations.Location location = null;

                if (isGPSEnabled || isNetEnabled)
                {
                    Criteria locCriteria = new Criteria()
                    {
                        Accuracy = Accuracy.Coarse, CostAllowed = false, PowerRequirement = Power.Low
                    };
                    string provider = locMan.GetBestProvider(locCriteria, true);
                    location = locMan.GetLastKnownLocation(provider);

                    if (location == null)
                    {
                        locMan.RequestSingleUpdate(provider, mLocListnr, null);
                    }
                    else
                    {
                        LocationData lastGPSLocData = await Settings.GetLastGPSLocData();

                        // Check previous location difference
                        if (lastGPSLocData.query != null &&
                            mLocation != null && ConversionMethods.CalculateGeopositionDistance(mLocation, location) < 1600)
                        {
                            return(false);
                        }

                        if (lastGPSLocData.query != null &&
                            Math.Abs(ConversionMethods.CalculateHaversine(lastGPSLocData.latitude, lastGPSLocData.longitude,
                                                                          location.Latitude, location.Longitude)) < 1600)
                        {
                            return(false);
                        }

                        LocationQueryViewModel view = null;

                        await Task.Run(async() =>
                        {
                            view = await wm.GetLocation(location);

                            if (String.IsNullOrEmpty(view.LocationQuery))
                            {
                                view = new LocationQueryViewModel();
                            }
                        });

                        if (String.IsNullOrWhiteSpace(view.LocationQuery))
                        {
                            // Stop since there is no valid query
                            return(false);
                        }

                        // Save oldkey
                        string oldkey = lastGPSLocData.query;

                        // Save location as last known
                        lastGPSLocData.SetData(view, location);
                        Settings.SaveLastGPSLocData(lastGPSLocData);

                        App.Context.StartService(
                            new Intent(App.Context, typeof(WearableDataListenerService))
                            .SetAction(WearableDataListenerService.ACTION_SENDLOCATIONUPDATE));

                        this.location = lastGPSLocData;
                        mLocation     = location;

                        // Update widget ids for location
                        if (oldkey != null && WidgetUtils.Exists(oldkey))
                        {
                            WidgetUtils.UpdateWidgetIds(oldkey, lastGPSLocData);
                        }

                        locationChanged = true;
                    }
                }
                else
                {
                    AppCompatActivity?.RunOnUiThread(() =>
                    {
                        Toast.MakeText(AppCompatActivity, Resource.String.error_retrieve_location, ToastLength.Short).Show();
                    });
                }
            }

            return(locationChanged);
        }