diff options
author | mdooley <mdooley@google.com> | 2017-10-03 01:00:59 +0000 |
---|---|---|
committer | android-build-merger <android-build-merger@google.com> | 2017-10-03 01:00:59 +0000 |
commit | d611ee9db80805edb6c7ebb5be7bb2d2b135ab0b (patch) | |
tree | f335b4bc18f85f9a27a762031a06d7f4cf386f2e | |
parent | b3491f9095e445e8be29819765d23b8d84173a71 (diff) | |
parent | 7c54c425841f5dd6a9f08be95a2ddc6ca8d03aeb (diff) |
Merge "Updating emergency location code to use non-deprecated location apis"
am: 7c54c42584
Change-Id: Ia3cd588e5608611abeaf54e4bf97740805d33bc8
-rw-r--r-- | java/com/android/incallui/calllocation/impl/LocationHelper.java | 85 |
1 files 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<Status>() { - @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 |