Skip to content

Commit

Permalink
touch gesture fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
johan12345 committed Jun 30, 2024
1 parent b0e84c5 commit 2455c64
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions app/src/main/java/net/vonforst/evmap/auto/MapSurfaceCallback.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package net.vonforst.evmap.auto
import android.animation.ValueAnimator
import android.app.Presentation
import android.content.Context
import android.graphics.Point
import android.graphics.Rect
import android.hardware.display.DisplayManager
import android.hardware.display.VirtualDisplay
import android.os.Build
import android.os.SystemClock
import android.util.Log
import android.view.MotionEvent
import androidx.car.app.CarContext
import androidx.car.app.SurfaceCallback
Expand Down Expand Up @@ -38,6 +38,10 @@ class MapSurfaceCallback(val ctx: CarContext, val lifecycleScope: LifecycleCorou
SurfaceCallback, OnMapReadyCallback {
private val VIRTUAL_DISPLAY_NAME = "evmap_map"
private val VELOCITY_THRESHOLD_IGNORE_FLING = 1000
private val STATUSBAR_OFFSET_SYSTEMS = listOf(
"VolvoCars/ihu_emulator_volvo_car/ihu_emulator:11",
"Google/sdk_gcar_x86_64/generic_64bitonly_x86_64:11"
)

private lateinit var virtualDisplay: VirtualDisplay
lateinit var presentation: Presentation
Expand Down Expand Up @@ -90,11 +94,13 @@ class MapSurfaceCallback(val ctx: CarContext, val lifecycleScope: LifecycleCorou
}

override fun onVisibleAreaChanged(visibleArea: Rect) {
Log.d("MapSurfaceCallback", "visible area: $visibleArea")
this.visibleArea = visibleArea
updateVisibleArea()
}

override fun onStableAreaChanged(stableArea: Rect) {
Log.d("MapSurfaceCallback", "stable area: $stableArea")
}

override fun onSurfaceDestroyed(surfaceContainer: SurfaceContainer) {
Expand Down Expand Up @@ -157,9 +163,12 @@ class MapSurfaceCallback(val ctx: CarContext, val lifecycleScope: LifecycleCorou
val map = map ?: return
if (scaleFactor == 2f) return

val focus = Point(focusX.roundToInt(), focusY.roundToInt())
// TODO: using focal point does not work correctly (at least not with mapbox)
val offsetX = (focusX - mapView.width / 2) * (1f - scaleFactor)
val offsetY = (offsetY(focusY) - mapView.height / 2) * (1f - scaleFactor)

Log.i("MapSurfaceCallback", "focus: $focusX, $focusY, scaleFactor: $scaleFactor")
map.moveCamera(map.cameraUpdateFactory.zoomBy(scaleFactor - 1))
map.moveCamera(map.cameraUpdateFactory.scrollBy(offsetX, offsetY))
dispatchCameraMoveStarted()
}

Expand Down Expand Up @@ -197,13 +206,14 @@ class MapSurfaceCallback(val ctx: CarContext, val lifecycleScope: LifecycleCorou
flingAnimator?.cancel()
val downTime: Long = SystemClock.uptimeMillis()
val eventTime: Long = downTime + 100
val yOffset = offsetY(y)

val downEvent = MotionEvent.obtain(
downTime,
downTime,
MotionEvent.ACTION_DOWN,
x,
y,
yOffset,
0
)
mapView.dispatchTouchEvent(downEvent)
Expand All @@ -213,13 +223,23 @@ class MapSurfaceCallback(val ctx: CarContext, val lifecycleScope: LifecycleCorou
eventTime,
MotionEvent.ACTION_UP,
x,
y,
yOffset,
0
)
mapView.dispatchTouchEvent(upEvent)
upEvent.recycle()
}

private fun offsetY(y: Float): Float {
if (!STATUSBAR_OFFSET_SYSTEMS.any { Build.FINGERPRINT.startsWith(it) }) return y

// In some emulators, touch locations are offset by the status bar height
// related: https://issuetracker.google.com/issues/256905247
val resId = ctx.resources.getIdentifier("status_bar_height", "dimen", "android")
val offset = resId.takeIf { it > 0 }?.let { ctx.resources.getDimensionPixelSize(it) } ?: 0
return y + offset
}

private fun createMap(ctx: Context): MapContainerView {
MapsConfiguration.getInstance().initialize(ctx)
return MapView(ctx)
Expand Down

0 comments on commit 2455c64

Please sign in to comment.