Skip to content

Commit 8a79db7

Browse files
committed
LocationGooglePlayServicesProvider compatible with Fragment mrmans0n#197
https://github.com/mrmans0n/smart-location-lib/pull/197/files
1 parent 7ea49b0 commit 8a79db7

File tree

2 files changed

+66
-2
lines changed

2 files changed

+66
-2
lines changed

library/src/main/java/io/nlopez/smartlocation/location/providers/LocationGooglePlayServicesProvider.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
import android.location.Location;
1010
import android.os.Bundle;
1111
import android.os.Looper;
12+
import android.support.annotation.NonNull;
1213
import android.support.v4.app.ActivityCompat;
14+
import android.support.v4.app.Fragment;
1315

1416
import com.google.android.gms.common.ConnectionResult;
1517
import com.google.android.gms.common.api.GoogleApiClient;
@@ -52,6 +54,7 @@ public class LocationGooglePlayServicesProvider implements ServiceLocationProvid
5254
private boolean checkLocationSettings;
5355
private boolean fulfilledCheckLocationSettings;
5456
private boolean alwaysShow = true;
57+
private Fragment mFragment;
5558

5659
public LocationGooglePlayServicesProvider() {
5760
checkLocationSettings = false;
@@ -68,6 +71,21 @@ public LocationGooglePlayServicesProvider(ServiceConnectionListener serviceListe
6871
this.serviceListener = serviceListener;
6972
}
7073

74+
public LocationGooglePlayServicesProvider(@NonNull Fragment fragment) {
75+
this();
76+
mFragment = fragment;
77+
}
78+
79+
public LocationGooglePlayServicesProvider(@NonNull Fragment fragment, GooglePlayServicesListener playServicesListener) {
80+
this(fragment);
81+
googlePlayServicesListener = playServicesListener;
82+
}
83+
84+
public LocationGooglePlayServicesProvider(@NonNull Fragment fragment, ServiceConnectionListener serviceListener) {
85+
this(fragment);
86+
this.serviceListener = serviceListener;
87+
}
88+
7189
@Override
7290
public void init(Context context, Logger logger) {
7391
this.logger = logger;
@@ -359,7 +377,13 @@ public void onResult(LocationSettingsResult locationSettingsResult) {
359377
logger.w("Location settings are not satisfied. Show the user a dialog to " +
360378
"upgrade location settings. You should hook into the Activity onActivityResult and call this provider's onActivityResult method for continuing this call flow. ");
361379

362-
if (context instanceof Activity) {
380+
if (mFragment != null) {
381+
try {
382+
mFragment.startIntentSenderForResult(status.getResolution().getIntentSender(), REQUEST_CHECK_SETTINGS, null, 0, 0, 0, null);
383+
} catch (IntentSender.SendIntentException e) {
384+
logger.i("getIntentSender error unable to execute request.");
385+
}
386+
} else if (context instanceof Activity) {
363387
try {
364388
// Show the dialog by calling startResolutionForResult(), and check the result
365389
// in onActivityResult().

library/src/main/java/io/nlopez/smartlocation/location/providers/LocationGooglePlayServicesWithFallbackProvider.java

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package io.nlopez.smartlocation.location.providers;
22

33
import android.content.Context;
4+
import android.content.Intent;
45
import android.location.Location;
56
import android.os.Bundle;
7+
import android.support.annotation.NonNull;
8+
import android.support.v4.app.Fragment;
69

710
import com.google.android.gms.common.ConnectionResult;
11+
import com.google.android.gms.common.GoogleApiAvailability;
812
import com.google.android.gms.common.GooglePlayServicesUtil;
913

1014
import io.nlopez.smartlocation.OnLocationUpdatedListener;
@@ -28,13 +32,43 @@ public class LocationGooglePlayServicesWithFallbackProvider implements LocationP
2832
private LocationProvider provider;
2933

3034
public LocationGooglePlayServicesWithFallbackProvider(Context context) {
31-
if (GooglePlayServicesUtil.isGooglePlayServicesAvailable(context) == ConnectionResult.SUCCESS) {
35+
if (GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(context) == ConnectionResult.SUCCESS) {
3236
provider = new LocationGooglePlayServicesProvider(this);
3337
} else {
3438
provider = new LocationManagerProvider();
3539
}
3640
}
3741

42+
public LocationGooglePlayServicesWithFallbackProvider(@NonNull Fragment fragment, Context context) {
43+
if (GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(context) == ConnectionResult.SUCCESS) {
44+
provider = new LocationGooglePlayServicesProvider(fragment, this);
45+
} else {
46+
provider = new LocationManagerProvider();
47+
}
48+
}
49+
50+
public LocationGooglePlayServicesWithFallbackProvider(Context context, boolean checkLocationSettings, boolean alwaysShow) {
51+
if (GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(context) == ConnectionResult.SUCCESS) {
52+
LocationGooglePlayServicesProvider locationGooglePlayServicesProvider = new LocationGooglePlayServicesProvider(this);
53+
locationGooglePlayServicesProvider.setCheckLocationSettings(checkLocationSettings);
54+
locationGooglePlayServicesProvider.setLocationSettingsAlwaysShow(alwaysShow);
55+
provider = locationGooglePlayServicesProvider;
56+
} else {
57+
provider = new LocationManagerProvider();
58+
}
59+
}
60+
61+
public LocationGooglePlayServicesWithFallbackProvider(@NonNull Fragment fragment, Context context, boolean checkLocationSettings, boolean alwaysShow) {
62+
if (GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(context) == ConnectionResult.SUCCESS) {
63+
LocationGooglePlayServicesProvider locationGooglePlayServicesProvider = new LocationGooglePlayServicesProvider(fragment, this);
64+
locationGooglePlayServicesProvider.setCheckLocationSettings(checkLocationSettings);
65+
locationGooglePlayServicesProvider.setLocationSettingsAlwaysShow(alwaysShow);
66+
provider = locationGooglePlayServicesProvider;
67+
} else {
68+
provider = new LocationManagerProvider();
69+
}
70+
}
71+
3872
@Override
3973
public void init(Context context, Logger logger) {
4074
this.logger = logger;
@@ -80,6 +114,12 @@ public void onConnectionFailed(ConnectionResult connectionResult) {
80114
fallbackToLocationManager();
81115
}
82116

117+
public void onActivityResult(int requestCode, int resultCode, Intent data) {
118+
if (provider instanceof LocationGooglePlayServicesProvider) {
119+
((LocationGooglePlayServicesProvider) provider).onActivityResult(requestCode, resultCode, data);
120+
}
121+
}
122+
83123
private void fallbackToLocationManager() {
84124
logger.d("FusedLocationProvider not working, falling back and using LocationManager");
85125
provider = new LocationManagerProvider();

0 commit comments

Comments
 (0)