From 6038c5d6d86087bed87a9475d5f7814195139b27 Mon Sep 17 00:00:00 2001 From: mdooley Date: Thu, 28 Sep 2017 15:26:01 -0700 Subject: Updating emergency location code to use non-deprecated location apis Also tuning the location request accuracy and minimum update interval. i noticed during testing that we often get locations with uncertainties greater than our threshold (100 meters), which we ignore. That combined with the long update interval can result in not showing a location for a long time. Bug: 67046739 Test: manual PiperOrigin-RevId: 170407138 Change-Id: I1a84088f9fa09b474b07e375b92af25852dd6f46 --- .../incallui/calllocation/impl/LocationHelper.java | 85 +++++++++------------- 1 file changed, 33 insertions(+), 52 deletions(-) diff --git a/java/com/android/incallui/calllocation/impl/LocationHelper.java b/java/com/android/incallui/calllocation/impl/LocationHelper.java index 99a759bbb..48baa020a 100644 --- a/java/com/android/incallui/calllocation/impl/LocationHelper.java +++ b/java/com/android/incallui/calllocation/impl/LocationHelper.java @@ -20,7 +20,6 @@ import android.content.Context; import android.location.Location; import android.net.ConnectivityManager; import android.net.NetworkInfo; -import android.os.Bundle; import android.os.Handler; import android.support.annotation.IntDef; import android.support.annotation.MainThread; @@ -28,12 +27,7 @@ import android.support.v4.os.UserManagerCompat; import com.android.dialer.common.Assert; import com.android.dialer.common.LogUtil; import com.android.dialer.util.PermissionsUtil; -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.FusedLocationProviderClient; import com.google.android.gms.location.LocationListener; import com.google.android.gms.location.LocationRequest; import com.google.android.gms.location.LocationServices; @@ -45,7 +39,7 @@ import java.util.List; /** Uses the Fused location service to get location and pass updates on to listeners. */ public class LocationHelper { - private static final int MIN_UPDATE_INTERVAL_MS = 30 * 1000; + private static final int MIN_UPDATE_INTERVAL_MS = 20 * 1000; private static final int LAST_UPDATE_THRESHOLD_MS = 60 * 1000; private static final int LOCATION_ACCURACY_THRESHOLD_METERS = 100; @@ -143,10 +137,7 @@ public class LocationHelper { Assert.isMainThread(); LogUtil.enterBlock("LocationHelper.close"); listeners.clear(); - - if (locationHelperInternal != null) { - locationHelperInternal.close(); - } + locationHelperInternal.close(); } @MainThread @@ -163,67 +154,57 @@ public class LocationHelper { * This class contains all the asynchronous callbacks. It only posts location changes back to the * outer class on the main thread. */ - private class LocationHelperInternal - implements ConnectionCallbacks, OnConnectionFailedListener, LocationListener { + private class LocationHelperInternal implements LocationListener { - private final GoogleApiClient apiClient; + private final FusedLocationProviderClient locationClient; private final ConnectivityManager connectivityManager; private final Handler mainThreadHandler = new Handler(); @MainThread LocationHelperInternal(Context context) { Assert.isMainThread(); - apiClient = - new GoogleApiClient.Builder(context) - .addApi(LocationServices.API) - .addConnectionCallbacks(this) - .addOnConnectionFailedListener(this) - .build(); - - LogUtil.i("LocationHelperInternal", "Connecting to location service..."); - apiClient.connect(); - + locationClient = LocationServices.getFusedLocationProviderClient(context); connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); + requestUpdates(); + getLocation(); } void close() { - if (apiClient.isConnected()) { - LogUtil.i("LocationHelperInternal", "disconnecting"); - LocationServices.FusedLocationApi.removeLocationUpdates(apiClient, this); - apiClient.disconnect(); - } + LogUtil.enterBlock("LocationHelperInternal.close"); + locationClient.removeLocationUpdates(this); } - @Override - public void onConnected(Bundle bundle) { - LogUtil.enterBlock("LocationHelperInternal.onConnected"); + private void requestUpdates() { + LogUtil.enterBlock("LocationHelperInternal.requestUpdates"); + LocationRequest locationRequest = LocationRequest.create() - .setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY) + .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY) .setInterval(MIN_UPDATE_INTERVAL_MS) .setFastestInterval(MIN_UPDATE_INTERVAL_MS); - LocationServices.FusedLocationApi.requestLocationUpdates(apiClient, locationRequest, this) - .setResultCallback( - new ResultCallback() { - @Override - public void onResult(Status status) { - if (status.getStatus().isSuccess()) { - onLocationChanged(LocationServices.FusedLocationApi.getLastLocation(apiClient)); - } - } - }); + locationClient + .requestLocationUpdates(locationRequest, this) + .addOnSuccessListener( + result -> LogUtil.i("LocationHelperInternal.requestUpdates", "onSuccess")) + .addOnFailureListener( + e -> LogUtil.e("LocationHelperInternal.requestUpdates", "onFailure", e)); } - @Override - public void onConnectionSuspended(int i) { - // Do nothing. - } - - @Override - public void onConnectionFailed(ConnectionResult result) { - // Do nothing. + private void getLocation() { + LogUtil.enterBlock("LocationHelperInternal.getLocation"); + + locationClient + .getLastLocation() + .addOnSuccessListener( + location -> { + LogUtil.i("LocationHelperInternal.getLocation", "onSuccess"); + Assert.isMainThread(); + LocationHelper.this.onLocationChanged(location, isConnected()); + }) + .addOnFailureListener( + e -> LogUtil.e("LocationHelperInternal.getLocation", "onFailure", e)); } @Override -- cgit v1.2.3