|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: 2026 microG Project Team |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package org.microg.gms.maps.hms.utils |
| 7 | + |
| 8 | +import android.content.Context |
| 9 | +import android.util.Log |
| 10 | +import android.view.View |
| 11 | +import android.view.ViewGroup |
| 12 | +import android.widget.FrameLayout |
| 13 | + |
| 14 | +/** |
| 15 | + * Adapter that applies app-specific compatibility fixes for HMS Maps |
| 16 | + * based on the calling application's package name. |
| 17 | + */ |
| 18 | +class MapCompatAdapter(context: Context) { |
| 19 | + |
| 20 | + private val callerPackageName: String = context.applicationContext?.packageName ?: context.packageName |
| 21 | + |
| 22 | + /** |
| 23 | + * Wraps the mapView in a traversal-blocking container if needed. |
| 24 | + * Some apps traverse the view hierarchy and encounter HMS internal views, |
| 25 | + * causing crashes or unexpected behavior. |
| 26 | + * |
| 27 | + * @return the wrapper view to add to the root, or mapView itself if no wrapping is needed. |
| 28 | + */ |
| 29 | + fun wrapMapView(mapContext: Context, mapView: View): View { |
| 30 | + if (!needsViewTraversalBlock().also { Log.d(TAG, "$callerPackageName need wrapMapView ? $it") }) return mapView |
| 31 | + |
| 32 | + val blocker = object : FrameLayout(mapContext) { |
| 33 | + override fun getChildCount(): Int = 0 |
| 34 | + override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { |
| 35 | + val lp = mapView.layoutParams |
| 36 | + ?: LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT) |
| 37 | + val childW = getChildMeasureSpec(widthMeasureSpec, 0, lp.width) |
| 38 | + val childH = getChildMeasureSpec(heightMeasureSpec, 0, lp.height) |
| 39 | + mapView.measure(childW, childH) |
| 40 | + setMeasuredDimension( |
| 41 | + resolveSize(mapView.measuredWidth, widthMeasureSpec), |
| 42 | + resolveSize(mapView.measuredHeight, heightMeasureSpec) |
| 43 | + ) |
| 44 | + } |
| 45 | + |
| 46 | + override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) { |
| 47 | + mapView.layout(0, 0, right - left, bottom - top) |
| 48 | + } |
| 49 | + } |
| 50 | + blocker.addView( |
| 51 | + mapView, |
| 52 | + ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT) |
| 53 | + ) |
| 54 | + return blocker |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Intercepts map click events in liteMode for apps that need special handling. |
| 59 | + * @return true if the click was intercepted and handled, false to proceed normally. |
| 60 | + */ |
| 61 | + fun interceptLiteModeClick(rootView: View): Boolean { |
| 62 | + return findLiteModeParent(rootView)?.let { |
| 63 | + it.performClick() |
| 64 | + true |
| 65 | + } ?: false |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Intercepts map long-click events in liteMode for apps that need special handling. |
| 70 | + * @return true if the long-click was intercepted and handled, false to proceed normally. |
| 71 | + */ |
| 72 | + fun interceptLiteModeLongClick(rootView: View): Boolean { |
| 73 | + return findLiteModeParent(rootView)?.let { |
| 74 | + it.performLongClick() |
| 75 | + true |
| 76 | + } ?: false |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Determines if the mapView needs to be wrapped in a traversal-blocking container. |
| 81 | + */ |
| 82 | + private fun needsViewTraversalBlock(): Boolean { |
| 83 | + return callerPackageName in VIEW_TRAVERSAL_BLOCK_PACKAGES |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Finds the parent view that should receive redirected click events in liteMode. |
| 88 | + * Returns null if no redirection is needed for the current app. |
| 89 | + */ |
| 90 | + private fun findLiteModeParent(rootView: View): ViewGroup? { |
| 91 | + val targetClass = LITE_MODE_CLICK_REDIRECT[callerPackageName] ?: return null |
| 92 | + val parentView = rootView.parent?.parent ?: return null |
| 93 | + if (parentView::class.qualifiedName == targetClass) { |
| 94 | + return parentView as? ViewGroup |
| 95 | + } |
| 96 | + return null |
| 97 | + } |
| 98 | + |
| 99 | + companion object { |
| 100 | + private const val TAG = "MapCompatAdapter" |
| 101 | + private val VIEW_TRAVERSAL_BLOCK_PACKAGES = setOf( |
| 102 | + "com.studioeleven.windfinder", |
| 103 | + ) |
| 104 | + private val LITE_MODE_CLICK_REDIRECT = mapOf( |
| 105 | + "com.microsoft.teams" to "com.microsoft.teams.location.ui.map.MapViewLite", |
| 106 | + ) |
| 107 | + } |
| 108 | +} |
0 commit comments