Skip to content

Commit

Permalink
Location: Add support for sending initial location where maxUpdateAge…
Browse files Browse the repository at this point in the history
…Millis allows it
  • Loading branch information
mar-v-in committed May 29, 2023
1 parent 8d8f009 commit 611155b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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
Expand Down

0 comments on commit 611155b

Please sign in to comment.