Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 8bf32c5a authored by Abhishek Aggarwal's avatar Abhishek Aggarwal
Browse files

fakelocation: Set location once and lets have proper arguments

parent 50933c2c
Loading
Loading
Loading
Loading
Loading
+63 −35
Original line number Diff line number Diff line
@@ -21,12 +21,11 @@ import android.content.Context
import android.content.Context.LOCATION_SERVICE
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.location.LocationManager.PASSIVE_PROVIDER
import android.os.Build
import android.os.SystemClock
import android.util.Log
import java.util.Date

/**
 * Implementation of the functionality of fake location.
@@ -43,19 +42,20 @@ class FakeLocationModule(private val context: Context): IFakeLocationModule {
     * Handy accessor to the locationManager service.
     * We avoid getting it on module initialization to wait for the context to be ready.
     */
    private val locationManager: LocationManager get() =
    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))
    private val providers = locationManager.allProviders.filter { it != PASSIVE_PROVIDER }

    /**
     * @see IFakeLocationModule.startFakeLocation
     */
    @Synchronized
    @Suppress("DEPRECATION")
    override fun startFakeLocation() {
        providers.forEach { provider ->
            try {
@@ -63,29 +63,65 @@ class FakeLocationModule(private val context: Context): IFakeLocationModule {
            } catch (e: Exception) {
                Log.w(TAG, "Test provider $provider already removed.")
            }
        }


        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            providers.forEach {
                val provider = locationManager.getProviderProperties(it)
                if (provider != null) {
                    locationManager.addTestProvider(
                        it,
                        provider.hasNetworkRequirement(),
                        provider.hasSatelliteRequirement(),
                        provider.hasCellRequirement(),
                        provider.hasMonetaryCost(),
                        provider.hasAltitudeSupport(),
                        provider.hasSpeedSupport(),
                        provider.hasBearingSupport(),
                        provider.powerUsage,
                        provider.accuracy
                    )
                    enableTestProvider(it)
                }
            }
        } else {
            providers.forEach {
                val provider = locationManager.getProvider(it)
                if (provider != null) {
                    locationManager.addTestProvider(
                provider,
                false,
                false,
                false,
                false,
                false,
                true,
                true,
                ProviderProperties.POWER_USAGE_LOW,
                ProviderProperties.ACCURACY_FINE)
                        provider.name,
                        provider.requiresNetwork(),
                        provider.requiresSatellite(),
                        provider.requiresCell(),
                        provider.hasMonetaryCost(),
                        provider.supportsAltitude(),
                        provider.supportsSpeed(),
                        provider.supportsBearing(),
                        provider.powerRequirement,
                        provider.accuracy
                    )
                    enableTestProvider(provider.name)
                }
            }
        }
    }

    private fun enableTestProvider(provider: String) {
        try {
            locationManager.setTestProviderEnabled(provider, true)
        } catch (e: Exception) {
            Log.e(TAG, "Can't enable test $provider", e)
        }
    }
    }

    override fun setFakeLocation(latitude: Double, longitude: Double) {
        context.startService(FakeLocationService.buildFakeLocationIntent(context, latitude, longitude))
        context.startService(
            FakeLocationService.buildFakeLocationIntent(
                context,
                latitude,
                longitude
            )
        )
    }

    internal fun setTestProviderLocation(latitude: Double, longitude: Double) {
@@ -95,18 +131,10 @@ class FakeLocationModule(private val context: Context): IFakeLocationModule {
            location.longitude = longitude

            // Set default value for all the other required fields.
            location.altitude = 3.0
            location.time = System.currentTimeMillis()
            location.speed = 0.01f
            location.bearing = 1f
            location.accuracy = 3f
            location.time = Date().time
            location.accuracy = 0.01f
            location.elapsedRealtimeNanos = SystemClock.elapsedRealtimeNanos()

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                location.bearingAccuracyDegrees = 0.1f
                location.verticalAccuracyMeters = 0.1f
                location.speedAccuracyMetersPerSecond = 0.01f
            }
            try {
                locationManager.setTestProviderLocation(provider, location)
            } catch (e: Exception) {
+10 −21
Original line number Diff line number Diff line
@@ -17,7 +17,6 @@

package foundation.e.privacymodules.fakelocation


import android.app.Service
import android.content.Context
import android.content.Intent
@@ -83,11 +82,7 @@ class FakeLocationService: Service() {
        super.onDestroy()
    }


    private fun initTimer() {
        countDownTimer?.cancel()
        countDownTimer = object: CountDownTimer(PERIOD_UPDATES_SERIE, PERIOD_LOCATION_UPDATE) {
            override fun onTick(millisUntilFinished: Long) {
        fakeLocation?.let {
            try {
                fakeLocationModule.setTestProviderLocation(
@@ -100,12 +95,6 @@ class FakeLocationService: Service() {
        }
    }

            override fun onFinish() {
                initTimer()
            }
        }.start()
    }

    override fun onBind(intent: Intent?): IBinder? {
        return null
    }