Chromium Code Reviews| 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.device.geolocation; | |
| 6 | |
| 7 import android.content.Context; | |
| 8 import android.location.Location; | |
| 9 import android.os.Bundle; | |
| 10 | |
| 11 import com.google.android.gms.common.ConnectionResult; | |
| 12 import com.google.android.gms.common.api.GoogleApiClient; | |
| 13 import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks; | |
| 14 import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListe ner; | |
| 15 import com.google.android.gms.location.FusedLocationProviderApi; | |
| 16 import com.google.android.gms.location.LocationListener; | |
| 17 import com.google.android.gms.location.LocationRequest; | |
| 18 import com.google.android.gms.location.LocationServices; | |
| 19 | |
| 20 import org.chromium.base.Log; | |
| 21 import org.chromium.base.ThreadUtils; | |
| 22 | |
| 23 /** | |
| 24 * This is a LocationProvider using Google Play Services. | |
| 25 * | |
| 26 * https://developers.google.com/android/reference/com/google/android/gms/locati on/package-summary | |
| 27 */ | |
| 28 public class LocationProviderGmsCore implements ConnectionCallbacks, OnConnectio nFailedListener, | |
| 29 LocationListener, | |
| 30 LocationProviderFactory.Location Provider { | |
| 31 private static final String TAG = "cr_LocationProvider"; | |
| 32 | |
| 33 private final GoogleApiClient mGoogleApiClient; | |
| 34 private FusedLocationProviderApi mLocationProviderApi = LocationServices.Fus edLocationApi; | |
| 35 | |
| 36 private boolean mEnablehighAccuracy = false; | |
|
agrieve
2017/04/20 15:22:23
nit: don't explicitly initialize fields to default
mcasas
2017/04/20 18:59:32
Ouch! Done.
| |
| 37 private LocationRequest mLocationRequest; | |
| 38 | |
| 39 LocationProviderGmsCore(Context context) { | |
| 40 mGoogleApiClient = new GoogleApiClient.Builder(context) | |
| 41 .addApi(LocationServices.API) | |
| 42 .addConnectionCallbacks(this) | |
| 43 .addOnConnectionFailedListener(this) | |
| 44 .build(); | |
| 45 assert mGoogleApiClient != null; | |
| 46 } | |
| 47 | |
| 48 LocationProviderGmsCore(GoogleApiClient client, FusedLocationProviderApi loc ationApi) { | |
| 49 mGoogleApiClient = client; | |
| 50 mLocationProviderApi = locationApi; | |
| 51 } | |
| 52 | |
| 53 // ConnectionCallbacks implementation | |
| 54 @Override | |
| 55 public void onConnected(Bundle connectionHint) { | |
| 56 ThreadUtils.assertOnUiThread(); | |
| 57 | |
| 58 mLocationRequest = LocationRequest.create(); | |
| 59 if (mEnablehighAccuracy) { | |
| 60 mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY) ; | |
| 61 } | |
| 62 | |
| 63 final Location location = mLocationProviderApi.getLastLocation(mGoogleAp iClient); | |
| 64 if (location != null) { | |
| 65 LocationProviderAdapter.onNewLocationAvailable(location); | |
| 66 } | |
| 67 | |
| 68 try { | |
| 69 // Request updates on UI Thread replicating LocationProviderAndroid' s behaviour. | |
| 70 mLocationProviderApi.requestLocationUpdates( | |
| 71 mGoogleApiClient, mLocationRequest, this, ThreadUtils.getUiT hreadLooper()); | |
| 72 } catch (IllegalStateException e) { | |
| 73 Log.e(TAG, "Caught IllegalStateException registering for location up dates."); | |
|
agrieve
2017/04/20 15:22:23
When does this happen? Would be good to have a com
mcasas
2017/04/20 18:59:32
Done.
| |
| 74 assert false; | |
| 75 } | |
| 76 } | |
| 77 | |
| 78 @Override | |
| 79 public void onConnectionSuspended(int cause) {} | |
| 80 | |
| 81 // OnConnectionFailedListener implementation | |
| 82 @Override | |
| 83 public void onConnectionFailed(ConnectionResult result) { | |
| 84 Log.e(TAG, "onConnectionFailed " + result.toString()); | |
|
agrieve
2017/04/20 15:22:23
Use ("onConnectionFailed %s", result). Although...
mcasas
2017/04/20 18:59:32
Moved to newErrorAvailable() and logged there.
| |
| 85 LocationProviderAdapter.newErrorAvailable( | |
| 86 "Failed to connect to Google Play Services :" + result.toString( )); | |
| 87 } | |
| 88 | |
| 89 // LocationProviderFactory.LocationProvider implementation | |
| 90 @Override | |
| 91 public void start(boolean enableHighAccuracy) { | |
| 92 ThreadUtils.assertOnUiThread(); | |
| 93 if (mGoogleApiClient.isConnected()) mGoogleApiClient.disconnect(); | |
| 94 | |
| 95 mEnablehighAccuracy = enableHighAccuracy; | |
| 96 mGoogleApiClient.connect(); // Should return via onConnected(). | |
| 97 } | |
| 98 | |
| 99 @Override | |
| 100 public void stop() { | |
| 101 ThreadUtils.assertOnUiThread(); | |
| 102 if (!mGoogleApiClient.isConnected()) return; | |
| 103 | |
| 104 mLocationProviderApi.removeLocationUpdates(mGoogleApiClient, this); | |
| 105 | |
| 106 mGoogleApiClient.disconnect(); | |
| 107 } | |
| 108 | |
| 109 @Override | |
| 110 public boolean isRunning() { | |
| 111 assert ThreadUtils.runningOnUiThread(); | |
| 112 if (mGoogleApiClient == null) return false; | |
| 113 return mGoogleApiClient.isConnecting() || mGoogleApiClient.isConnected() ; | |
| 114 } | |
| 115 | |
| 116 // LocationListener implementation | |
| 117 @Override | |
| 118 public void onLocationChanged(Location location) { | |
| 119 Log.i(TAG, "onLocationChanged: " + location.toString()); | |
|
agrieve
2017/04/20 15:22:23
Might be PII here? Don't want to log user's locati
mcasas
2017/04/20 18:59:32
Correct. This was for development only anyway. Rem
| |
| 120 LocationProviderAdapter.onNewLocationAvailable(location); | |
| 121 } | |
| 122 } | |
| OLD | NEW |