OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.chrome.browser.permissions; |
| 6 |
| 7 import android.content.Context; |
| 8 |
| 9 import com.google.android.gms.common.ConnectionResult; |
| 10 import com.google.android.gms.common.api.GoogleApiClient; |
| 11 import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks; |
| 12 import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListe
ner; |
| 13 import com.google.android.gms.common.api.ResultCallback; |
| 14 import com.google.android.gms.common.api.Status; |
| 15 import com.google.android.gms.location.SettingsApi; |
| 16 import com.google.android.gms.location.LocationRequest; |
| 17 import com.google.android.gms.location.LocationServices; |
| 18 import com.google.android.gms.location.LocationSettingsRequest; |
| 19 import com.google.android.gms.location.LocationSettingsResult; |
| 20 import com.google.android.gms.location.LocationSettingsStatusCodes; |
| 21 import com.google.android.gms.common.api.PendingResult; |
| 22 |
| 23 /** |
| 24 * This class represents a connection to Google Play Services that shows the |
| 25 * Location Settings Dialog (LSD). |
| 26 */ |
| 27 public class LocationSettingsDialogRequester implements ConnectionCallbacks, |
| 28 OnConnectionFailedListener { |
| 29 private final GoogleApiClient mGoogleApiClient; |
| 30 private final LocationRequest mLocationRequest; |
| 31 |
| 32 /** |
| 33 * An interface for classes which need to be informed of the outcome of the L
SD. |
| 34 */ |
| 35 public interface RequestDelegate { |
| 36 void onLSDAccepted(); |
| 37 void onLSDCanceled(); |
| 38 } |
| 39 |
| 40 LocationSettingsDialogRequester(Context context) { |
| 41 mGoogleApiClient = new GoogleApiClient.Builder(context) |
| 42 .addApi(LocationServices.API) |
| 43 .addConnectionCallbacks(this) |
| 44 .addOnConnectionFailedListener(this) |
| 45 .build(); |
| 46 mGoogleApiClient.connect(); |
| 47 |
| 48 mlocationRequest = LocationRequest.create(); |
| 49 mlocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); |
| 50 mlocationRequest.setInterval(30 * 1000); |
| 51 mlocationRequest.setFastestInterval(5 * 1000); |
| 52 } |
| 53 |
| 54 protected void disconnect() { |
| 55 mGoogleApiClient.disconnect(); |
| 56 } |
| 57 |
| 58 @Override |
| 59 public void onConnected(Bundle connectionHint) { |
| 60 showLocationSettingsDialog(); |
| 61 } |
| 62 |
| 63 @Override |
| 64 public void onConnectionSuspended(int cause) { |
| 65 // Log.i(TAG, "LSD connection suspended: " + cause); |
| 66 } |
| 67 |
| 68 @Override |
| 69 public void onConnectionFailed(ConnectionResult result) { |
| 70 // Log.i(TAG, "LSD connection failed: " + result); |
| 71 } |
| 72 |
| 73 protected GoogleApiClient getGoogleApiClient() { |
| 74 return mGoogleApiClient; |
| 75 } |
| 76 |
| 77 |
| 78 /** |
| 79 * Returns true if the Location Settings Dialog should be shown. Otherwise r
eturns false. |
| 80 * |
| 81 * If true is returned, this method will asynchronously request the LSD to b
e shown. |
| 82 */ |
| 83 public static boolean showLocationSettingsDialog(final RequestDelegate deleg
ate) { |
| 84 |
| 85 LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Buil
der() |
| 86 .addLocationRequest(mLocationRequest); |
| 87 |
| 88 builder.setAlwaysShow(true); |
| 89 |
| 90 PendingResult<LocationSettingsResult> result = |
| 91 LocationServices.SettingsApi.checkLocationSettings(mGoogleClient, |
| 92 builder.build()); |
| 93 |
| 94 result.setResultCallback(new ResultCallback<LocationSettingsResult>() { |
| 95 @Override |
| 96 public void onResult(LocationSettingsResult result) { |
| 97 final Status status = result.getStatus(); |
| 98 final LocationSettingsStates states = result.getLocationSettingsStat
es(); |
| 99 switch (status.getStatusCode()) { |
| 100 case LocationSettingsStatusCodes.SUCCESS: |
| 101 // All location settings are satisfied. The client can |
| 102 // initialize location requests here. |
| 103 break; |
| 104 case LocationSettingsStatusCodes.RESOLUTION_REQUIRED: |
| 105 // Location settings are not satisfied, but this can be fixed |
| 106 // by showing the user a dialog. |
| 107 try { |
| 108 // Show the dialog by calling startResolutionForResult(), |
| 109 // and check the result in onActivityResult(). |
| 110 status.startResolutionForResult( |
| 111 LocationSettingsDialogRequester.this, |
| 112 REQUEST_CHECK_SETTINGS); |
| 113 } catch (SendIntentException e) { |
| 114 // Ignore the error. |
| 115 } |
| 116 break; |
| 117 case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE: |
| 118 // Location settings are not satisfied. However, we have no way |
| 119 // to fix the settings so we won't show the dialog. |
| 120 break; |
| 121 } |
| 122 } |
| 123 }); |
| 124 } |
| 125 |
| 126 @Override |
| 127 protected void onActivityResult(int requestCode, int resultCode, Intent data
) { |
| 128 final LocationSettingsStates states = LocationSettingsStates.fromIntent(in
tent); |
| 129 switch (requestCode) { |
| 130 case REQUEST_CHECK_SETTINGS: |
| 131 switch (resultCode) { |
| 132 case Activity.RESULT_OK: |
| 133 // All required changes were successfully made. |
| 134 // Call back the native code. |
| 135 break; |
| 136 case Activity.RESULT_CANCELED: |
| 137 // The user was asked to change settings, but chose not to. |
| 138 // Call back the native code. |
| 139 break; |
| 140 default: |
| 141 break; |
| 142 } |
| 143 break; |
| 144 } |
| 145 } |
| 146 } |
OLD | NEW |