| Index: chrome/android/java/src/org/chromium/chrome/browser/permissions/LocationSettingsDialogRequester.java
|
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/permissions/LocationSettingsDialogRequester.java b/chrome/android/java/src/org/chromium/chrome/browser/permissions/LocationSettingsDialogRequester.java
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..4456a2de95a1367d99409009cf98e8a67ae19312
|
| --- /dev/null
|
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/permissions/LocationSettingsDialogRequester.java
|
| @@ -0,0 +1,146 @@
|
| +// Copyright 2017 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +package org.chromium.chrome.browser.permissions;
|
| +
|
| +import android.content.Context;
|
| +
|
| +import com.google.android.gms.common.ConnectionResult;
|
| +import com.google.android.gms.common.api.GoogleApiClient;
|
| +import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
|
| +import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
|
| +import com.google.android.gms.common.api.ResultCallback;
|
| +import com.google.android.gms.common.api.Status;
|
| +import com.google.android.gms.location.SettingsApi;
|
| +import com.google.android.gms.location.LocationRequest;
|
| +import com.google.android.gms.location.LocationServices;
|
| +import com.google.android.gms.location.LocationSettingsRequest;
|
| +import com.google.android.gms.location.LocationSettingsResult;
|
| +import com.google.android.gms.location.LocationSettingsStatusCodes;
|
| +import com.google.android.gms.common.api.PendingResult;
|
| +
|
| +/**
|
| + * This class represents a connection to Google Play Services that shows the
|
| + * Location Settings Dialog (LSD).
|
| + */
|
| +public class LocationSettingsDialogRequester implements ConnectionCallbacks,
|
| + OnConnectionFailedListener {
|
| + private final GoogleApiClient mGoogleApiClient;
|
| + private final LocationRequest mLocationRequest;
|
| +
|
| + /**
|
| + * An interface for classes which need to be informed of the outcome of the LSD.
|
| + */
|
| + public interface RequestDelegate {
|
| + void onLSDAccepted();
|
| + void onLSDCanceled();
|
| + }
|
| +
|
| + LocationSettingsDialogRequester(Context context) {
|
| + mGoogleApiClient = new GoogleApiClient.Builder(context)
|
| + .addApi(LocationServices.API)
|
| + .addConnectionCallbacks(this)
|
| + .addOnConnectionFailedListener(this)
|
| + .build();
|
| + mGoogleApiClient.connect();
|
| +
|
| + mlocationRequest = LocationRequest.create();
|
| + mlocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
|
| + mlocationRequest.setInterval(30 * 1000);
|
| + mlocationRequest.setFastestInterval(5 * 1000);
|
| + }
|
| +
|
| + protected void disconnect() {
|
| + mGoogleApiClient.disconnect();
|
| + }
|
| +
|
| + @Override
|
| + public void onConnected(Bundle connectionHint) {
|
| + showLocationSettingsDialog();
|
| + }
|
| +
|
| + @Override
|
| + public void onConnectionSuspended(int cause) {
|
| + // Log.i(TAG, "LSD connection suspended: " + cause);
|
| + }
|
| +
|
| + @Override
|
| + public void onConnectionFailed(ConnectionResult result) {
|
| + // Log.i(TAG, "LSD connection failed: " + result);
|
| + }
|
| +
|
| + protected GoogleApiClient getGoogleApiClient() {
|
| + return mGoogleApiClient;
|
| + }
|
| +
|
| +
|
| + /**
|
| + * Returns true if the Location Settings Dialog should be shown. Otherwise returns false.
|
| + *
|
| + * If true is returned, this method will asynchronously request the LSD to be shown.
|
| + */
|
| + public static boolean showLocationSettingsDialog(final RequestDelegate delegate) {
|
| +
|
| + LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
|
| + .addLocationRequest(mLocationRequest);
|
| +
|
| + builder.setAlwaysShow(true);
|
| +
|
| + PendingResult<LocationSettingsResult> result =
|
| + LocationServices.SettingsApi.checkLocationSettings(mGoogleClient,
|
| + builder.build());
|
| +
|
| + result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
|
| + @Override
|
| + public void onResult(LocationSettingsResult result) {
|
| + final Status status = result.getStatus();
|
| + final LocationSettingsStates states = result.getLocationSettingsStates();
|
| + switch (status.getStatusCode()) {
|
| + case LocationSettingsStatusCodes.SUCCESS:
|
| + // All location settings are satisfied. The client can
|
| + // initialize location requests here.
|
| + break;
|
| + case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
|
| + // Location settings are not satisfied, but this can be fixed
|
| + // by showing the user a dialog.
|
| + try {
|
| + // Show the dialog by calling startResolutionForResult(),
|
| + // and check the result in onActivityResult().
|
| + status.startResolutionForResult(
|
| + LocationSettingsDialogRequester.this,
|
| + REQUEST_CHECK_SETTINGS);
|
| + } catch (SendIntentException e) {
|
| + // Ignore the error.
|
| + }
|
| + break;
|
| + case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
|
| + // Location settings are not satisfied. However, we have no way
|
| + // to fix the settings so we won't show the dialog.
|
| + break;
|
| + }
|
| + }
|
| + });
|
| + }
|
| +
|
| + @Override
|
| + protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
| + final LocationSettingsStates states = LocationSettingsStates.fromIntent(intent);
|
| + switch (requestCode) {
|
| + case REQUEST_CHECK_SETTINGS:
|
| + switch (resultCode) {
|
| + case Activity.RESULT_OK:
|
| + // All required changes were successfully made.
|
| + // Call back the native code.
|
| + break;
|
| + case Activity.RESULT_CANCELED:
|
| + // The user was asked to change settings, but chose not to.
|
| + // Call back the native code.
|
| + break;
|
| + default:
|
| + break;
|
| + }
|
| + break;
|
| + }
|
| + }
|
| +}
|
|
|