Search in sources :

Example 6 with LocationSettingsResult

use of com.google.android.gms.location.LocationSettingsResult in project Android-ReactiveLocation by mcharmas.

the class MainActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    lastKnownLocationView = (TextView) findViewById(R.id.last_known_location_view);
    updatableLocationView = (TextView) findViewById(R.id.updated_location_view);
    addressLocationView = (TextView) findViewById(R.id.address_for_location_view);
    currentActivityView = (TextView) findViewById(R.id.activity_recent_view);
    locationProvider = new ReactiveLocationProvider(getApplicationContext());
    lastKnownLocationObservable = locationProvider.getLastKnownLocation();
    final LocationRequest locationRequest = LocationRequest.create().setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY).setNumUpdates(5).setInterval(100);
    locationUpdatesObservable = locationProvider.checkLocationSettings(new LocationSettingsRequest.Builder().addLocationRequest(locationRequest).setAlwaysShow(//Refrence: http://stackoverflow.com/questions/29824408/google-play-services-locationservices-api-new-option-never
    true).build()).doOnNext(new Action1<LocationSettingsResult>() {

        @Override
        public void call(LocationSettingsResult locationSettingsResult) {
            Status status = locationSettingsResult.getStatus();
            if (status.getStatusCode() == LocationSettingsStatusCodes.RESOLUTION_REQUIRED) {
                try {
                    status.startResolutionForResult(MainActivity.this, REQUEST_CHECK_SETTINGS);
                } catch (IntentSender.SendIntentException th) {
                    Log.e("MainActivity", "Error opening settings activity.", th);
                }
            }
        }
    }).flatMap(new Func1<LocationSettingsResult, Observable<Location>>() {

        @Override
        public Observable<Location> call(LocationSettingsResult locationSettingsResult) {
            return locationProvider.getUpdatedLocation(locationRequest);
        }
    });
    addressObservable = locationProvider.getUpdatedLocation(locationRequest).flatMap(new Func1<Location, Observable<List<Address>>>() {

        @Override
        public Observable<List<Address>> call(Location location) {
            return locationProvider.getReverseGeocodeObservable(location.getLatitude(), location.getLongitude(), 1);
        }
    }).map(new Func1<List<Address>, Address>() {

        @Override
        public Address call(List<Address> addresses) {
            return addresses != null && !addresses.isEmpty() ? addresses.get(0) : null;
        }
    }).map(new AddressToStringFunc()).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());
    activityObservable = locationProvider.getDetectedActivity(50);
}
Also used : Status(com.google.android.gms.common.api.Status) LocationRequest(com.google.android.gms.location.LocationRequest) LocationSettingsResult(com.google.android.gms.location.LocationSettingsResult) Address(android.location.Address) LocationSettingsRequest(com.google.android.gms.location.LocationSettingsRequest) List(java.util.List) ReactiveLocationProvider(pl.charmas.android.reactivelocation.ReactiveLocationProvider) Func1(rx.functions.Func1) AddressToStringFunc(pl.charmas.android.reactivelocation.sample.utils.AddressToStringFunc) Location(android.location.Location)

Example 7 with LocationSettingsResult

use of com.google.android.gms.location.LocationSettingsResult in project IITB-App by wncc.

the class MapFragment method onStart.

@Override
public void onStart() {
    super.onStart();
    locationButton = (FloatingActionButton) getActivity().findViewById(R.id.location_button);
    locationButton.setImageResource(R.drawable.ic_my_location_black_24dp);
    locationButton.getDrawable().setColorFilter(ContextCompat.getColor(getContext(), R.color.colorPrimaryDark), PorterDuff.Mode.SRC_IN);
    locationButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Log.v("MapFragment", "Location button pressed");
            try {
                LocationManager lm = (LocationManager) getContext().getSystemService(Context.LOCATION_SERVICE);
                boolean gps_enabled = false;
                boolean network_enabled = false;
                if (ActivityCompat.checkSelfPermission(getActivity(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getContext(), android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions(getActivity(), new String[] { Manifest.permission.ACCESS_COARSE_LOCATION }, MY_PERMISSIONS_REQUEST_LOCATION);
                    return;
                }
                try {
                    gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
                } catch (Exception ex) {
                }
                try {
                    network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
                } catch (Exception ex) {
                }
                if (!gps_enabled && !network_enabled) {
                    LocationRequest locationRequest = LocationRequest.create();
                    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
                    locationRequest.setInterval(30 * 1000);
                    locationRequest.setFastestInterval(5 * 1000);
                    LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().addLocationRequest(locationRequest);
                    // **************************
                    // this is the key ingredient
                    builder.setAlwaysShow(true);
                    // **************************
                    PendingResult<LocationSettingsResult> result = LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, builder.build());
                    result.setResultCallback(new ResultCallback<LocationSettingsResult>() {

                        @Override
                        public void onResult(LocationSettingsResult result) {
                            final Status status = result.getStatus();
                            final LocationSettingsStates state = result.getLocationSettingsStates();
                            switch(status.getStatusCode()) {
                                case LocationSettingsStatusCodes.SUCCESS:
                                    // requests here.
                                    break;
                                case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                                    // a dialog.
                                    try {
                                        // Show the dialog by calling startResolutionForResult(),
                                        // and check the result in onActivityResult().
                                        status.startResolutionForResult(getActivity(), 1000);
                                    } catch (IntentSender.SendIntentException e) {
                                    // Ignore the error.
                                    }
                                    break;
                                case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                                    // settings so we won't show the dialog.
                                    break;
                            }
                        }
                    });
                }
                currentLocation = getLastKnownLocation();
                if (currentLocation != null) {
                    CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(currentLocation.getLatitude(), currentLocation.getLongitude()), 17);
                    googleMap.animateCamera(cameraUpdate);
                    locationButton.getDrawable().setColorFilter(ContextCompat.getColor(getContext(), R.color.colorPrimary), PorterDuff.Mode.SRC_IN);
                }
            } catch (Exception e) {
                checkLocationPermission();
                Toast.makeText(getContext(), "Please turn on Location from the Settings", Toast.LENGTH_SHORT).show();
            }
        }
    });
}
Also used : LocationManager(android.location.LocationManager) Status(com.google.android.gms.common.api.Status) ResultCallback(com.google.android.gms.common.api.ResultCallback) LocationRequest(com.google.android.gms.location.LocationRequest) LocationSettingsResult(com.google.android.gms.location.LocationSettingsResult) LocationSettingsStates(com.google.android.gms.location.LocationSettingsStates) View(android.view.View) PendingResult(com.google.android.gms.common.api.PendingResult) LatLng(com.google.android.gms.maps.model.LatLng) IntentSender(android.content.IntentSender) CameraUpdate(com.google.android.gms.maps.CameraUpdate)

Example 8 with LocationSettingsResult

use of com.google.android.gms.location.LocationSettingsResult in project Thesis by bajnax.

the class LeScanActivity method enableLocation.

public void enableLocation() {
    if (googleApiClient == null) {
        googleApiClient = new GoogleApiClient.Builder(LeScanActivity.this).addApi(LocationServices.API).addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {

            @Override
            public void onConnected(Bundle bundle) {
            }

            @Override
            public void onConnectionSuspended(int i) {
                googleApiClient.connect();
            }
        }).addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {

            @Override
            public void onConnectionFailed(ConnectionResult connectionResult) {
                Log.d("Location error", "Location error " + connectionResult.getErrorCode());
            }
        }).build();
        googleApiClient.connect();
    }
    LocationRequest locationRequest = LocationRequest.create();
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    locationRequest.setInterval(30 * 1000);
    locationRequest.setFastestInterval(5 * 1000);
    LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().addLocationRequest(locationRequest);
    builder.setAlwaysShow(true);
    PendingResult<LocationSettingsResult> result = LocationServices.SettingsApi.checkLocationSettings(googleApiClient, builder.build());
    result.setResultCallback(new ResultCallback<LocationSettingsResult>() {

        @Override
        public void onResult(LocationSettingsResult result) {
            final Status status = result.getStatus();
            switch(status.getStatusCode()) {
                case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                    try {
                        // requesting to enable Bluetooth
                        status.startResolutionForResult(LeScanActivity.this, REQUEST_ENABLE_LS);
                    } catch (IntentSender.SendIntentException e) {
                    // Ignore the error.
                    }
                    break;
            }
        }
    });
}
Also used : Status(com.google.android.gms.common.api.Status) GoogleApiClient(com.google.android.gms.common.api.GoogleApiClient) LocationRequest(com.google.android.gms.location.LocationRequest) LocationSettingsResult(com.google.android.gms.location.LocationSettingsResult) Bundle(android.os.Bundle) LocationSettingsRequest(com.google.android.gms.location.LocationSettingsRequest) ConnectionResult(com.google.android.gms.common.ConnectionResult)

Example 9 with LocationSettingsResult

use of com.google.android.gms.location.LocationSettingsResult in project zendrive-sdk-android-sample by zendrive.

the class MainActivity method maybeResolveErrors.

private boolean maybeResolveErrors() {
    // The activity may have been launched by tapping on a notification
    // for a google play settings or location permission error.
    // Check and start resolution if that's the case.
    Intent intent = getIntent();
    if (intent == null || intent.getAction() == null) {
        return false;
    }
    String action = intent.getAction();
    boolean skipZendriveSettingsCheck = false;
    switch(action) {
        case Constants.EVENT_GOOGLE_PLAY_SETTING_ERROR:
            LocationSettingsResult result = intent.getParcelableExtra(EVENT_GOOGLE_PLAY_SETTING_ERROR);
            if (result != null && !result.getStatus().isSuccess()) {
                LocationSettingsHelper.resolveLocationSettings(this, result);
            }
            setIntent(null);
            skipZendriveSettingsCheck = true;
            break;
        case Constants.EVENT_LOCATION_PERMISSION_ERROR:
            requestLocationPermission();
            setIntent(null);
            skipZendriveSettingsCheck = true;
            break;
        case EVENT_ACTIVITY_PERMISSION_ERROR:
            requestActivityPermission();
            setIntent(null);
            skipZendriveSettingsCheck = true;
            break;
        case EVENT_MULTIPLE_PERMISSIONS_ERROR:
            List<String> missingPermissionList = intent.getStringArrayListExtra(MULTIPLE_PERMISSIONS_DENIED_LIST);
            if (missingPermissionList == null || missingPermissionList.isEmpty()) {
                throw new RuntimeException("Cannot find missing permission list in the activity intent");
            }
            requestMultiplePermissions(missingPermissionList);
            setIntent(null);
            skipZendriveSettingsCheck = true;
            break;
    }
    return skipZendriveSettingsCheck;
}
Also used : LocationSettingsResult(com.google.android.gms.location.LocationSettingsResult) Intent(android.content.Intent)

Example 10 with LocationSettingsResult

use of com.google.android.gms.location.LocationSettingsResult in project GmsCore by microg.

the class GoogleLocationManagerServiceImpl method requestLocationSettingsDialog.

@Override
public void requestLocationSettingsDialog(LocationSettingsRequest settingsRequest, ISettingsCallbacks callback, String packageName) throws RemoteException {
    Log.d(TAG, "requestLocationSettingsDialog: " + settingsRequest);
    PackageUtils.getAndCheckCallingPackage(context, packageName);
    (new Handler(Looper.getMainLooper())).post(() -> {
        try {
            callback.onLocationSettingsResult(new LocationSettingsResult(new LocationSettingsStates(true, true, true, true, true, true), Status.SUCCESS));
        } catch (RemoteException e) {
            Log.w(TAG, e);
        }
    });
}
Also used : LocationSettingsResult(com.google.android.gms.location.LocationSettingsResult) Handler(android.os.Handler) LocationSettingsStates(com.google.android.gms.location.LocationSettingsStates) RemoteException(android.os.RemoteException)

Aggregations

LocationSettingsResult (com.google.android.gms.location.LocationSettingsResult)11 LocationRequest (com.google.android.gms.location.LocationRequest)8 Status (com.google.android.gms.common.api.Status)7 LocationSettingsRequest (com.google.android.gms.location.LocationSettingsRequest)6 GoogleApiClient (com.google.android.gms.common.api.GoogleApiClient)4 Bundle (android.os.Bundle)3 ConnectionResult (com.google.android.gms.common.ConnectionResult)3 LocationSettingsStates (com.google.android.gms.location.LocationSettingsStates)3 IntentSender (android.content.IntentSender)2 Address (android.location.Address)2 Location (android.location.Location)2 LocationManager (android.location.LocationManager)2 Handler (android.os.Handler)2 RemoteException (android.os.RemoteException)2 PendingResult (com.google.android.gms.common.api.PendingResult)2 List (java.util.List)2 Intent (android.content.Intent)1 AsyncTask (android.os.AsyncTask)1 View (android.view.View)1 ResultCallback (com.google.android.gms.common.api.ResultCallback)1