Skip to content

Commit

Permalink
Fix app settings and prepare map rotation
Browse files Browse the repository at this point in the history
  • Loading branch information
Pygmalion69 committed Dec 14, 2024
1 parent 728188c commit 9240ba7
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 33 deletions.
4 changes: 2 additions & 2 deletions app/src/main/java/org/nitri/opentopo/CacheSettingsFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import androidx.appcompat.app.AlertDialog
import androidx.appcompat.widget.SwitchCompat
import androidx.fragment.app.DialogFragment
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import androidx.preference.PreferenceManager
import org.osmdroid.config.Configuration
import java.io.File

Expand All @@ -28,8 +29,7 @@ class CacheSettingsFragment : DialogFragment() {
val inflater = requireActivity().layoutInflater
@SuppressLint("InflateParams") val view =
inflater.inflate(R.layout.fragment_cache_settings, null)
val prefs =
requireActivity().getSharedPreferences(MapFragment.MAP_PREFS, Context.MODE_PRIVATE)
val prefs = PreferenceManager.getDefaultSharedPreferences(requireActivity().applicationContext)
val tvExternalStorageRoot = view.findViewById<TextView>(R.id.tvExternalStorageRoot)
val swExternalStorage = view.findViewById<SwitchCompat>(R.id.swExternalStorage)
etTileCache = view.findViewById(R.id.etTileCache)
Expand Down
35 changes: 31 additions & 4 deletions app/src/main/java/org/nitri/opentopo/MapFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import android.view.View
import android.view.ViewGroup
import android.view.ViewTreeObserver.OnGlobalLayoutListener
import android.view.Window
import android.view.WindowManager
import android.widget.PopupMenu
import android.widget.TextView
import android.widget.Toast
Expand All @@ -36,6 +37,7 @@ import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.viewModels
import androidx.lifecycle.ViewModelProvider
import androidx.preference.PreferenceManager
import io.ticofab.androidgpxparser.parser.domain.Gpx
import org.nitri.opentopo.SettingsActivity.Companion.PREF_FULLSCREEN
import org.nitri.opentopo.SettingsActivity.Companion.PREF_KEEP_SCREEN_ON
Expand All @@ -47,6 +49,7 @@ import org.nitri.opentopo.overlay.ClickableCompassOverlay
import org.nitri.opentopo.overlay.GestureOverlay
import org.nitri.opentopo.overlay.GestureOverlay.GestureCallback
import org.nitri.opentopo.overlay.OverlayHelper
import org.nitri.opentopo.util.MapOrientation
import org.nitri.opentopo.util.Util
import org.osmdroid.config.Configuration
import org.osmdroid.events.DelayedMapListener
Expand All @@ -69,7 +72,8 @@ import org.osmdroid.views.overlay.mylocation.MyLocationNewOverlay
import java.io.File

class MapFragment : Fragment(), LocationListener, PopupMenu.OnMenuItemClickListener,
GestureCallback {
GestureCallback, ClickableCompassOverlay.OnCompassClickListener {
private var mapRotation: Boolean = false
private lateinit var mMapView: MapView
private var mLocationOverlay: MyLocationNewOverlay? = null
private var mCompassOverlay: CompassOverlay? = null
Expand Down Expand Up @@ -109,7 +113,13 @@ class MapFragment : Fragment(), LocationListener, PopupMenu.OnMenuItemClickListe
}

fun setKeepScreenOn(value: Boolean) {
Log.d(TAG, "keepScreenOn: $value")
mMapView.keepScreenOn = value
if (value) {
requireActivity().window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
} else {
requireActivity().window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
}
}

private var mFollow = false
Expand All @@ -132,7 +142,7 @@ class MapFragment : Fragment(), LocationListener, PopupMenu.OnMenuItemClickListe
super.onCreate(savedInstanceState)
setHasOptionsMenu(true)
val context = requireActivity().applicationContext
mPrefs = requireActivity().getSharedPreferences(MAP_PREFS, Context.MODE_PRIVATE)
mPrefs = PreferenceManager.getDefaultSharedPreferences(context)
val configuration = Configuration.getInstance()
configuration.userAgentValue = BuildConfig.APPLICATION_ID
val basePath = Util.getOsmdroidBasePath(context, mPrefs.getBoolean(CacheSettingsFragment.PREF_EXTERNAL_STORAGE, false))
Expand Down Expand Up @@ -206,7 +216,11 @@ class MapFragment : Fragment(), LocationListener, PopupMenu.OnMenuItemClickListe
}
}))

mCompassOverlay = ClickableCompassOverlay(
// TODO: mCompassOverlay = ClickableCompassOverlay(
// activity, InternalCompassOrientationProvider(activity),
// mMapView, this
// )
mCompassOverlay = CompassOverlay(
activity, InternalCompassOrientationProvider(activity),
mMapView
)
Expand Down Expand Up @@ -810,6 +824,9 @@ class MapFragment : Fragment(), LocationListener, PopupMenu.OnMenuItemClickListe
String.format("Location: %f, %f", location.latitude, location.longitude)
)
mLocationViewModel?.currentLocation?.value = location
if (mapRotation) {
MapOrientation.setTargetMapOrientation(mMapView, location.bearing)
}
}

@Deprecated("Deprecated in Java")
Expand Down Expand Up @@ -856,6 +873,16 @@ class MapFragment : Fragment(), LocationListener, PopupMenu.OnMenuItemClickListe
}
}

override fun onCompassClicked() {
mapRotation = !mapRotation
if (mapRotation) {
Toast.makeText(requireContext(), R.string.rotation_on, Toast.LENGTH_SHORT).show()
} else {
MapOrientation.setTargetMapOrientation(mMapView, 0f)
Toast.makeText(requireContext(), R.string.rotation_off, Toast.LENGTH_SHORT).show()
}
}

interface OnFragmentInteractionListener {
/**
* Start GPX file selection flow
Expand Down Expand Up @@ -935,7 +962,6 @@ class MapFragment : Fragment(), LocationListener, PopupMenu.OnMenuItemClickListe
private const val STATE_LATITUDE = "latitude"
private const val STATE_LONGITUDE = "longitude"
private const val STATE_ZOOM = "zoom"
const val MAP_PREFS = "map_prefs"
private const val PREF_BASE_MAP = "base_map"
private const val PREF_OVERLAY = "overlay"
private const val PREF_LATITUDE = "latitude"
Expand All @@ -958,4 +984,5 @@ class MapFragment : Fragment(), LocationListener, PopupMenu.OnMenuItemClickListe
return mapFragment
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,22 @@ import org.osmdroid.views.overlay.compass.InternalCompassOrientationProvider
class ClickableCompassOverlay(
context: Context,
orientationProvider: InternalCompassOrientationProvider,
mapView: MapView
mapView: MapView,
private val onCompassClickListener: OnCompassClickListener
) : CompassOverlay(context, orientationProvider, mapView) {


override fun onSingleTapConfirmed(e: MotionEvent, mapView: MapView): Boolean {
val reuse = Point()
mapView.projection.rotateAndScalePoint(e.x.toInt(), e.y.toInt(), reuse)
if (reuse.x < mCompassFrameCenterX * 2 && reuse.y < mCompassFrameCenterY * 2) {
mapView.controller?.animateTo(null, null, 300, 0f)
onCompassClickListener.onCompassClicked()
return true
}

return super.onSingleTapConfirmed(e, mapView)
}

interface OnCompassClickListener {
fun onCompassClicked()
}
}
48 changes: 26 additions & 22 deletions app/src/main/java/org/nitri/opentopo/util/MapOrientation.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.nitri.opentopo.util

import android.util.Log
import android.view.animation.AccelerateDecelerateInterpolator
import android.view.animation.Interpolator
import kotlinx.coroutines.CoroutineScope
Expand All @@ -13,33 +12,41 @@ import kotlin.math.abs
import kotlin.math.roundToInt


class MapOrientation {
object MapOrientation {

private val TAG = MapOrientation::class.java.simpleName
private const val ORIENTATION_ANIMATION_STEP_SIZE: Float = 0.1f // degrees
private const val ORIENTATION_ANIMATION_DELTA_TIME: Int = 3 // ms
private const val ORIENTATION_EPSILON: Int = 10 // noise threshold value

private lateinit var animationJob: Job
private var mMapOrientation = 0f
private var mTargetMapOrientation = 0f
private var mPreviousMapOrientation = 0f
private var mapOrientation = 0f
private var targetMapOrientation = 0f
private var previousMapOrientation = 0f

private val orientationAnimationInterpolator: Interpolator = AccelerateDecelerateInterpolator()

private var mMapOrientationAnimationRunning = false

val currentMapOrientation: Float
get() = mapOrientation

/**
* Target orientation based on heading. Start orientation animation on change above noise threshold.
*
* @param degrees 0 - 360 degrees
*/
fun setTargetMapOrientation(mapView: MapView, degrees: Float) {
mTargetMapOrientation = degrees
targetMapOrientation = degrees

val roundedTargetMapOrientation = mTargetMapOrientation.roundToInt()
val roundedPreviousMapOrientation = mPreviousMapOrientation.roundToInt()
val roundedTargetMapOrientation = targetMapOrientation.roundToInt()
val roundedPreviousMapOrientation = previousMapOrientation.roundToInt()

if (abs(mTargetMapOrientation - mPreviousMapOrientation) > ORIENTATION_EPSILON
if (abs(targetMapOrientation - previousMapOrientation) > ORIENTATION_EPSILON
&& roundedTargetMapOrientation != roundedPreviousMapOrientation
) {
if (!mMapOrientationAnimationRunning) {
animateToMapOrientation(mapView, mMapOrientation, 360 - mTargetMapOrientation)
animateToMapOrientation(mapView, mapOrientation, 360 - targetMapOrientation)
}
}
}
Expand All @@ -50,7 +57,11 @@ class MapOrientation {
* @param originalOrientation deg
* @param targetMapOrientation deg
*/
private fun animateToMapOrientation(mapView: MapView, originalOrientation: Float, targetMapOrientation: Float) {
private fun animateToMapOrientation(
mapView: MapView,
originalOrientation: Float,
targetMapOrientation: Float
) {

animationJob.cancel()

Expand All @@ -63,12 +74,12 @@ class MapOrientation {
for (step in 1..numberOfSteps) {
val timeIndex = step.toFloat() / numberOfSteps
val angularProgress = orientationAnimationInterpolator.getInterpolation(timeIndex)
mMapOrientation = originalOrientation + angularDistance * angularProgress
mapOrientation = originalOrientation + angularDistance * angularProgress

mMapOrientation = (mMapOrientation + 360) % 360 // Keep within 0-360 range
mapOrientation = (mapOrientation + 360) % 360 // Keep within 0-360 range

mPreviousMapOrientation = mMapOrientation
mapView.mapOrientation = mMapOrientation
previousMapOrientation = mapOrientation
mapView.mapOrientation = mapOrientation

delay(ORIENTATION_ANIMATION_DELTA_TIME.toLong())
}
Expand All @@ -82,11 +93,4 @@ class MapOrientation {
val distance = if (phi > 180) 360 - phi else phi
return distance * if ((alpha - beta + 360) % 360 > 180) -1 else 1
}

companion object {
private val TAG = MapOrientation::class.java.simpleName
const val ORIENTATION_ANIMATION_STEP_SIZE: Float = 0.1f // degrees
const val ORIENTATION_ANIMATION_DELTA_TIME: Int = 3 // ms
const val ORIENTATION_EPSILON: Int = 10 // noise threshold value
}
}
2 changes: 0 additions & 2 deletions app/src/main/java/org/nitri/opentopo/util/Util.kt
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ object Util {
* @param source
* @return
*/
@JvmStatic
@Suppress("deprecation")
fun fromHtml(source: String?): Spanned {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Expand All @@ -171,7 +170,6 @@ object Util {
* @param nmea NMEA
* @return antenna altitude
*/
@JvmStatic
fun elevationFromNmea(nmea: String): Double {
if (!TextUtils.isEmpty(nmea) && nmea.startsWith("\$GPGGA")) {
val tokens = nmea.split(",".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
<string name="follow">Folgen</string>
<string name="follow_enabled">Folgen aktiviert</string>
<string name="follow_disabled">Folgen deaktiviert</string>
<string name="rotation_on">Rotation ein</string>
<string name="rotation_off">Rotation aus</string>
<string name="gpx">GPX</string>
<string name="discard_current_gpx">Die aktuelle GPX wird verworfen.</string>
<string name="gpx_details">GPX Details</string>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-es/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
<string name="follow">Seguir</string>
<string name="follow_enabled">Seguimiento activado</string>
<string name="follow_disabled">Seguimiento desactivado</string>
<string name="rotation_on">Rotación activada</string>
<string name="rotation_off">Rotación desactivada</string>
<string name="gpx">GPX</string>
<string name="discard_current_gpx">El GPX actual será descartado.</string>
<string name="gpx_details">Detalles del GPX</string>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-fr/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
<string name="follow">Suivre</string>
<string name="follow_enabled">Suivi activé</string>
<string name="follow_disabled">Suivi désactivé</string>
<string name="rotation_on">Rotation activée</string>
<string name="rotation_off">Rotation désactivée</string>
<string name="gpx">GPX</string>
<string name="discard_current_gpx">Le GPX actuel sera supprimé.</string>
<string name="gpx_details">Détails du GPX</string>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-nl/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
<string name="follow">Volgen</string>
<string name="follow_enabled">Volgen ingeschakeld</string>
<string name="follow_disabled">Volgen uitgeschakeld</string>
<string name="rotation_on">Rotatie aan</string>
<string name="rotation_off">Rotatie uit</string>
<string name="gpx">GPX</string>
<string name="discard_current_gpx">De huidige GPX wordt verwijderd.</string>
<string name="gpx_details">GPX details</string>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
<string name="follow">Follow</string>
<string name="follow_enabled">Follow enabled</string>
<string name="follow_disabled">Follow disabled</string>
<string name="rotation_on">Rotation on</string>
<string name="rotation_off">Rotation off</string>
<string name="gpx">GPX</string>
<string name="discard_current_gpx">The current GPX will be discarded.</string>
<string name="gpx_details">GPX details</string>
Expand Down

0 comments on commit 9240ba7

Please sign in to comment.