Skip to content

Commit f0339d6

Browse files
committed
OSM compass should re-draw when the screen rotates
1 parent ac07c5d commit f0339d6

File tree

1 file changed

+28
-5
lines changed

1 file changed

+28
-5
lines changed

project/app/src/main/java/org/owntracks/android/ui/map/osm/OSMMapFragment.kt

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package org.owntracks.android.ui.map.osm
22

3+
import android.content.Context
34
import android.graphics.Bitmap
45
import android.graphics.drawable.BitmapDrawable
56
import android.location.Location
67
import android.os.Bundle
78
import android.view.LayoutInflater
89
import android.view.MotionEvent.ACTION_BUTTON_RELEASE
10+
import android.view.Surface
911
import android.view.View
1012
import android.view.ViewGroup
1113
import androidx.core.content.res.ResourcesCompat
@@ -19,7 +21,6 @@ import org.osmdroid.events.MapListener
1921
import org.osmdroid.events.ScrollEvent
2022
import org.osmdroid.events.ZoomEvent
2123
import org.osmdroid.tileprovider.tilesource.TileSourceFactory
22-
import org.osmdroid.util.GeoPoint
2324
import org.osmdroid.views.CustomZoomButtonsController
2425
import org.osmdroid.views.MapView
2526
import org.osmdroid.views.overlay.CopyrightOverlay
@@ -143,14 +144,25 @@ class OSMMapFragment internal constructor(
143144
}
144145
})
145146

146-
class MapRotationOrientationProvider : IOrientationProvider {
147+
class MapRotationOrientationProvider(private val context: Context) : IOrientationProvider {
148+
val display = context.display
147149
private var myOrientationConsumer: IOrientationConsumer? = null
148150
private var lastOrientation = 0f
149151
fun updateOrientation(orientation: Float) {
150-
lastOrientation = orientation
151-
myOrientationConsumer?.onOrientationChanged(-orientation, this)
152+
val correctedOrientation = orientation
153+
lastOrientation = -(correctedOrientation + displayRotationToDegrees())
154+
myOrientationConsumer?.onOrientationChanged(lastOrientation, this)
152155
}
153156

157+
private fun displayRotationToDegrees(): Float =
158+
when (display?.rotation) {
159+
Surface.ROTATION_0 -> 0f
160+
Surface.ROTATION_90 -> 90f
161+
Surface.ROTATION_180 -> 180f
162+
Surface.ROTATION_270 -> 270f
163+
else -> 0f
164+
}
165+
154166
override fun startOrientationProvider(orientationConsumer: IOrientationConsumer?): Boolean {
155167
myOrientationConsumer = orientationConsumer
156168
return true
@@ -164,7 +176,7 @@ class OSMMapFragment internal constructor(
164176
override fun destroy() {}
165177
}
166178

167-
val orientationProvider = MapRotationOrientationProvider()
179+
val orientationProvider by lazy { MapRotationOrientationProvider(requireContext()) }
168180
val compassOrientationMapListener = object : MapListener {
169181
private fun updateOrientation() {
170182
mapView?.mapOrientation?.run {
@@ -326,6 +338,17 @@ class OSMMapFragment internal constructor(
326338
super.onDetach()
327339
}
328340

341+
/**
342+
* This gets fired on rotate. We need to trigger an onScroll event to reset the orientation
343+
* provider and re-draw the compass
344+
*
345+
* @param newConfig
346+
*/
347+
override fun onConfigurationChanged(newConfig: android.content.res.Configuration) {
348+
super.onConfigurationChanged(newConfig)
349+
compassOrientationMapListener.onScroll(null)
350+
}
351+
329352
override fun drawRegions(regions: Set<WaypointModel>) {
330353
if (preferences.showRegionsOnMap) {
331354
mapView?.run {

0 commit comments

Comments
 (0)