diff options
author | Guillaume Jacquart <guillaume.jacquart-ext@mousquetaires.com> | 2022-08-17 08:21:32 +0200 |
---|---|---|
committer | Guillaume Jacquart <guillaume.jacquart-ext@mousquetaires.com> | 2022-08-18 09:57:49 +0200 |
commit | 31f4fec8b8ab48ed9fffebd62fe23fa3b568af83 (patch) | |
tree | c32234269ebf121354a809f99e2876f245e96b90 /fakelocation/src/main | |
parent | 8884e0423a4e50406b221ee07321a8560bb7420c (diff) |
5898-Fix fake location crash
Diffstat (limited to 'fakelocation/src/main')
-rw-r--r-- | fakelocation/src/main/java/foundation/e/privacymodules/fakelocation/FakeLocationModule.kt | 48 |
1 files changed, 32 insertions, 16 deletions
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. @@ -47,17 +48,24 @@ class FakeLocationModule(protected val context: Context) { 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.") } } } |