Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class MapFragmentImpl(private val activity: Activity) : IMapFragmentDelegate.Stu
if (options == null) {
options = GoogleMapOptions()
}
if (options?.liteMode == true) {
if (options?.liteMode == true && !useKeylessRasterTiles()) {
map = LiteGoogleMapImpl(activity, options ?: GoogleMapOptions())
} else {
map = GoogleMapImpl(activity, options ?: GoogleMapOptions())
Expand All @@ -66,7 +66,7 @@ class MapFragmentImpl(private val activity: Activity) : IMapFragmentDelegate.Stu
}
Log.d(TAG, "onCreateView: ${options?.camera?.target}")
if (map == null) {
map = if (options?.liteMode == true) {
map = if (options?.liteMode == true && !useKeylessRasterTiles()) {
LiteGoogleMapImpl(activity, options ?: GoogleMapOptions())
} else {
GoogleMapImpl(activity, options ?: GoogleMapOptions())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ class MapViewImpl(private val context: Context, options: GoogleMapOptions?) : IM

override fun onCreate(savedInstanceState: Bundle?) {
Log.d(TAG, "onCreate: ${options?.camera?.target}")
map = if (options.liteMode) {
// The lite-mode snapshotter draws a logo overlay that MapLibre cannot build for the
// key-less raster fallback (crashes in MapSnapshotter). Use the full renderer in that case.
map = if (options.liteMode && !useKeylessRasterTiles()) {
LiteGoogleMapImpl(context, options)
} else {
GoogleMapImpl(context, options)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,46 @@ const val KEY_SOURCE_URL = "url"
const val KEY_SOURCE_TILES = "tiles"


fun getStyle(
context: MapContext, mapType: Int, styleOptions: MapStyleOptions?, styleFromFileWorkaround: Boolean = false
): Style.Builder {
/**
* When no Mapbox or Stadia key is configured at build time, the vector styles cannot fetch any tiles
* and the map renders blank. In that case fall back to key-less OpenStreetMap raster tiles, which
* MapLibre renders without any access token, so the map remains usable out of the box.
*/
internal fun useKeylessRasterTiles(): Boolean =
BuildConfig.MAPBOX_KEY.isEmpty() && BuildConfig.STADIA_KEY.isEmpty()

private fun buildKeylessRasterStyle(mapType: Int): JSONObject {
val tilesUrl = when (mapType) {
GoogleMap.MAP_TYPE_SATELLITE, GoogleMap.MAP_TYPE_HYBRID ->
"https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}"
else -> "https://tile.openstreetmap.org/{z}/{x}/{y}.png"
}
return JSONObject().apply {
put("version", 8)
put("sources", JSONObject().apply {
put("osm", JSONObject().apply {
put("type", "raster")
put("tiles", JSONArray().put(tilesUrl))
put("tileSize", 256)
put("maxzoom", 19)
})
})
put("layers", JSONArray().apply {
put(JSONObject().apply {
put("id", "background")
put("type", "background")
put("paint", JSONObject().put("background-color", "#e6e4e0"))
})
put(JSONObject().apply {
put("id", "osm")
put("type", "raster")
put("source", "osm")
})
})
}
}

private fun loadVectorStyle(context: MapContext, mapType: Int): JSONObject {
val styleJson = JSONObject(
context.assets.open(
if (BuildConfig.STADIA_KEY.isNotEmpty()) when (mapType) {
Expand Down Expand Up @@ -75,6 +111,18 @@ fun getStyle(
}
}
}
return styleJson
}

fun getStyle(
context: MapContext, mapType: Int, styleOptions: MapStyleOptions?, styleFromFileWorkaround: Boolean = false
): Style.Builder {

val styleJson = if (useKeylessRasterTiles()) {
buildKeylessRasterStyle(mapType)
} else {
loadVectorStyle(context, mapType)
}

styleOptions?.apply(styleJson)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import android.view.LayoutInflater
import org.microg.gms.common.Constants
import java.io.File

class MapContext(private val context: Context) : ContextWrapper(context.createPackageContext(Constants.GMS_PACKAGE_NAME, Context.CONTEXT_INCLUDE_CODE or Context.CONTEXT_IGNORE_SECURITY)) {
class MapContext(private val context: Context) : ContextWrapper(context.createPackageContext(resolveSelfPackage(context), Context.CONTEXT_INCLUDE_CODE or Context.CONTEXT_IGNORE_SECURITY)) {
private var layoutInflater: LayoutInflater? = null
private val appContext: Context
get() = context.applicationContext ?: context
Expand Down Expand Up @@ -77,5 +77,21 @@ class MapContext(private val context: Context) : ContextWrapper(context.createPa

companion object {
val TAG = "GmsMapContext"

// ReVanced fix: GmsCore may be installed under app.revanced.android.gms instead of
// com.google.android.gms. Resolve to whichever GmsCore package is actually installed so
// we load the correct APK (the one bundling libmapbox-gl.so). Prefer the ReVanced package
// to avoid accidentally binding to a (possibly disabled) real Google GMS install.
fun resolveSelfPackage(context: Context): String {
for (pkg in listOf(Constants.USER_MICROG_PACKAGE_NAME, Constants.GMS_PACKAGE_NAME)) {
try {
context.packageManager.getApplicationInfo(pkg, 0)
return pkg
} catch (e: Exception) {
// not installed, try next candidate
}
}
return Constants.GMS_PACKAGE_NAME
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static void setDelegate(@NonNull IBitmapDescriptorFactoryDelegate delegat
BitmapDescriptorFactory.delegate = delegate;
}
private static IBitmapDescriptorFactoryDelegate getDelegate() {
if (delegate == null) throw new IllegalStateException("CameraUpdateFactory is not initialized");
if (delegate == null) throw new IllegalStateException("BitmapDescriptorFactory is not initialized");
return delegate;
}
}