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

Unverified Commit f3b45ecb authored by Marvin W.'s avatar Marvin W. 🐿️
Browse files

Map: Add support for custom location sources

parent ff103e75
Loading
Loading
Loading
Loading
+37 −13
Original line number Diff line number Diff line
@@ -57,6 +57,7 @@ import com.mapbox.mapboxsdk.WellKnownTileServer
import org.microg.gms.maps.mapbox.model.InfoWindow
import org.microg.gms.maps.mapbox.model.getInfoWindowViewFor
import com.mapbox.mapboxsdk.camera.CameraUpdateFactory
import com.mapbox.mapboxsdk.location.engine.*
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback
import org.microg.gms.maps.mapbox.model.*
import org.microg.gms.maps.mapbox.utils.MultiArchLoader
@@ -120,6 +121,8 @@ class GoogleMapImpl(context: Context, var options: GoogleMapOptions) : AbstractG
    val waitingCameraUpdates = mutableListOf<CameraUpdate>()
    var locationEnabled: Boolean = false

    var locationEngine: LocationEngine? = null

    var isStarted = false

    init {
@@ -396,8 +399,17 @@ class GoogleMapImpl(context: Context, var options: GoogleMapOptions) : AbstractG
        synchronized(mapLock) {
            locationEnabled = myLocation
            if (!loaded) return
            val locationComponent = map?.locationComponent ?: return
            try {
                updateLocationEngineListener(myLocation)
            } catch (e: SecurityException) {
                Log.w(TAG, e)
                locationEnabled = false
            }
        }
    }

    private fun updateLocationEngineListener(myLocation: Boolean) {
        val locationComponent = map?.locationComponent ?: return
        if (locationComponent.isLocationComponentActivated) {
            locationComponent.isLocationComponentEnabled = myLocation
            if (myLocation) {
@@ -410,10 +422,21 @@ class GoogleMapImpl(context: Context, var options: GoogleMapOptions) : AbstractG
                locationComponent.locationEngine?.removeLocationUpdates(locationEngineCallback)
            }
        }
            } catch (e: SecurityException) {
                Log.w(TAG, e)
                locationEnabled = false
    }

    override fun setLocationSource(locationSource: ILocationSourceDelegate?) {
        synchronized(mapLock) {
            updateLocationEngineListener(false)
            locationEngine = locationSource?.let { SourceLocationEngine(it) }
            if (!loaded) return
            if (map?.locationComponent?.isLocationComponentActivated == true) {
                if (locationEngine != null) {
                    map?.locationComponent?.locationEngine = locationEngine
                } else {
                    map?.locationComponent?.locationEngine = LocationEngineDefault.getDefaultLocationEngine(mapContext)
                }
            }
            updateLocationEngineListener(locationEnabled)
        }
    }

@@ -727,6 +750,7 @@ class GoogleMapImpl(context: Context, var options: GoogleMapOptions) : AbstractG

                map.locationComponent.apply {
                    activateLocationComponent(LocationComponentActivationOptions.builder(mapContext, it)
                            .locationEngine(this@GoogleMapImpl.locationEngine)
                            .useSpecializedLocationLayer(true)
                            .locationComponentOptions(LocationComponentOptions.builder(mapContext).pulseEnabled(true).build())
                            .build())
+9 −3
Original line number Diff line number Diff line
@@ -90,7 +90,7 @@ class LiteGoogleMapImpl(context: Context, var options: GoogleMapOptions) : Abstr

    private var myLocationEnabled = false
    private var myLocation: Location? = null
    private var locationEngineProvider: LocationEngine = LocationEngineDefault.getDefaultLocationEngine(mapContext)
    private var locationEngine: LocationEngine = LocationEngineDefault.getDefaultLocationEngine(mapContext)

    internal val markers: MutableList<LiteMarkerImpl> = mutableListOf()
    internal val polygons: MutableList<LitePolygonImpl> = mutableListOf()
@@ -486,7 +486,7 @@ class LiteGoogleMapImpl(context: Context, var options: GoogleMapOptions) : Abstr
                mapContext, Manifest.permission.ACCESS_COARSE_LOCATION
            ) == PackageManager.PERMISSION_GRANTED
        ) {
            locationEngineProvider.requestLocationUpdates(
            locationEngine.requestLocationUpdates(
                LocationEngineRequest.Builder(DEFAULT_INTERVAL_MILLIS)
                    .setFastestInterval(DEFAULT_FASTEST_INTERVAL_MILLIS)
                    .setPriority(LocationEngineRequest.PRIORITY_HIGH_ACCURACY)
@@ -499,7 +499,13 @@ class LiteGoogleMapImpl(context: Context, var options: GoogleMapOptions) : Abstr
    }

    private fun deactivateLocationProvider() {
        locationEngineProvider.removeLocationUpdates(locationEngineCallback)
        locationEngine.removeLocationUpdates(locationEngineCallback)
    }

    override fun setLocationSource(locationSource: ILocationSourceDelegate?) {
        if (myLocationEnabled) deactivateLocationProvider()
        locationEngine = locationSource?.let { SourceLocationEngine(it) } ?: LocationEngineDefault.getDefaultLocationEngine(mapContext)
        if (myLocationEnabled) activateLocationProvider()
    }

    override fun onLocationUpdate(location: Location) {
+57 −0
Original line number Diff line number Diff line
/*
 * SPDX-FileCopyrightText: 2023 microG Project Team
 * SPDX-License-Identifier: Apache-2.0
 */

package org.microg.gms.maps.mapbox

import android.app.PendingIntent
import android.location.Location
import android.os.Handler
import android.os.Looper
import com.google.android.gms.maps.internal.ILocationSourceDelegate
import com.google.android.gms.maps.internal.IOnLocationChangeListener
import com.mapbox.mapboxsdk.location.engine.LocationEngine
import com.mapbox.mapboxsdk.location.engine.LocationEngineCallback
import com.mapbox.mapboxsdk.location.engine.LocationEngineRequest
import com.mapbox.mapboxsdk.location.engine.LocationEngineResult

class SourceLocationEngine(private val locationSource: ILocationSourceDelegate) : LocationEngine, IOnLocationChangeListener.Stub() {
    val callbacks: MutableSet<Pair<LocationEngineCallback<LocationEngineResult>, Handler>> = hashSetOf()
    var lastLocation: Location? = null

    override fun getLastLocation(callback: LocationEngineCallback<LocationEngineResult>) {
        callback.onSuccess(LocationEngineResult.create(lastLocation))
    }

    override fun requestLocationUpdates(request: LocationEngineRequest, callback: LocationEngineCallback<LocationEngineResult>, looper: Looper?) {
        callbacks.add(callback to Handler(looper ?: Looper.myLooper() ?: Looper.getMainLooper()))
        if (callbacks.size == 1) {
            locationSource.activate(this)
        }
    }

    override fun requestLocationUpdates(request: LocationEngineRequest, pendingIntent: PendingIntent?) {
        throw UnsupportedOperationException()
    }

    override fun removeLocationUpdates(callback: LocationEngineCallback<LocationEngineResult>) {
        callbacks.removeAll { it.first == callback }
        if (callbacks.isEmpty()) {
            locationSource.deactivate()
        }
    }

    override fun removeLocationUpdates(pendingIntent: PendingIntent?) {
        throw UnsupportedOperationException()
    }

    override fun onLocationChanged(location: Location?) {
        lastLocation = location
        for ((callback, handler) in callbacks) {
            handler.post {
                callback.onSuccess(LocationEngineResult.create(location))
            }
        }
    }
}
 No newline at end of file