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 | |
parent | 8884e0423a4e50406b221ee07321a8560bb7420c (diff) |
5898-Fix fake location crash
3 files changed, 36 insertions, 18 deletions
diff --git a/app/src/main/java/foundation/e/privacycentralapp/domain/usecases/FakeLocationStateUseCase.kt b/app/src/main/java/foundation/e/privacycentralapp/domain/usecases/FakeLocationStateUseCase.kt index f7b5439..aad3b63 100644 --- a/app/src/main/java/foundation/e/privacycentralapp/domain/usecases/FakeLocationStateUseCase.kt +++ b/app/src/main/java/foundation/e/privacycentralapp/domain/usecases/FakeLocationStateUseCase.kt @@ -28,7 +28,7 @@ import android.util.Log import foundation.e.privacycentralapp.data.repositories.LocalStateRepository import foundation.e.privacycentralapp.domain.entities.LocationMode import foundation.e.privacycentralapp.dummy.CityDataSource -import foundation.e.privacymodules.fakelocation.FakeLocationModule +import foundation.e.privacymodules.location.IFakeLocationModule import foundation.e.privacymodules.permissions.PermissionsPrivacyModule import foundation.e.privacymodules.permissions.data.AppOpModes import foundation.e.privacymodules.permissions.data.ApplicationDescription @@ -39,7 +39,7 @@ import kotlinx.coroutines.launch import kotlin.random.Random class FakeLocationStateUseCase( - private val fakeLocationModule: FakeLocationModule, + private val fakeLocationModule: IFakeLocationModule, private val permissionsModule: PermissionsPrivacyModule, private val localStateRepository: LocalStateRepository, private val citiesRepository: CityDataSource, diff --git a/fakelocation/build.gradle b/fakelocation/build.gradle index ea28e44..945cb66 100644 --- a/fakelocation/build.gradle +++ b/fakelocation/build.gradle @@ -51,4 +51,6 @@ dependencies { Libs.AndroidX.coreKtx, Libs.Coroutines.core ) + implementation project(':privacymodule-api') + } 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.") } } } |