Vedroid36
4/2/2018 - 7:56 AM

BaseAct.java


public void checkLocationTrackingAvailable(boolean isGpsNecessary) {
    this.isGpsNecessary = isGpsNecessary;

    if (!isGpsEnabled()) {
        showGpsWarningDialog(REQUEST_CODE_LOCATION_SETTINGS);
    } else if (!isAppLocationPermissionGranted()) {
        showGpsWarningDialog(REQUEST_CODE_APP_SETTINGS);
    } else {
        getLastLocation();

    }

}

private void showGpsWarningDialog(int settingsType) {
    NoGpsWarningFragment dialogFragment = NoGpsWarningFragment.newInstance(settingsType);
    dialogFragment.show(getSupportFragmentManager(), null);
}


=====

public void onGpsWarningYesClicked(int settingsType) {
    switch (settingsType) {
        case REQUEST_CODE_LOCATION_SETTINGS:
            Intent locationSettingsIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivityForResult(locationSettingsIntent, REQUEST_CODE_LOCATION_SETTINGS);
            break;
        case REQUEST_CODE_APP_SETTINGS:
            Intent appSettingIntent = new Intent();
            appSettingIntent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
            Uri uri = Uri.fromParts(PACKAGE_URI, getPackageName(), null);
            appSettingIntent.setData(uri);
            startActivityForResult(appSettingIntent, REQUEST_CODE_APP_SETTINGS);
    }
}


=====


@Override
public void notifyLocationObservers(Location location) {
    for (LocationObserver observer : locationObservers) {
        // Got last known location. In some rare situations this can be null.
        if (null != location) {
            observer.onGetLocationSuccess(location);
        } else {
            observer.onGetLocationFailed();
        }
    }
    // can stop location service after got users cordinates
    fusedLocationClient.removeLocationUpdates(locationCallback);
}

@SuppressLint("MissingPermission")
public void getLastLocation() {
    LogUtil.d(TAG, "getLastLocation");

    fusedLocationClient = getFusedLocationProviderClient(this);
    fusedLocationClient.getLastLocation()
            .addOnSuccessListener(this, new OnSuccessListener<Location>() {
                @Override
                public void onSuccess(Location location) {
                        notifyLocationObservers(location);
                    }
                });
}