Skip to content

Commit

Permalink
Android 12 accuracy issue (#62) (#63)
Browse files Browse the repository at this point in the history
* Move distance location provider

* Fix package name after movement

* Added logging for provider selection

* Hard code gps

* Fixes #59 - Use GPS provider on android 31.

* Fix hard coded parameter
  • Loading branch information
HarelM authored Jan 20, 2022
1 parent bbec5be commit 0169583
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
1. location is not persisted to db anymore, but broadcasted using intents instead
*/

package com.tenforwardconsulting.bgloc;
package com.marianhello.bgloc.provider;

import android.app.AlarmManager;
import android.app.PendingIntent;
Expand All @@ -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 @@ -198,11 +199,18 @@ private void setPace(Boolean value) {
List<String> matchingProviders = locationManager.getAllProviders();
for (String provider: matchingProviders) {
if (provider != LocationManager.PASSIVE_PROVIDER) {
logger.info("Requesting location updates from provider {}", provider);
locationManager.requestLocationUpdates(provider, 0, 0, this);
}
}
} else {
locationManager.requestLocationUpdates(locationManager.getBestProvider(criteria, true), mConfig.getInterval(), scaledDistanceFilter, this);
String provider = locationManager.getBestProvider(criteria, true);
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) {
logger.error("Security exception: {}", e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import android.content.Context;

import com.marianhello.bgloc.Config;
import com.tenforwardconsulting.bgloc.DistanceFilterLocationProvider;

import java.lang.IllegalArgumentException;

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,18 +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 {
locationManager.requestLocationUpdates(locationManager.getBestProvider(criteria, true), mConfig.getInterval(), mConfig.getDistanceFilter(), this);
logger.info("Requesting location updates from provider {}", provider);
locationManager.requestLocationUpdates(provider, mConfig.getInterval(), mConfig.getDistanceFilter(), this);
isStarted = true;
} catch (SecurityException e) {
logger.error("Security exception: {}", e.getMessage());
Expand Down
2 changes: 1 addition & 1 deletion plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
<source-file src="android/common/src/main/java/com/marianhello/bgloc/PostLocationTask.java" target-dir="src/com/marianhello/bgloc" />
<source-file src="android/common/src/main/java/com/marianhello/bgloc/provider/AbstractLocationProvider.java" target-dir="src/com/marianhello/bgloc/provider" />
<source-file src="android/common/src/main/java/com/marianhello/bgloc/provider/ActivityRecognitionLocationProvider.java" target-dir="src/com/marianhello/bgloc/provider" />
<source-file src="android/common/src/main/java/com/marianhello/bgloc/provider/DistanceFilterLocationProvider.java" target-dir="src/com/marianhello/bgloc/provider" />
<source-file src="android/common/src/main/java/com/marianhello/bgloc/provider/LocationProviderFactory.java" target-dir="src/com/marianhello/bgloc/provider" />
<source-file src="android/common/src/main/java/com/marianhello/bgloc/provider/LocationProvider.java" target-dir="src/com/marianhello/bgloc/provider" />
<source-file src="android/common/src/main/java/com/marianhello/bgloc/provider/ProviderDelegate.java" target-dir="src/com/marianhello/bgloc/provider" />
Expand All @@ -114,7 +115,6 @@
<source-file src="android/common/src/main/java/com/marianhello/utils/Convert.java" target-dir="src/com/marianhello/utils" />
<source-file src="android/common/src/main/java/com/marianhello/utils/TextUtils.java" target-dir="src/com/marianhello/utils" />
<source-file src="android/common/src/main/java/com/marianhello/utils/ToneGenerator.java" target-dir="src/com/marianhello/utils" />
<source-file src="android/common/src/main/java/com/tenforwardconsulting/bgloc/DistanceFilterLocationProvider.java" target-dir="src/com/tenforwardconsulting/bgloc" />
<source-file src="android/common/src/main/java/org/apache/commons/io/Charsets.java" target-dir="src/org/apache/commons/io" />
<source-file src="android/common/src/main/java/org/apache/commons/io/input/ReversedLinesFileReader.java" target-dir="src/org/apache/commons/io/input" />
<source-file src="android/common/src/main/java/org/chromium/content/browser/ThreadUtils.java" target-dir="src/org/chromium/content/browser" />
Expand Down

0 comments on commit 0169583

Please sign in to comment.