use of com.google.android.gms.common.api.ApiException in project android-play-location by googlesamples.
the class MainActivity method startLocationUpdates.
/**
* Requests location updates from the FusedLocationApi. Note: we don't call this unless location
* runtime permission has been granted.
*/
private void startLocationUpdates() {
// Begin by checking if the device has the necessary location settings.
mSettingsClient.checkLocationSettings(mLocationSettingsRequest).addOnSuccessListener(this, new OnSuccessListener<LocationSettingsResponse>() {
@Override
public void onSuccess(LocationSettingsResponse locationSettingsResponse) {
Log.i(TAG, "All location settings are satisfied.");
// noinspection MissingPermission
mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper());
updateUI();
}
}).addOnFailureListener(this, new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
int statusCode = ((ApiException) e).getStatusCode();
switch(statusCode) {
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
Log.i(TAG, "Location settings are not satisfied. Attempting to upgrade " + "location settings ");
try {
// Show the dialog by calling startResolutionForResult(), and check the
// result in onActivityResult().
ResolvableApiException rae = (ResolvableApiException) e;
rae.startResolutionForResult(MainActivity.this, REQUEST_CHECK_SETTINGS);
} catch (IntentSender.SendIntentException sie) {
Log.i(TAG, "PendingIntent unable to execute request.");
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
String errorMessage = "Location settings are inadequate, and cannot be " + "fixed here. Fix in Settings.";
Log.e(TAG, errorMessage);
Toast.makeText(MainActivity.this, errorMessage, Toast.LENGTH_LONG).show();
mRequestingLocationUpdates = false;
}
updateUI();
}
});
}
use of com.google.android.gms.common.api.ApiException in project coursera-android by aporter.
the class LocationGetLocationActivity method continueAcquiringLocations.
private void continueAcquiringLocations() {
// Start location services
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(POLLING_FREQ);
mLocationRequest.setFastestInterval(FASTEST_UPDATE_FREQ);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().addLocationRequest(mLocationRequest);
// Used if needed to turn on settings related to location services
SettingsClient client = LocationServices.getSettingsClient(this);
Task<LocationSettingsResponse> task = client.checkLocationSettings(builder.build());
task.addOnSuccessListener(this, new OnSuccessListener<LocationSettingsResponse>() {
@Override
public void onSuccess(LocationSettingsResponse locationSettingsResponse) {
// All location settings are satisfied. The client can initialize location requests here.
if (checkSelfPermission(android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
mLocationCallback = getLocationCallback();
mLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, null);
// Schedule a runnable to stop location updates after a period of time
Executors.newScheduledThreadPool(1).schedule(new Runnable() {
@Override
public void run() {
mLocationClient.removeLocationUpdates(mLocationCallback);
}
}, MEASURE_TIME, TimeUnit.MILLISECONDS);
}
}
});
task.addOnFailureListener(this, new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
int statusCode = ((ApiException) e).getStatusCode();
switch(statusCode) {
case CommonStatusCodes.RESOLUTION_REQUIRED:
// by showing the user a dialog.
try {
// Show the dialog by calling startResolutionForResult(),
// and check the result in onActivityResult().
ResolvableApiException resolvable = (ResolvableApiException) e;
resolvable.startResolutionForResult(LocationGetLocationActivity.this, REQUEST_CHECK_SETTINGS);
} catch (IntentSender.SendIntentException sendEx) {
// Ignore the error.
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
// to fix the settings so we won't show the dialog.
break;
}
}
});
}
use of com.google.android.gms.common.api.ApiException in project quickstart-android by firebase.
the class GoogleSignInFragment method onActivityResult.
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
if (requestCode == RC_SIGN_IN) {
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
try {
// Google Sign In was successful, authenticate with Firebase
GoogleSignInAccount account = task.getResult(ApiException.class);
Log.d(TAG, "firebaseAuthWithGoogle:" + account.getId());
firebaseAuthWithGoogle(account.getIdToken());
} catch (ApiException e) {
// Google Sign In failed, update UI appropriately
Log.w(TAG, "Google sign in failed", e);
updateUI(null);
}
}
}
use of com.google.android.gms.common.api.ApiException in project androidApp by InspectorIncognito.
the class PositionProvider method startLocationUpdates.
private void startLocationUpdates(LocationRequest request) {
Log.d(TAG, "startLocationUpdates");
if (ActivityCompat.checkSelfPermission(googleApiClient.getContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(googleApiClient.getContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
Log.d(TAG, "Asked to update positions without permissions");
throw new IllegalStateException("Asked to update positions without permissions");
}
if (googleApiClient.isConnected()) {
requesting = true;
if (Looper.myLooper() == null) {
Looper.prepare();
}
Log.d(TAG, "Connected to API, requesting updates");
fusedLocationProviderClient.requestLocationUpdates(request, locationCallback, null).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Log.d(TAG, "Task success");
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
if (e instanceof ApiException) {
Log.d(TAG, "onFailure: " + ((ApiException) e).getStatusMessage());
} else {
Log.d(TAG, "onFailure: " + e.getMessage());
}
}
});
}
}
use of com.google.android.gms.common.api.ApiException in project FirebaseUI-Android by firebase.
the class SignInKickstarter method start.
public void start() {
if (!TextUtils.isEmpty(getArguments().emailLink)) {
setResult(Resource.forFailure(new IntentRequiredException(EmailLinkCatcherActivity.createIntent(getApplication(), getArguments()), RequestCodes.EMAIL_FLOW)));
return;
}
// Signing in with Generic IDP puts the app in the background - it can be reclaimed by the
// OS during the sign in flow.
Task<AuthResult> pendingResultTask = getAuth().getPendingAuthResult();
if (pendingResultTask != null) {
pendingResultTask.addOnSuccessListener(authResult -> {
final IdpResponse response = new IdpResponse.Builder(new User.Builder(authResult.getCredential().getProvider(), authResult.getUser().getEmail()).build()).build();
handleSuccess(response, authResult);
}).addOnFailureListener(e -> setResult(Resource.forFailure(e)));
return;
}
// Only support password credentials if email auth is enabled
boolean supportPasswords = ProviderUtils.getConfigFromIdps(getArguments().providers, EmailAuthProvider.PROVIDER_ID) != null;
List<String> accountTypes = getCredentialAccountTypes();
// If the request will be empty, avoid the step entirely
boolean willRequestCredentials = supportPasswords || accountTypes.size() > 0;
if (getArguments().enableCredentials && willRequestCredentials) {
setResult(Resource.forLoading());
GoogleApiUtils.getCredentialsClient(getApplication()).request(new CredentialRequest.Builder().setPasswordLoginSupported(supportPasswords).setAccountTypes(accountTypes.toArray(new String[accountTypes.size()])).build()).addOnCompleteListener(task -> {
try {
handleCredential(task.getResult(ApiException.class).getCredential());
} catch (ResolvableApiException e) {
if (e.getStatusCode() == CommonStatusCodes.RESOLUTION_REQUIRED) {
setResult(Resource.forFailure(new PendingIntentRequiredException(e.getResolution(), RequestCodes.CRED_HINT)));
} else {
startAuthMethodChoice();
}
} catch (ApiException e) {
startAuthMethodChoice();
}
});
} else {
startAuthMethodChoice();
}
}
Aggregations