Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Showing Settings dialog multiple times #117

Merged
merged 1 commit into from
Jan 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

onResume was used to keep the GoogleClient connection on, but since now that is taken care of by FusedLocationProviderClient directly, responsibility here is changed to request a location update so that is guaranteed that location provider client will be connected to this listener.

Requesting a location update multiple times will only replace the existing request, so it is safe to use it for keeping connection here. Any other action onResume can cause multiple calls. For example; Calling LocationManager.getLocation in Activity.onCreate will trigger get call and after onCreate, onResume will be triggered by the system. So, that is why, onResume should only re-attach listeners or re-connect clients nothing more.

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