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

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

Location: Add support for sending initial location where maxUpdateAgeMillis allows it

parent 8d8f0099
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -33,7 +33,7 @@ internal val FEATURES = arrayOf(
    Feature("use_safe_parcelable_in_intents", 1)
)

internal fun Long.formatRealtime(): CharSequence = DateUtils.getRelativeTimeSpanString((SystemClock.elapsedRealtime() - this) + System.currentTimeMillis())
internal fun Long.formatRealtime(): CharSequence = DateUtils.getRelativeTimeSpanString((this - SystemClock.elapsedRealtime()) + System.currentTimeMillis(), System.currentTimeMillis(), 0)
internal fun Long.formatDuration(): CharSequence {
    if (this == 0L) return "0ms"
    if (this > 315360000000L /* ten years */) return "\u221e"
+3 −3
Original line number Diff line number Diff line
@@ -102,7 +102,7 @@ class LocationManager(private val context: Context, private val lifecycle: Lifec

    suspend fun addBinderRequest(clientIdentity: ClientIdentity, binder: IBinder, callback: ILocationCallback, request: LocationRequest) {
        request.verify(context, clientIdentity)
        requestManager.add(binder, clientIdentity, callback, request)
        requestManager.add(binder, clientIdentity, callback, request, lastLocationCapsule)
    }

    suspend fun updateBinderRequest(
@@ -113,7 +113,7 @@ class LocationManager(private val context: Context, private val lifecycle: Lifec
        request: LocationRequest
    ) {
        request.verify(context, clientIdentity)
        requestManager.update(oldBinder, binder, clientIdentity, callback, request)
        requestManager.update(oldBinder, binder, clientIdentity, callback, request, lastLocationCapsule)
    }

    suspend fun removeBinderRequest(binder: IBinder) {
@@ -122,7 +122,7 @@ class LocationManager(private val context: Context, private val lifecycle: Lifec

    suspend fun addIntentRequest(clientIdentity: ClientIdentity, pendingIntent: PendingIntent, request: LocationRequest) {
        request.verify(context, clientIdentity)
        requestManager.add(pendingIntent, clientIdentity, request)
        requestManager.add(pendingIntent, clientIdentity, request, lastLocationCapsule)
    }

    suspend fun removeIntentRequest(pendingIntent: PendingIntent) {
+19 −5
Original line number Diff line number Diff line
@@ -61,11 +61,16 @@ class LocationRequestManager(private val context: Context, private val lifecycle
        }
    }

    suspend fun add(binder: IBinder, clientIdentity: ClientIdentity, callback: ILocationCallback, request: LocationRequest) {
    suspend fun add(binder: IBinder, clientIdentity: ClientIdentity, callback: ILocationCallback, request: LocationRequest, lastLocationCapsule: LastLocationCapsule) {
        lock.withLock {
            val holder = LocationRequestHolder(context, clientIdentity, request, callback, null)
            try {
                holder.start()
                holder.start().also {
                    var effectiveGranularity = it.effectiveGranularity
                    if (effectiveGranularity == GRANULARITY_FINE && database.getForceCoarse(it.clientIdentity.packageName)) effectiveGranularity = GRANULARITY_COARSE
                    val lastLocation = lastLocationCapsule.getLocation(effectiveGranularity, request.maxUpdateAgeMillis)
                    if (lastLocation != null) it.processNewLocation(lastLocation)
                }
                binderRequests[binder] = holder
                binder.linkToDeath(this, 0)
            } catch (e: Exception) {
@@ -76,12 +81,17 @@ class LocationRequestManager(private val context: Context, private val lifecycle
        notifyRequestDetailsUpdated()
    }

    suspend fun update(oldBinder: IBinder, binder: IBinder, clientIdentity: ClientIdentity, callback: ILocationCallback, request: LocationRequest) {
    suspend fun update(oldBinder: IBinder, binder: IBinder, clientIdentity: ClientIdentity, callback: ILocationCallback, request: LocationRequest, lastLocationCapsule: LastLocationCapsule) {
        lock.withLock {
            oldBinder.unlinkToDeath(this, 0)
            val holder = binderRequests.remove(oldBinder)
            try {
                val startedHolder = holder?.update(callback, request) ?: LocationRequestHolder(context, clientIdentity, request, callback, null).start()
                val startedHolder = holder?.update(callback, request) ?: LocationRequestHolder(context, clientIdentity, request, callback, null).start().also {
                    var effectiveGranularity = it.effectiveGranularity
                    if (effectiveGranularity == GRANULARITY_FINE && database.getForceCoarse(it.clientIdentity.packageName)) effectiveGranularity = GRANULARITY_COARSE
                    val lastLocation = lastLocationCapsule.getLocation(effectiveGranularity, request.maxUpdateAgeMillis)
                    if (lastLocation != null) it.processNewLocation(lastLocation)
                }
                binderRequests[binder] = startedHolder
                binder.linkToDeath(this, 0)
            } catch (e: Exception) {
@@ -100,11 +110,15 @@ class LocationRequestManager(private val context: Context, private val lifecycle
        notifyRequestDetailsUpdated()
    }

    suspend fun add(pendingIntent: PendingIntent, clientIdentity: ClientIdentity, request: LocationRequest) {
    suspend fun add(pendingIntent: PendingIntent, clientIdentity: ClientIdentity, request: LocationRequest, lastLocationCapsule: LastLocationCapsule) {
        lock.withLock {
            try {
                pendingIntentRequests[pendingIntent] = LocationRequestHolder(context, clientIdentity, request, null, pendingIntent).start().also {
                    cacheManager.add(it.asParcelable()) { it.pendingIntent == pendingIntent }
                    var effectiveGranularity = it.effectiveGranularity
                    if (effectiveGranularity == GRANULARITY_FINE && database.getForceCoarse(it.clientIdentity.packageName)) effectiveGranularity = GRANULARITY_COARSE
                    val lastLocation = lastLocationCapsule.getLocation(effectiveGranularity, request.maxUpdateAgeMillis)
                    if (lastLocation != null) it.processNewLocation(lastLocation)
                }
            } catch (e: Exception) {
                // Ignore