networklocation: add offline GeocodeService + compile-only provider stubs
- Commit
58bda3481b0e82d5adea1db94009b23dd13f6ac0- Author
- Vayun Mathur <[email protected]>
- When
- 2026-08-09 21:15 UTC (3 months ago)
- Parents
7c65076
Diff
library/locationprovider/build.gradle.kts
diff --git a/library/locationprovider/build.gradle.kts b/library/locationprovider/build.gradle.ktsnew file mode 100644index 0000000000..f9ddad05cb--- /dev/null+++ b/library/locationprovider/build.gradle.kts@@ -0,0 +1,12 @@+plugins {+ id("common-conventions-library")+}++// Compile-only stubs for the framework's unbundled location/geocode provider API+// (`com.android.location.provider.*`). These classes exist at runtime on the device (provided+// via `<uses-library android:name="com.android.location.provider">`), so this module is depended+// on with `compileOnly` and its classes must NOT be packaged into any APK.+//+// Signatures are copied verbatim from the GrapheneOS/AOSP source in the platform tree+// (frameworks/base/location/lib/.../com/android/location/provider) so our provider subclasses+// override the real abstract methods exactly.
library/locationprovider/src/main/java/android/location/GeocoderParams.java
diff --git a/library/locationprovider/src/main/java/android/location/GeocoderParams.java b/library/locationprovider/src/main/java/android/location/GeocoderParams.javanew file mode 100644index 0000000000..10a43cf161--- /dev/null+++ b/library/locationprovider/src/main/java/android/location/GeocoderParams.java@@ -0,0 +1,21 @@+package android.location;++import java.util.Locale;++/**+ * Compile-only stub of the hidden @SystemApi android.location.GeocoderParams. Needed because it+ * appears in GeocodeProvider's method signatures but isn't in the public SDK. This module is+ * depended on with compileOnly, so this class is NOT packaged โ at runtime the real framework+ * GeocoderParams (on the boot classpath) is used.+ */+public class GeocoderParams {+ public Locale getLocale() {+ throw new RuntimeException("stub");+ }++ public String getClientPackage() {+ throw new RuntimeException("stub");+ }++ private GeocoderParams() {}+}
library/locationprovider/src/main/java/com/android/location/provider/GeocodeProvider.java
diff --git a/library/locationprovider/src/main/java/com/android/location/provider/GeocodeProvider.java b/library/locationprovider/src/main/java/com/android/location/provider/GeocodeProvider.javanew file mode 100644index 0000000000..82d1bf05fc--- /dev/null+++ b/library/locationprovider/src/main/java/com/android/location/provider/GeocodeProvider.java@@ -0,0 +1,28 @@+package com.android.location.provider;++import android.location.Address;+import android.location.GeocoderParams;+import android.os.IBinder;++import java.util.List;++/**+ * Compile-only stub of the platform's unbundled geocode provider base. Both onGet* methods are+ * abstract; return null on success (results go into {@code addrs}) or an error string on failure.+ * Do not package into an APK.+ */+public abstract class GeocodeProvider {+ public abstract String onGetFromLocation(+ double latitude, double longitude, int maxResults,+ GeocoderParams params, List<Address> addrs);++ public abstract String onGetFromLocationName(+ String locationName,+ double lowerLeftLatitude, double lowerLeftLongitude,+ double upperRightLatitude, double upperRightLongitude,+ int maxResults, GeocoderParams params, List<Address> addrs);++ public IBinder getBinder() {+ throw new RuntimeException("stub");+ }+}
library/locationprovider/src/main/java/com/android/location/provider/LocationProviderBase.java
diff --git a/library/locationprovider/src/main/java/com/android/location/provider/LocationProviderBase.java b/library/locationprovider/src/main/java/com/android/location/provider/LocationProviderBase.javanew file mode 100644index 0000000000..62987824ad--- /dev/null+++ b/library/locationprovider/src/main/java/com/android/location/provider/LocationProviderBase.java@@ -0,0 +1,33 @@+package com.android.location.provider;++import android.content.Context;+import android.location.Location;+import android.os.IBinder;+import android.os.WorkSource;++import java.util.List;++/**+ * Compile-only stub of the platform's unbundled network/fused location provider base. Only the+ * members our subclass uses/overrides are declared; the real class supplies the rest.+ * `onSetRequest` is the sole abstract method. Do not package into an APK.+ */+public abstract class LocationProviderBase {+ public LocationProviderBase(String tag, ProviderPropertiesUnbundled properties) {}++ public LocationProviderBase(Context context, String tag, ProviderPropertiesUnbundled properties) {}++ public IBinder getBinder() {+ throw new RuntimeException("stub");+ }++ public void setEnabled(boolean enabled) {}++ public void setProperties(ProviderPropertiesUnbundled properties) {}++ public void reportLocation(Location location) {}++ public void reportLocations(List<Location> locations) {}++ protected abstract void onSetRequest(ProviderRequestUnbundled request, WorkSource source);+}
library/locationprovider/src/main/java/com/android/location/provider/ProviderPropertiesUnbundled.java
diff --git a/library/locationprovider/src/main/java/com/android/location/provider/ProviderPropertiesUnbundled.java b/library/locationprovider/src/main/java/com/android/location/provider/ProviderPropertiesUnbundled.javanew file mode 100644index 0000000000..a746af0d89--- /dev/null+++ b/library/locationprovider/src/main/java/com/android/location/provider/ProviderPropertiesUnbundled.java@@ -0,0 +1,22 @@+package com.android.location.provider;++/**+ * Compile-only stub. Real implementation lives in the platform's com.android.location.provider+ * shared library. Do not package into an APK.+ */+public final class ProviderPropertiesUnbundled {+ public static ProviderPropertiesUnbundled create(+ boolean requiresNetwork,+ boolean requiresSatellite,+ boolean requiresCell,+ boolean hasMonetaryCost,+ boolean supportsAltitude,+ boolean supportsSpeed,+ boolean supportsBearing,+ int powerUsage,+ int accuracy) {+ throw new RuntimeException("stub");+ }++ private ProviderPropertiesUnbundled() {}+}
library/locationprovider/src/main/java/com/android/location/provider/ProviderRequestUnbundled.java
diff --git a/library/locationprovider/src/main/java/com/android/location/provider/ProviderRequestUnbundled.java b/library/locationprovider/src/main/java/com/android/location/provider/ProviderRequestUnbundled.javanew file mode 100644index 0000000000..2c32f5ee5c--- /dev/null+++ b/library/locationprovider/src/main/java/com/android/location/provider/ProviderRequestUnbundled.java@@ -0,0 +1,17 @@+package com.android.location.provider;++/**+ * Compile-only stub. Real implementation lives in the platform's com.android.location.provider+ * shared library. Do not package into an APK.+ */+public final class ProviderRequestUnbundled {+ public boolean getReportLocation() {+ throw new RuntimeException("stub");+ }++ public long getInterval() {+ throw new RuntimeException("stub");+ }++ private ProviderRequestUnbundled() {}+}
networklocation/build.gradle.kts
diff --git a/networklocation/build.gradle.kts b/networklocation/build.gradle.ktsindex ce0e13f46d..28c8716021 100644--- a/networklocation/build.gradle.kts+++ b/networklocation/build.gradle.kts@@ -25,6 +25,9 @@ tasks.withType<Test>().configureEach { } dependencies {+ // Compile-only stubs for the framework's unbundled provider API (com.android.location.provider).+ // Provided at runtime by <uses-library>; must NOT be packaged.+ compileOnly(project(":library:locationprovider")) // Geocoder DB is a self-contained mmap'd binary (see geocoder/). Block compression uses // java.util.zip (Deflate) โ no external dependency needed for the core. // kotlinx-serialization-json (from the app convention) is used only by the generator to
networklocation/src/main/AndroidManifest.xml
diff --git a/networklocation/src/main/AndroidManifest.xml b/networklocation/src/main/AndroidManifest.xmlindex 7b1bdd3177..093fede52e 100644--- a/networklocation/src/main/AndroidManifest.xml+++ b/networklocation/src/main/AndroidManifest.xml@@ -9,9 +9,21 @@ android:supportsRtl="true" android:theme="@style/Theme.General"> - <!-- Status/info screen. The geocoder + network-location provider services (which bind- to the framework via com.android.location.provider) are added in a follow-up once- the compile-only provider stubs + priv-app allowlist are in place. -->+ <!-- The unbundled provider base classes (com.android.location.provider.*) are supplied by+ the platform at runtime, not packaged in this APK. -->+ <uses-library android:name="com.android.location.provider" android:required="false" />++ <!-- Offline geocoder. Bound by the framework via the GeocodeProvider action; becomes THE+ system geocoder once config_geocodeProviderPackageName points here (MAOS overlay). -->+ <service+ android:name=".GeocodeService"+ android:exported="true">+ <intent-filter>+ <action android:name="com.android.location.service.GeocodeProvider" />+ </intent-filter>+ </service>++ <!-- Status/info screen. --> <activity android:name=".MainActivity" android:exported="true">
networklocation/src/main/java/com/vayunmathur/networklocation/GeocodeService.kt
diff --git a/networklocation/src/main/java/com/vayunmathur/networklocation/GeocodeService.kt b/networklocation/src/main/java/com/vayunmathur/networklocation/GeocodeService.ktnew file mode 100644index 0000000000..2ec7d96433--- /dev/null+++ b/networklocation/src/main/java/com/vayunmathur/networklocation/GeocodeService.kt@@ -0,0 +1,100 @@+package com.vayunmathur.networklocation++import android.app.Service+import android.content.Intent+import android.location.Address+import android.location.GeocoderParams+import android.os.IBinder+import com.android.location.provider.GeocodeProvider+import com.vayunmathur.networklocation.geocoder.GeoDbAssets+import com.vayunmathur.networklocation.geocoder.GeoDbReader+import com.vayunmathur.networklocation.geocoder.GeoResult+import java.util.Locale++/**+ * System geocoder backed entirely by the bundled offline planet DB โ no network. Bound by the+ * framework via the `com.android.location.service.GeocodeProvider` action; the app must be the+ * configured geocode provider (config_geocodeProviderPackageName) and hold INSTALL_LOCATION_PROVIDER.+ *+ * The provider contract: return null on success (results appended to `addrs`) or an error string.+ */+class GeocodeService : Service() {+ private var db: GeoDbReader? = null++ private val provider: GeocodeProvider by lazy {+ object : GeocodeProvider() {+ override fun onGetFromLocation(+ latitude: Double,+ longitude: Double,+ maxResults: Int,+ params: GeocoderParams,+ addrs: MutableList<Address>,+ ): String? {+ val reader = db ?: return "geocoder database unavailable"+ val r = reader.reverse(latitude, longitude) ?: return null+ addrs.add(r.toAddress(params.locale))+ return null+ }++ override fun onGetFromLocationName(+ locationName: String,+ lowerLeftLatitude: Double,+ lowerLeftLongitude: Double,+ upperRightLatitude: Double,+ upperRightLongitude: Double,+ maxResults: Int,+ params: GeocoderParams,+ addrs: MutableList<Address>,+ ): String? {+ val reader = db ?: return "geocoder database unavailable"+ for (r in forwardSearch(reader, locationName, maxResults)) {+ addrs.add(r.toAddress(params.locale))+ }+ return null+ }+ }+ }++ override fun onCreate() {+ super.onCreate()+ db = GeoDbAssets.open(this)+ }++ override fun onBind(intent: Intent?): IBinder? = provider.binder++ override fun onDestroy() {+ db?.close()+ db = null+ super.onDestroy()+ }++ /**+ * Best-effort freeform forward geocoding over structured data. The DB indexes by+ * country/state/city/street, so we map the trailing comma-separated components of the query+ * onto those fields. Full natural-language parsing is a known limitation.+ */+ private fun forwardSearch(db: GeoDbReader, query: String, maxResults: Int): List<GeoResult> {+ val parts = query.split(",").map { it.trim() }.filter { it.isNotEmpty() }+ if (parts.size < 4) return emptyList()+ val country = parts[parts.size - 1]+ val state = parts[parts.size - 2]+ val city = parts[parts.size - 3]+ val street = parts[parts.size - 4]+ return db.forward(country, state, city, street, limit = maxResults.coerceIn(1, 50))+ }++ private fun GeoResult.toAddress(locale: Locale): Address {+ val a = Address(locale)+ a.latitude = lat+ a.longitude = lon+ val line = listOf(house, street).filter { it.isNotEmpty() }.joinToString(" ")+ if (line.isNotEmpty()) a.setAddressLine(0, line)+ if (house.isNotEmpty()) a.featureName = house+ if (street.isNotEmpty()) a.thoroughfare = street+ if (city.isNotEmpty()) a.locality = city+ if (state.isNotEmpty()) a.adminArea = state+ if (postcode.isNotEmpty()) a.postalCode = postcode+ if (country.isNotEmpty()) a.countryCode = country+ return a+ }+}
settings.gradle.kts
diff --git a/settings.gradle.kts b/settings.gradle.ktsindex 2d816392cd..fdca86ec1f 100644--- a/settings.gradle.kts+++ b/settings.gradle.kts@@ -87,6 +87,7 @@ include(":speech") include(":vpn") include(":web") include(":appstore")+include(":library:locationprovider") include(":networklocation") include(":fooddelivery") include(":musicbrainz")