1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
|
package org.terst.nav
import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.Bitmap
import android.graphics.Canvas
import android.media.MediaPlayer
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.FrameLayout
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.lifecycle.lifecycleScope
import com.google.android.material.bottomnavigation.BottomNavigationView
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.floatingactionbutton.FloatingActionButton
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.filterNotNull
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.maplibre.android.MapLibre
import org.maplibre.android.maps.MapView
import org.maplibre.android.maps.Style
import org.maplibre.android.style.layers.RasterLayer
import org.maplibre.android.style.sources.RasterSource
import org.maplibre.android.style.sources.TileSet
import org.terst.nav.ui.*
import org.terst.nav.ui.doc.DocFragment
import org.terst.nav.ui.safety.SafetyFragment
import org.terst.nav.ui.voicelog.VoiceLogFragment
import java.util.*
class MainActivity : AppCompatActivity(), SafetyFragment.SafetyListener {
private var mapView: MapView? = null
private var mobHandler: MobHandler? = null
private var instrumentHandler: InstrumentHandler? = null
private var mapHandler: MapHandler? = null
private var anchorWatchHandler: AnchorWatchHandler? = null
private val loadedStyleFlow = MutableStateFlow<Style?>(null)
private lateinit var bottomSheetBehavior: BottomSheetBehavior<View>
private lateinit var fragmentContainer: FrameLayout
private lateinit var fabRecordTrack: FloatingActionButton
private val safetyFragment = SafetyFragment().apply { setSafetyListener(this@MainActivity) }
private val viewModel: MainViewModel by viewModels()
private var pendingServiceStart = false
override fun onResume() {
super.onResume()
mapView?.onResume()
if (pendingServiceStart) {
pendingServiceStart = false
startServices()
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
MapLibre.getInstance(this)
setContentView(R.layout.activity_main)
checkForegroundPermissions()
initializeUI()
}
private fun initializeUI() {
fragmentContainer = findViewById(R.id.fragment_container)
setupMap()
setupBottomSheet()
setupBottomNavigation()
setupHandlers()
findViewById<FloatingActionButton>(R.id.fab_mob).setOnClickListener {
onActivateMob()
}
fabRecordTrack = findViewById(R.id.fab_record_track)
fabRecordTrack.setOnClickListener {
if (viewModel.isRecording.value) viewModel.stopTrack() else viewModel.startTrack()
}
// Observe immediately — pure UI state, not gated on GPS permission
lifecycleScope.launch {
viewModel.isRecording.collect { recording ->
val icon = if (recording) R.drawable.ic_close else R.drawable.ic_track_record
fabRecordTrack.setImageResource(icon)
fabRecordTrack.contentDescription = if (recording) "Stop Recording" else "Record Track"
}
}
}
private fun setupBottomSheet() {
val sheet = findViewById<View>(R.id.instrument_bottom_sheet)
bottomSheetBehavior = BottomSheetBehavior.from(sheet)
bottomSheetBehavior.state = BottomSheetBehavior.STATE_COLLAPSED
}
private fun setupBottomNavigation() {
val nav = findViewById<BottomNavigationView>(R.id.bottom_navigation)
nav.setOnItemSelectedListener { item ->
when (item.itemId) {
R.id.nav_map -> {
hideOverlays()
bottomSheetBehavior.isHideable = false
bottomSheetBehavior.peekHeight = 120.dpToPx()
bottomSheetBehavior.state = BottomSheetBehavior.STATE_COLLAPSED
true
}
R.id.nav_instruments -> {
hideOverlays()
bottomSheetBehavior.isHideable = false
bottomSheetBehavior.state = BottomSheetBehavior.STATE_EXPANDED
true
}
R.id.nav_log -> {
showOverlay(VoiceLogFragment())
bottomSheetBehavior.isHideable = true
bottomSheetBehavior.state = BottomSheetBehavior.STATE_HIDDEN
true
}
R.id.nav_safety -> {
showOverlay(safetyFragment)
bottomSheetBehavior.isHideable = true
bottomSheetBehavior.state = BottomSheetBehavior.STATE_HIDDEN
true
}
else -> false
}
}
}
private fun showOverlay(fragment: androidx.fragment.app.Fragment) {
fragmentContainer.visibility = View.VISIBLE
supportFragmentManager.beginTransaction()
.replace(R.id.fragment_container, fragment)
.commit()
}
private fun hideOverlays() {
fragmentContainer.visibility = View.GONE
}
override fun onActivateMob() {
lifecycleScope.launch {
LocationService.locationFlow.firstOrNull()?.let { gpsData ->
val mediaPlayer = MediaPlayer.create(this@MainActivity, R.raw.mob_alarm)
mobHandler?.activateMob(gpsData.latitude, gpsData.longitude, mediaPlayer)
}
}
}
override fun onConfigureAnchor() {
anchorWatchHandler?.toggleVisibility()
}
private fun setupHandlers() {
instrumentHandler = InstrumentHandler(
valueAws = findViewById(R.id.value_aws),
valueTws = findViewById(R.id.value_tws),
valueHdg = findViewById(R.id.value_hdg),
valueCog = findViewById(R.id.value_cog),
valueBsp = findViewById(R.id.value_bsp),
valueSog = findViewById(R.id.value_sog),
valueVmg = findViewById(R.id.value_vmg),
valueDepth = findViewById(R.id.value_depth),
valuePolarPct = findViewById(R.id.value_polar_pct),
valueBaro = findViewById(R.id.value_baro),
labelTrend = null, // simplified
barometerTrendView = null, // simplified
polarDiagramView = findViewById(R.id.polar_diagram_view)
)
// anchorWatchHandler is initialized when the anchor config UI is available
val mockPolarTable = createMockPolarTable()
findViewById<PolarDiagramView>(R.id.polar_diagram_view).setPolarTable(mockPolarTable)
startInstrumentSimulation(mockPolarTable)
}
// Helper to convert dp to px
private fun Int.dpToPx(): Int = (this * resources.displayMetrics.density).toInt()
// ... (Keep existing permission and service logic)
private fun checkForegroundPermissions() {
val fineLocationPermission = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
val coarseLocationPermission = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
if (fineLocationPermission == PackageManager.PERMISSION_GRANTED || coarseLocationPermission == PackageManager.PERMISSION_GRANTED) {
startServices()
} else {
requestPermissionLauncher.launch(arrayOf(
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION
))
}
}
private val requestPermissionLauncher =
registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { permissions ->
if (permissions[Manifest.permission.ACCESS_FINE_LOCATION] == true) {
pendingServiceStart = true
}
}
private fun startServices() {
val intent = Intent(this, LocationService::class.java).apply {
action = LocationService.ACTION_START_FOREGROUND_SERVICE
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(intent)
} else {
startService(intent)
}
observeDataSources()
}
private fun setupMap() {
mapView = findViewById(R.id.mapView)
mapView?.onCreate(null)
mapView?.getMapAsync { maplibreMap ->
mapHandler = MapHandler(maplibreMap)
val style = Style.Builder()
.fromUri("https://tiles.openfreemap.org/styles/liberty")
.withSource(RasterSource("openseamap-source",
TileSet("2.2.0", "https://tiles.openseamap.org/seamark/{z}/{x}/{y}.png").also {
it.setMaxZoom(18f)
}, 256))
.withLayer(RasterLayer("openseamap-layer", "openseamap-source"))
maplibreMap.setStyle(style) { style ->
loadedStyleFlow.value = style
val anchorBitmap = rasterizeDrawable(R.drawable.ic_anchor)
val arrowBitmap = rasterizeDrawable(R.drawable.ic_tidal_arrow)
mapHandler?.setupLayers(style, anchorBitmap, arrowBitmap)
}
}
}
private fun observeDataSources() {
lifecycleScope.launch {
LocationService.locationFlow.collect { gpsData ->
mapHandler?.centerOnLocation(gpsData.latitude, gpsData.longitude)
val sogKnots = gpsData.speedOverGround * 1.94384
viewModel.addGpsPoint(gpsData.latitude, gpsData.longitude, sogKnots, gpsData.courseOverGround.toDouble())
}
}
lifecycleScope.launch {
LocationService.anchorWatchState.collect { state ->
safetyFragment.updateAnchorStatus(if (state.isActive) "Active: ${state.watchCircleRadiusMeters}m" else "Inactive")
}
}
lifecycleScope.launch {
loadedStyleFlow.filterNotNull()
.combine(viewModel.trackPoints) { style, points -> style to points }
.collect { (style, points) -> mapHandler?.updateTrackLayer(style, points) }
}
}
private fun startInstrumentSimulation(polarTable: PolarTable) {
lifecycleScope.launch {
var simulatedTws = 8.0
var simulatedTwa = 40.0
while (true) {
val bsp = polarTable.interpolateBsp(simulatedTws, simulatedTwa)
instrumentHandler?.updateDisplay(
aws = "%.1f".format(Locale.getDefault(), simulatedTws * 1.1),
tws = "%.1f".format(Locale.getDefault(), simulatedTws),
bsp = "%.1f".format(Locale.getDefault(), bsp),
sog = "%.1f".format(Locale.getDefault(), bsp * 0.95),
vmg = "%.1f".format(Locale.getDefault(), polarTable.curves.firstOrNull { it.twS == simulatedTws }?.calculateVmg(simulatedTwa, bsp) ?: 0.0),
polarPct = "%.0f%%".format(Locale.getDefault(), polarTable.calculatePolarPercentage(simulatedTws, simulatedTwa, bsp)),
baro = "1013.2"
)
instrumentHandler?.updatePolarDiagram(simulatedTws, simulatedTwa, bsp)
simulatedTwa = (simulatedTwa + 0.5).let { if (it > 170) 40.0 else it }
delay(1000)
}
}
}
private fun rasterizeDrawable(drawableId: Int): Bitmap {
val drawable = ContextCompat.getDrawable(this, drawableId)!!
val bitmap = Bitmap.createBitmap(drawable.intrinsicWidth, drawable.intrinsicHeight, Bitmap.Config.ARGB_8888)
val canvas = Canvas(bitmap)
drawable.setBounds(0, 0, canvas.width, canvas.height)
drawable.draw(canvas)
return bitmap
}
private fun createMockPolarTable(): PolarTable {
val curves = listOf(6.0, 8.0, 10.0).map { tws ->
PolarCurve(tws, listOf(30.0, 45.0, 60.0, 90.0, 120.0, 150.0, 180.0).map { twa ->
PolarPoint(twa, tws * (0.4 + twa / 200.0))
})
}
return PolarTable(curves)
}
override fun onStart() { super.onStart(); mapView?.onStart() }
override fun onPause() { super.onPause(); mapView?.onPause() }
override fun onStop() { super.onStop(); mapView?.onStop() }
override fun onDestroy() { super.onDestroy(); mapView?.onDestroy() }
override fun onLowMemory() { super.onLowMemory(); mapView?.onLowMemory() }
}
|