Skip to content

Commit

Permalink
Fix: Showing Settings dialog multiple times (#117)
Browse files Browse the repository at this point in the history
  • Loading branch information
yayaa authored Jan 3, 2021
1 parent bcfa5f2 commit 23a3001
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 186 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import com.google.android.gms.location.LocationResult;
import com.google.android.gms.location.LocationSettingsResponse;
import com.google.android.gms.location.LocationSettingsStatusCodes;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.yayandroid.locationmanager.constants.FailType;
import com.yayandroid.locationmanager.constants.ProcessType;
Expand All @@ -39,10 +38,8 @@ public class GooglePlayServicesLocationProvider extends LocationProvider impleme

@Override
public void onResume() {
// not getSourceProvider, because we don't want to connect if it is not already attempt
if (!settingsDialogIsOn && googlePlayServicesLocationSource != null &&
(isWaiting() || getConfiguration().keepTracking())) {
onConnected();
if (!settingsDialogIsOn && (isWaiting() || getConfiguration().keepTracking())) {
requestLocationUpdate();
}
}

Expand Down Expand Up @@ -72,7 +69,17 @@ public void get() {
setWaiting(true);

if (getContext() != null) {
onConnected();
LogUtils.logI("Start request location updates.");

if (getConfiguration().googlePlayServicesConfiguration().ignoreLastKnowLocation()) {
LogUtils.logI("Configuration requires to ignore last know location from GooglePlayServices Api.");

// Request fresh location
locationRequired();
} else {
// Try to get last location, if failed then request fresh location
getSourceProvider().requestLastLocation();
}
} else {
failed(FailType.VIEW_DETACHED);
}
Expand Down Expand Up @@ -105,21 +112,6 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {

}

@Override
public void onConnected() {
LogUtils.logI("Start request location updates.");

if (getConfiguration().googlePlayServicesConfiguration().ignoreLastKnowLocation()) {
LogUtils.logI("Configuration requires to ignore last know location from GooglePlayServices Api.");

// Request fresh location
requestLocation(false);
} else {
// Try to get last location, if failed then request fresh location
checkLastKnowLocation();
}
}

public void onLocationChanged(@NonNull Location location) {
if (getListener() != null) {
getListener().onLocationChanged(location);
Expand Down Expand Up @@ -204,43 +196,25 @@ void resolveSettingsApi(@NonNull ResolvableApiException resolvable) {
}
}

void checkLastKnowLocation() {
getSourceProvider().getLastLocation()
.addOnCompleteListener(new OnCompleteListener<Location>() {
@Override
public void onComplete(@NonNull Task<Location> task) {
/*
* Returns the best most recent location currently available.
*
* If a location is not available, which should happen very rarely, null will be returned.
* The best accuracy available while respecting the location permissions will be returned.
*
* This method provides a simplified way to get location.
* It is particularly well suited for applications that do not require an accurate location and that do not want to maintain extra logic for location updates.
*
* GPS location can be null if GPS is switched off
*/
if (task.isSuccessful() && task.getResult() != null) {
Location lastKnownLocation = task.getResult();

LogUtils.logI("LastKnowLocation is available.");
onLocationChanged(lastKnownLocation);

requestLocation(true);
} else {
LogUtils.logI("LastKnowLocation is not available.");

requestLocation(false);
}
}
});
}
/**
* Task result can be null in certain conditions
* See: https://developers.google.com/android/reference/com/google/android/gms/location/FusedLocationProviderClient#getLastLocation()
*/
@Override
public void onLastKnowLocationTaskReceived(@NonNull Task<Location> task) {
if (task.isSuccessful() && task.getResult() != null) {
Location lastKnownLocation = task.getResult();

void requestLocation(boolean locationIsAlreadyAvailable) {
if (getConfiguration().keepTracking() || !locationIsAlreadyAvailable) {
locationRequired();
LogUtils.logI("LastKnowLocation is available.");
onLocationChanged(lastKnownLocation);

if (getConfiguration().keepTracking()) {
LogUtils.logI("Configuration requires keepTracking.");
locationRequired();
}
} else {
LogUtils.logI("We got location, no need to ask for location updates.");
LogUtils.logI("LastKnowLocation is not available.");
locationRequired();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.location.LocationSettingsRequest;
import com.google.android.gms.location.LocationSettingsResponse;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
Expand All @@ -29,13 +30,13 @@ class GooglePlayServicesLocationSource extends LocationCallback {
private final SourceListener sourceListener;

interface SourceListener extends OnSuccessListener<LocationSettingsResponse>, OnFailureListener {
void onConnected();

void onSuccess(LocationSettingsResponse locationSettingsResponse);

void onFailure(@NonNull Exception exception);

void onLocationResult(@Nullable LocationResult locationResult);

void onLastKnowLocationTaskReceived(@NonNull Task<Location> task);
}

GooglePlayServicesLocationSource(Context context, LocationRequest locationRequest, SourceListener sourceListener) {
Expand All @@ -54,7 +55,8 @@ void checkLocationSettings() {
.addOnSuccessListener(new OnSuccessListener<LocationSettingsResponse>() {
@Override
public void onSuccess(LocationSettingsResponse locationSettingsResponse) {
if (sourceListener != null) sourceListener.onSuccess(locationSettingsResponse);
if (sourceListener != null)
sourceListener.onSuccess(locationSettingsResponse);
}
})
.addOnFailureListener(new OnFailureListener() {
Expand All @@ -81,9 +83,15 @@ Task<Void> removeLocationUpdates() {
}

@SuppressWarnings("ResourceType")
@NonNull
Task<Location> getLastLocation() {
return fusedLocationProviderClient.getLastLocation();
void requestLastLocation() {
fusedLocationProviderClient.getLastLocation()
.addOnCompleteListener(new OnCompleteListener<Location>() {
@Override
public void onComplete(@NonNull Task<Location> task) {
if (sourceListener != null)
sourceListener.onLastKnowLocationTaskReceived(task);
}
});
}

@Override
Expand Down
Loading

0 comments on commit 23a3001

Please sign in to comment.