protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            // Create your application here
            SetContentView(Resource.Layout.activity_setup);

            cts = new CancellationTokenSource();
            wm  = WeatherData.WeatherManager.GetInstance();

            // Set default API to HERE
            // if a valid API key hasn't been entered yet
            if (wm.KeyRequired && !Settings.KeyVerified)
            {
                Settings.API = WeatherData.WeatherAPI.Here;
                wm.UpdateAPI();

                if (String.IsNullOrWhiteSpace(wm.GetAPIKey()))
                {
                    // If (internal) key doesn't exist, fallback to Yahoo
                    Settings.API = WeatherData.WeatherAPI.Yahoo;
                    wm.UpdateAPI();
                    Settings.UsePersonalKey = true;
                    Settings.KeyVerified    = false;
                }
                else
                {
                    // If key exists, go ahead
                    Settings.UsePersonalKey = false;
                    Settings.KeyVerified    = true;
                }
            }

            // Controls
            searchButton        = FindViewById <FloatingActionButton>(Resource.Id.search_button);
            searchButton.Click += (sender, e) =>
            {
                FragmentManager.BeginTransaction()
                .Replace(Resource.Id.search_fragment_container, new LocationSearchFragment())
                .Commit();

                mWearableActionDrawer.Controller.CloseDrawer();
            };
            locationButton        = FindViewById <FloatingActionButton>(Resource.Id.location_button);
            locationButton.Click += async(sender, e) =>
            {
                await FetchGeoLocation();
            };

            mWearableActionDrawer = FindViewById <WearableActionDrawerView>(Resource.Id.bottom_action_drawer);
            mWearableActionDrawer.SetOnMenuItemClickListener(this);
            mWearableActionDrawer.LockedWhenClosed = true;
            mWearableActionDrawer.Controller.PeekDrawer();
            progressBar            = FindViewById <ProgressBar>(Resource.Id.progressBar);
            progressBar.Visibility = ViewStates.Gone;

            // Location client
            if (WearableHelper.IsGooglePlayServicesInstalled && !WearableHelper.HasGPS)
            {
                mFusedLocationClient         = new FusedLocationProviderClient(this);
                mLocCallback                 = new LocationCallback();
                mLocCallback.LocationResult += async(sender, e) =>
                {
                    if (e.Result == null)
                    {
                        mLocation = null;
                    }
                    else
                    {
                        mLocation = e.Result.LastLocation;
                    }

                    if (mLocation == null)
                    {
                        EnableControls(true);
                        Toast.MakeText(this, Resource.String.error_retrieve_location, ToastLength.Short).Show();
                    }
                    else
                    {
                        await FetchGeoLocation();
                    }

                    await mFusedLocationClient.RemoveLocationUpdatesAsync(mLocCallback);
                };
                mLocCallback.LocationAvailability += async(sender, e) =>
                {
                    await mFusedLocationClient.FlushLocationsAsync();

                    if (!e.Availability.IsLocationAvailable)
                    {
                        EnableControls(true);
                        Toast.MakeText(this, Resource.String.error_retrieve_location, ToastLength.Short).Show();
                    }
                };
            }
            else
            {
                mLocListnr = new Droid.Helpers.LocationListener();
                mLocListnr.LocationChanged += async(Location location) =>
                {
                    mLocation = location;
                    await FetchGeoLocation();
                };
            }
        }
Exemple #2
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;
        }