Skip to content

Commit

Permalink
Location: Add support for getCurrentLocation()
Browse files Browse the repository at this point in the history
  • Loading branch information
mar-v-in committed Sep 20, 2023
1 parent 4a7bac3 commit d4118d7
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class DeviceOrientationManager(private val context: Context, private val lifecyc
override fun getLifecycle(): Lifecycle = lifecycle
private var lock = Mutex(false)
private var started: Boolean = false
private var sensors: Set<Sensor>? = null
private var handlerThread: HandlerThread? = null
private val requests = mutableMapOf<IBinder, DeviceOrientationRequestHolder>()

Expand Down Expand Up @@ -92,6 +93,7 @@ class DeviceOrientationManager(private val context: Context, private val lifecyc
for (sensor in sensors) {
sensorManager.registerListener(sensor, handler)
}
this.sensors = sensors
started = true
} catch (e: Exception) {
Log.w(TAG, e)
Expand Down Expand Up @@ -257,7 +259,7 @@ class DeviceOrientationManager(private val context: Context, private val lifecyc
}

fun dump(writer: PrintWriter) {
writer.println("Current device orientation request (started=$started)")
writer.println("Current device orientation request (started=$started, sensors=${sensors?.map { it.name }})")
for (request in requests.values.toList()) {
writer.println("- ${request.workSource} (pending: ${request.updatesPending.let { if (it == Int.MAX_VALUE) "\u221e" else "$it" }} ${request.timePendingMillis.formatDuration()})")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import android.content.pm.PackageManager.PERMISSION_GRANTED
import android.location.Location
import android.location.LocationManager.GPS_PROVIDER
import android.location.LocationManager.NETWORK_PROVIDER
import android.os.Binder
import android.os.IBinder
import android.os.Parcel
import android.os.SystemClock
Expand All @@ -28,6 +29,7 @@ import com.google.android.gms.location.*
import com.google.android.gms.location.internal.*
import com.google.android.gms.location.internal.DeviceOrientationRequestUpdateData.REMOVE_UPDATES
import com.google.android.gms.location.internal.DeviceOrientationRequestUpdateData.REQUEST_UPDATES
import kotlinx.coroutines.*
import org.microg.gms.common.NonCancelToken
import org.microg.gms.location.hasNetworkLocationServiceBuiltIn
import org.microg.gms.utils.warnOnTransactionIssues
Expand Down Expand Up @@ -131,8 +133,61 @@ class LocationManagerInstance(
override fun getCurrentLocationWithReceiver(request: CurrentLocationRequest, receiver: LocationReceiver): ICancelToken {
Log.d(TAG, "getCurrentLocationWithReceiver by ${getClientIdentity().packageName}")
checkHasAnyLocationPermission()
Log.d(TAG, "Not yet implemented: getCurrentLocationWithReceiver")
return NonCancelToken()
var returned = false
val callback = receiver.statusCallback
val clientIdentity = getClientIdentity()
val binderIdentity = Binder()
val job = lifecycleScope.launchWhenStarted {
try {
val scope = this
val callbackForRequest = object : ILocationCallback.Stub() {
override fun onLocationResult(result: LocationResult?) {
if (!returned) runCatching { callback.onLocationStatus(Status.SUCCESS, result?.lastLocation) }
returned = true
scope.cancel()
}

override fun onLocationAvailability(availability: LocationAvailability?) {
// Ignore
}

override fun cancel() {
if (!returned) runCatching { callback.onLocationStatus(Status.SUCCESS, null) }
returned = true
scope.cancel()
}
}
val currentLocationRequest = LocationRequest.Builder(request.priority, 1000)
.setGranularity(request.granularity)
.setMaxUpdateAgeMillis(request.maxUpdateAgeMillis)
.setDurationMillis(request.durationMillis)
.setPriority(request.priority)
.setWorkSource(request.workSource)
.setThrottleBehavior(request.throttleBehavior)
.build()
locationManager.addBinderRequest(clientIdentity, binderIdentity, callbackForRequest, currentLocationRequest)
awaitCancellation()
} catch (e: CancellationException) {
// Don't send result. Either this was cancelled from the CancelToken or because a location was retrieved.
// Both cases send the result themselves.
} catch (e: Exception) {
try {
if (!returned) callback.onLocationStatus(Status(CommonStatusCodes.ERROR, e.message), null)
returned = true
} catch (e2: Exception) {
Log.w(TAG, "Failed", e)
}
} finally {
runCatching { locationManager.removeBinderRequest(binderIdentity) }
}
}
return object : ICancelToken.Stub() {
override fun cancel() {
if (!returned) runCatching { callback.onLocationStatus(Status.CANCELED, null) }
returned = true
job.cancel()
}
}
}

override fun getLastLocationWithReceiver(request: LastLocationRequest, receiver: LocationReceiver) {
Expand Down

0 comments on commit d4118d7

Please sign in to comment.