Skip to content

Commit

Permalink
OSM compass should re-draw when the screen rotates
Browse files Browse the repository at this point in the history
  • Loading branch information
growse committed Aug 22, 2022
1 parent ac07c5d commit f0339d6
Showing 1 changed file with 28 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package org.owntracks.android.ui.map.osm

import android.content.Context
import android.graphics.Bitmap
import android.graphics.drawable.BitmapDrawable
import android.location.Location
import android.os.Bundle
import android.view.LayoutInflater
import android.view.MotionEvent.ACTION_BUTTON_RELEASE
import android.view.Surface
import android.view.View
import android.view.ViewGroup
import androidx.core.content.res.ResourcesCompat
Expand All @@ -19,7 +21,6 @@ import org.osmdroid.events.MapListener
import org.osmdroid.events.ScrollEvent
import org.osmdroid.events.ZoomEvent
import org.osmdroid.tileprovider.tilesource.TileSourceFactory
import org.osmdroid.util.GeoPoint
import org.osmdroid.views.CustomZoomButtonsController
import org.osmdroid.views.MapView
import org.osmdroid.views.overlay.CopyrightOverlay
Expand Down Expand Up @@ -143,14 +144,25 @@ class OSMMapFragment internal constructor(
}
})

class MapRotationOrientationProvider : IOrientationProvider {
class MapRotationOrientationProvider(private val context: Context) : IOrientationProvider {
val display = context.display
private var myOrientationConsumer: IOrientationConsumer? = null
private var lastOrientation = 0f
fun updateOrientation(orientation: Float) {
lastOrientation = orientation
myOrientationConsumer?.onOrientationChanged(-orientation, this)
val correctedOrientation = orientation
lastOrientation = -(correctedOrientation + displayRotationToDegrees())
myOrientationConsumer?.onOrientationChanged(lastOrientation, this)
}

private fun displayRotationToDegrees(): Float =
when (display?.rotation) {
Surface.ROTATION_0 -> 0f
Surface.ROTATION_90 -> 90f
Surface.ROTATION_180 -> 180f
Surface.ROTATION_270 -> 270f
else -> 0f
}

override fun startOrientationProvider(orientationConsumer: IOrientationConsumer?): Boolean {
myOrientationConsumer = orientationConsumer
return true
Expand All @@ -164,7 +176,7 @@ class OSMMapFragment internal constructor(
override fun destroy() {}
}

val orientationProvider = MapRotationOrientationProvider()
val orientationProvider by lazy { MapRotationOrientationProvider(requireContext()) }
val compassOrientationMapListener = object : MapListener {
private fun updateOrientation() {
mapView?.mapOrientation?.run {
Expand Down Expand Up @@ -326,6 +338,17 @@ class OSMMapFragment internal constructor(
super.onDetach()
}

/**
* This gets fired on rotate. We need to trigger an onScroll event to reset the orientation
* provider and re-draw the compass
*
* @param newConfig
*/
override fun onConfigurationChanged(newConfig: android.content.res.Configuration) {
super.onConfigurationChanged(newConfig)
compassOrientationMapListener.onScroll(null)
}

override fun drawRegions(regions: Set<WaypointModel>) {
if (preferences.showRegionsOnMap) {
mapView?.run {
Expand Down

0 comments on commit f0339d6

Please sign in to comment.