Skip to content

Commit

Permalink
Fixes #59 - Use GPS provider on android 31.
Browse files Browse the repository at this point in the history
  • Loading branch information
HarelM committed Jan 19, 2022
1 parent 1372d89 commit 49811b1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Build;
import android.os.Bundle;

import com.marianhello.bgloc.Config;
Expand Down Expand Up @@ -204,7 +205,11 @@ private void setPace(Boolean value) {
}
} else {
String provider = locationManager.getBestProvider(criteria, true);
logger.info("Requesting location updates from provider {}", provider);
if (locationManager.getAllProviders().contains(LocationManager.GPS_PROVIDER) &&
Build.VERSION.SDK_INT > 30) {
provider = LocationManager.GPS_PROVIDER;
}
logger.info("Requesting location updates from provider {}", provider);
locationManager.requestLocationUpdates(provider, mConfig.getInterval(), scaledDistanceFilter, this);
}
} catch (SecurityException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Build;
import android.os.Bundle;

import com.marianhello.bgloc.Config;
Expand Down Expand Up @@ -35,20 +36,22 @@ public void onStart() {
if (isStarted) {
return;
}

//Criteria criteria = new Criteria();
//criteria.setAltitudeRequired(false);
//criteria.setBearingRequired(false);
//criteria.setSpeedRequired(true);
//criteria.setCostAllowed(true);
//criteria.setAccuracy(Criteria.ACCURACY_FINE);
//criteria.setHorizontalAccuracy(translateDesiredAccuracy(mConfig.getDesiredAccuracy()));
//criteria.setPowerRequirement(Criteria.POWER_HIGH);

String provider = LocationManager.GPS_PROVIDER;
if (!locationManager.getAllProviders().contains(LocationManager.GPS_PROVIDER) ||
Build.VERSION.SDK_INT <= 30) {
Criteria criteria = new Criteria();
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setSpeedRequired(true);
criteria.setCostAllowed(true);
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setHorizontalAccuracy(translateDesiredAccuracy(mConfig.getDesiredAccuracy()));
criteria.setPowerRequirement(Criteria.POWER_HIGH);
provider = locationManager.getBestProvider(criteria, true);
}
try {
//String provider = locationManager.getBestProvider(criteria, true);
//logger.info("Requesting location updates from provider {}", provider);
locationManager.requestLocationUpdates("gps", mConfig.getInterval(), mConfig.getDistanceFilter(), this);
logger.info("Requesting location updates from provider {}", provider);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, mConfig.getInterval(), mConfig.getDistanceFilter(), this);
isStarted = true;
} catch (SecurityException e) {
logger.error("Security exception: {}", e.getMessage());
Expand Down

0 comments on commit 49811b1

Please sign in to comment.