From 31f4fec8b8ab48ed9fffebd62fe23fa3b568af83 Mon Sep 17 00:00:00 2001 From: Guillaume Jacquart Date: Wed, 17 Aug 2022 08:21:32 +0200 Subject: 5898-Fix fake location crash --- .../fakelocation/FakeLocationModule.kt | 48 ++++++++++++++-------- 1 file changed, 32 insertions(+), 16 deletions(-) (limited to 'fakelocation/src') diff --git a/fakelocation/src/main/java/foundation/e/privacymodules/fakelocation/FakeLocationModule.kt b/fakelocation/src/main/java/foundation/e/privacymodules/fakelocation/FakeLocationModule.kt index 43a4545..cd01e95 100644 --- a/fakelocation/src/main/java/foundation/e/privacymodules/fakelocation/FakeLocationModule.kt +++ b/fakelocation/src/main/java/foundation/e/privacymodules/fakelocation/FakeLocationModule.kt @@ -19,12 +19,15 @@ package foundation.e.privacymodules.fakelocation import android.content.Context import android.content.Context.LOCATION_SERVICE -import android.location.Criteria import android.location.Location import android.location.LocationManager +import android.location.LocationManager.GPS_PROVIDER +import android.location.LocationManager.NETWORK_PROVIDER +import android.location.provider.ProviderProperties import android.os.Build import android.os.SystemClock import android.util.Log +import foundation.e.privacymodules.location.IFakeLocationModule /** * Implementation of the functionality of fake location. @@ -32,12 +35,10 @@ import android.util.Log * * @param context an Android context, to retrieve system services for example. */ -class FakeLocationModule(protected val context: Context) { - - /** - * List of all the Location provider that will be mocked. - */ - private val providers = listOf(LocationManager.NETWORK_PROVIDER, LocationManager.GPS_PROVIDER) +class FakeLocationModule(private val context: Context): IFakeLocationModule { + companion object { + private const val TAG = "FakeLocationModule" + } /** * Handy accessor to the locationManager service. @@ -46,18 +47,25 @@ class FakeLocationModule(protected val context: Context) { private val locationManager: LocationManager get() = context.getSystemService(LOCATION_SERVICE) as LocationManager + /** + * List of all the Location provider that will be mocked. + */ + private val providers = locationManager.allProviders + .intersect(listOf(GPS_PROVIDER, NETWORK_PROVIDER)) + /** * @see IFakeLocationModule.startFakeLocation */ @Synchronized - fun startFakeLocation() { + override fun startFakeLocation() { providers.forEach { provider -> try { locationManager.removeTestProvider(provider) } catch(e: Exception) { - Log.d("FakeLocationModule", "Test provider $provider already removed.") + Log.w(TAG, "Test provider $provider already removed.") } + locationManager.addTestProvider( provider, false, @@ -67,12 +75,17 @@ class FakeLocationModule(protected val context: Context) { false, true, true, - Criteria.POWER_LOW, Criteria.ACCURACY_FINE) - locationManager.setTestProviderEnabled(provider, true) + ProviderProperties.POWER_USAGE_LOW, + ProviderProperties.ACCURACY_FINE) + try { + locationManager.setTestProviderEnabled(provider, true) + } catch (e: Exception) { + Log.e(TAG, "Can't enable test $provider", e) + } } } - fun setFakeLocation(latitude: Double, longitude: Double) { + override fun setFakeLocation(latitude: Double, longitude: Double) { context.startService(FakeLocationService.buildFakeLocationIntent(context, latitude, longitude)) } @@ -95,22 +108,25 @@ class FakeLocationModule(protected val context: Context) { location.verticalAccuracyMeters = 0.1f location.speedAccuracyMetersPerSecond = 0.01f } - - locationManager.setTestProviderLocation(provider, location) + try { + locationManager.setTestProviderLocation(provider, location) + } catch (e: Exception) { + Log.e(TAG, "Can't set location for test provider $provider", e) + } } } /** * @see IFakeLocationModule.stopFakeLocation */ - fun stopFakeLocation() { + override fun stopFakeLocation() { context.stopService(FakeLocationService.buildStopIntent(context)) providers.forEach { provider -> try { locationManager.setTestProviderEnabled(provider, false) locationManager.removeTestProvider(provider) } catch (e: Exception) { - Log.d("FakeLocationModule", "Test provider $provider already removed.") + Log.d(TAG, "Test provider $provider already removed.") } } } -- cgit v1.2.1