private async Task UpdateLocation() { if (ContextCompat.CheckSelfPermission(this, Manifest.Permission.AccessFineLocation) != Permission.Granted && ContextCompat.CheckSelfPermission(this, Manifest.Permission.AccessCoarseLocation) != Permission.Granted) { RequestPermissions(new String[] { Manifest.Permission.AccessCoarseLocation, Manifest.Permission.AccessFineLocation }, PERMISSION_LOCATION_REQUEST_CODE); return; } Location location = null; if (WearableHelper.IsGooglePlayServicesInstalled && !WearableHelper.HasGPS) { location = await mFusedLocationClient.GetLastLocationAsync(); if (location == null) { var mLocationRequest = new LocationRequest(); mLocationRequest.SetInterval(10000); mLocationRequest.SetFastestInterval(1000); mLocationRequest.SetPriority(LocationRequest.PriorityHighAccuracy); mLocationRequest.SetNumUpdates(1); await mFusedLocationClient.RequestLocationUpdatesAsync(mLocationRequest, mLocCallback, null); await mFusedLocationClient.FlushLocationsAsync(); } } else { LocationManager locMan = (LocationManager)GetSystemService(Context.LocationService); bool isGPSEnabled = locMan.IsProviderEnabled(LocationManager.GpsProvider); bool isNetEnabled = locMan.IsProviderEnabled(LocationManager.NetworkProvider); if (isGPSEnabled) { location = locMan.GetLastKnownLocation(LocationManager.GpsProvider); if (location == null) { location = locMan.GetLastKnownLocation(LocationManager.NetworkProvider); } if (location == null) { locMan.RequestSingleUpdate(LocationManager.GpsProvider, mLocListnr, null); } } else if (isNetEnabled) { location = locMan.GetLastKnownLocation(LocationManager.NetworkProvider); if (location == null) { locMan.RequestSingleUpdate(LocationManager.NetworkProvider, mLocListnr, null); } } else { EnableControls(true); RunOnUiThread(() => { Toast.MakeText(this, Resource.String.error_retrieve_location, ToastLength.Short).Show(); }); } } if (location != null) { mLocation = location; await FetchGeoLocation(); } }
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(); }; } }
private async Task <bool> UpdateLocation() { bool locationChanged = false; if (Settings.DataSync == WearableDataSync.Off && Settings.FollowGPS && (location == null || location.locationType == LocationType.GPS)) { if (Activity != null && ContextCompat.CheckSelfPermission(Activity, Manifest.Permission.AccessFineLocation) != Permission.Granted) { RequestPermissions(new String[] { Manifest.Permission.AccessFineLocation }, PERMISSION_LOCATION_REQUEST_CODE); return(false); } Android.Locations.Location location = null; if (WearableHelper.IsGooglePlayServicesInstalled && !WearableHelper.HasGPS) { location = await mFusedLocationClient.GetLastLocationAsync(); if (location == null) { var mLocationRequest = new LocationRequest(); mLocationRequest.SetInterval(10000); mLocationRequest.SetFastestInterval(1000); mLocationRequest.SetPriority(LocationRequest.PriorityHighAccuracy); await mFusedLocationClient.RequestLocationUpdatesAsync(mLocationRequest, mLocCallback, null); await mFusedLocationClient.FlushLocationsAsync(); } } else { LocationManager locMan = Activity?.GetSystemService(Context.LocationService) as LocationManager; bool isGPSEnabled = (bool)locMan?.IsProviderEnabled(LocationManager.GpsProvider); bool isNetEnabled = (bool)locMan?.IsProviderEnabled(LocationManager.NetworkProvider); 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 { Toast.MakeText(Activity, Resource.String.error_retrieve_location, ToastLength.Short).Show(); } } if (location != null) { 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 location as last known lastGPSLocData.SetData(view, location); Settings.SaveHomeData(lastGPSLocData); this.location = lastGPSLocData; mLocation = location; locationChanged = true; } } return(locationChanged); }