com.google.android.gms.location.FusedLocationProviderClient Java Examples

The following examples show how to use com.google.android.gms.location.FusedLocationProviderClient. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MainActivity.java    From Beauty-Compass with Apache License 2.0 7 votes vote down vote up
/**
 *
 */
private void initialize() {
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return;
    }

    FusedLocationProviderClient fusedLocationProviderClient = new FusedLocationProviderClient(getApplicationContext());
    fusedLocationProviderClient.getLastLocation()
            .addOnSuccessListener(new OnSuccessListener<Location>() {
                @Override
                public void onSuccess(Location location) {
                    if (location != null) {
                        updateDayState(location);
                        mPositionTextView.setVisibility(View.VISIBLE);
                        mPositionTextView.setText(AppUtils.convert(location.getLatitude(), location.getLongitude()));
                    }
                }
            });
}
 
Example #2
Source File: WhereAmIActivity.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 6 votes vote down vote up
private void getLastLocation() {
  FusedLocationProviderClient fusedLocationClient;
  fusedLocationClient =
    LocationServices.getFusedLocationProviderClient(this);
  if (ActivityCompat.checkSelfPermission(this, ACCESS_FINE_LOCATION)
      == PERMISSION_GRANTED ||
      ActivityCompat.checkSelfPermission(this, ACCESS_COARSE_LOCATION)
      == PERMISSION_GRANTED) {
    fusedLocationClient.getLastLocation()
      .addOnSuccessListener(this, new OnSuccessListener<Location>() {
        @Override
        public void onSuccess(Location location) {
          updateTextView(location);
        }
      });
  }
}
 
Example #3
Source File: LocationRepository.java    From Nibo with MIT License 6 votes vote down vote up
@Override
public Observable<Location> getLocationObservable() {
    return Observable.create(new ObservableOnSubscribe<Location>() {
        @Override
        public void subscribe(final ObservableEmitter<Location> source) throws Exception {
            FusedLocationProviderClient locationClient = LocationServices.getFusedLocationProviderClient(context);
            locationClient.getLastLocation().addOnSuccessListener(new OnSuccessListener<Location>() {
                @Override
                public void onSuccess(Location location) {
                    if (location != null) {
                        source.onNext(location);
                    }
                }
            }).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    source.onError(e);
                }
            });
        }
    }).subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread());
}
 
Example #4
Source File: LocationHelper.java    From Compass with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("MissingPermission")
public void onCreate() {
    DLog.d(TAG, "onCreate() called");
    if (permissionGranted()) {
        if (!Utility.isNetworkAvailable(mContext)) {
            return;
        }
        LocationManager locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
        mLocationListener = new LocationListener(mContext);
        mLocationListener.setLocationValueListener(mLocationValueListener);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 10, mLocationListener);
        FusedLocationProviderClient client = getFusedLocationProviderClient(mContext);
        client.getLastLocation().addOnSuccessListener(new OnSuccessListener<Location>() {
            @Override
            public void onSuccess(Location location) {
                // Got last known location. In some rare situations this can be null.
                if (location != null) {
                    // Logic to handle location object
                    mLocationListener.onLocationChanged(location);
                }
            }
        });
    } else {
        requestPermission();
    }
}
 
Example #5
Source File: GoogleLocationEngineImplAdditionalTest.java    From mapbox-events-android with MIT License 6 votes vote down vote up
@Before
public void setUp() {
  location = mock(Location.class);
  when(location.getLatitude()).thenReturn(1.0);
  when(location.getLongitude()).thenReturn(2.0);
  locationList.clear();
  locationList.add(location);
  fusedLocationProviderClient = mock(FusedLocationProviderClient.class);
  mockTask = mock(Task.class);
  mockLocationTask = mock(Task.class);
  when(fusedLocationProviderClient.getLastLocation()).thenReturn(mockLocationTask);
  when(fusedLocationProviderClient
    .requestLocationUpdates(any(LocationRequest.class), any(LocationCallback.class), any(Looper.class)))
    .thenAnswer(new Answer<Task<Void>>() {
      @Override
      public Task<Void> answer(InvocationOnMock invocation) {
        LocationCallback listener = (LocationCallback) invocation.getArguments()[1];
        listener.onLocationResult(LocationResult.create(locationList));
        return mockTask;
      }
    });

  engine = new LocationEngineProxy<>(new GoogleLocationEngineImpl(fusedLocationProviderClient));
}
 
Example #6
Source File: WhereAmIActivity.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 6 votes vote down vote up
private void getLastLocation() {
  FusedLocationProviderClient fusedLocationClient;
  fusedLocationClient =
    LocationServices.getFusedLocationProviderClient(this);
  if (ActivityCompat.checkSelfPermission(this, ACCESS_FINE_LOCATION)
      == PERMISSION_GRANTED ||
      ActivityCompat.checkSelfPermission(this, ACCESS_COARSE_LOCATION)
      == PERMISSION_GRANTED) {
    fusedLocationClient.getLastLocation()
      .addOnSuccessListener(this, new OnSuccessListener<Location>() {
        @Override
        public void onSuccess(Location location) {
          updateTextView(location);
        }
      });
  }
}
 
Example #7
Source File: WhereAmIActivity.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 6 votes vote down vote up
private void getLastLocation() {
  FusedLocationProviderClient fusedLocationClient;
  fusedLocationClient =
    LocationServices.getFusedLocationProviderClient(this);
  if (ActivityCompat.checkSelfPermission(this, ACCESS_FINE_LOCATION)
      == PERMISSION_GRANTED ||
      ActivityCompat.checkSelfPermission(this, ACCESS_COARSE_LOCATION)
      == PERMISSION_GRANTED) {
    fusedLocationClient.getLastLocation()
      .addOnSuccessListener(this, new OnSuccessListener<Location>() {
        @Override
        public void onSuccess(Location location) {
          updateTextView(location);
        }
      });
  }
}
 
Example #8
Source File: WhereAmIActivity.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 6 votes vote down vote up
private void getLastLocation() {
  FusedLocationProviderClient fusedLocationClient;
  fusedLocationClient =
    LocationServices.getFusedLocationProviderClient(this);
  if (ActivityCompat.checkSelfPermission(this, ACCESS_FINE_LOCATION)
      == PERMISSION_GRANTED ||
      ActivityCompat.checkSelfPermission(this, ACCESS_COARSE_LOCATION)
      == PERMISSION_GRANTED) {
    fusedLocationClient.getLastLocation()
      .addOnSuccessListener(this, new OnSuccessListener<Location>() {
        @Override
        public void onSuccess(Location location) {
          updateTextView(location);
        }
      });
  }
}
 
Example #9
Source File: SplashActivity.java    From Beauty-Compass with Apache License 2.0 6 votes vote down vote up
private void initialize() {
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        navigate();
    }

    FusedLocationProviderClient fusedLocationProviderClient = new FusedLocationProviderClient(getApplicationContext());
    fusedLocationProviderClient.getLastLocation()
            .addOnSuccessListener(new OnSuccessListener<Location>() {
                @Override
                public void onSuccess(Location location) {
                    if (location != null) {
                        mLocation = location;
                        updateDayState(location);
                    } else {
                        mBackground = R.drawable.bg_default;
                        mGreetingMessage = R.string.standard_greeting;
                        navigate();
                    }


                }
            });
}
 
Example #10
Source File: LocationActivity.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 6 votes vote down vote up
private void listing15_8() {
  if (
    ActivityCompat
      .checkSelfPermission(this, ACCESS_FINE_LOCATION) == PERMISSION_GRANTED ||
      ActivityCompat
        .checkSelfPermission(this, ACCESS_COARSE_LOCATION) == PERMISSION_GRANTED) {

    // Listing 15-8: Requesting location updates using a Pending Intent
    FusedLocationProviderClient fusedLocationClient = LocationServices.getFusedLocationProviderClient(this);

    LocationRequest request = new LocationRequest()
                                .setInterval(60000 * 10) // Update every 10 minutes.
                                .setPriority(LocationRequest.PRIORITY_NO_POWER);

    final int locationUpdateRC = 0;
    int flags = PendingIntent.FLAG_UPDATE_CURRENT;

    Intent intent = new Intent(this, MyLocationUpdateReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, locationUpdateRC, intent, flags);

    fusedLocationClient.requestLocationUpdates(request, pendingIntent);
  }
}
 
Example #11
Source File: LocationActivity.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 6 votes vote down vote up
private void startTrackingLocation() {
  if (
    ActivityCompat
      .checkSelfPermission(this, ACCESS_FINE_LOCATION) == PERMISSION_GRANTED ||
      ActivityCompat
        .checkSelfPermission(this, ACCESS_COARSE_LOCATION) == PERMISSION_GRANTED) {

    FusedLocationProviderClient locationClient = LocationServices.getFusedLocationProviderClient(this);
    LocationRequest request =
      new LocationRequest()
        .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
        .setInterval(5000); // Update every 5 seconds.

    locationClient.requestLocationUpdates(request, mLocationCallback, null);
  }
}
 
Example #12
Source File: LocationActivity.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 6 votes vote down vote up
private void listing15_5() {
  int permission = ActivityCompat.checkSelfPermission(this,
    ACCESS_FINE_LOCATION);
  if (permission == PERMISSION_GRANTED) {

    // LISTING 15-5: Obtaining the last known device Location
    FusedLocationProviderClient fusedLocationClient;
    fusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
    fusedLocationClient.getLastLocation()
      .addOnSuccessListener(this, new OnSuccessListener<Location>() {
        @Override
        public void onSuccess(Location location) {
          // In some rare situations this can be null.
          if (location != null) {
            // TODO Do something with the returned location.
          }
        }
      });
  }
}
 
Example #13
Source File: CalculationModulesArrayList.java    From GNSS_Compare with Apache License 2.0 6 votes vote down vote up
public void registerForGnssUpdates(FusedLocationProviderClient fusedLocationClient, LocationManager locationManager){
    try {

        fusedLocationProviderClientReference = fusedLocationClient;
        locationManagerReference = locationManager;

        fusedLocationClient.requestLocationUpdates(
                locationRequest,
                locationCallback,
                Looper.myLooper());

        locationManager.registerGnssMeasurementsCallback(
                gnssCallback);

    } catch (SecurityException e){
        e.printStackTrace();
    }
}
 
Example #14
Source File: WhereAmIActivity.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 6 votes vote down vote up
private void getLastLocation() {
  FusedLocationProviderClient fusedLocationClient;
  fusedLocationClient =
    LocationServices.getFusedLocationProviderClient(this);
  if (ActivityCompat.checkSelfPermission(this, ACCESS_FINE_LOCATION)
      == PERMISSION_GRANTED ||
      ActivityCompat.checkSelfPermission(this, ACCESS_COARSE_LOCATION)
      == PERMISSION_GRANTED) {
    fusedLocationClient.getLastLocation()
      .addOnSuccessListener(this, new OnSuccessListener<Location>() {
        @Override
        public void onSuccess(Location location) {
          updateTextView(location);
        }
      });
  }
}
 
Example #15
Source File: GnssCoreService.java    From GNSS_Compare with Apache License 2.0 6 votes vote down vote up
private boolean tryRegisterForGnssUpdates(){
    if (hasGnssAndLogPermissions()) {
        FusedLocationProviderClient fusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
        LocationManager locationManager = (LocationManager) getApplicationContext().getSystemService(LOCATION_SERVICE);
        try {
            calculationModules.registerForGnssUpdates(fusedLocationClient, locationManager);
            calculationModules.assignPoseUpdatedListener(poseListener);
        } catch (IllegalStateException e){
            e.printStackTrace();
            calculationModules.unregisterFromGnssUpdates();
            return false;
        }
        return true;
    }
    return false;
}
 
Example #16
Source File: WhereAmIActivity.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 5 votes vote down vote up
private void requestLocationUpdates() {
  if (ActivityCompat.checkSelfPermission(this, ACCESS_FINE_LOCATION)
        == PERMISSION_GRANTED ||
        ActivityCompat.checkSelfPermission(this, ACCESS_COARSE_LOCATION)
        == PERMISSION_GRANTED) {

    FusedLocationProviderClient fusedLocationClient
      = LocationServices.getFusedLocationProviderClient(this);

    fusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, null);
  }
}
 
Example #17
Source File: LocationService.java    From io.appium.settings with Apache License 2.0 5 votes vote down vote up
private FusedLocationProvider createFusedLocationProvider() {
    GoogleApiClient googleApiClient = new GoogleApiClient.Builder(this)
            .addApi(LocationServices.API)
            .build();
    FusedLocationProviderClient locationProviderClient = LocationServices.getFusedLocationProviderClient(this);
    return new FusedLocationProvider(googleApiClient, locationProviderClient, this);
}
 
Example #18
Source File: ScannerService.java    From com.ruuvi.station with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void updateLocation() {
    FusedLocationProviderClient mFusedLocationClient = LocationServices.getFusedLocationProviderClient(getApplicationContext());
    if (ContextCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
        mFusedLocationClient.getLastLocation().addOnSuccessListener(new OnSuccessListener<Location>() {
            @Override
            public void onSuccess(Location location) {
                tagLocation = location;
            }
        });
    }
}
 
Example #19
Source File: WhereAmIActivity.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 5 votes vote down vote up
private void requestLocationUpdates() {
  if (ActivityCompat.checkSelfPermission(this, ACCESS_FINE_LOCATION)
        == PERMISSION_GRANTED ||
        ActivityCompat.checkSelfPermission(this, ACCESS_COARSE_LOCATION)
        == PERMISSION_GRANTED) {

    FusedLocationProviderClient fusedLocationClient
      = LocationServices.getFusedLocationProviderClient(this);

    fusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, null);
  }
}
 
Example #20
Source File: SingleLocationUpdate.java    From react-native-geolocation-service with MIT License 5 votes vote down vote up
public SingleLocationUpdate(
  FusedLocationProviderClient fusedLocationProviderClient,
  LocationRequest locationRequest,
  long timeout,
  Callback success,
  Callback error
) {
  mFusedProviderClient = fusedLocationProviderClient;
  mLocationRequest = locationRequest;
  mTimeout = timeout;
  mSuccessCallback = success;
  mErrorCallback = error;
}
 
Example #21
Source File: WhereAmIActivity.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 5 votes vote down vote up
private void requestLocationUpdates() {
  if (ActivityCompat.checkSelfPermission(this, ACCESS_FINE_LOCATION)
        == PERMISSION_GRANTED ||
        ActivityCompat.checkSelfPermission(this, ACCESS_COARSE_LOCATION)
        == PERMISSION_GRANTED) {

    FusedLocationProviderClient fusedLocationClient
      = LocationServices.getFusedLocationProviderClient(this);

    fusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, null);
  }
}
 
Example #22
Source File: Location.java    From UberClone with MIT License 5 votes vote down vote up
public Location(AppCompatActivity activity, final locationListener locationListener) {
    this.activity=activity;
    fusedLocationClient=new FusedLocationProviderClient(activity.getApplicationContext());

    inicializeLocationRequest();
    locationCallback=new LocationCallback(){
        @Override
        public void onLocationResult(LocationResult locationResult) {
            super.onLocationResult(locationResult);
            locationListener.locationResponse(locationResult);
        }
    };
}
 
Example #23
Source File: WhereAmIActivity.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 5 votes vote down vote up
private void requestLocationUpdates() {
  if (ActivityCompat.checkSelfPermission(this, ACCESS_FINE_LOCATION)
        == PERMISSION_GRANTED ||
        ActivityCompat.checkSelfPermission(this, ACCESS_COARSE_LOCATION)
        == PERMISSION_GRANTED) {

    FusedLocationProviderClient fusedLocationClient
      = LocationServices.getFusedLocationProviderClient(this);

    fusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, null);
  }
}
 
Example #24
Source File: Location.java    From UberClone with MIT License 5 votes vote down vote up
public Location(AppCompatActivity activity, final locationListener locationListener) {
    this.activity=activity;
    fusedLocationClient=new FusedLocationProviderClient(activity.getApplicationContext());

    inicializeLocationRequest();
    locationCallback=new LocationCallback(){
        @Override
        public void onLocationResult(LocationResult locationResult) {
            super.onLocationResult(locationResult);
            locationListener.locationResponse(locationResult);
        }
    };
}
 
Example #25
Source File: LocationUpdatesBroadcastReceiver.java    From background_location_updates with Apache License 2.0 5 votes vote down vote up
@SuppressLint("MissingPermission")
public static void startTrackingBroadcastBased(Context context, int requestInterval) {
    FusedLocationProviderClient client = LocationServices.getFusedLocationProviderClient(context);
    LocationRequest request = new LocationRequest();
    request.setInterval(requestInterval);
    request.setFastestInterval(requestInterval);
    request.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    client.requestLocationUpdates(request, LocationUpdatesBroadcastReceiver.getPendingIntent(context));
}
 
Example #26
Source File: LocationActivity.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 5 votes vote down vote up
@Override
protected void onStop() {
  super.onStop();

  FusedLocationProviderClient fusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
  fusedLocationClient.removeLocationUpdates(mLocationCallback);
}
 
Example #27
Source File: BackgroundScanner.java    From com.ruuvi.station with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void onReceive(final Context context, Intent intent) {
    /*
    PowerManager powerManager = (PowerManager) context.getSystemService(POWER_SERVICE);
    wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
            "MyWakelockTag");
    wakeLock.acquire();
    */
    Log.d(TAG, "Woke up");
    scheduleNextScan(context);

    FusedLocationProviderClient mFusedLocationClient = LocationServices.getFusedLocationProviderClient(context);
    if (ContextCompat.checkSelfPermission(context, android.Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
        mFusedLocationClient.getLastLocation().addOnSuccessListener(new OnSuccessListener<Location>() {
            @Override
            public void onSuccess(Location location) {
                tagLocation = location;
            }
        });
    }

    scanSettings = new ScanSettings.Builder()
            .setReportDelay(0)
            .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
            .build();

    final BluetoothManager bluetoothManager = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);
    bluetoothAdapter = bluetoothManager.getAdapter();
    scanner = bluetoothAdapter.getBluetoothLeScanner();

    scanResults = new ArrayList<>();

    if (!canScan()) {
        Log.d(TAG, "Could not start scanning in background, scheduling next attempt");
        return;
    }

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            scanner.stopScan(nsCallback);
            processFoundDevices(context);
            scanResults = new ArrayList<LeScanResult>();
        }
    }, SCAN_TIME_MS);

    try {
        scanner.startScan(Utils.getScanFilters(), scanSettings, nsCallback);
    } catch (Exception e) {
        Log.e(TAG, e.getMessage());
    }
}
 
Example #28
Source File: ScannerJobService.java    From com.ruuvi.station with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public boolean onStartJob(JobParameters jobParameters) {
    Log.d(TAG, "Woke up");
    this.jobParameters = jobParameters;

    FusedLocationProviderClient mFusedLocationClient = LocationServices.getFusedLocationProviderClient(getApplicationContext());
    if (ContextCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
        mFusedLocationClient.getLastLocation().addOnSuccessListener(new OnSuccessListener<Location>() {
            @Override
            public void onSuccess(Location location) {
                tagLocation = location;
            }
        });
    }

    scanSettings = new ScanSettings.Builder()
            .setReportDelay(0)
            .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
            .build();

    final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    bluetoothAdapter = bluetoothManager.getAdapter();
    scanner = bluetoothAdapter.getBluetoothLeScanner();

    scanResults = new ArrayList<>();

    if (!canScan()) {
        Log.d(TAG, "Could not start scanning in background, scheduling next attempt");
        return false;
    }

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            scanner.stopScan(nsCallback);
            processFoundDevices(getApplicationContext());
            scanResults = new ArrayList<LeScanResult>();
        }
    }, SCAN_TIME_MS);

    try {
        scanner.startScan(Utils.getScanFilters(), scanSettings, nsCallback);
    } catch (Exception e) {
        Log.e(TAG, e.getMessage());
    }
    return true;
}
 
Example #29
Source File: GoogleLocationEngineImpl.java    From mapbox-events-android with MIT License 4 votes vote down vote up
@VisibleForTesting
GoogleLocationEngineImpl(FusedLocationProviderClient fusedLocationProviderClient) {
  this.fusedLocationProviderClient = fusedLocationProviderClient;
}
 
Example #30
Source File: LocationUpdateWorker.java    From PixelWatchFace with GNU General Public License v3.0 4 votes vote down vote up
@NonNull
@Override
public ListenableFuture<Result> startWork() {
  String TAG = "location_update_worker";
  Log.d(TAG, "starting location work...");
  FusedLocationProviderClient fusedLocationClient = LocationServices
      .getFusedLocationProviderClient(getApplicationContext());

  return CallbackToFutureAdapter.getFuture(completer -> {
    if (ActivityCompat
        .checkSelfPermission(getApplicationContext(), permission.ACCESS_FINE_LOCATION)
        != PackageManager.PERMISSION_GRANTED
        && ActivityCompat
        .checkSelfPermission(getApplicationContext(), permission.ACCESS_COARSE_LOCATION)
        != PackageManager.PERMISSION_GRANTED) {
      completer.set(Result.failure());
    } else {
      fusedLocationClient.getLastLocation().addOnSuccessListener(location -> {
        if (location != null) {
          Log.d(TAG, "location: (" + location.getLatitude() + "," + location
              .getLongitude() + ")");
          //mCurrentWeather.setLocation(location);
          //Log.d(TAG, "gson-ized location: " + new Gson().toJson(location));
          Data output = new Data.Builder()
              .putDouble(KEY_LATITUDE, location.getLatitude())
              .putDouble(KEY_LONGITUDE, location.getLongitude())
              .putDouble(KEY_ALTITUDE, location.getAltitude())
              .putString(KEY_LOCATION_PROVIDER, location.getProvider())
              .build();

          completer.set(Result.success(output));
        } else {
          if (ActivityCompat.checkSelfPermission(getApplicationContext(),
              Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            completer.set(Result.failure());
          } else {
            // if no location, but permission exists, try again
            Log.d(TAG, "error retrieving location");
            completer.set(Result.retry());
          }
        }
      });
    }
    return completer;
  });
}