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

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

Maps: Get location from microG (likely better results than system provider)

parent f3b45ecb
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ apply plugin: 'kotlin-android'
dependencies {
    implementation project(':play-services-maps')
    implementation project(':play-services-base-core')
    implementation project(':play-services-location')
    implementation("org.maplibre.gl:android-sdk:10.0.0")
    implementation("org.maplibre.gl:android-plugin-annotation-v9:1.0.0") {
        exclude group: 'com.google.android.gms'
+63 −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.content.Context
import android.os.Looper
import com.google.android.gms.location.LocationListener
import com.google.android.gms.location.LocationRequest
import com.google.android.gms.location.LocationServices
import com.google.android.gms.location.Priority
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 GoogleLocationEngine(context: Context) : LocationEngine {
    private val listenerMap: MutableMap<LocationEngineCallback<LocationEngineResult>, LocationListener> = hashMapOf()
    private val client = LocationServices.getFusedLocationProviderClient(context)

    override fun getLastLocation(callback: LocationEngineCallback<LocationEngineResult>) {
        client.lastLocation.addOnCompleteListener {
            if (it.isSuccessful) callback.onSuccess(LocationEngineResult.create(it.result))
            else callback.onFailure(it.exception)
        }
    }

    override fun requestLocationUpdates(request: LocationEngineRequest, callback: LocationEngineCallback<LocationEngineResult>, looper: Looper?) {
        listenerMap[callback] = listenerMap[callback] ?: LocationListener { callback.onSuccess(LocationEngineResult.create(it)) }
        client.requestLocationUpdates(
            LocationRequest.Builder(request.interval)
                .setPriority(
                    when (request.priority) {
                        LocationEngineRequest.PRIORITY_HIGH_ACCURACY -> Priority.PRIORITY_HIGH_ACCURACY
                        LocationEngineRequest.PRIORITY_BALANCED_POWER_ACCURACY -> Priority.PRIORITY_BALANCED_POWER_ACCURACY
                        LocationEngineRequest.PRIORITY_LOW_POWER -> Priority.PRIORITY_LOW_POWER
                        LocationEngineRequest.PRIORITY_NO_POWER -> Priority.PRIORITY_PASSIVE
                        else -> Priority.PRIORITY_BALANCED_POWER_ACCURACY
                    }
                )
                .setMinUpdateDistanceMeters(request.displacement)
                .setMinUpdateIntervalMillis(request.fastestInterval)
                .setMaxUpdateDelayMillis(request.maxWaitTime)
                .build(), listenerMap[callback], looper
        )
    }

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

    override fun removeLocationUpdates(callback: LocationEngineCallback<LocationEngineResult>) {
        listenerMap[callback]?.let { client.removeLocationUpdates(it) }
        listenerMap.remove(callback)
    }

    override fun removeLocationUpdates(pendingIntent: PendingIntent?) {
        throw UnsupportedOperationException()
    }
}
 No newline at end of file
+5 −8
Original line number Diff line number Diff line
@@ -121,7 +121,8 @@ class GoogleMapImpl(context: Context, var options: GoogleMapOptions) : AbstractG
    val waitingCameraUpdates = mutableListOf<CameraUpdate>()
    var locationEnabled: Boolean = false

    var locationEngine: LocationEngine? = null
    val defaultLocationEngine = GoogleLocationEngine(context)
    var locationEngine: LocationEngine = defaultLocationEngine

    var isStarted = false

@@ -427,14 +428,10 @@ class GoogleMapImpl(context: Context, var options: GoogleMapOptions) : AbstractG
    override fun setLocationSource(locationSource: ILocationSourceDelegate?) {
        synchronized(mapLock) {
            updateLocationEngineListener(false)
            locationEngine = locationSource?.let { SourceLocationEngine(it) }
            locationEngine = locationSource?.let { SourceLocationEngine(it) } ?: defaultLocationEngine
            if (!loaded) return
            if (map?.locationComponent?.isLocationComponentActivated == true) {
                if (locationEngine != null) {
                map?.locationComponent?.locationEngine = locationEngine
                } else {
                    map?.locationComponent?.locationEngine = LocationEngineDefault.getDefaultLocationEngine(mapContext)
                }
            }
            updateLocationEngineListener(locationEnabled)
        }
@@ -754,7 +751,7 @@ class GoogleMapImpl(context: Context, var options: GoogleMapOptions) : AbstractG
                            .useSpecializedLocationLayer(true)
                            .locationComponentOptions(LocationComponentOptions.builder(mapContext).pulseEnabled(true).build())
                            .build())
                    cameraMode = CameraMode.TRACKING
                    cameraMode = CameraMode.NONE
                    renderMode = RenderMode.COMPASS
                }

+3 −2
Original line number Diff line number Diff line
@@ -90,7 +90,8 @@ class LiteGoogleMapImpl(context: Context, var options: GoogleMapOptions) : Abstr

    private var myLocationEnabled = false
    private var myLocation: Location? = null
    private var locationEngine: LocationEngine = LocationEngineDefault.getDefaultLocationEngine(mapContext)
    private val defaultLocationEngine = GoogleLocationEngine(context)
    private var locationEngine: LocationEngine = defaultLocationEngine

    internal val markers: MutableList<LiteMarkerImpl> = mutableListOf()
    internal val polygons: MutableList<LitePolygonImpl> = mutableListOf()
@@ -504,7 +505,7 @@ class LiteGoogleMapImpl(context: Context, var options: GoogleMapOptions) : Abstr

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