Skip to content

Commit 9240ba7

Browse files
committed
Fix app settings and prepare map rotation
1 parent 728188c commit 9240ba7

File tree

10 files changed

+75
-33
lines changed

10 files changed

+75
-33
lines changed

app/src/main/java/org/nitri/opentopo/CacheSettingsFragment.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import androidx.appcompat.app.AlertDialog
1414
import androidx.appcompat.widget.SwitchCompat
1515
import androidx.fragment.app.DialogFragment
1616
import androidx.localbroadcastmanager.content.LocalBroadcastManager
17+
import androidx.preference.PreferenceManager
1718
import org.osmdroid.config.Configuration
1819
import java.io.File
1920

@@ -28,8 +29,7 @@ class CacheSettingsFragment : DialogFragment() {
2829
val inflater = requireActivity().layoutInflater
2930
@SuppressLint("InflateParams") val view =
3031
inflater.inflate(R.layout.fragment_cache_settings, null)
31-
val prefs =
32-
requireActivity().getSharedPreferences(MapFragment.MAP_PREFS, Context.MODE_PRIVATE)
32+
val prefs = PreferenceManager.getDefaultSharedPreferences(requireActivity().applicationContext)
3333
val tvExternalStorageRoot = view.findViewById<TextView>(R.id.tvExternalStorageRoot)
3434
val swExternalStorage = view.findViewById<SwitchCompat>(R.id.swExternalStorage)
3535
etTileCache = view.findViewById(R.id.etTileCache)

app/src/main/java/org/nitri/opentopo/MapFragment.kt

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import android.view.View
2626
import android.view.ViewGroup
2727
import android.view.ViewTreeObserver.OnGlobalLayoutListener
2828
import android.view.Window
29+
import android.view.WindowManager
2930
import android.widget.PopupMenu
3031
import android.widget.TextView
3132
import android.widget.Toast
@@ -36,6 +37,7 @@ import androidx.fragment.app.Fragment
3637
import androidx.fragment.app.FragmentManager
3738
import androidx.fragment.app.viewModels
3839
import androidx.lifecycle.ViewModelProvider
40+
import androidx.preference.PreferenceManager
3941
import io.ticofab.androidgpxparser.parser.domain.Gpx
4042
import org.nitri.opentopo.SettingsActivity.Companion.PREF_FULLSCREEN
4143
import org.nitri.opentopo.SettingsActivity.Companion.PREF_KEEP_SCREEN_ON
@@ -47,6 +49,7 @@ import org.nitri.opentopo.overlay.ClickableCompassOverlay
4749
import org.nitri.opentopo.overlay.GestureOverlay
4850
import org.nitri.opentopo.overlay.GestureOverlay.GestureCallback
4951
import org.nitri.opentopo.overlay.OverlayHelper
52+
import org.nitri.opentopo.util.MapOrientation
5053
import org.nitri.opentopo.util.Util
5154
import org.osmdroid.config.Configuration
5255
import org.osmdroid.events.DelayedMapListener
@@ -69,7 +72,8 @@ import org.osmdroid.views.overlay.mylocation.MyLocationNewOverlay
6972
import java.io.File
7073

7174
class MapFragment : Fragment(), LocationListener, PopupMenu.OnMenuItemClickListener,
72-
GestureCallback {
75+
GestureCallback, ClickableCompassOverlay.OnCompassClickListener {
76+
private var mapRotation: Boolean = false
7377
private lateinit var mMapView: MapView
7478
private var mLocationOverlay: MyLocationNewOverlay? = null
7579
private var mCompassOverlay: CompassOverlay? = null
@@ -109,7 +113,13 @@ class MapFragment : Fragment(), LocationListener, PopupMenu.OnMenuItemClickListe
109113
}
110114

111115
fun setKeepScreenOn(value: Boolean) {
116+
Log.d(TAG, "keepScreenOn: $value")
112117
mMapView.keepScreenOn = value
118+
if (value) {
119+
requireActivity().window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
120+
} else {
121+
requireActivity().window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
122+
}
113123
}
114124

115125
private var mFollow = false
@@ -132,7 +142,7 @@ class MapFragment : Fragment(), LocationListener, PopupMenu.OnMenuItemClickListe
132142
super.onCreate(savedInstanceState)
133143
setHasOptionsMenu(true)
134144
val context = requireActivity().applicationContext
135-
mPrefs = requireActivity().getSharedPreferences(MAP_PREFS, Context.MODE_PRIVATE)
145+
mPrefs = PreferenceManager.getDefaultSharedPreferences(context)
136146
val configuration = Configuration.getInstance()
137147
configuration.userAgentValue = BuildConfig.APPLICATION_ID
138148
val basePath = Util.getOsmdroidBasePath(context, mPrefs.getBoolean(CacheSettingsFragment.PREF_EXTERNAL_STORAGE, false))
@@ -206,7 +216,11 @@ class MapFragment : Fragment(), LocationListener, PopupMenu.OnMenuItemClickListe
206216
}
207217
}))
208218

209-
mCompassOverlay = ClickableCompassOverlay(
219+
// TODO: mCompassOverlay = ClickableCompassOverlay(
220+
// activity, InternalCompassOrientationProvider(activity),
221+
// mMapView, this
222+
// )
223+
mCompassOverlay = CompassOverlay(
210224
activity, InternalCompassOrientationProvider(activity),
211225
mMapView
212226
)
@@ -810,6 +824,9 @@ class MapFragment : Fragment(), LocationListener, PopupMenu.OnMenuItemClickListe
810824
String.format("Location: %f, %f", location.latitude, location.longitude)
811825
)
812826
mLocationViewModel?.currentLocation?.value = location
827+
if (mapRotation) {
828+
MapOrientation.setTargetMapOrientation(mMapView, location.bearing)
829+
}
813830
}
814831

815832
@Deprecated("Deprecated in Java")
@@ -856,6 +873,16 @@ class MapFragment : Fragment(), LocationListener, PopupMenu.OnMenuItemClickListe
856873
}
857874
}
858875

876+
override fun onCompassClicked() {
877+
mapRotation = !mapRotation
878+
if (mapRotation) {
879+
Toast.makeText(requireContext(), R.string.rotation_on, Toast.LENGTH_SHORT).show()
880+
} else {
881+
MapOrientation.setTargetMapOrientation(mMapView, 0f)
882+
Toast.makeText(requireContext(), R.string.rotation_off, Toast.LENGTH_SHORT).show()
883+
}
884+
}
885+
859886
interface OnFragmentInteractionListener {
860887
/**
861888
* Start GPX file selection flow
@@ -935,7 +962,6 @@ class MapFragment : Fragment(), LocationListener, PopupMenu.OnMenuItemClickListe
935962
private const val STATE_LATITUDE = "latitude"
936963
private const val STATE_LONGITUDE = "longitude"
937964
private const val STATE_ZOOM = "zoom"
938-
const val MAP_PREFS = "map_prefs"
939965
private const val PREF_BASE_MAP = "base_map"
940966
private const val PREF_OVERLAY = "overlay"
941967
private const val PREF_LATITUDE = "latitude"
@@ -958,4 +984,5 @@ class MapFragment : Fragment(), LocationListener, PopupMenu.OnMenuItemClickListe
958984
return mapFragment
959985
}
960986
}
987+
961988
}

app/src/main/java/org/nitri/opentopo/overlay/ClickableCompassOverlay.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,22 @@ import org.osmdroid.views.overlay.compass.InternalCompassOrientationProvider
1010
class ClickableCompassOverlay(
1111
context: Context,
1212
orientationProvider: InternalCompassOrientationProvider,
13-
mapView: MapView
13+
mapView: MapView,
14+
private val onCompassClickListener: OnCompassClickListener
1415
) : CompassOverlay(context, orientationProvider, mapView) {
1516

1617

1718
override fun onSingleTapConfirmed(e: MotionEvent, mapView: MapView): Boolean {
1819
val reuse = Point()
1920
mapView.projection.rotateAndScalePoint(e.x.toInt(), e.y.toInt(), reuse)
2021
if (reuse.x < mCompassFrameCenterX * 2 && reuse.y < mCompassFrameCenterY * 2) {
21-
mapView.controller?.animateTo(null, null, 300, 0f)
22+
onCompassClickListener.onCompassClicked()
2223
return true
2324
}
24-
2525
return super.onSingleTapConfirmed(e, mapView)
2626
}
2727

28+
interface OnCompassClickListener {
29+
fun onCompassClicked()
30+
}
2831
}
Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package org.nitri.opentopo.util
22

3-
import android.util.Log
43
import android.view.animation.AccelerateDecelerateInterpolator
54
import android.view.animation.Interpolator
65
import kotlinx.coroutines.CoroutineScope
@@ -13,33 +12,41 @@ import kotlin.math.abs
1312
import kotlin.math.roundToInt
1413

1514

16-
class MapOrientation {
15+
object MapOrientation {
16+
17+
private val TAG = MapOrientation::class.java.simpleName
18+
private const val ORIENTATION_ANIMATION_STEP_SIZE: Float = 0.1f // degrees
19+
private const val ORIENTATION_ANIMATION_DELTA_TIME: Int = 3 // ms
20+
private const val ORIENTATION_EPSILON: Int = 10 // noise threshold value
1721

1822
private lateinit var animationJob: Job
19-
private var mMapOrientation = 0f
20-
private var mTargetMapOrientation = 0f
21-
private var mPreviousMapOrientation = 0f
23+
private var mapOrientation = 0f
24+
private var targetMapOrientation = 0f
25+
private var previousMapOrientation = 0f
2226

2327
private val orientationAnimationInterpolator: Interpolator = AccelerateDecelerateInterpolator()
2428

2529
private var mMapOrientationAnimationRunning = false
2630

31+
val currentMapOrientation: Float
32+
get() = mapOrientation
33+
2734
/**
2835
* Target orientation based on heading. Start orientation animation on change above noise threshold.
2936
*
3037
* @param degrees 0 - 360 degrees
3138
*/
3239
fun setTargetMapOrientation(mapView: MapView, degrees: Float) {
33-
mTargetMapOrientation = degrees
40+
targetMapOrientation = degrees
3441

35-
val roundedTargetMapOrientation = mTargetMapOrientation.roundToInt()
36-
val roundedPreviousMapOrientation = mPreviousMapOrientation.roundToInt()
42+
val roundedTargetMapOrientation = targetMapOrientation.roundToInt()
43+
val roundedPreviousMapOrientation = previousMapOrientation.roundToInt()
3744

38-
if (abs(mTargetMapOrientation - mPreviousMapOrientation) > ORIENTATION_EPSILON
45+
if (abs(targetMapOrientation - previousMapOrientation) > ORIENTATION_EPSILON
3946
&& roundedTargetMapOrientation != roundedPreviousMapOrientation
4047
) {
4148
if (!mMapOrientationAnimationRunning) {
42-
animateToMapOrientation(mapView, mMapOrientation, 360 - mTargetMapOrientation)
49+
animateToMapOrientation(mapView, mapOrientation, 360 - targetMapOrientation)
4350
}
4451
}
4552
}
@@ -50,7 +57,11 @@ class MapOrientation {
5057
* @param originalOrientation deg
5158
* @param targetMapOrientation deg
5259
*/
53-
private fun animateToMapOrientation(mapView: MapView, originalOrientation: Float, targetMapOrientation: Float) {
60+
private fun animateToMapOrientation(
61+
mapView: MapView,
62+
originalOrientation: Float,
63+
targetMapOrientation: Float
64+
) {
5465

5566
animationJob.cancel()
5667

@@ -63,12 +74,12 @@ class MapOrientation {
6374
for (step in 1..numberOfSteps) {
6475
val timeIndex = step.toFloat() / numberOfSteps
6576
val angularProgress = orientationAnimationInterpolator.getInterpolation(timeIndex)
66-
mMapOrientation = originalOrientation + angularDistance * angularProgress
77+
mapOrientation = originalOrientation + angularDistance * angularProgress
6778

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

70-
mPreviousMapOrientation = mMapOrientation
71-
mapView.mapOrientation = mMapOrientation
81+
previousMapOrientation = mapOrientation
82+
mapView.mapOrientation = mapOrientation
7283

7384
delay(ORIENTATION_ANIMATION_DELTA_TIME.toLong())
7485
}
@@ -82,11 +93,4 @@ class MapOrientation {
8293
val distance = if (phi > 180) 360 - phi else phi
8394
return distance * if ((alpha - beta + 360) % 360 > 180) -1 else 1
8495
}
85-
86-
companion object {
87-
private val TAG = MapOrientation::class.java.simpleName
88-
const val ORIENTATION_ANIMATION_STEP_SIZE: Float = 0.1f // degrees
89-
const val ORIENTATION_ANIMATION_DELTA_TIME: Int = 3 // ms
90-
const val ORIENTATION_EPSILON: Int = 10 // noise threshold value
91-
}
9296
}

app/src/main/java/org/nitri/opentopo/util/Util.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,6 @@ object Util {
155155
* @param source
156156
* @return
157157
*/
158-
@JvmStatic
159158
@Suppress("deprecation")
160159
fun fromHtml(source: String?): Spanned {
161160
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
@@ -171,7 +170,6 @@ object Util {
171170
* @param nmea NMEA
172171
* @return antenna altitude
173172
*/
174-
@JvmStatic
175173
fun elevationFromNmea(nmea: String): Double {
176174
if (!TextUtils.isEmpty(nmea) && nmea.startsWith("\$GPGGA")) {
177175
val tokens = nmea.split(",".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()

app/src/main/res/values-de/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
<string name="follow">Folgen</string>
55
<string name="follow_enabled">Folgen aktiviert</string>
66
<string name="follow_disabled">Folgen deaktiviert</string>
7+
<string name="rotation_on">Rotation ein</string>
8+
<string name="rotation_off">Rotation aus</string>
79
<string name="gpx">GPX</string>
810
<string name="discard_current_gpx">Die aktuelle GPX wird verworfen.</string>
911
<string name="gpx_details">GPX Details</string>

app/src/main/res/values-es/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
<string name="follow">Seguir</string>
55
<string name="follow_enabled">Seguimiento activado</string>
66
<string name="follow_disabled">Seguimiento desactivado</string>
7+
<string name="rotation_on">Rotación activada</string>
8+
<string name="rotation_off">Rotación desactivada</string>
79
<string name="gpx">GPX</string>
810
<string name="discard_current_gpx">El GPX actual será descartado.</string>
911
<string name="gpx_details">Detalles del GPX</string>

app/src/main/res/values-fr/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
<string name="follow">Suivre</string>
55
<string name="follow_enabled">Suivi activé</string>
66
<string name="follow_disabled">Suivi désactivé</string>
7+
<string name="rotation_on">Rotation activée</string>
8+
<string name="rotation_off">Rotation désactivée</string>
79
<string name="gpx">GPX</string>
810
<string name="discard_current_gpx">Le GPX actuel sera supprimé.</string>
911
<string name="gpx_details">Détails du GPX</string>

app/src/main/res/values-nl/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
<string name="follow">Volgen</string>
55
<string name="follow_enabled">Volgen ingeschakeld</string>
66
<string name="follow_disabled">Volgen uitgeschakeld</string>
7+
<string name="rotation_on">Rotatie aan</string>
8+
<string name="rotation_off">Rotatie uit</string>
79
<string name="gpx">GPX</string>
810
<string name="discard_current_gpx">De huidige GPX wordt verwijderd.</string>
911
<string name="gpx_details">GPX details</string>

app/src/main/res/values/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
<string name="follow">Follow</string>
55
<string name="follow_enabled">Follow enabled</string>
66
<string name="follow_disabled">Follow disabled</string>
7+
<string name="rotation_on">Rotation on</string>
8+
<string name="rotation_off">Rotation off</string>
79
<string name="gpx">GPX</string>
810
<string name="discard_current_gpx">The current GPX will be discarded.</string>
911
<string name="gpx_details">GPX details</string>

0 commit comments

Comments
 (0)