-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Location: Provide "fused" system location provider
- Loading branch information
Showing
6 changed files
with
238 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
...provider/src/main/kotlin/org/microg/gms/location/provider/FusedLocationProviderService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2024 microG Project Team | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.microg.gms.location.provider | ||
|
||
import android.app.PendingIntent | ||
import android.content.Intent | ||
import android.location.Criteria | ||
import android.location.Location | ||
import android.os.Build.VERSION.SDK_INT | ||
import android.util.Log | ||
import androidx.core.location.LocationRequestCompat | ||
import com.android.location.provider.ProviderPropertiesUnbundled | ||
import com.android.location.provider.ProviderRequestUnbundled | ||
import com.google.android.gms.location.LocationRequest | ||
import com.google.android.gms.location.LocationResult | ||
import com.google.android.gms.location.LocationServices | ||
import com.google.android.gms.location.Priority | ||
import kotlin.math.max | ||
|
||
class FusedLocationProviderService : IntentLocationProviderService() { | ||
override fun extractLocation(intent: Intent): Location? = LocationResult.extractResult(intent)?.lastLocation | ||
|
||
override fun requestIntentUpdated(currentRequest: ProviderRequestUnbundled?, pendingIntent: PendingIntent?) { | ||
val intervalMillis = if (currentRequest?.reportLocation == true) { | ||
max(currentRequest.interval, minIntervalMillis) | ||
} else { | ||
Long.MAX_VALUE | ||
} | ||
val request = LocationRequest.Builder(intervalMillis) | ||
if (SDK_INT >= 31 && currentRequest != null) { | ||
request.setPriority(when(currentRequest.quality) { | ||
LocationRequestCompat.QUALITY_LOW_POWER -> Priority.PRIORITY_LOW_POWER | ||
LocationRequestCompat.QUALITY_HIGH_ACCURACY -> Priority.PRIORITY_HIGH_ACCURACY | ||
else -> Priority.PRIORITY_BALANCED_POWER_ACCURACY | ||
}) | ||
request.setMaxUpdateDelayMillis(currentRequest.maxUpdateDelayMillis) | ||
} | ||
try { | ||
LocationServices.getFusedLocationProviderClient(this).requestLocationUpdates(request.build(), pendingIntent) | ||
} catch (e: SecurityException) { | ||
Log.d(TAG, "Failed requesting location updated", e) | ||
} | ||
} | ||
|
||
override fun stopIntentUpdated(pendingIntent: PendingIntent?) { | ||
LocationServices.getFusedLocationProviderClient(this).removeLocationUpdates(pendingIntent) | ||
} | ||
|
||
override val minIntervalMillis: Long | ||
get() = MIN_INTERVAL_MILLIS | ||
override val minReportMillis: Long | ||
get() = MIN_REPORT_MILLIS | ||
override val properties: ProviderPropertiesUnbundled | ||
get() = PROPERTIES | ||
override val providerName: String | ||
get() = "fused" | ||
|
||
companion object { | ||
private const val MIN_INTERVAL_MILLIS = 20000L | ||
private const val MIN_REPORT_MILLIS = 1000L | ||
private val PROPERTIES = ProviderPropertiesUnbundled.create(false, false, false, false, true, true, true, Criteria.POWER_LOW, Criteria.ACCURACY_COARSE) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
...rovider/src/main/kotlin/org/microg/gms/location/provider/IntentLocationProviderService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2024 microG Project Team | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.microg.gms.location.provider | ||
|
||
import android.app.PendingIntent | ||
import android.app.Service | ||
import android.content.Intent | ||
import android.location.Location | ||
import android.os.Binder | ||
import android.os.Build.VERSION.SDK_INT | ||
import android.os.Handler | ||
import android.os.HandlerThread | ||
import android.os.IBinder | ||
import android.os.Process | ||
import android.util.Log | ||
import com.android.location.provider.ProviderPropertiesUnbundled | ||
import com.android.location.provider.ProviderRequestUnbundled | ||
import java.io.FileDescriptor | ||
import java.io.PrintWriter | ||
|
||
abstract class IntentLocationProviderService : Service() { | ||
private lateinit var handlerThread: HandlerThread | ||
private lateinit var handler: Handler | ||
private var bound: Boolean = false | ||
private var provider: GenericLocationProvider? = null | ||
|
||
override fun onCreate() { | ||
super.onCreate() | ||
handlerThread = HandlerThread(this.javaClass.simpleName) | ||
handlerThread.start() | ||
handler = Handler(handlerThread.looper) | ||
} | ||
|
||
abstract fun requestIntentUpdated(currentRequest: ProviderRequestUnbundled?, pendingIntent: PendingIntent?) | ||
|
||
abstract fun stopIntentUpdated(pendingIntent: PendingIntent?) | ||
|
||
abstract fun extractLocation(intent: Intent): Location? | ||
|
||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { | ||
if (Binder.getCallingUid() == Process.myUid() && intent?.action == ACTION_REPORT_LOCATION) { | ||
handler.post { | ||
val location = extractLocation(intent) | ||
if (location != null) { | ||
provider?.reportLocationToSystem(location) | ||
} | ||
} | ||
} | ||
return START_NOT_STICKY | ||
} | ||
|
||
override fun onBind(intent: Intent?): IBinder? { | ||
bound = true | ||
if (provider == null) { | ||
provider = when { | ||
// TODO: Migrate to Tiramisu provider. Not yet required thanks to backwards compat | ||
// SDK_INT >= 33 -> | ||
SDK_INT >= 31 -> | ||
IntentLocationProviderPreTiramisu(this, properties) | ||
|
||
else -> | ||
@Suppress("DEPRECATION") | ||
(IntentLocationProviderPreTiramisu(this, properties, Unit)) | ||
} | ||
provider?.enable() | ||
} | ||
return provider?.getBinder() | ||
} | ||
|
||
override fun dump(fd: FileDescriptor, writer: PrintWriter, args: Array<out String>) { | ||
writer.println("Bound: $bound") | ||
provider?.dump(writer) | ||
} | ||
|
||
override fun onDestroy() { | ||
if (SDK_INT >= 18) handlerThread.looper.quitSafely() | ||
else handlerThread.looper.quit() | ||
provider?.disable() | ||
provider = null | ||
bound = false | ||
super.onDestroy() | ||
} | ||
|
||
abstract val minIntervalMillis: Long | ||
abstract val minReportMillis: Long | ||
abstract val properties: ProviderPropertiesUnbundled | ||
abstract val providerName: String | ||
} |
Oops, something went wrong.