From 016958380b1a1c1e2e578a83f9d5cc5b4ff62fe1 Mon Sep 17 00:00:00 2001 From: Harel M Date: Thu, 20 Jan 2022 14:01:08 +0200 Subject: [PATCH] Android 12 accuracy issue (#62) (#63) * 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 --- .../DistanceFilterLocationProvider.java | 12 +++++++-- .../provider/LocationProviderFactory.java | 1 - .../bgloc/provider/RawLocationProvider.java | 27 +++++++++++-------- plugin.xml | 2 +- 4 files changed, 27 insertions(+), 15 deletions(-) rename android/common/src/main/java/com/{tenforwardconsulting/bgloc => marianhello/bgloc/provider}/DistanceFilterLocationProvider.java (97%) diff --git a/android/common/src/main/java/com/tenforwardconsulting/bgloc/DistanceFilterLocationProvider.java b/android/common/src/main/java/com/marianhello/bgloc/provider/DistanceFilterLocationProvider.java similarity index 97% rename from android/common/src/main/java/com/tenforwardconsulting/bgloc/DistanceFilterLocationProvider.java rename to android/common/src/main/java/com/marianhello/bgloc/provider/DistanceFilterLocationProvider.java index bada0c48f..dfbcb04dc 100644 --- a/android/common/src/main/java/com/tenforwardconsulting/bgloc/DistanceFilterLocationProvider.java +++ b/android/common/src/main/java/com/marianhello/bgloc/provider/DistanceFilterLocationProvider.java @@ -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; @@ -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; @@ -198,11 +199,18 @@ private void setPace(Boolean value) { List 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()); diff --git a/android/common/src/main/java/com/marianhello/bgloc/provider/LocationProviderFactory.java b/android/common/src/main/java/com/marianhello/bgloc/provider/LocationProviderFactory.java index 4002e47c4..99873485a 100644 --- a/android/common/src/main/java/com/marianhello/bgloc/provider/LocationProviderFactory.java +++ b/android/common/src/main/java/com/marianhello/bgloc/provider/LocationProviderFactory.java @@ -12,7 +12,6 @@ import android.content.Context; import com.marianhello.bgloc.Config; -import com.tenforwardconsulting.bgloc.DistanceFilterLocationProvider; import java.lang.IllegalArgumentException; diff --git a/android/common/src/main/java/com/marianhello/bgloc/provider/RawLocationProvider.java b/android/common/src/main/java/com/marianhello/bgloc/provider/RawLocationProvider.java index fe781d368..8bf5d4399 100644 --- a/android/common/src/main/java/com/marianhello/bgloc/provider/RawLocationProvider.java +++ b/android/common/src/main/java/com/marianhello/bgloc/provider/RawLocationProvider.java @@ -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; @@ -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()); diff --git a/plugin.xml b/plugin.xml index 312e37e2c..e9aa0a971 100644 --- a/plugin.xml +++ b/plugin.xml @@ -88,6 +88,7 @@ + @@ -114,7 +115,6 @@ -