Skip to content

Commit

Permalink
[TIMOB-20522] Only use FusedLocationProviderClient when Google Play S…
Browse files Browse the repository at this point in the history
…ervices is available
  • Loading branch information
Gary Mathews committed Nov 29, 2017
1 parent 47c3139 commit 7ce1881
Show file tree
Hide file tree
Showing 5 changed files with 246 additions and 132 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import ti.modules.titanium.geolocation.TiLocation.GeocodeResponseHandler;
import ti.modules.titanium.geolocation.android.AndroidModule;
import ti.modules.titanium.geolocation.android.FusedLocationProvider;
import ti.modules.titanium.geolocation.android.LocationProviderProxy;
import ti.modules.titanium.geolocation.android.LocationProviderProxy.LocationProviderListener;
import ti.modules.titanium.geolocation.android.LocationRuleProxy;
Expand All @@ -43,17 +44,6 @@
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
import com.google.android.gms.common.api.ApiException;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.Task;

/**
* GeolocationModule exposes all common methods and properties relating to geolocation behavior
* associated with Ti.Geolocation to the Titanium developer. Only cross platform API points should
Expand Down Expand Up @@ -137,7 +127,7 @@
TiC.PROPERTY_PREFERRED_PROVIDER
})
public class GeolocationModule extends KrollModule
implements Handler.Callback, LocationProviderListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener
implements Handler.Callback, LocationProviderListener
{
// TODO: remove these constants, they are deprecated and have moved into the Android proxy
@Kroll.constant @Deprecated public static final String PROVIDER_PASSIVE = AndroidModule.PROVIDER_PASSIVE;
Expand Down Expand Up @@ -192,12 +182,7 @@ public class GeolocationModule extends KrollModule
@Deprecated private double legacyLocationFrequency = 5000;
@Deprecated private String legacyLocationPreferredProvider = PROVIDER_NETWORK;

private static int googleApiCode;
private static int googleApiVersion;
private static GoogleApiClient googleApiClient;
private static FusedLocationProviderClient fusedLocationClient;
private ArrayList<LocationProviderProxy> fusedLocationQueue = new ArrayList<LocationProviderProxy>();
private static ArrayList<LocationProviderProxy> fusedLocationProviders = new ArrayList<LocationProviderProxy>();
private FusedLocationProvider fusedLocationProvider;

/**
* Constructor
Expand All @@ -208,6 +193,8 @@ public GeolocationModule()

Context context = TiApplication.getInstance().getRootOrCurrentActivity();

fusedLocationProvider = new FusedLocationProvider(context, this);

tiLocation = new TiLocation();
tiCompass = new TiCompass(this, tiLocation);

Expand All @@ -226,25 +213,6 @@ public GeolocationModule()
// create these now but we don't want to include these in the rule set unless the simple GPS provider is enabled
simpleLocationGpsRule = new LocationRuleProxy(PROVIDER_GPS, null, SIMPLE_LOCATION_GPS_MIN_AGE_RULE, null);
simpleLocationNetworkRule = new LocationRuleProxy(PROVIDER_NETWORK, SIMPLE_LOCATION_NETWORK_DISTANCE_RULE, SIMPLE_LOCATION_NETWORK_MIN_AGE_RULE, null);

googleApiCode = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(context);
if (googleApiCode == ConnectionResult.SUCCESS) {

// requires Google Play Services 11.0.0 or later
if (googleApiClient == null && googleApiVersion >= 11000000) {
googleApiClient = new GoogleApiClient.Builder(context)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();

googleApiClient.connect();

if (fusedLocationClient == null) {
fusedLocationClient = LocationServices.getFusedLocationProviderClient(context);
}
}
}
}

/**
Expand Down Expand Up @@ -705,52 +673,8 @@ public void registerLocationProvider(final LocationProviderProxy locationProvide
return;
}

if (googleApiClient != null) {
if (googleApiClient.isConnected()) {
final LocationRequest request = LocationRequest.create();
request.setSmallestDisplacement((float) locationProvider.getMinUpdateDistance());
request.setInterval((long) locationProvider.getMinUpdateTime());

int accuracy = getProperties().optInt(TiC.PROPERTY_ACCURACY, ACCURACY_LOW);
switch (accuracy) {
case ACCURACY_LOW:
case ACCURACY_HUNDRED_METERS:
case ACCURACY_KILOMETER:
case ACCURACY_THREE_KILOMETERS: {
request.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
}
break;
case ACCURACY_HIGH:
case ACCURACY_NEAREST_TEN_METERS:
default: {
request.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}
}

fusedLocationClient.requestLocationUpdates(request, locationProvider.getLocationCallback(), null)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
fusedLocationProviders.add(locationProvider);
} else {
Log.e(TAG, "requestLocationUpdates() task failed");
}
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
if (e instanceof ApiException) {
Log.e(TAG, ((ApiException) e).getStatusMessage());
} else {
Log.e(TAG, e.getMessage());
}
}
});
} else {
fusedLocationQueue.add(locationProvider);
}
if (FusedLocationProvider.hasPlayServices()) {
fusedLocationProvider.registerLocationProvider(locationProvider);
} else {
String provider = TiConvert.toString(locationProvider.getProperty(TiC.PROPERTY_NAME));

Expand All @@ -773,11 +697,8 @@ public void onFailure(@NonNull Exception e) {
@SuppressLint("MissingPermission")
public void unregisterLocationProvider(LocationProviderProxy locationProvider)
{
if (fusedLocationClient != null) {
if (fusedLocationProviders.contains(locationProvider)) {
fusedLocationClient.removeLocationUpdates(locationProvider.getLocationCallback());
fusedLocationProviders.remove(locationProvider);
}
if (FusedLocationProvider.hasPlayServices()) {
fusedLocationProvider.unregisterLocationProvider(locationProvider);
} else {
tiLocation.locationManager.removeUpdates(locationProvider);
}
Expand Down Expand Up @@ -1060,24 +981,4 @@ public void onDestroy(Activity activity) {
super.onDestroy(activity);

}

@Override
public void onConnected(@Nullable Bundle bundle) {
if (fusedLocationQueue.size() > 0) {
for (LocationProviderProxy provider : fusedLocationQueue) {
registerLocationProvider(provider);
}
fusedLocationQueue.clear();
}
}

@Override
public void onConnectionSuspended(int i) {
Log.e(TAG, "Google Play Services connection suspended!");
}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
Log.e(TAG, "Google Play Services connection failed!");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import android.os.Handler;
import android.os.Message;


/**
* AndroidModule exposes all Android specific methods and properties relating to geolocation behavior
* associated with Ti.Geolocation.Android to the Titanium developer. Cross platform API points should
Expand Down

0 comments on commit 7ce1881

Please sign in to comment.