コード例 #1
0
        private async Task CancelDataSync()
        {
            if (timer.Enabled)
            {
                timer.Stop();
            }

            if (Settings.DataSync == WearableDataSync.DeviceOnly)
            {
                if (location == null && Settings.HomeData != null)
                {
                    // Load whatever we have available
                    location = Settings.HomeData;
                    wLoader  = new WeatherDataLoader(location, this, this);
                    await wLoader.ForceLoadSavedWeatherData();
                }
                else
                {
                    Activity?.RunOnUiThread(() =>
                    {
                        Toast.MakeText(Activity, GetString(Resource.String.werror_noweather), ToastLength.Long).Show();
                    });
                }
            }
        }
        private async Task <Weather> GetWeather()
        {
            Weather weather = null;

            try
            {
                if (Settings.DataSync == WearableDataSync.Off && Settings.FollowGPS)
                {
                    await UpdateLocation();
                }

                var wloader = new WeatherDataLoader(Settings.HomeData);

                if (Settings.DataSync == WearableDataSync.Off)
                {
                    await wloader.LoadWeatherData(false);
                }
                else
                {
                    await wloader.ForceLoadSavedWeatherData();
                }

                locData = Settings.HomeData;
                weather = wloader.GetWeather();
            }
            catch (Exception ex)
            {
                Logger.WriteLine(LoggerLevel.Error, ex, "SimpleWeather: {0}: GetWeather: error", TAG);
            }

            return(weather);
        }
コード例 #3
0
        private async Task DataSyncResume()
        {
            if (!receiverRegistered)
            {
                IntentFilter filter = new IntentFilter();
                filter.AddAction(WearableHelper.SettingsPath);
                filter.AddAction(WearableHelper.LocationPath);
                filter.AddAction(WearableHelper.WeatherPath);
                filter.AddAction(WearableHelper.IsSetupPath);

                LocalBroadcastManager.GetInstance(Activity)
                .RegisterReceiver(dataReceiver, filter);
                receiverRegistered = true;
            }

            /* Update view on resume
             * ex. If temperature unit changed
             */
            // New Page = loaded - true
            // Navigating back to frag = !loaded - false
            if (loaded || wLoader == null)
            {
                DataSyncRestore();
            }
            else if (wLoader != null && !loaded)
            {
                if (wLoader.GetWeather()?.IsValid() == true)
                {
                    Weather weather = wLoader.GetWeather();

                    /*
                     *  DateTime < 0 - This instance is earlier than value.
                     *  DateTime == 0 - This instance is the same as value.
                     *  DateTime > 0 - This instance is later than value.
                     */
                    if (Settings.UpdateTime.CompareTo(weather.update_time.UtcDateTime) > 0)
                    {
                        // Data was updated while we we're away; loaded it up
                        if (location == null || !location.Equals(Settings.HomeData))
                        {
                            location = Settings.HomeData;
                        }

                        Activity?.RunOnUiThread(() => refreshLayout.Refreshing = true);

                        wLoader = new WeatherDataLoader(this.location, this, this);
                        await wLoader.ForceLoadSavedWeatherData();
                    }
                    else
                    {
                        // Check weather data expiration
                        TimeSpan span = DateTimeOffset.Now - weather.update_time;
                        if (span.TotalMinutes > Settings.DefaultInterval)
                        {
                            // send request to refresh data on connected device
                            Activity?.StartService(new Intent(Activity, typeof(WearableDataListenerService))
                                                   .SetAction(WearableDataListenerService.ACTION_REQUESTWEATHERUPDATE)
                                                   .PutExtra(WearableDataListenerService.EXTRA_FORCEUPDATE, true));
                        }

                        weatherView.UpdateView(wLoader.GetWeather());
                        SetView(weatherView);
                        mCallback?.OnWeatherViewUpdated(weatherView);
                        loaded = true;
                    }
                }
                else
                {
                    // Data is null; restore
                    DataSyncRestore();
                }
            }
        }
コード例 #4
0
        public override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            // Create your fragment here
            if (Arguments != null)
            {
                using (var jsonTextReader = new Newtonsoft.Json.JsonTextReader(
                           new System.IO.StringReader(Arguments.GetString("data", null))))
                {
                    location = LocationData.FromJson(jsonTextReader);
                }

                if (location != null && wLoader == null)
                {
                    wLoader = new WeatherDataLoader(location, this, this);
                }
            }

            if (WearableHelper.IsGooglePlayServicesInstalled && !WearableHelper.HasGPS)
            {
                mFusedLocationClient         = new FusedLocationProviderClient(Activity);
                mLocCallback                 = new LocationCallback();
                mLocCallback.LocationResult += async(sender, e) =>
                {
                    mLocation = e.Result.LastLocation;

                    if (Settings.FollowGPS && await UpdateLocation())
                    {
                        // Setup loader from updated location
                        wLoader = new WeatherDataLoader(this.location, this, this);

                        await RefreshWeather(false);
                    }

                    await mFusedLocationClient.RemoveLocationUpdatesAsync(mLocCallback);
                };
            }
            else
            {
                mLocListnr = new Droid.Helpers.LocationListener();
                mLocListnr.LocationChanged += async(Android.Locations.Location location) =>
                {
                    if (Settings.FollowGPS && await UpdateLocation())
                    {
                        // Setup loader from updated location
                        wLoader = new WeatherDataLoader(this.location, this, this);

                        await RefreshWeather(false);
                    }
                };
            }

            dataReceiver = new LocalBroadcastReceiver();
            dataReceiver.BroadcastReceived += async(context, intent) =>
            {
                if (WearableHelper.LocationPath.Equals(intent?.Action) ||
                    WearableHelper.WeatherPath.Equals(intent?.Action))
                {
                    if (WearableHelper.WeatherPath.Equals(intent.Action) ||
                        (!loaded && location != null))
                    {
                        if (timer.Enabled)
                        {
                            timer.Stop();
                        }

                        // We got all our data; now load the weather
                        wLoader = new WeatherDataLoader(location, this, this);
                        await wLoader.ForceLoadSavedWeatherData();
                    }

                    if (WearableHelper.LocationPath.Equals(intent.Action))
                    {
                        // We got the location data
                        location = Settings.HomeData;
                        loaded   = false;
                    }
                }
                else if (WearableHelper.ErrorPath.Equals(intent?.Action))
                {
                    // An error occurred; cancel the sync operation
                    await CancelDataSync();
                }
                else if (WearableHelper.IsSetupPath.Equals(intent?.Action))
                {
                    if (Settings.DataSync != WearableDataSync.Off)
                    {
                        bool isDeviceSetup = intent.GetBooleanExtra(WearableDataListenerService.EXTRA_DEVICESETUPSTATUS, false);
                        var  connStatus    = (WearConnectionStatus)intent.GetIntExtra(WearableDataListenerService.EXTRA_CONNECTIONSTATUS, 0);

                        if (isDeviceSetup &&
                            connStatus == WearConnectionStatus.Connected)
                        {
                            // Device is setup and connected; proceed with sync
                            Activity?.StartService(new Intent(Activity, typeof(WearableDataListenerService))
                                                   .SetAction(WearableDataListenerService.ACTION_REQUESTLOCATIONUPDATE));
                            Activity?.StartService(new Intent(Activity, typeof(WearableDataListenerService))
                                                   .SetAction(WearableDataListenerService.ACTION_REQUESTWEATHERUPDATE));

                            ResetTimer();
                        }
                        else
                        {
                            // Device is not connected; cancel sync
                            await CancelDataSync();
                        }
                    }
                }
            };

            loaded = true;
        }