Page MenuHomeGitPull.it

No OneTemporary

Authored By
Unknown
Size
161 KB
Referenced Files
None
Subscribers
None
diff --git a/app/src/main/java/it/reyboz/bustorino/backend/VehicleUtils.kt b/app/src/main/java/it/reyboz/bustorino/backend/VehicleUtils.kt
index e87bd82..4c95abb 100644
--- a/app/src/main/java/it/reyboz/bustorino/backend/VehicleUtils.kt
+++ b/app/src/main/java/it/reyboz/bustorino/backend/VehicleUtils.kt
@@ -1,92 +1,92 @@
package it.reyboz.bustorino.backend
import it.reyboz.bustorino.backend.VehicleUtils.VehicleType
import java.util.Locale.getDefault
data class VehicleClassInfo(
val vehClass: Int,
val name: String,
val kindString: String,
val type: VehicleType,
val matricolaStart: Int,
val matricolaEnd: Int,
) {
constructor(vehicleClass: Int, name: String, type: String, matricolaStart: Int, matricolaEnd: Int) : this(
vehicleClass, name, type,VehicleType.fromString(type), matricolaStart, matricolaEnd
)
}
object VehicleUtils {
private val items = listOf(
VehicleClassInfo(2800, "Arancio", "Tram", 2801, 2857),
VehicleClassInfo(2800, "Arancio", "Tram", 2858, 2902),
VehicleClassInfo(5000, "5000", "Tram", 5000, 5053),
VehicleClassInfo(6000, "6000", "Tram", 6000, 6005),
VehicleClassInfo(6000, "6000", "Tram", 6006, 6054),
VehicleClassInfo(8000, "Hitachi", "Tram", 8000, 8099),
VehicleClassInfo(800, "Citelis 18m", "Bus 18m", 790, 797),
VehicleClassInfo(800, "Citelis 18m", "Bus 18m", 800, 869),
VehicleClassInfo(800, "Citelis 18m", "Bus 18m", 870, 874),
VehicleClassInfo(800, "Citelis 18m", "Bus 18m", 1310, 1313),
VehicleClassInfo(1350, "Conecto 18m", "Bus 18m", 1350, 1396),
VehicleClassInfo(9300, "UrbanWay 18m", "Bus 18m", 9300, 9318),
VehicleClassInfo(9300, "UrbanWay 18m", "Bus 18m", 9320, 9356),
VehicleClassInfo(30, "BYD K9", "E-Bus", 30, 49),
VehicleClassInfo(50, "BYD K7", "E-Bus", 50, 57),
VehicleClassInfo(60, "MiniBusE", "E-Bus", 60, 81),
VehicleClassInfo(110, "Neocity", "Bus", 110, 115),
VehicleClassInfo(2300, "Cityclass", "Bus", 2300, 2349),
VehicleClassInfo(3400, "Conecto", "Bus", 2400, 2447),
VehicleClassInfo(2300, "Cityclass", "Bus", 2700, 2787),
VehicleClassInfo(3000, "Citelis", "Bus", 3000, 3099),
VehicleClassInfo(3000, "Citelis", "Bus", 3300, 3380),
VehicleClassInfo(3400, "Conecto", "Bus", 3400, 3440),
VehicleClassInfo(9000, "BYD K9", "E-Bus", 9000, 9059),
VehicleClassInfo(9000, "BYD K9", "E-Bus", 9060, 9119),
VehicleClassInfo(9000, "BYD K9", "E-Bus", 9120, 9121),
VehicleClassInfo(9200, "Citymood", "Bus", 9200, 9251),
VehicleClassInfo(9200, "Citymood", "Bus", 9252, 9261),
- VehicleClassInfo(9400, "E-Way", "E-Bus", 9400, 9599),
- VehicleClassInfo(9600, "E-Way 18m", "E-Bus", 9600, 9699)
+ VehicleClassInfo(9400, "Iveco E-Way", "E-Bus", 9400, 9599),
+ VehicleClassInfo(9600, "Iveco E-Way 18m", "E-Bus", 9600, 9699)
)
fun getTypeForLabel(label: String): VehicleClassInfo? {
try {
val matricola = Integer.parseInt(label)
for (el in items) {
if(matricola >= el.matricolaStart && matricola<= el.matricolaEnd) {
return el
}
}
return null
} catch (e: Exception) {
return null
}
}
enum class VehicleType {
BUS, TRAM, ELECTRIC_BUS;
fun getName(): String {
return when (this) {
BUS -> "Bus"
TRAM -> "Tram"
ELECTRIC_BUS -> "E-Bus"
}
}
companion object {
@JvmStatic
fun fromString(string: String): VehicleType {
return when (string.lowercase(getDefault())) {
"bus" -> BUS
"bus 18m" -> BUS
"tram" -> TRAM
"e-bus" -> ELECTRIC_BUS
"e-bus 18m" -> ELECTRIC_BUS
else -> throw IllegalArgumentException("Unknown vehicle type: $string")
}
}
}
}
}
\ No newline at end of file
diff --git a/app/src/main/java/it/reyboz/bustorino/data/MatoTripsDownloadWorker.kt b/app/src/main/java/it/reyboz/bustorino/data/MatoTripsDownloadWorker.kt
index f2015b3..c592a23 100644
--- a/app/src/main/java/it/reyboz/bustorino/data/MatoTripsDownloadWorker.kt
+++ b/app/src/main/java/it/reyboz/bustorino/data/MatoTripsDownloadWorker.kt
@@ -1,157 +1,158 @@
/*
BusTO - Data components
Copyright (C) 2023 Fabio Mazza
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package it.reyboz.bustorino.data
import android.app.NotificationManager
import android.content.Context
import android.util.Log
import androidx.work.*
import it.reyboz.bustorino.backend.Notifications
import it.reyboz.bustorino.data.gtfs.GtfsTrip
import java.util.concurrent.CountDownLatch
import kotlin.math.min
class MatoTripsDownloadWorker(appContext: Context, workerParams: WorkerParameters)
: CoroutineWorker(appContext, workerParams) {
+ private val DOWNLOAD_STEP = 50
override suspend fun doWork(): Result {
val tripsList = inputData.getStringArray(TRIPS_KEYS)
if (tripsList== null){
Log.e(DEBUG_TAG,"trips list given is null")
return Result.failure()
}
val numTrips = tripsList.size
var i = 0
var totDown = 0
while (i <numTrips){
- val res = downloadGtfsTrips(tripsList.copyOfRange(i, min(i+100, numTrips)))
+ val res = downloadGtfsTrips(tripsList.copyOfRange(i, min(i+DOWNLOAD_STEP, numTrips)))
if (res < 0){
return Result.failure();
}
totDown += res
Log.d("MatoTripsDownloadWorker", "Downloaded ${res} trips")
i+= 100
}
return Result.success(Data.Builder().putInt("tripsDownloaded", totDown).build())
}
/**
* Download GTFS Trips from Mato
*/
private fun downloadGtfsTrips(tripsList: Array<String>): Int{
val gtfsRepository = GtfsRepository(applicationContext)
val matoRepository = MatoRepository(applicationContext)
//clear the matoTrips
val queriedMatoTrips = HashSet<String>()
val downloadedMatoTrips = ArrayList<GtfsTrip>()
val failedMatoTripsDownload = HashSet<String>()
Log.i(DEBUG_TAG, "Requesting download for ${tripsList.size} trips")
val requestCountDown = CountDownLatch(tripsList.size);
for(trip in tripsList){
queriedMatoTrips.add(trip)
matoRepository.requestTripUpdate(trip,{error->
Log.e(DEBUG_TAG, "Cannot download Gtfs Trip $trip, error: $error")
//val stacktrace = error.stackTrace.take(5)
//Log.w(DEBUG_TAG, "Stacktrace:\n$stacktrace")
failedMatoTripsDownload.add(trip)
requestCountDown.countDown()
}){
if(it.isSuccess){
if (it.result == null){
Log.e(DEBUG_TAG, "Got null result");
}
downloadedMatoTrips.add(it.result!!)
} else{
failedMatoTripsDownload.add(trip)
}
Log.i(
DEBUG_TAG,"Result download, so far, trips: ${queriedMatoTrips.size}, failed: ${failedMatoTripsDownload.size}," +
" succeded: ${downloadedMatoTrips.size}")
//check if we can insert the trips
requestCountDown.countDown()
}
}
requestCountDown.await()
val tripsIDsCompleted = downloadedMatoTrips.map { trip-> trip.tripID }
//clear cache to avoid tripping memory
matoRepository.clearVolleyCache()
if (tripsIDsCompleted.isEmpty()){
Log.d(DEBUG_TAG, "No trips have been downloaded, set work to fail")
return -5
} else {
val doInsert = (queriedMatoTrips subtract failedMatoTripsDownload).containsAll(tripsIDsCompleted)
Log.i(DEBUG_TAG, "Inserting missing GtfsTrips in the database, should insert $doInsert")
if (doInsert) {
gtfsRepository.gtfsDao.insertTrips(downloadedMatoTrips)
}
return downloadedMatoTrips.size
}
}
override suspend fun getForegroundInfo(): ForegroundInfo {
val notificationManager =
applicationContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val context = applicationContext
Notifications.createDBNotificationChannelIfNeeded(context)
return ForegroundInfo(NOTIFICATION_ID, Notifications.makeMatoDownloadNotification(context))
}
companion object{
const val TRIPS_KEYS = "tripsToDownload"
const val DEBUG_TAG="BusTO:MatoTripDownWRK"
const val NOTIFICATION_ID=42424221
const val TAG_TRIPS ="gtfsTripsDownload"
fun requestMatoTripsDownload(trips: List<String>, context: Context, debugTag: String): OneTimeWorkRequest? {
if (trips.isEmpty()) return null
val workManager = WorkManager.getInstance(context)
val info = workManager.getWorkInfosForUniqueWork(TAG_TRIPS).get()
val runNewWork = if(info.isEmpty()) true
else info[0].state!= WorkInfo.State.RUNNING && info[0].state!= WorkInfo.State.ENQUEUED
val addDat = if(info.isEmpty())
null else info[0].state
Log.d(debugTag, "Request to download and insert ${trips.size} trips, proceed: $runNewWork, workstate: $addDat")
if(runNewWork) {
val tripsArr: Array<String?> = trips.toTypedArray()
val dataBuilder = Data.Builder().putStringArray(TRIPS_KEYS, tripsArr)
//build()
val requ = OneTimeWorkRequest.Builder(MatoTripsDownloadWorker::class.java)
.setInputData(dataBuilder.build()).setExpedited(OutOfQuotaPolicy.RUN_AS_NON_EXPEDITED_WORK_REQUEST)
.addTag(TAG_TRIPS)
.build()
workManager.enqueueUniqueWork(TAG_TRIPS, ExistingWorkPolicy.KEEP, requ)
return requ
} else return null;
}
}
}
diff --git a/app/src/main/java/it/reyboz/bustorino/fragments/GeneralMapLibreFragment.kt b/app/src/main/java/it/reyboz/bustorino/fragments/GeneralMapLibreFragment.kt
index fd42089..e487c29 100644
--- a/app/src/main/java/it/reyboz/bustorino/fragments/GeneralMapLibreFragment.kt
+++ b/app/src/main/java/it/reyboz/bustorino/fragments/GeneralMapLibreFragment.kt
@@ -1,1191 +1,1210 @@
/*
BusTO - Fragments components
Copyright (C) 2025 Fabio Mazza
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package it.reyboz.bustorino.fragments
import android.Manifest
import android.animation.ValueAnimator
import android.annotation.SuppressLint
import android.content.Context
import android.content.Context.LOCATION_SERVICE
import android.content.SharedPreferences
import android.content.res.ColorStateList
import android.graphics.Color
import android.location.Location
import android.location.LocationManager
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.animation.LinearInterpolator
import android.widget.ImageButton
import android.widget.ImageView
-import android.widget.RelativeLayout
import android.widget.TextView
import android.widget.Toast
import androidx.activity.result.ActivityResultCallback
import androidx.activity.result.contract.ActivityResultContracts
import androidx.cardview.widget.CardView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.content.ContextCompat
import androidx.core.content.res.ResourcesCompat
import androidx.core.view.ViewCompat
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
+import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.gson.JsonObject
import it.reyboz.bustorino.BuildConfig
import it.reyboz.bustorino.R
import it.reyboz.bustorino.backend.FiveTNormalizer
import it.reyboz.bustorino.backend.LivePositionTripPattern
import it.reyboz.bustorino.backend.LivePositionsServiceStatus
import it.reyboz.bustorino.backend.Stop
import it.reyboz.bustorino.backend.VehicleUtils
import it.reyboz.bustorino.backend.gtfs.GtfsUtils
import it.reyboz.bustorino.backend.gtfs.LivePositionUpdate
import it.reyboz.bustorino.backend.utils
import it.reyboz.bustorino.data.PreferencesHolder
import it.reyboz.bustorino.data.gtfs.TripAndPatternWithStops
import it.reyboz.bustorino.map.MapLibreLocationEngine
import it.reyboz.bustorino.map.MapLibreUtils
import it.reyboz.bustorino.middleware.FusedNativeLocationProvider
import it.reyboz.bustorino.util.Permissions
import it.reyboz.bustorino.util.ViewUtils
import it.reyboz.bustorino.viewmodels.LivePositionsViewModel
import it.reyboz.bustorino.viewmodels.MapStateViewModel
+import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import org.maplibre.android.MapLibre
import org.maplibre.android.camera.CameraPosition
import org.maplibre.android.geometry.LatLng
import org.maplibre.android.location.LocationComponent
import org.maplibre.android.location.LocationComponentActivationOptions
import org.maplibre.android.location.engine.LocationEngineCallback
import org.maplibre.android.location.engine.LocationEngineRequest
import org.maplibre.android.location.engine.LocationEngineResult
import org.maplibre.android.maps.MapLibreMap
import org.maplibre.android.maps.MapView
import org.maplibre.android.maps.OnMapReadyCallback
import org.maplibre.android.maps.Style
import org.maplibre.android.plugins.annotation.Symbol
import org.maplibre.android.plugins.annotation.SymbolManager
import org.maplibre.android.plugins.annotation.SymbolOptions
import org.maplibre.android.style.expressions.Expression
import org.maplibre.android.style.layers.Property.ICON_ANCHOR_CENTER
import org.maplibre.android.style.layers.Property.ICON_ROTATION_ALIGNMENT_MAP
import org.maplibre.android.style.layers.Property.TEXT_ANCHOR_CENTER
import org.maplibre.android.style.layers.Property.TEXT_ROTATION_ALIGNMENT_VIEWPORT
import org.maplibre.android.style.layers.PropertyFactory
import org.maplibre.android.style.layers.SymbolLayer
import org.maplibre.android.style.sources.GeoJsonSource
import org.maplibre.geojson.Feature
import org.maplibre.geojson.FeatureCollection
import org.maplibre.geojson.Point
import kotlin.time.Duration.Companion.milliseconds
abstract class GeneralMapLibreFragment: ScreenBaseFragment(), OnMapReadyCallback {
protected var map: MapLibreMap? = null
protected var shownStopInBottomSheet : Stop? = null
//protected var savedMapStateOnPause : Bundle? = null
protected var fragmentListener: CommonFragmentListener? = null
// Declare a variable for MapView
protected var mapView: MapView? = null
protected lateinit var mapStyle: Style
protected lateinit var stopsSource: GeoJsonSource
protected lateinit var busesSource: GeoJsonSource
protected lateinit var selectedStopSource: GeoJsonSource
protected lateinit var selectedBusSource: GeoJsonSource //= GeoJsonSource(SEL_BUS_SOURCE)
protected lateinit var sharedPreferences: SharedPreferences
protected lateinit var bottomSheetBehavior: BottomSheetBehavior<ConstraintLayout>
protected var locationEngine: MapLibreLocationEngine? = null
- protected lateinit var locationProvider: FusedNativeLocationProvider
+ protected var locationProvider: FusedNativeLocationProvider? = null
protected var shownToastNoPosition = false
protected var locationEnabledOnDevice = true
+ protected var busLayerStarted = false
//TODO ACTIVATE THIS
private val preferenceChangeListener = SharedPreferences.OnSharedPreferenceChangeListener(){ pref, key ->
/*when(key){
SettingsFragment.LIBREMAP_STYLE_PREF_KEY -> reloadMap()
}
*/
if(key == SettingsFragment.LIBREMAP_STYLE_PREF_KEY){
Log.d(DEBUG_TAG,"ASKING RELOAD OF MAP")
//reloadMap()
}
}
/**
* What to do when requesting the permission, when it's ok, initialize the map location component
*/
protected val positionRequestResponder = registerForActivityResult(
ActivityResultContracts.RequestMultiplePermissions(), ActivityResultCallback{ res ->
if(!(res.containsKey(PERM_LOC_COARSE)&&res.containsKey(PERM_LOC_FINE))){
Log.e(DEBUG_TAG, "Location request does not have the correct keys")
} else if(res[PERM_LOC_COARSE]!! && res[PERM_LOC_FINE]!!){
//permission OK, init map location
val mMap = map
if(mMap == null){
Log.w(DEBUG_TAG, "Location request completed, but map is null!")
}else{
initializeMapLocationComponent(mMap,requireContext(), null)
}
} else{
// PERMISSION DENIED
// TODO find better way to show the necessity of the permission
if(shouldShowRequestPermissionRationale(PERM_LOC_FINE))
Toast.makeText(requireContext(),
R.string.enable_position_message_map, Toast.LENGTH_SHORT).show()
}
}
)
//Bottom sheet behavior in GeneralMapLibreFragment
protected var bottomLayout: ConstraintLayout? = null
protected lateinit var stopTitleTextView: TextView
protected lateinit var stopNumberTextView: TextView
protected lateinit var linesPassingTextView: TextView
protected lateinit var extraBottomTextView: TextView
protected lateinit var arrivalsCard: CardView
protected lateinit var directionsCard: CardView
protected lateinit var bottomrightImage: ImageView
protected lateinit var locationComponent: LocationComponent
protected lateinit var busPositionsIconButton: ImageButton
protected lateinit var vehicleIcon: ImageView
protected var lastLocation : Location? = null
private var lastMapStyle =""
//BUS POSITIONS
protected val updatesByVehDict = HashMap<String, LivePositionTripPattern>(5)
protected val animatorsByVeh = HashMap<String, ValueAnimator>()
- protected var vehShowing = ""
+ protected var vehShowing: String? = null
protected var lastUpdateTime:Long = -2
+ protected var jobUpdate: Job? = null
- private val lifecycleOwnerLiveData = getViewLifecycleOwnerLiveData()
+ //private val lifecycleOwnerLiveData = viewLifecycleOwnerLiveData
//extra items to use the LibreMap
protected var symbolManager : SymbolManager? = null
protected var stopActiveSymbol: Symbol? = null
protected var stopsLayerStarted = false
protected val livePositionsViewModel : LivePositionsViewModel by activityViewModels()
//private lateinit var symbolManager: SymbolManager
protected val mapStateViewModel: MapStateViewModel by viewModels()
protected var locationInitialized = false
protected var mapInitialized = false
protected var receivedFirstLocation = false
//location callback to decide if to zoom to the user position
@SuppressLint("MissingPermission")
protected val mapLibreLocationCallback = object : LocationEngineCallback<LocationEngineResult> {
override fun onSuccess(result: LocationEngineResult) {
val location: Location? = result.lastLocation
Log.d(DEBUG_TAG, "Received location $location")
location?.let {
//check timing of the location
val currentTime = System.currentTimeMillis()
val discard = (currentTime - it.time) > 90 * 1000.0 // discard if it is Older than 60 seconds
if(!discard) {
if (!receivedFirstLocation) {
onFirstReceivedLocation(it)
}
receivedFirstLocation = true
}
}
if(receivedFirstLocation){
//remove this listener once we have received the location
locationEngine?.removeLocationUpdates(this)
}
}
override fun onFailure(exception: Exception) {
Log.e(DEBUG_TAG, "Error in getting position: ${exception.message}")
}
}
protected val deviceLocationStatusListener = FusedNativeLocationProvider.LocationStatusListener { isEnabled ->
mapStateViewModel.locationDeviceEnabled.value = isEnabled
if(locationEnabledOnDevice && !isEnabled && locationInitialized) {
warnLocationNotEnabledOnDevice()
//setMapLocationEnabled(false)
}
locationEnabledOnDevice = isEnabled
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
//sharedPreferences = PreferenceManager.getDefaultSharedPreferences(requireContext())
lastMapStyle = PreferencesHolder.getMapLibreStyleFile(requireContext())
//init map
MapLibre.getInstance(requireContext())
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
+ //TODO: Re-create map when this preference changes
lastMapStyle = PreferencesHolder.getMapLibreStyleFile(requireContext())
Log.d(DEBUG_TAG, "onCreateView lastMapStyle: $lastMapStyle")
return super.onCreateView(inflater, container, savedInstanceState)
}
protected fun initBottomSheet(view: View){
val bottomSheet = view.findViewById<ConstraintLayout>(R.id.bottom_sheet)
bottomLayout = bottomSheet
stopTitleTextView = view.findViewById(R.id.stopTitleTextView)
stopNumberTextView = view.findViewById(R.id.stopNumberTextView)
linesPassingTextView = view.findViewById(R.id.descriptionTextView)
arrivalsCard = view.findViewById(R.id.arrivalsCardButton)
directionsCard = view.findViewById(R.id.directionsCardButton)
vehicleIcon = view.findViewById(R.id.vehicleIcon)
bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet)
bottomSheetBehavior.state = BottomSheetBehavior.STATE_HIDDEN
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
//init bottom sheet
initBottomSheet(view)
bottomrightImage = view.findViewById(R.id.rightmostImageView)
extraBottomTextView = view.findViewById(R.id.extraBottomTextView)
}
override fun onResume() {
super.onResume()
mapView?.onResume()
val newMapStyle = PreferencesHolder.getMapLibreStyleFile(requireContext())
Log.d(DEBUG_TAG, "onResume newMapStyle: $newMapStyle, lastMapStyle: $lastMapStyle")
// TODO: reload style if user changed preferences
//if(newMapStyle!=lastMapStyle){
// reloadMap()
//}
+ if(busLayerStarted)
+ updatePositionsIcons(false)
}
override fun onLowMemory() {
mapView?.onLowMemory()
super.onLowMemory()
}
override fun onStart() {
super.onStart()
mapView?.onStart()
}
override fun onDestroy() {
mapView?.onDestroy()
Log.d(DEBUG_TAG, "Destroyed mapView Fragment!!")
+ busLayerStarted = false
super.onDestroy()
}
override fun onStop() {
+ locationProvider?.removeListener(deviceLocationStatusListener)
mapView?.onStop()
super.onStop()
}
override fun onPause() {
+ jobUpdate?.cancel()
mapView?.onPause()
super.onPause()
}
override fun onDestroyView() {
bottomLayout = null
- locationProvider.removeListener(deviceLocationStatusListener)
mapInitialized = false
locationInitialized = false
super.onDestroyView()
}
protected fun warnLocationNotEnabledOnDevice(){
context?.let{
Toast.makeText(it,R.string.enable_location_message,Toast.LENGTH_SHORT).show()
}
}
protected fun reloadMap(){
/*map?.let {
Log.d("GeneralMapFragment", "RELOADING MAP")
//save map state
savedMapStateOnPause = saveMapStateInBundle()
onMapDestroy()
//Destroy and recreate MAP
mapView.onDestroy()
mapView.onCreate(null)
mapView.getMapAsync(this)
}
*/
}
//For extra stuff to do when the map is destroyed
abstract fun onMapDestroy()
override fun onAttach(context: Context) {
super.onAttach(context)
if(context is CommonFragmentListener){
fragmentListener = context
} else throw RuntimeException("$context must implement CommonFragmentListener")
}
protected fun stopToGeoJsonFeature(s: Stop): Feature{
return Feature.fromGeometry(
Point.fromLngLat(s.longitude!!, s.latitude!!),
JsonObject().apply {
addProperty("id", s.ID)
addProperty("name", s.stopDefaultName)
//addProperty("routes", s.routesThatStopHereToString()) // Add routes array to JSON object
}
)
}
protected fun isPointInsideVisibleRegion(p: LatLng, other: Boolean): Boolean{
val bounds = map?.projection?.visibleRegion?.latLngBounds
var inside = other
bounds?.let { inside = it.contains(p) }
return inside
}
protected fun isPointInsideVisibleRegion(lat: Double, lon: Double, other: Boolean): Boolean{
val p = LatLng(lat, lon)
return isPointInsideVisibleRegion(p, other)
}
protected fun removeVehiclesData(vehs: List<String>){
for(v in vehs){
if (updatesByVehDict.contains(v)) {
updatesByVehDict.remove(v)
if (animatorsByVeh.contains(v)){
animatorsByVeh[v]?.cancel()
animatorsByVeh.remove(v)
}
}
if (vehShowing==v){
hideStopOrBusBottomSheet()
}
}
}
// Hide the bottom sheet and remove extra symbol
protected open fun hideStopOrBusBottomSheet(){
if (stopActiveSymbol!=null){
symbolManager?.delete(stopActiveSymbol)
stopActiveSymbol = null
}
if(!showOpenStopWithSymbolLayer()){
selectedStopSource.setGeoJson(FeatureCollection.fromFeatures(ArrayList<Feature>()))
}
bottomSheetBehavior.state = BottomSheetBehavior.STATE_HIDDEN
//isBottomSheetShowing = false
//reset states
shownStopInBottomSheet = null
- if (vehShowing!=""){
+ if (vehShowing!=null){
//we are hiding a vehicle
- vehShowing = ""
+ vehShowing = null
updatePositionsIcons(true)
}
extraBottomTextView.visibility = View.GONE
}
protected fun initSymbolManager(mapReady: MapLibreMap , style: Style){
val sm = SymbolManager(mapView!!, mapReady, style)
sm.iconAllowOverlap = true
sm.textAllowOverlap = false
sm.addClickListener { _ ->
if (stopActiveSymbol != null) {
hideStopOrBusBottomSheet()
return@addClickListener true
} else
return@addClickListener false
}
symbolManager = sm
}
/**
* Change the icon indicating the status of the live Positions
*/
protected fun setBusPositionsIcon(enabled: Boolean, error: Boolean){
val ctx = requireContext()
if(!enabled)
busPositionsIconButton.setImageDrawable(ContextCompat.getDrawable(ctx, R.drawable.bus_pos_circle_inactive))
else if(error)
busPositionsIconButton.setImageDrawable(ContextCompat.getDrawable(ctx, R.drawable.bus_pos_circle_notworking))
else
busPositionsIconButton.setImageDrawable(ContextCompat.getDrawable(ctx, R.drawable.bus_pos_circle_active))
}
abstract fun onMapLocationComponentInitialized()
@SuppressLint("MissingPermission")
protected fun setLocationComponentEnabled(enabled: Boolean): Boolean{
var changed = false
map?.apply {
if(locationComponent.isLocationComponentEnabled !=enabled)
locationComponent.isLocationComponentEnabled= enabled
changed = true}
Log.d(DEBUG_TAG, "Asked to set location component enabled: $enabled, changed: $changed")
mapStateViewModel.locationUserActive.value = enabled
return changed
}
@SuppressLint("MissingPermission")
protected fun initializeMapLocationComponent(map: MapLibreMap, context: Context, style: Style?){
val mStyle = style ?: map.style
if(locationInitialized){
Log.w(DEBUG_TAG, "trying to initialize Location Component, but it is already done")
return
}
mStyle?.let{ style ->
locationComponent = map.locationComponent
- locationProvider = FusedNativeLocationProvider(context)
- locationProvider.addListener(deviceLocationStatusListener)
- locationEngine = MapLibreLocationEngine(locationProvider)
+ val locProvider = FusedNativeLocationProvider(context)
+ locProvider.addListener(deviceLocationStatusListener)
+ locationEngine = MapLibreLocationEngine(locProvider)
+ locationProvider = locProvider
val options = LocationComponentActivationOptions.builder(context, style)
.useDefaultLocationEngine(false)
.locationEngine(locationEngine)
.build()
locationComponent.activateLocationComponent(options)
if(BuildConfig.DEBUG) Log.d(DEBUG_TAG, "Initializing location, request initial position")
startInitialPositionRequest()
if(!locationEnabledOnDevice){
warnLocationNotEnabledOnDevice()
}else {
setLocationComponentEnabled(true)
}
locationInitialized = true
onMapLocationComponentInitialized()
}
}
@SuppressLint("MissingPermission")
protected fun startInitialPositionRequest(){
locationEngine?.requestLocationUpdates(LocationEngineRequest.Builder(500).setDisplacement(20.0f).build(),
mapLibreLocationCallback, null)
}
protected fun stopInitialPositionRequest(){
locationEngine?.removeLocationUpdates(mapLibreLocationCallback)
}
/**
* Update function for the bus positions
* Takes the processed updates and saves them accordingly
* Unified version that works with both fragments
*
* @param incomingData Map of updates with optional trip and pattern information
* @param hasVehicleTracking If true, checks if vehShowing is updated and calls callback (default: true)
* @param trackVehicleCallback Optional callback to show vehicle details when vehShowing is updated
*/
protected fun updateBusPositionsInMap(
incomingData: HashMap<String, Pair<LivePositionUpdate,TripAndPatternWithStops?>>,
- hasVehicleTracking: Boolean = false,
+ hasVehicleTracking: Boolean = true,
trackVehicleCallback: ((String) -> Unit)? = null
) {
val vehsNew = HashSet(incomingData.values.map { up -> up.first.vehicle })
val vehsOld = HashSet(updatesByVehDict.keys)
Log.d(DEBUG_TAG, "In fragment, have ${incomingData.size} updates to show")
var countUpds = 0
var createdVehs = 0
for (upsWithTrp in incomingData.values) {
val newPos = upsWithTrp.first
val patternStops = upsWithTrp.second
val vehID = newPos.vehicle
// Validate coordinates
if (!vehsOld.contains(vehID)) {
if (newPos.latitude <= 0 || newPos.longitude <= 0) {
Log.w(DEBUG_TAG, "Update ignored for veh $vehID on line ${newPos.routeID}, lat: ${newPos.latitude}, lon ${newPos.longitude}")
continue
}
}
if (vehsOld.contains(vehID)) {
// Changing the location of an existing bus
val oldPosData = updatesByVehDict[vehID]!!
val oldPos = oldPosData.posUpdate
val oldPattern = oldPosData.pattern
var avoidShowingUpdateBecauseIsImpossible = false
// Check for impossible route changes
if (oldPos.routeID != newPos.routeID) {
val dist = LatLng(oldPos.latitude, oldPos.longitude).distanceTo(
LatLng(newPos.latitude, newPos.longitude)
)
val speed = dist * 3.6 / (newPos.timestamp - oldPos.timestamp) // km/h
Log.w(DEBUG_TAG, "Vehicle $vehID changed route from ${oldPos.routeID} to ${newPos.routeID}, distance: $dist, speed: $speed")
if (speed > 120 || speed < 0) {
avoidShowingUpdateBecauseIsImpossible = true
}
}
if (avoidShowingUpdateBecauseIsImpossible) {
Log.w(DEBUG_TAG, "Update for vehicle $vehID skipped")
continue
}
// Check if position actually changed
val samePosition = (oldPos.latitude == newPos.latitude) &&
(oldPos.longitude == newPos.longitude)
val setPattern = (oldPattern == null) && (patternStops != null)
// Copy old bearing if new one is missing
if (newPos.bearing == null && oldPos.bearing != null) {
newPos.bearing = oldPos.bearing
}
if (!samePosition || setPattern) {
val newOrOldPosInBounds = isPointInsideVisibleRegion(
newPos.latitude, newPos.longitude, true
) || isPointInsideVisibleRegion(oldPos.latitude, oldPos.longitude, true)
if (newOrOldPosInBounds) {
// Update pattern data if available
patternStops?.let {
updatesByVehDict[vehID]!!.pattern = it.pattern
}
// Animate the position change
animateNewPositionMove(newPos)
} else {
// Update position without animation
updatesByVehDict[vehID] = LivePositionTripPattern(
newPos,
patternStops?.pattern
)
}
}
countUpds++
} else {
// New vehicle - create entry
updatesByVehDict[vehID] = LivePositionTripPattern(
newPos,
patternStops?.pattern
)
createdVehs++
}
// Update vehicle details if this is the shown/tracked vehicle
- if (hasVehicleTracking && vehShowing.isNotEmpty() && vehID == vehShowing) {
+ if (hasVehicleTracking && vehShowing?.isNotEmpty() == true && vehID == vehShowing) {
trackVehicleCallback?.invoke(vehID)
}
}
// Remove old positions
Log.d(DEBUG_TAG, "Updated $countUpds vehicles, created $createdVehs vehicles")
vehsOld.removeAll(vehsNew)
// Clean up stale vehicles (not updated for 2 minutes)
val currentTimeStamp = System.currentTimeMillis() / 1000
for (vehID in vehsOld) {
val posData = updatesByVehDict[vehID]!!
if (currentTimeStamp - posData.posUpdate.timestamp > 2 * 60) {
// Remove the bus
updatesByVehDict.remove(vehID)
// Cancel and remove animator if exists
animatorsByVeh[vehID]?.cancel()
animatorsByVeh.remove(vehID)
}
}
// Update UI
updatePositionsIcons(false)
}
/**
* Shared bottom sheet setup. The [onDirectionsClick] lambda is called when
* directionsCard is tapped; it receives the pattern code (empty string when
* no pattern is available) so each subclass can navigate as it sees fit.
*/
protected fun showVehicleTripInBottomSheet(
veh: String,
onDirectionsClick: (patternCode: String, veh: String) -> Unit
) {
val data = updatesByVehDict[veh] ?: run {
Log.w(DEBUG_TAG, "Asked to show vehicle $veh, but it's not present in the updates")
return
}
bottomLayout?.let {
val lineName = FiveTNormalizer.fixShortNameForDisplay(
GtfsUtils.getLineNameFromGtfsID(data.posUpdate.routeID), false
)
val pat = data.pattern
if (pat != null) {
stopTitleTextView.text = pat.headsign
stopTitleTextView.visibility = View.VISIBLE
stopNumberTextView.text = getString(R.string.line_fill_towards, lineName)
} else {
stopTitleTextView.visibility = View.GONE
stopNumberTextView.text = getString(R.string.line_fill, lineName)
}
directionsCard.setOnClickListener {
onDirectionsClick(pat?.code ?: "", veh)
}
directionsCard.visibility = View.VISIBLE
bottomrightImage.setImageDrawable(
ResourcesCompat.getDrawable(resources, R.drawable.ic_magnifying_glass, activity?.theme)
)
// if you change this, remember to change the color of the vehicleIcon
val colorBlue = ResourcesCompat.getColor(resources, R.color.blue_500, activity?.theme)
ViewCompat.setBackgroundTintList(directionsCard, ColorStateList.valueOf(colorBlue))
linesPassingTextView.text = getString(R.string.vehicle_fill, data.posUpdate.vehicle)
arrivalsCard.visibility = View.GONE
extraBottomTextView.text = getString(R.string.updated_fill, utils.unixTimestampToLocalTime(data.posUpdate.timestamp))
extraBottomTextView.visibility = View.VISIBLE
val update = data.posUpdate
val vehInfo = VehicleUtils.getTypeForLabel(update.vehicle)
if(vehInfo == null){
vehicleIcon.visibility = View.GONE
} else{
val ico = when(vehInfo.type){
VehicleUtils.VehicleType.BUS -> R.drawable.ic_bus_small
VehicleUtils.VehicleType.ELECTRIC_BUS -> R.drawable.ic_bus_electric_small
VehicleUtils.VehicleType.TRAM -> R.drawable.ic_tram_24
}
vehicleIcon.setImageDrawable(ResourcesCompat.getDrawable(resources, ico, activity?.theme))
vehicleIcon.visibility = View.VISIBLE
vehicleIcon.setOnClickListener {
val print = "${vehInfo.type.getName()} ${vehInfo.name}"
makeToast(print)
}
}
}
- vehShowing = veh
+ vehShowing = null
bottomSheetBehavior.state = BottomSheetBehavior.STATE_EXPANDED
updatePositionsIcons(true)
Log.d(DEBUG_TAG, "Shown vehicle $veh in bottom sheet")
}
/**
* Update the bus positions displayed on the map, from the existing data
*
- * @param forced If true, forces immediate update ignoring the 60ms throttle
+ * @param forced If true, forces immediate update ignoring the 100ms throttle
*/
protected fun updatePositionsIcons(forced: Boolean) {
// Avoid frequent updates - throttle to max once per 60ms
val currentTime = System.currentTimeMillis()
- if (!forced && currentTime - lastUpdateTime < 60) {
+ val isStarted = (lifecycle.currentState.isAtLeast(Lifecycle.State.STARTED))
+ if(forced){
+ // if we're running a forced update, cancel the pending one
+ jobUpdate?.cancel()
+ }
+ else if (currentTime - lastUpdateTime < 100) {
// Schedule delayed update
- if(lifecycleOwnerLiveData.value != null)
- viewLifecycleOwner.lifecycleScope.launch {
- delay(200.milliseconds)
- updatePositionsIcons(forced)
+ if(viewLifecycleOwnerLiveData.value != null) {
+ jobUpdate?.cancel()
+ jobUpdate = viewLifecycleOwner.lifecycleScope.launch {
+ delay(100.milliseconds)
+ updatePositionsIcons(false)
}
+ }
return
}
+ if(!isStarted){
+ Log.w(DEBUG_TAG, "fragment is not started, ")
+ }
val busFeatures = ArrayList<Feature>()
val selectedBusFeatures = ArrayList<Feature>()
for (dat in updatesByVehDict.values) {
val pos = dat.posUpdate
val point = Point.fromLngLat(pos.longitude, pos.latitude)
val newFeature = Feature.fromGeometry(
point,
JsonObject().apply {
addProperty("veh", pos.vehicle)
addProperty("trip", pos.tripID)
addProperty("bearing", pos.bearing ?: 0.0f)
addProperty("line", pos.routeID.substringBeforeLast('U'))
}
)
// Separate selected vehicle from others
- if (vehShowing.isNotEmpty() && vehShowing == dat.posUpdate.vehicle) {
+ if (vehShowing?.isNotEmpty() == true && vehShowing == dat.posUpdate.vehicle) {
selectedBusFeatures.add(newFeature)
} else {
busFeatures.add(newFeature)
}
}
busesSource.setGeoJson(FeatureCollection.fromFeatures(busFeatures))
selectedBusSource.setGeoJson(FeatureCollection.fromFeatures(selectedBusFeatures))
lastUpdateTime = System.currentTimeMillis()
}
/**
* Animates the transition of a vehicle from its current position to a new position
* This is the tricky part - we need to set the new positions with the data and redraw them all
*
* @param positionUpdate The new position update to animate to
*/
protected fun animateNewPositionMove(positionUpdate: LivePositionUpdate) {
val vehID = positionUpdate.vehicle
// Check if vehicle exists in our tracking dictionary
if (vehID !in updatesByVehDict.keys) {
return
}
val currentUpdate = updatesByVehDict[vehID] ?: run {
Log.e(DEBUG_TAG, "Have to run animation for veh $vehID but not in the dict")
return
}
// Cancel any current animation for this vehicle
animatorsByVeh[vehID]?.cancel()
val posUp = currentUpdate.posUpdate
val currentPos = LatLng(posUp.latitude, posUp.longitude)
val newPos = LatLng(positionUpdate.latitude, positionUpdate.longitude)
// Create animator for smooth transition
val valueAnimator = ValueAnimator.ofObject(
MapLibreUtils.LatLngEvaluator(),
currentPos,
newPos
)
valueAnimator.addUpdateListener { animation ->
val latLng = animation.animatedValue as LatLng
// Update position during animation
updatesByVehDict[vehID]?.let { update ->
update.posUpdate.latitude = latLng.latitude
update.posUpdate.longitude = latLng.longitude
updatePositionsIcons(false)
} ?: run {
Log.w(DEBUG_TAG, "The bus position to animate has been removed, but the animator is still running!")
}
}
// Set the new position as current but keep old coordinates for animation start
positionUpdate.latitude = posUp.latitude
positionUpdate.longitude = posUp.longitude
updatesByVehDict[vehID]!!.posUpdate = positionUpdate
// Configure and start animation
valueAnimator.duration = 300
valueAnimator.interpolator = LinearInterpolator()
valueAnimator.start()
// Store animator for potential cancellation
animatorsByVeh[vehID] = valueAnimator
}
/// STOP OPENING
abstract fun showOpenStopWithSymbolLayer(): Boolean
/**
* Update the bottom sheet with the stop information
*/
protected fun openStopInBottomSheet(stop: Stop){
bottomLayout?.let {
//lay.findViewById<TextView>(R.id.stopTitleTextView).text ="${stop.ID} - ${stop.stopDefaultName}"
val stopName = stop.stopUserName ?: stop.stopDefaultName
stopTitleTextView.text = stopName//stop.stopDefaultName
stopNumberTextView.text = getString(R.string.stop_fill,stop.ID)
stopTitleTextView.visibility = View.VISIBLE
val string_show = if (stop.numRoutesStopping==0) ""
else requireContext().getString(R.string.lines_fill, stop.routesThatStopHereToString())
linesPassingTextView.text = string_show
linesPassingTextView.visibility = View.VISIBLE
//SET ON CLICK LISTENER
arrivalsCard.setOnClickListener{
fragmentListener?.requestArrivalsForStopID(stop.ID)
}
arrivalsCard.visibility = View.VISIBLE
directionsCard.visibility = View.VISIBLE
directionsCard.setOnClickListener {
ViewUtils.openStopInOutsideApp(stop, context)
}
context?.let {
val colorIcon = ViewUtils.getColorFromTheme(it, android.R.attr.colorAccent)//ResourcesCompat.getColor(resources,R.attr.colorAccent,activity?.theme)
ViewCompat.setBackgroundTintList(directionsCard, ColorStateList.valueOf(colorIcon))
}
bottomrightImage.setImageDrawable(ResourcesCompat.getDrawable(resources, R.drawable.navigation_right, activity?.theme))
vehicleIcon.visibility = View.GONE
}
//add stop marker
if (stop.latitude!=null && stop.longitude!=null) {
Log.d(DEBUG_TAG, "Showing stop: ${stop.ID}")
if (showOpenStopWithSymbolLayer()) {
stopActiveSymbol = symbolManager?.create(
SymbolOptions()
.withLatLng(LatLng(stop.latitude!!, stop.longitude!!))
.withIconImage(STOP_ACTIVE_IMG)
.withIconAnchor(ICON_ANCHOR_CENTER)
)
} else {
val list = ArrayList<Feature>()
list.add(stopToGeoJsonFeature(stop))
selectedStopSource.setGeoJson(
FeatureCollection.fromFeatures(list)
)
}
}
Log.d(DEBUG_TAG, "Shown stop $stop in bottom sheet")
shownStopInBottomSheet = stop
bottomSheetBehavior.state = BottomSheetBehavior.STATE_EXPANDED
}
protected fun stopAnimations(){
for(anim in animatorsByVeh.values){
anim.cancel()
}
}
protected fun addImagesStyle(style: Style){
style.addImage(
STOP_IMAGE_ID,
ResourcesCompat.getDrawable(resources,R.drawable.bus_stop_new, activity?.theme)!!)
style.addImage(STOP_ACTIVE_IMG, ResourcesCompat.getDrawable(resources, R.drawable.bus_stop_new_highlight, activity?.theme)!!)
style.addImage("ball",ResourcesCompat.getDrawable(resources, R.drawable.ball, activity?.theme)!!)
style.addImage(BUS_IMAGE_ID,ResourcesCompat.getDrawable(resources, R.drawable.map_bus_position_icon, activity?.theme)!!)
style.addImage(BUS_SEL_IMAGE_ID, ResourcesCompat.getDrawable(resources, R.drawable.map_bus_position_icon_sel, activity?.theme)!!)
val polyIconArrow = ResourcesCompat.getDrawable(resources, R.drawable.arrow_up_box_fill, activity?.theme)!!
style.addImage(POLY_ARROW, polyIconArrow)
}
protected fun initStopsLayer(style: Style, stopsFeatures: FeatureCollection?){
//determine default layer
var layerAbove = ""
if (lastMapStyle == MapLibreUtils.STYLE_OSM_RASTER){
layerAbove = "osm-raster"
} else if (lastMapStyle == MapLibreUtils.STYLE_VECTOR){
layerAbove = "symbol-transit-airfield"
}
initStopsLayer(style, stopsFeatures, layerAbove)
}
protected fun initStopsLayer(style: Style, stopsFeatures: FeatureCollection?, stopsLayerAbove: String){
stopsSource = GeoJsonSource(STOPS_SOURCE_ID,stopsFeatures ?: FeatureCollection.fromFeatures(ArrayList<Feature>()))
style.addSource(stopsSource)
// Stops layer
val stopsLayer = SymbolLayer(STOPS_LAYER_ID, STOPS_SOURCE_ID)
stopsLayer.withProperties(
PropertyFactory.iconImage(STOP_IMAGE_ID),
PropertyFactory.iconAnchor(ICON_ANCHOR_CENTER),
PropertyFactory.iconAllowOverlap(true),
PropertyFactory.iconIgnorePlacement(true)
)
style.addLayerAbove(stopsLayer, stopsLayerAbove ) //"label_country_1") this with OSM Bright
selectedStopSource = GeoJsonSource(SEL_STOP_SOURCE, FeatureCollection.fromFeatures(ArrayList<Feature>()))
style.addSource(selectedStopSource)
val selStopLayer = SymbolLayer(SEL_STOP_LAYER, SEL_STOP_SOURCE)
selStopLayer.withProperties(
PropertyFactory.iconImage(STOP_ACTIVE_IMG),
PropertyFactory.iconAllowOverlap(true),
PropertyFactory.iconIgnorePlacement(true),
PropertyFactory.iconAnchor(ICON_ANCHOR_CENTER),
)
style.addLayerAbove(selStopLayer, STOPS_LAYER_ID)
stopsLayerStarted = true
}
/**
* Setup the Map Layers
*/
protected fun setupBusLayer(style: Style, withLabels: Boolean =false, busIconsScale: Float = 1.0f) {
// Buses source
busesSource = GeoJsonSource(BUSES_SOURCE_ID)
style.addSource(busesSource)
//style.addImage("bus_symbol",ResourcesCompat.getDrawable(resources, R.drawable.map_bus_position_icon, activity?.theme)!!)
selectedBusSource = GeoJsonSource(SEL_BUS_SOURCE)
style.addSource(selectedBusSource)
// Buses layer
val busesLayer = SymbolLayer(BUSES_LAYER_ID, BUSES_SOURCE_ID).apply {
withProperties(
PropertyFactory.iconImage(BUS_IMAGE_ID),
PropertyFactory.iconSize(busIconsScale),
PropertyFactory.iconAllowOverlap(true),
PropertyFactory.iconIgnorePlacement(true),
PropertyFactory.iconRotate(Expression.get("bearing")),
PropertyFactory.iconRotationAlignment(ICON_ROTATION_ALIGNMENT_MAP)
)
if (withLabels){
withProperties(PropertyFactory.textAnchor(TEXT_ANCHOR_CENTER),
PropertyFactory.textAllowOverlap(true),
PropertyFactory.textField(Expression.get("line")),
PropertyFactory.textColor(Color.WHITE),
PropertyFactory.textRotationAlignment(TEXT_ROTATION_ALIGNMENT_VIEWPORT),
PropertyFactory.textSize(12f),
PropertyFactory.textFont(arrayOf("noto_sans_regular")))
}
}
style.addLayerAbove(busesLayer, STOPS_LAYER_ID)
val selectedBusLayer = SymbolLayer(SEL_BUS_LAYER, SEL_BUS_SOURCE).apply {
withProperties(
PropertyFactory.iconImage(BUS_SEL_IMAGE_ID),
PropertyFactory.iconSize(busIconsScale),
PropertyFactory.iconAllowOverlap(true),
PropertyFactory.iconIgnorePlacement(true),
PropertyFactory.iconRotate(Expression.get("bearing")),
PropertyFactory.iconRotationAlignment(ICON_ROTATION_ALIGNMENT_MAP)
)
if (withLabels){
withProperties(PropertyFactory.textAnchor(TEXT_ANCHOR_CENTER),
PropertyFactory.textAllowOverlap(true),
PropertyFactory.textField(Expression.get("line")),
PropertyFactory.textColor(Color.WHITE),
PropertyFactory.textRotationAlignment(TEXT_ROTATION_ALIGNMENT_VIEWPORT),
PropertyFactory.textSize(12f),
PropertyFactory.textFont(arrayOf("noto_sans_regular")))
}
}
style.addLayerAbove(selectedBusLayer, BUSES_LAYER_ID)
-
+ busLayerStarted = true
}
/**
* Method used for enabling / disabling the location from the buttons
*/
protected fun switchUserLocationStatus(view: View?){
val enabled = if(locationInitialized) locationComponent.isLocationComponentEnabled else false
val context = context ?: return
if(enabled) {
if(!receivedFirstLocation){
//use case: the user has decided to disable the location before the first position arrived
stopInitialPositionRequest()
}
// we have to disable it
setMapLocationEnabled(false)
}
else if(deviceHasLocationProvider()) {
if(Permissions.bothLocationPermissionsGranted(context)){
if(!locationEnabledOnDevice){
warnLocationNotEnabledOnDevice()
} else{
setMapLocationEnabled(true)
}
} else{
Log.d(DEBUG_TAG, "Requesting permissions to show location")
Permissions.getInstance(context).checkRequestLocationPermissions(requireActivity(), positionRequestResponder)
}
} else{
context.let {
Toast.makeText(it, R.string.no_gps_on_device, Toast.LENGTH_SHORT).show()
}
//adjust ui
setLocationIconEnabled(false)
}
}
/**
* Set the map location component enabled
*/
@SuppressLint("MissingPermission")
protected fun setMapLocationEnabled(enabled: Boolean){
Log.d(DEBUG_TAG, "Setting map location enabled: $enabled")
map?.locationComponent?.isLocationComponentEnabled = enabled
//map?.cameraPosition =
mapStateViewModel.locationUserActive.value = enabled
onMapLocationEnabled(enabled)
}
/**
* Function to run at the first time the fragment is opened
* Check if we have the permissions, and then initialize the map location component
* If we don't have it, request the permission
*/
protected fun checkInitMapLocation(mapReady: MapLibreMap,style: Style, context: Context) {
//enable location
val hasGps = deviceHasLocationProvider()
val permissions = Permissions.getInstance(context)
if(hasGps) {
if (Permissions.bothLocationPermissionsGranted(context)) {
Log.d(DEBUG_TAG, "Have got the location permission, init location component")
initializeMapLocationComponent(mapReady, context, style)
}else {
var req = false
activity?.let{
req = permissions.checkRequestLocationPermissions(it, positionRequestResponder)
}
if(!req) {
setMapLocationEnabled(false)
}
}
}
}
/**
* Set the UI elements showing that the user location is disabled
*/
abstract fun onMapLocationEnabled(active: Boolean)
/**
* Helper function to actually set the icon
*/
abstract fun setLocationIconEnabled(enabled: Boolean)
/**
* Called when we receive the first fix on the user location
*/
abstract fun onFirstReceivedLocation(location: Location)
protected fun isBottomSheetShowing(): Boolean {
return bottomSheetBehavior.state == BottomSheetBehavior.STATE_EXPANDED
}
protected fun deviceHasLocationProvider(): Boolean{
val locManager = requireContext().getSystemService(LOCATION_SERVICE) as LocationManager
return locManager.allProviders.isNotEmpty()
}
/**
* Update automatically the icon when the live position service changes status
*/
protected fun observeStatusLivePositions(){
livePositionsViewModel.serviceStatus.observe(viewLifecycleOwner){ status ->
//if service is active, update the bus positions icon
when(status) {
LivePositionsServiceStatus.OK ->
setBusPositionsIcon(true, error = false)
LivePositionsServiceStatus.NO_POSITIONS -> setBusPositionsIcon(true, error = true)
else -> setBusPositionsIcon( true, error = true)
}
}
}
/**
* Clear all buses from the map
*/
protected fun clearAllBusPositionsInMap(){
for ((k, anim) in animatorsByVeh){
anim.cancel()
}
animatorsByVeh.clear()
updatesByVehDict.clear()
updatePositionsIcons(forced = false)
}
protected fun setCameraPosition(latitude: Double, longitude: Double, zoom: Double) {
map?.cameraPosition = CameraPosition.Builder()
.target(LatLng(latitude, longitude))
.zoom(zoom)
.build()
}
protected fun showToastLocation(enabled: Boolean){
val textid = if (enabled) R.string.location_enabled else R.string.location_disabled
context?.let{
Toast.makeText(it,textid,Toast.LENGTH_SHORT).show()
}
}
companion object{
private const val DEBUG_TAG="GeneralMapLibreFragment"
const val BUSES_SOURCE_ID = "buses-source"
const val BUSES_LAYER_ID = "buses-layer"
const val SEL_STOP_SOURCE="selected-stop-source"
const val SEL_STOP_LAYER = "selected-stop-layer"
const val SEL_BUS_SOURCE = "sel_bus_source"
const val SEL_BUS_LAYER = "sel_bus_layer"
const val KEY_LOCATION_ENABLED="location_enabled"
protected const val STOPS_SOURCE_ID = "stops-source"
protected const val STOPS_LAYER_ID = "stops-layer"
protected const val STOP_IMAGE_ID = "stop-img"
protected const val STOP_ACTIVE_IMG = "stop_active_img"
protected const val BUS_IMAGE_ID = "bus_symbol"
protected const val BUS_SEL_IMAGE_ID = "sel_bus_symbol"
protected const val POLYLINE_LAYER = "polyline-layer"
protected const val POLYLINE_SOURCE = "polyline-source"
protected const val POLY_ARROWS_LAYER = "arrows-layer"
protected const val POLY_ARROWS_SOURCE = "arrows-source"
protected const val POLY_ARROW ="poly-arrow-img"
private const val PERM_LOC_COARSE = Manifest.permission.ACCESS_COARSE_LOCATION
private const val PERM_LOC_FINE = Manifest.permission.ACCESS_FINE_LOCATION
//TODO: this is hardcoded, make it modifiable by the user
protected const val MAX_DIST_KM = 90.0
}
}
\ No newline at end of file
diff --git a/app/src/main/java/it/reyboz/bustorino/fragments/LinesDetailFragment.kt b/app/src/main/java/it/reyboz/bustorino/fragments/LinesDetailFragment.kt
index 83118e7..74e0d9f 100644
--- a/app/src/main/java/it/reyboz/bustorino/fragments/LinesDetailFragment.kt
+++ b/app/src/main/java/it/reyboz/bustorino/fragments/LinesDetailFragment.kt
@@ -1,1188 +1,1188 @@
/*
BusTO - Fragments components
Copyright (C) 2023 Fabio Mazza
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package it.reyboz.bustorino.fragments
import android.animation.ObjectAnimator
import android.annotation.SuppressLint
import android.content.Context
import android.content.SharedPreferences
import android.location.Location
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.*
import androidx.appcompat.content.res.AppCompatResources
import androidx.core.content.ContextCompat
import androidx.core.content.res.ResourcesCompat
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.preference.PreferenceManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.gson.JsonObject
import it.reyboz.bustorino.R
import it.reyboz.bustorino.adapters.NameCapitalize
import it.reyboz.bustorino.adapters.StopAdapterListener
import it.reyboz.bustorino.adapters.StopRecyclerAdapter
import it.reyboz.bustorino.backend.Stop
import it.reyboz.bustorino.backend.gtfs.GtfsUtils
import it.reyboz.bustorino.backend.gtfs.PolylineParser
import it.reyboz.bustorino.backend.utils
import it.reyboz.bustorino.data.MatoTripsDownloadWorker
import it.reyboz.bustorino.data.PreferencesHolder
import it.reyboz.bustorino.data.gtfs.MatoPatternWithStops
import it.reyboz.bustorino.map.*
import it.reyboz.bustorino.util.Permissions
import it.reyboz.bustorino.viewmodels.LinesViewModel
import it.reyboz.bustorino.viewmodels.MapStateViewModel
import it.reyboz.bustorino.viewmodels.ServiceAlertsViewModel
import kotlinx.coroutines.Runnable
import org.maplibre.android.camera.CameraPosition
import org.maplibre.android.camera.CameraUpdateFactory
import org.maplibre.android.geometry.LatLng
import org.maplibre.android.geometry.LatLngBounds
import org.maplibre.android.maps.MapLibreMap
import org.maplibre.android.maps.Style
import org.maplibre.android.style.expressions.Expression
import org.maplibre.android.style.layers.LineLayer
import org.maplibre.android.style.layers.Property
import org.maplibre.android.style.layers.Property.ICON_ROTATION_ALIGNMENT_MAP
import org.maplibre.android.style.layers.PropertyFactory
import org.maplibre.android.style.layers.SymbolLayer
import org.maplibre.android.style.sources.GeoJsonSource
import org.maplibre.geojson.Feature
import org.maplibre.geojson.FeatureCollection
import org.maplibre.geojson.LineString
import org.maplibre.geojson.Point
class LinesDetailFragment() : GeneralMapLibreFragment() {
private var lineID = "" // the GTFS line ID (e.g. "gtt:10U")
private lateinit var patternsSpinner: Spinner
private var patternsAdapter: ArrayAdapter<String>? = null
//private var isBottomSheetShowing = false
private var shouldMapLocationBeReactivated = true
private var toRunWhenMapReady : Runnable? = null
//private var mapInitialized = AtomicBoolean(false)
//private var patternsSpinnerState: Parcelable? = null
private lateinit var currentPatterns: List<MatoPatternWithStops>
//private lateinit var map: MapView
private var patternShown: MatoPatternWithStops? = null
private val viewModel: LinesViewModel by viewModels()
private val alertsViewModel: ServiceAlertsViewModel by activityViewModels()
//private var firstInit = true
private var pausedFragment = false
private lateinit var switchButton: ImageButton
private lateinit var lineInfoButton: ImageButton
private var favoritesButton: ImageButton? = null
private var locationIcon: ImageButton? = null
private var isLineInFavorite = false
private var appContext: Context? = null
private var isLocationPermissionOK = false
private val lineSharedPrefMonitor = SharedPreferences.OnSharedPreferenceChangeListener { pref, keychanged ->
if(keychanged!=PreferencesHolder.PREF_FAVORITE_LINES || lineID.isEmpty()) return@OnSharedPreferenceChangeListener
val newFavorites = pref.getStringSet(PreferencesHolder.PREF_FAVORITE_LINES, HashSet())
newFavorites?.let {favorites->
isLineInFavorite = favorites.contains(lineID)
//if the button has been intialized, change the icon accordingly
favoritesButton?.let { button->
//avoid crashes if fragment not attached
if(context==null) return@let
if(isLineInFavorite) {
button.setImageDrawable(ResourcesCompat.getDrawable(resources, R.drawable.ic_star_filled, null))
appContext?.let { Toast.makeText(it,R.string.favorites_line_add,Toast.LENGTH_SHORT).show()}
} else {
button.setImageDrawable(ResourcesCompat.getDrawable(resources, R.drawable.ic_star_outline, null))
appContext?.let {Toast.makeText(it,R.string.favorites_line_remove,Toast.LENGTH_SHORT).show()}
}
}
}
}
private lateinit var stopsRecyclerView: RecyclerView
private lateinit var descripTextView: TextView
private var stopIDFromToShow = ""
private var patternIdToShow = ""
//adapter for recyclerView
private val stopAdapterListener= object : StopAdapterListener {
override fun onTappedStop(stop: Stop?) {
if(viewModel.shouldShowMessage) {
Toast.makeText(context, R.string.long_press_stop_4_options, Toast.LENGTH_SHORT).show()
viewModel.shouldShowMessage=false
}
stop?.let {
fragmentListener?.requestArrivalsForStopID(it.ID)
}
if(stop == null){
Log.e(DEBUG_TAG,"Passed wrong stop")
}
if(fragmentListener == null){
Log.e(DEBUG_TAG, "Fragment listener is null")
}
}
override fun onLongPressOnStop(stop: Stop?): Boolean {
TODO("Not yet implemented")
}
}
private val patternsSorter = Comparator{ p1: MatoPatternWithStops, p2: MatoPatternWithStops ->
if(p1.pattern.directionId != p2.pattern.directionId)
return@Comparator p1.pattern.directionId - p2.pattern.directionId
else
return@Comparator -1*(p1.stopsIndices.size - p2.stopsIndices.size)
}
//map data
//style and sources are in GeneralMapLibreFragment
private lateinit var polylineSource: GeoJsonSource
private lateinit var polyArrowSource: GeoJsonSource
private var savedCameraPosition: CameraPosition? = null
private var lastStopsSizeShown = 0
//BUS POSITIONS
private var enablingPositionFromClick = false
private var polyline: LineString? = null
//private var stopPosList = ArrayList<GeoPoint>()
//fragment actions
private var showOnTopOfLine = false
private var recyclerInitDone = false
private var usingMQTTPositions = true
private var restoredCameraInMap = false
//position of live markers
private val tripMarkersAnimators = HashMap<String, ObjectAnimator>()
//extra items to use the LibreMap
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val args = requireArguments()
lineID = args.getString(LINEID_KEY,"")
stopIDFromToShow = args.getString(STOPID_FROM_KEY, "") //can be null
patternIdToShow = args.getString(PATTERN_SHOW_KEY, "")
}
@SuppressLint("SetTextI18n")
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
//reset statuses
//isBottomSheetShowing = false
//stopsLayerStarted = false
lastStopsSizeShown = 0
mapInitialized = false
val rootView = inflater.inflate(R.layout.fragment_lines_detail, container, false)
//lineID = requireArguments().getString(LINEID_KEY, "")
arguments?.let {
lineID = it.getString(LINEID_KEY, "")
stopIDFromToShow = it.getString(STOPID_FROM_KEY, "") //can be null
patternIdToShow = it.getString(PATTERN_SHOW_KEY, "")
Log.d(DEBUG_TAG, "LineID selected: $lineID, stopIDFromToShow: $stopIDFromToShow, patternIdToShow: $patternIdToShow")
}
switchButton = rootView.findViewById(R.id.switchImageButton)
locationIcon = rootView.findViewById(R.id.locationEnableIcon)
busPositionsIconButton = rootView.findViewById(R.id.busPositionsImageButton)
lineInfoButton = rootView.findViewById(R.id.lineInfoWarningButton)
favoritesButton = rootView.findViewById(R.id.favoritesButton)
stopsRecyclerView = rootView.findViewById(R.id.patternStopsRecyclerView)
descripTextView = rootView.findViewById(R.id.lineDescripTextView)
descripTextView.visibility = View.INVISIBLE
//map stuff
mapView = rootView.findViewById(R.id.lineMap)
mapView!!.getMapAsync(this)
// Setup close button
rootView.findViewById<View>(R.id.btnClose).setOnClickListener {
hideStopOrBusBottomSheet()
}
val titleTextView = rootView.findViewById<TextView>(R.id.titleTextView)
titleTextView.text = getString(R.string.line)+" "+ GtfsUtils.lineNameDisplayFromGtfsID(lineID)
favoritesButton?.isClickable = true
favoritesButton?.setOnClickListener {
if(lineID.isNotEmpty())
PreferencesHolder.addOrRemoveLineToFavorites(requireContext(),lineID,!isLineInFavorite)
}
val preferences = PreferencesHolder.getMainSharedPreferences(requireContext())
val favorites = preferences.getStringSet(PreferencesHolder.PREF_FAVORITE_LINES, HashSet())
if(favorites!=null && favorites.contains(lineID)){
favoritesButton?.setImageDrawable(ResourcesCompat.getDrawable(resources, R.drawable.ic_star_filled, null))
isLineInFavorite = true
}
appContext = requireContext().applicationContext
preferences.registerOnSharedPreferenceChangeListener(lineSharedPrefMonitor)
patternsSpinner = rootView.findViewById(R.id.patternsSpinner)
patternsAdapter = ArrayAdapter(requireContext(), android.R.layout.simple_spinner_dropdown_item, ArrayList<String>())
patternsSpinner.adapter = patternsAdapter
initializeRecyclerView()
switchButton.setOnClickListener{
if(mapView?.visibility == View.VISIBLE){
hideMapAndShowStopList()
} else{
hideStopListAndShowMap()
}
}
locationIcon?.let {view ->
//set click Listener
view.setOnClickListener(this::switchUserLocationStatus)
}
busPositionsIconButton.setOnClickListener {
LivePositionsDialogFragment().show(parentFragmentManager, "LivePositionsDialog")
}
//set
lineInfoButton.setOnClickListener {
AlertsDialogFragment.newInstanceForLine(lineID).show(parentFragmentManager, "Alerts-Line$lineID")
}
/*
*/
Log.d(DEBUG_TAG,"Data ${viewModel.stopsForPatternLiveData.value}")
//listeners
patternsSpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(p0: AdapterView<*>?, p1: View?, position: Int, p3: Long) {
val currentShownPattern = patternShown?.pattern
val patternWithStops = currentPatterns[position]
Log.d(DEBUG_TAG, "request stops for pattern ${patternWithStops.pattern.code}")
setPatternAndReqStops(patternWithStops)
if(mapView?.visibility == View.VISIBLE) {
//Clear buses if we are changing direction
currentShownPattern?.let { patt ->
if(patt.directionId != patternWithStops.pattern.directionId){
stopAnimations()
updatesByVehDict.clear()
updatePositionsIcons(true)
livePositionsViewModel.retriggerPositionUpdate()
}
if (shownStopInBottomSheet!=null){
//check if the stop is inside the new pattern
/*val s = shownStopInBottomSheet!!
val newPatternStops = patternWithStops.stopsIndices
val filterPStops = newPatternStops.filter { ps -> ps.stopGtfsId == "gtt:${s.ID}" }
if (filterPStops.isEmpty()){
hideStopOrBusBottomSheet()
}
*/
// do another thing, just close the stop when the pattern is changed
if (patt.code != patternWithStops.pattern.code){
hideStopOrBusBottomSheet()
}
}
}
}
livePositionsViewModel.setGtfsLineToFilterPos(lineID, patternWithStops.pattern)
}
override fun onNothingSelected(p0: AdapterView<*>?) {
}
}
Log.d(DEBUG_TAG, "Views created!")
observeStatusLivePositions()
return rootView
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
//reflect UI
//INITIALIZE VIEW MODELS
viewModel.setRouteIDQuery(lineID)
livePositionsViewModel.setGtfsLineToFilterPos(lineID, null)
//observe the change, clear buses when switching position
livePositionsViewModel.useMQTTPositionsLiveData.observe(viewLifecycleOwner){ useMQTT->
//Log.d(DEBUG_TAG, "Changed MQTT positions, now have to use MQTT: $useMQTT")
if (isResumed) {
//Log.d(DEBUG_TAG, "Deciding to switch, the current source is using MQTT: $usingMQTTPositions")
if(useMQTT!=usingMQTTPositions){
// we have to switch
val clearPos = PreferenceManager.getDefaultSharedPreferences(requireContext()).getBoolean("positions_clear_on_switch_pref", true)
livePositionsViewModel.clearOldPositionsUpdates()
if(useMQTT){
//switching to MQTT, the GTFS positions are disabled automatically
livePositionsViewModel.requestMatoPosUpdates(GtfsUtils.getLineNameFromGtfsID(lineID))
} else{
//switching to GTFS RT: stop Mato, launch first request
livePositionsViewModel.stopMatoUpdates()
livePositionsViewModel.requestGTFSUpdates()
}
Log.d(DEBUG_TAG, "Should clear positions: $clearPos")
if (clearPos) {
livePositionsViewModel.clearAllPositions()
//force clear of the viewed data
- if(vehShowing.isNotEmpty()) hideStopOrBusBottomSheet()
+ if(!vehShowing.isNullOrEmpty()) hideStopOrBusBottomSheet()
clearAllBusPositionsInMap()
}
}
}
usingMQTTPositions = useMQTT
}
val keySourcePositions = getString(R.string.pref_positions_source)
usingMQTTPositions = PreferenceManager.getDefaultSharedPreferences(requireContext())
.getString(keySourcePositions, "mqtt").contentEquals("mqtt")
viewModel.patternsWithStopsByRouteLiveData.observe(viewLifecycleOwner, this::savePatternsToShow)
/*
*/
viewModel.stopsForPatternLiveData.observe(viewLifecycleOwner) { stops ->
val pattern = viewModel.selectedPatternLiveData.value
if (pattern == null) {
Log.w(DEBUG_TAG, "The selectedPattern is null!")
return@observe
}
if(mapView?.visibility ==View.VISIBLE) {
// We have the pattern and the stops here, time to display them
//TODO: Decide if we should follow the camera view given by the previous screen (probably the map fragment)
// use !restoredCameraInMap to do so
// val shouldZoom = (shownStopInBottomSheet == null) //use this if we want to avoid zoom when we're keeping the stop open
displayPatternWithStopsOnMap(pattern, stops, true)
} else {
if(stopsRecyclerView.visibility==View.VISIBLE) {
patternShown = pattern
showStopsInRecyclerView(stops)
}
}
}
viewModel.gtfsRoute.observe(viewLifecycleOwner){route->
if(route == null){
//need to close the fragment
activity?.supportFragmentManager?.popBackStack()
return@observe
}
descripTextView.text = route.longName
descripTextView.visibility = View.VISIBLE
}
mapStateViewModel.locationUserActive.observe(viewLifecycleOwner) {
setLocationIconEnabled(it)
}
// enable info button if there are alerts on the line
alertsViewModel.setGtfsLineFilter(lineID)
alertsViewModel.alertsByRouteLiveData.observe(viewLifecycleOwner){ list ->
Log.d(DEBUG_TAG, "alerts for line $lineID: ${list.size}")
if(list.isNotEmpty()){
lineInfoButton.visibility = View.VISIBLE
//Log.d(DEBUG_TAG, "First alert is:\n ${list[0].longPrint()}")
} else
lineInfoButton.visibility = View.GONE
}
lineInfoButton.setOnClickListener {
AlertsDialogFragment.newInstanceForLine(lineID).show(parentFragmentManager, "Alerts-Line$lineID")
}
}
// ------------- UI switch stuff ---------
private fun hideMapAndShowStopList(){
mapView?.visibility = View.GONE
stopsRecyclerView.visibility = View.VISIBLE
locationIcon?.visibility = View.GONE
busPositionsIconButton?.visibility = View.GONE
viewModel.setMapShowing(false)
if(usingMQTTPositions) livePositionsViewModel.stopMatoUpdates()
//map.overlayManager.remove(busPositionsOverlay)
switchButton.setImageDrawable(AppCompatResources.getDrawable(requireContext(), R.drawable.ic_map_white_30))
hideStopOrBusBottomSheet()
if(locationComponent.isLocationComponentEnabled){
setLocationComponentEnabled(false)
shouldMapLocationBeReactivated = true
} else
shouldMapLocationBeReactivated = false
}
private fun hideStopListAndShowMap(){
stopsRecyclerView.visibility = View.GONE
mapView?.visibility = View.VISIBLE
locationIcon?.visibility = View.VISIBLE
busPositionsIconButton.visibility = View.VISIBLE
viewModel.setMapShowing(true)
//map.overlayManager.add(busPositionsOverlay)
//map.
if(usingMQTTPositions)
livePositionsViewModel.requestMatoPosUpdates(GtfsUtils.getLineNameFromGtfsID(lineID))
else
livePositionsViewModel.requestGTFSUpdates()
switchButton.setImageDrawable(AppCompatResources.getDrawable(requireContext(), R.drawable.ic_list_30))
if(shouldMapLocationBeReactivated){
setLocationComponentEnabled(Permissions.bothLocationPermissionsGranted(requireContext()))
}
}
override fun setLocationIconEnabled(enabled: Boolean){
if(enabled) {
locationIcon?.setImageDrawable(ContextCompat.getDrawable(requireContext(), R.drawable.location_circlew_red))
}
else {
locationIcon?.setImageDrawable(ContextCompat.getDrawable(
requireContext(),
R.drawable.location_circlew_grey
)
)
}
}
override fun onMapLocationEnabled(active: Boolean) {
//extra thing: show the toast
showToastLocation(active)
}
override fun onMapLocationComponentInitialized() {
//enable the position after the first fix
//onMapLocationEnabled(true)
}
@SuppressLint("MissingPermission")
override fun onFirstReceivedLocation(location: Location) {
if(mapInitialized){
val center = map!!.cameraPosition.target
val newPos = LatLng(location.latitude, location.longitude)
Log.d(DEBUG_TAG, "Center of the map : $center")
val newStatus = if(center==null || newPos.distanceTo(center) > 20*1000){
Log.d(DEBUG_TAG, "Distance from center of map to location: "+center?.distanceTo(newPos))
if(!shownToastNoPosition) context?.let{ c->
Toast.makeText(c, R.string.too_far_not_showing_location, Toast.LENGTH_LONG).show()
shownToastNoPosition = true
}
false
} else{
true
}
if(!newStatus) setLocationComponentEnabled(newStatus)
mapStateViewModel.locationUserActive.value = newStatus
}
}
// ------------- Map Code -------------------------
/**
* This method sets up the map and the layers
*/
override fun onMapReady(mapReady: MapLibreMap) {
this.map = mapReady
var setViewAlready = false
val context = requireContext()
val mjson = MapLibreStyles.getJsonStyleFromAsset(context, PreferencesHolder.getMapLibreStyleFile(context)) //ViewUtils.loadJsonFromAsset(requireContext(),"map_style_good.json")
activity?.run {
val builder = Style.Builder().fromJson(mjson!!)
mapReady.setStyle(builder) { style ->
addImagesStyle(style)
mapStyle = style
//setupLayers(style)
//checkInitMapLocation(mapReady, style,requireContext())
//if(!stopsLayerStarted)
initPolylineStopsLayers(style, null)
setupBusLayer(style)
initSymbolManager(mapReady, style)
toRunWhenMapReady?.run()
toRunWhenMapReady = null
mapInitialized = true
if(patternShown!=null){
viewModel.stopsForPatternLiveData.value?.let {
Log.d(DEBUG_TAG, "Show stops from the cache")
displayPatternWithStopsOnMap(patternShown!!, it, true)
//Show stop from cache
mapStateViewModel.lastOpenStopID.value?.let{ sID->
val s= it.filter { stop -> stop.ID==sID }
if (s.isEmpty()) {
if(sID.isNotEmpty())
Log.w(DEBUG_TAG,"Wanted to open stop $sID in map but it was not loaded!")
}
else openStopInBottomSheet(s[0])
}
}
}
var restoredMapState = mapStateViewModel.restoreMapState(mapReady)
arguments?.let { args ->
// if there is a Camera State in the arguments, set it for the new camera (doesn't work yet!)
if (!restoredMapState && MapCameraState.checkInBundle(args)) {
val initCamState = MapCameraState.fromBundle(args)
//map?.let{
MapStateViewModel.restoreMapState(mapReady, initCamState)
setViewAlready = true
restoredMapState = true
}
}
restoredCameraInMap = restoredMapState
}
mapReady.addOnMapClickListener { point ->
val screenPoint = mapReady.projection.toScreenLocation(point)
val stopsNearby = mapReady.queryRenderedFeatures(screenPoint, STOPS_LAYER_ID)
val busNearby = mapReady.queryRenderedFeatures(screenPoint, BUSES_LAYER_ID)
//Log.d(DEBUG_TAG, "onMapClick, stopsNearby: $stopsNearby \nstopShown: $shownStopInBottomSheet \nbusNearby: $busNearby,")
if (stopsNearby.isNotEmpty()) {
val feature = stopsNearby[0]
val id = feature.getStringProperty("id")
val stop = viewModel.getStopByID(id)
stop?.let {
- if (isBottomSheetShowing() || vehShowing.isNotEmpty()) {
+ if (isBottomSheetShowing()) {
hideStopOrBusBottomSheet()
}
openStopInBottomSheet(it)
//move camera
if(it.latitude!=null && it.longitude!=null)
mapReady.animateCamera(CameraUpdateFactory.newLatLng(LatLng(it.latitude!!,it.longitude!!)),750)
}
return@addOnMapClickListener true
} else if (busNearby.isNotEmpty()){
val feature = busNearby[0]
openBusFromMapClick(feature)
return@addOnMapClickListener true
}
false
}
// we start requesting the bus positions now
observeBusPositionUpdates()
}
val zoom = 12.0
val latlngTarget = LatLng(MapLibreFragment.DEFAULT_CENTER_LAT, MapLibreFragment.DEFAULT_CENTER_LON)
if(!setViewAlready)
mapReady.cameraPosition = savedCameraPosition ?:CameraPosition.Builder().target(latlngTarget).zoom(zoom).build()
savedCameraPosition = null
if(shouldMapLocationBeReactivated)
mapReady.style?.let{ checkInitMapLocation(mapReady,it, context)}
}
override fun showOpenStopWithSymbolLayer(): Boolean {
return true
}
/**
* Separate function to find the vehicle associated with a feature and display it
*/
private fun openBusFromMapClick(feature: Feature){
val vehid = feature.getStringProperty("veh")
if(isBottomSheetShowing())
hideStopOrBusBottomSheet()
showVehicleTripInBottomSheet(vehid)
updatesByVehDict[vehid]?.let {
map?.animateCamera(
CameraUpdateFactory.newLatLng(LatLng(it.posUpdate.latitude, it.posUpdate.longitude)),
750
)
}
}
private fun observeBusPositionUpdates(){
//live bus positions
livePositionsViewModel.filteredLocationUpdates.observe(viewLifecycleOwner){ pair ->
//Log.d(DEBUG_TAG, "Received ${updates.size} updates for the positions")
val updates = pair.first
val vehiclesNotOnCorrectDir = pair.second
if(mapView?.visibility == View.GONE || patternShown ==null){
//DO NOTHING
Log.w(DEBUG_TAG, "not doing anything because map is not visible")
return@observe
}
//remove vehicles not on this direction
removeVehiclesData(vehiclesNotOnCorrectDir)
updateBusPositionsInMap(updates, hasVehicleTracking = true) { veh->
showVehicleTripInBottomSheet(veh)
}
//if not using MQTT positions
if(!usingMQTTPositions){
livePositionsViewModel.requestDelayedGTFSUpdates(2000)
}
}
//download missing tripIDs
livePositionsViewModel.tripsGtfsIDsToQuery.observe(viewLifecycleOwner){
//gtfsPosViewModel.downloadTripsFromMato(dat);
MatoTripsDownloadWorker.requestMatoTripsDownload(
it, requireContext().applicationContext,
"BusTO-MatoTripDownload"
)
}
}
private fun showVehicleTripInBottomSheet(veh: String) {
super.showVehicleTripInBottomSheet(veh) { patternCode, veh ->
//this is checked in @GeneralMapLibreFragment
//val data = updatesByVehDict[veh] ?: return@showVehicleTripInBottomSheet
if (patternCode.isEmpty()) return@showVehicleTripInBottomSheet
if (patternShown?.pattern?.code == patternCode) {
//center view on vehicle
updatesByVehDict[veh]?.let { up->
map?.let{
/*
val c = it.cameraPosition
it.moveCamera(CameraUpdateFactory.CameraPositionUpdate(c.bearing,
LatLng(up.posUpdate.latitude, up.posUpdate.longitude),
c.tilt,c.zoom, c.padding)
)
*/
it.animateCamera(CameraUpdateFactory.newLatLng(LatLng(up.posUpdate.latitude, up.posUpdate.longitude)))
}
} ?: {
Toast.makeText(context, R.string.showing_same_direction, Toast.LENGTH_SHORT).show()
}
} else {
showPatternWithCode(patternCode)
}
}
}
// ------- MAP LAYERS INITIALIZE ----
/**
* Initialize the map layers for the stops
*/
private fun initPolylineStopsLayers(style: Style, arrowFeatures: FeatureCollection?){
Log.d(DEBUG_TAG, "INIT STOPS CALLED")
stopsSource = GeoJsonSource(STOPS_SOURCE_ID)
//val context = requireContext()
val stopIcon = ResourcesCompat.getDrawable(resources,R.drawable.ball, activity?.theme)!!
val imgStop = ResourcesCompat.getDrawable(resources,R.drawable.bus_stop_new, activity?.theme)!!
val polyIconArrow = ResourcesCompat.getDrawable(resources, R.drawable.arrow_up_box_fill, activity?.theme)!!
//set the image tint
//DrawableCompat.setTint(imgBus,ContextCompat.getColor(context,R.color.line_drawn_poly))
// add icons
style.addImage(STOP_IMAGE_ID,stopIcon)
style.addImage(POLY_ARROW, polyIconArrow)
style.addImage(STOP_ACTIVE_IMG, ResourcesCompat.getDrawable(resources, R.drawable.bus_stop_new_highlight, activity?.theme)!!)
polylineSource = GeoJsonSource(POLYLINE_SOURCE) //lineFeature?.let { GeoJsonSource(POLYLINE_SOURCE, it) } ?: GeoJsonSource(POLYLINE_SOURCE)
style.addSource(polylineSource)
val color=ContextCompat.getColor(requireContext(),R.color.line_drawn_poly)
//paint.style = Paint.Style.FILL_AND_STROKE
//paint.strokeJoin = Paint.Join.ROUND
//paint.strokeCap = Paint.Cap.ROUND
val lineLayer = LineLayer(POLYLINE_LAYER, POLYLINE_SOURCE).withProperties(
PropertyFactory.lineColor(color),
PropertyFactory.lineWidth(5.0f), //originally 13f
PropertyFactory.lineOpacity(1.0f),
PropertyFactory.lineJoin(Property.LINE_JOIN_ROUND),
PropertyFactory.lineCap(Property.LINE_CAP_ROUND)
)
polyArrowSource = GeoJsonSource(POLY_ARROWS_SOURCE, arrowFeatures)
style.addSource(polyArrowSource)
val arrowsLayer = SymbolLayer(POLY_ARROWS_LAYER, POLY_ARROWS_SOURCE).withProperties(
PropertyFactory.iconImage(POLY_ARROW),
PropertyFactory.iconRotate(Expression.get("bearing")),
PropertyFactory.iconRotationAlignment(ICON_ROTATION_ALIGNMENT_MAP)
)
val layers = style.layers
val lastLayers = layers.filter { l-> l.id.contains("city") }
//Log.d(DEBUG_TAG,"Layers:\n ${style.layers.map { l -> l.id }}")
Log.d(DEBUG_TAG, "City layers: ${lastLayers.map { l-> l.id }}")
if(lastLayers.isNotEmpty())
style.addLayerAbove(lineLayer,lastLayers[0].id)
else
style.addLayerBelow(lineLayer,"label_country_1")
//style.addLayerAbove(stopsLayer, POLYLINE_LAYER)
style.addLayerAbove(arrowsLayer, POLYLINE_LAYER)
stopsLayerStarted = true
initStopsLayer(style, null, POLY_ARROWS_LAYER)
}
private fun filterPatternFromArgs(patterns: List<MatoPatternWithStops>): MatoPatternWithStops?{
var p: MatoPatternWithStops? = null
if (patternIdToShow.isNotEmpty()){
for (patt in patterns) {
if (patt.pattern.code == patternIdToShow){
p = patt
}
}
if(p==null)
Log.w(DEBUG_TAG, "We had to show the pattern with code $patternIdToShow, but we didn't find it")
else
Log.d(DEBUG_TAG, "Requesting to show pattern with code $patternIdToShow, found pattern ${p.pattern.code}")
}
// if we are loading from a stop, find it
else if(stopIDFromToShow.isNotEmpty()) {
val stopGtfsID = "gtt:$stopIDFromToShow"
var pLength = 0
for (patt in patterns) {
for (pstop in patt.stopsIndices) {
if (pstop.stopGtfsId == stopGtfsID) {
//found
if (patt.stopsIndices.size > pLength) {
p = patt
pLength = patt.stopsIndices.size
}
//break here, we have determined this pattern has the stop we're looking for
break
}
}
}
if(p==null)
Log.w(DEBUG_TAG, "We had to show the pattern from stop $stopIDFromToShow, but we didn't find it")
else
Log.d(DEBUG_TAG, "Requesting to show pattern from stop $stopIDFromToShow, found pattern ${p.pattern.code}")
}
// the flag of showing pattern is not necessary anymore, we have set the pattern
patternIdToShow = ""
// the flag of selecting from stop needs to be used again when displaying the pattern
return p
}
/**
* Save the loaded pattern data, without the stops!
*/
private fun savePatternsToShow(patterns: List<MatoPatternWithStops>){
currentPatterns = patterns.sortedWith(patternsSorter)
patternsAdapter?.let {
it.clear()
it.addAll(currentPatterns.map { p->"${p.pattern.directionId} - ${p.pattern.headsign}" })
it.notifyDataSetChanged()
}
val patternToShow = filterPatternFromArgs(currentPatterns)
if(patternToShow!=null) {
//showPattern(patternToShow)
patternShown = patternToShow
}
patternShown?.let {
showPattern(it)
}
}
/**
* Called when the position of the spinner is updated
*/
private fun setPatternAndReqStops(patternWithStops: MatoPatternWithStops){
Log.d(DEBUG_TAG, "Requesting stops for pattern ${patternWithStops.pattern.code}")
viewModel.selectedPatternLiveData.value = patternWithStops
viewModel.currentPatternStops.value = patternWithStops.stopsIndices.sortedBy { i-> i.order }
viewModel.requestStopsForPatternWithStops(patternWithStops)
}
private fun showPattern(patternWs: MatoPatternWithStops){
//Log.d(DEBUG_TAG, "Finding pattern to show: ${patternWs.pattern.code}")
var pos = -2
val code = patternWs.pattern.code.trim()
for (k in currentPatterns.indices) {
if (currentPatterns[k].pattern.code.trim() == code) {
pos = k
break
}
}
Log.d(DEBUG_TAG, "Requesting stops fro pattern $code in position: $pos")
// this triggers the showing on the map / recyclerview
if (pos !=-2)
patternsSpinner.setSelection(pos)
else
Log.e(DEBUG_TAG, "Pattern with code $code not found!!")
}
/**
* Zoom on the map to get the pattern
*/
private fun zoomToCurrentPattern(){
if(polyline==null) return
val NULL_VALUE = -4000.0
var maxLat = NULL_VALUE
var minLat = NULL_VALUE
var minLong = NULL_VALUE
var maxLong = NULL_VALUE
polyline?.let {
for(p in it.coordinates()){
val lat = p.latitude()
val lon = p.longitude()
// get max latitude
if(maxLat == NULL_VALUE)
maxLat =lat
else if (maxLat < lat) maxLat = lat
// find min latitude
if (minLat ==NULL_VALUE)
minLat = lat
else if (minLat > lat) minLat = lat
if(maxLong == NULL_VALUE || maxLong < lon )
maxLong = lon
if (minLong == NULL_VALUE || minLong > lon)
minLong = lon
}
val padding = 50 // Pixel di padding intorno ai limiti
Log.d(DEBUG_TAG, "Setting limits of bounding box of line: $minLat -> $maxLat, $minLong -> $maxLong")
val bbox = LatLngBounds.from(maxLat,maxLong, minLat, minLong)
//map.zoomToBoundingBox(BoundingBox(maxLat+del, maxLong+del, minLat-del, minLong-del), false)
map?.animateCamera(CameraUpdateFactory.newLatLngBounds(bbox, padding))
}
}
private fun displayPatternWithStopsOnMap(patternWs: MatoPatternWithStops, stopsToSort: List<Stop>, zoomToPattern: Boolean){
if(!mapInitialized){
//set the runnable and do nothing else
Log.d(DEBUG_TAG, "Delaying pattern display to when map is Ready: ${patternWs.pattern.code}")
toRunWhenMapReady = Runnable {
displayPatternWithStopsOnMap(patternWs, stopsToSort, zoomToPattern)
}
return
}
Log.d(DEBUG_TAG, "Got the stops: ${stopsToSort.map { s->s.gtfsID }}}")
patternShown = patternWs
//Problem: stops are not sorted
val stopOrderD = patternWs.stopsIndices.withIndex().associate{it.value.stopGtfsId to it.index}
val stopsSorted = stopsToSort.sortedBy { s-> stopOrderD[s.gtfsID] }
val pattern = patternWs.pattern
val pointsList = PolylineParser.decodePolyline(pattern.patternGeometryPoly, pattern.patternGeometryLength)
val pointsToShow = pointsList.map { Point.fromLngLat(it.longitude, it.latitude) }
Log.d(DEBUG_TAG, "The polyline has ${pointsToShow.size} points to display")
polyline = LineString.fromLngLats(pointsToShow)
val lineFeature = Feature.fromGeometry(polyline)
//Log.d(DEBUG_TAG, "Polyline in JSON is: ${lineFeature.toJson()}")
// --- STOPS---
val features = ArrayList<Feature>()
for (s in stopsSorted){
if (s.latitude!=null && s.longitude!=null) {
val loc = if (showOnTopOfLine) findOptimalPosition(s, pointsList)
else LatLng(s.latitude!!, s.longitude!!)
features.add(
Feature.fromGeometry(
Point.fromLngLat(loc.longitude, loc.latitude),
JsonObject().apply {
addProperty("id", s.ID)
addProperty("name", s.stopDefaultName)
//addProperty("routes", s.routesThatStopHereToString()) // Add routes array to JSON object
}
)
)
}
}
// -- ARROWS --
//val splitPolyline = MapLibreUtils.splitPolyWhenDistanceTooBig(pointsList, 200.0)
val arrowFeatures = ArrayList<Feature>()
val pointsIndexToShowIcon = MapLibreUtils.findPointsToPutDirectionMarkers(pointsList, stopsSorted, 750.0)
for (idx in pointsIndexToShowIcon){
val pnow = pointsList[idx]
val otherp = if(idx>1) pointsList[idx-1] else pointsList[idx+1]
val bearing = if (idx>1) MapLibreUtils.getBearing(pointsList[idx-1], pnow) else MapLibreUtils.getBearing(pnow, pointsList[idx+1])
arrowFeatures.add(Feature.fromGeometry(
Point.fromLngLat((pnow.longitude+otherp.longitude)/2, (pnow.latitude+otherp.latitude)/2 ), //average
JsonObject().apply {
addProperty("bearing", bearing)
}
))
}
Log.d(DEBUG_TAG,"Have put ${features.size} stops to display")
// if the layer is already started, substitute the stops inside, otherwise start it
if (stopsLayerStarted) {
stopsSource.setGeoJson(FeatureCollection.fromFeatures(features))
polylineSource.setGeoJson(lineFeature)
polyArrowSource.setGeoJson(FeatureCollection.fromFeatures(arrowFeatures))
lastStopsSizeShown = features.size
} else
map?.let {
Log.d(DEBUG_TAG, "Map stop layer is not started yet, init layer")
initPolylineStopsLayers(mapStyle, FeatureCollection.fromFeatures(arrowFeatures))
Log.d(DEBUG_TAG,"Started stops layer on map")
lastStopsSizeShown = features.size
stopsLayerStarted = true
} ?:{
Log.e(DEBUG_TAG, "Stops layer is not started!!")
}
var reallyZoomToPattern = zoomToPattern
if(stopIDFromToShow.isNotEmpty()){
//open the stop
val stopfilt = stopsSorted.filter { s -> s.ID == stopIDFromToShow }
if (stopfilt.isEmpty()){
Log.e(DEBUG_TAG, "Tried to show stop but it's not in the selected pattern")
} else{
val stop = stopfilt[0]
openStopInBottomSheet(stop)
if(stop.hasCoords()) {
reallyZoomToPattern = false
setCameraPosition(stop.latitude!!, stop.longitude!!, 13.5)
}
}
// Reset this to avoid checking again when showing
stopIDFromToShow = ""
//camera set
}
if(reallyZoomToPattern) zoomToCurrentPattern()
}
private fun initializeRecyclerView(){
val llManager = LinearLayoutManager(context)
llManager.orientation = LinearLayoutManager.VERTICAL
stopsRecyclerView.layoutManager = llManager
}
private fun showStopsInRecyclerView(stops: List<Stop>){
Log.d(DEBUG_TAG, "Setting stops from: "+viewModel.currentPatternStops.value)
val orderBy = viewModel.currentPatternStops.value!!.withIndex().associate{it.value.stopGtfsId to it.index}
val stopsSorted = stops.sortedBy { s -> orderBy[s.gtfsID] }
val numStops = stopsSorted.size
Log.d(DEBUG_TAG, "RecyclerView adapter is: ${stopsRecyclerView.adapter}")
val setNewAdapter = true
if(setNewAdapter){
stopsRecyclerView.adapter = StopRecyclerAdapter(
stopsSorted, stopAdapterListener, StopRecyclerAdapter.Use.LINES,
NameCapitalize.FIRST
)
}
}
/**
* This method fixes the display of the pattern, to be used when clicking on a bus
*/
private fun showPatternWithCode(patternId: String){
//var index = 0
Log.d(DEBUG_TAG, "Showing pattern with code $patternId ")
for (i in currentPatterns.indices){
val pattStop = currentPatterns[i]
if(pattStop.pattern.code == patternId){
Log.d(DEBUG_TAG, "Pattern found in position $i")
//setPatternAndReqStops(pattStop)
patternsSpinner.setSelection(i)
break
}
}
}
override fun onResume() {
super.onResume()
Log.d(DEBUG_TAG, "Resetting paused from onResume")
pausedFragment = false
val keySourcePositions = getString(R.string.pref_positions_source)
usingMQTTPositions = PreferenceManager.getDefaultSharedPreferences(requireContext())
.getString(keySourcePositions, "mqtt").contentEquals("mqtt")
//separate paths
if(usingMQTTPositions)
livePositionsViewModel.requestMatoPosUpdates(GtfsUtils.getLineNameFromGtfsID(lineID))
else
livePositionsViewModel.requestGTFSUpdates()
//initialize GUI here
fragmentListener?.readyGUIfor(FragmentKind.LINES)
}
override fun onPause() {
super.onPause()
if(usingMQTTPositions) livePositionsViewModel.stopMatoUpdates()
pausedFragment = true
//save map
map?.let{
//if map is initialized
mapStateViewModel.saveMapState(it)
}
mapStateViewModel.lastOpenStopID.postValue(shownStopInBottomSheet?.ID)
}
override fun onStop() {
super.onStop()
if(locationInitialized)
shouldMapLocationBeReactivated = locationComponent.isLocationComponentEnabled
else
shouldMapLocationBeReactivated = false
}
override fun onDestroyView() {
map?.run {
Log.d(DEBUG_TAG, "Saving camera position")
savedCameraPosition = cameraPosition
}
super.onDestroyView()
Log.d(DEBUG_TAG, "Destroying the views")
/*mapStyle.removeLayer(STOPS_LAYER_ID)
mapStyle?.removeSource(STOPS_SOURCE_ID)
mapStyle.removeLayer(POLYLINE_LAYER)
mapStyle.removeSource(POLYLINE_SOURCE)
*/
//stopsLayerStarted = false
}
override fun onMapDestroy() {
mapStyle.removeLayer(STOPS_LAYER_ID)
mapStyle.removeSource(STOPS_SOURCE_ID)
mapStyle.removeLayer(POLYLINE_LAYER)
mapStyle.removeSource(POLYLINE_SOURCE)
mapStyle.removeLayer(BUSES_LAYER_ID)
mapStyle.removeSource(BUSES_SOURCE_ID)
//map?.locationComponent?.isLocationComponentEnabled = false
setLocationComponentEnabled(false)
}
override fun getBaseViewForSnackBar(): View? {
return null
}
companion object {
private const val LINEID_KEY="lineID"
private const val STOPID_FROM_KEY="stopID"
private const val PATTERN_SHOW_KEY ="patternIDShow"
private const val DEBUG_TAG="BusTO-LineDetalFragment"
fun makeArgs(lineID: String, stopIDFrom: String?): Bundle{
val b = Bundle()
b.putString(LINEID_KEY, lineID)
b.putString(STOPID_FROM_KEY, stopIDFrom)
return b
}
fun makeArgsPattern(lineID: String, patternShow: String?, extraArgs: Bundle?): Bundle {
val b= extraArgs ?: Bundle()
b.putString(LINEID_KEY, lineID)
b.putString(PATTERN_SHOW_KEY, patternShow)
return b
}
fun newInstance(lineID: String?, stopIDFrom: String?) = LinesDetailFragment().apply {
lineID?.let { arguments = makeArgs(it, stopIDFrom) }
}
@JvmStatic
private fun findOptimalPosition(stop: Stop, pointsList: MutableList<LatLng>): LatLng{
if(stop.latitude==null || stop.longitude ==null|| pointsList.isEmpty())
throw IllegalArgumentException()
val sLat = stop.latitude!!
val sLong = stop.longitude!!
if(pointsList.size < 2)
return pointsList[0]
pointsList.sortBy { utils.measuredistanceBetween(sLat, sLong, it.latitude, it.longitude) }
val p1 = pointsList[0]
val p2 = pointsList[1]
if (p1.longitude == p2.longitude){
//Log.e(DEBUG_TAG, "Same longitude")
return LatLng(sLat, p1.longitude)
} else if (p1.latitude == p2.latitude){
//Log.d(DEBUG_TAG, "Same latitude")
return LatLng(p2.latitude,sLong)
}
val m = (p1.latitude - p2.latitude) / (p1.longitude - p2.longitude)
val minv = (p1.longitude-p2.longitude)/(p1.latitude - p2.latitude)
val cR = p1.latitude - p1.longitude * m
val longNew = (minv * sLong + sLat -cR ) / (m+minv)
val latNew = (m*longNew + cR)
//Log.d(DEBUG_TAG,"Stop ${stop.ID} old pos: ($sLat, $sLong), new pos ($latNew,$longNew)")
return LatLng(latNew,longNew)
}
private const val DEFAULT_CENTER_LAT = 45.12
private const val DEFAULT_CENTER_LON = 7.6858
}
}
\ No newline at end of file
diff --git a/app/src/main/java/it/reyboz/bustorino/fragments/MapLibreFragment.kt b/app/src/main/java/it/reyboz/bustorino/fragments/MapLibreFragment.kt
index 9b01873..6309490 100644
--- a/app/src/main/java/it/reyboz/bustorino/fragments/MapLibreFragment.kt
+++ b/app/src/main/java/it/reyboz/bustorino/fragments/MapLibreFragment.kt
@@ -1,748 +1,756 @@
/*
BusTO - Fragments components
Copyright (C) 2025 Fabio Mazza
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package it.reyboz.bustorino.fragments
import android.annotation.SuppressLint
import android.content.Context
import android.location.Location
import android.location.LocationManager
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageButton
import android.widget.RelativeLayout
import android.widget.Toast
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.preference.PreferenceManager
import androidx.room.concurrent.AtomicBoolean
import com.google.android.material.bottomsheet.BottomSheetBehavior
import it.reyboz.bustorino.R
import it.reyboz.bustorino.backend.Stop
import it.reyboz.bustorino.backend.gtfs.LivePositionUpdate
import it.reyboz.bustorino.backend.mato.MQTTMatoClient
import it.reyboz.bustorino.data.PreferencesHolder
import it.reyboz.bustorino.data.gtfs.TripAndPatternWithStops
import it.reyboz.bustorino.map.MapLibreLocationEngine
import it.reyboz.bustorino.map.MapLibreStyles
import it.reyboz.bustorino.viewmodels.StopsMapViewModel
import org.maplibre.android.camera.CameraPosition
import org.maplibre.android.camera.CameraUpdateFactory
import org.maplibre.android.geometry.LatLng
import org.maplibre.android.geometry.LatLngBounds
import org.maplibre.android.location.engine.LocationEngineCallback
import org.maplibre.android.location.engine.LocationEngineResult
import org.maplibre.android.location.modes.CameraMode
import org.maplibre.android.location.modes.RenderMode
import org.maplibre.android.maps.MapLibreMap
import org.maplibre.android.maps.Style
import org.maplibre.android.plugins.annotation.Symbol
import org.maplibre.geojson.Feature
import org.maplibre.geojson.FeatureCollection
/**
* A simple [Fragment] subclass.
* Use the [MapLibreFragment.newInstance] factory method to
* create an instance of this fragment.
*/
class MapLibreFragment : GeneralMapLibreFragment() {
private val stopsViewModel: StopsMapViewModel by viewModels()
private var stopsShowing = ArrayList<Stop>(0)
// Sources for stops and buses are in GeneralMapLibreFragment
private var isUserMovingCamera = false
private var lastStopsSizeShown = 0
private var lastBBox = LatLngBounds.from(2.0, 2.0, 1.0,1.0)
private var stopsRedrawnTimes = 0
//bottom Sheet behavior in GeneralMapLibreFragment
//private var stopActiveSymbol: Symbol? = null
// Location stuff
private lateinit var locationManager: LocationManager
private lateinit var userLocationButton: ImageButton
private lateinit var centerUserButton: ImageButton
private lateinit var followUserButton: ImageButton
private var followingUserLocation = false
private var ignoreCameraMovementForFollowing = true
private var restoredMapCamera = AtomicBoolean()
//BUS POSITIONS
private var usingMQTTPositions = true // THIS IS INSIDE VIEW MODEL NOW
private val symbolsToUpdate = ArrayList<Symbol>()
private var initialStopToShow : Stop? = null
private var initialStopShown = false
private var waitingDelayedBusUpdate = false
//shown stuff
//private var savedStateOnStop : Bundle? = null
private val showBusLayer = true
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
initialStopToShow = Stop.fromBundle(arguments)
if (initialStopToShow==null){
} else if(!initialStopToShow!!.hasCoords()){
//null the stop if it doesn't have coordinates, we cannot find it
initialStopToShow = null
}
}
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
val rootView = inflater.inflate(R.layout.fragment_map_libre,
container, false)
//reset the counter
lastStopsSizeShown = 0
stopsRedrawnTimes = 0
stopsLayerStarted = false
symbolsToUpdate.clear()
// Init layout view
// Init the MapView
mapView = rootView.findViewById(R.id.libreMapView)
mapView!!.onCreate(savedInstanceState)
mapView!!.getMapAsync(this)
//init bottom sheet
initBottomSheet(rootView)
userLocationButton = rootView.findViewById(R.id.locationEnableIcon)
userLocationButton.setOnClickListener(this::switchUserLocationStatus)
followUserButton = rootView.findViewById(R.id.followUserImageButton)
centerUserButton = rootView.findViewById(R.id.centerMapImageButton)
busPositionsIconButton = rootView.findViewById(R.id.busPositionsImageButton)
busPositionsIconButton.setOnClickListener {
LivePositionsDialogFragment().show(parentFragmentManager, "LivePositionsDialog")
}
arrivalsCard.setOnClickListener {
if(context!=null){
Toast.makeText(context,"ARRIVALS", Toast.LENGTH_SHORT).show()
}
}
centerUserButton.setOnClickListener {
if(context!=null && locationComponent.isLocationComponentEnabled) {
val location = locationComponent.lastKnownLocation
location?.let {
mapView?.getMapAsync { map ->
map.animateCamera(CameraUpdateFactory.newCameraPosition(
CameraPosition.Builder().target(LatLng(location.latitude, location.longitude)).build()), 500)
}
}
}
}
followUserButton.setOnClickListener {
// onClick user following button
if(context!=null && locationInitialized && locationComponent.isLocationComponentEnabled){
// CameraMode.TRACKING makes the camera move and jump to the location
setFollowUserLocation(!followingUserLocation)
}
}
//locationManager = requireActivity().getSystemService(Context.LOCATION_SERVICE) as LocationManager
/*
if (Permissions.bothLocationPermissionsGranted(requireContext()) && deviceHasGpsProvider()) {
requestInitialUserLocation()
} else{
if (shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_FINE_LOCATION)) {
//TODO: show dialog for permission rationale
Toast.makeText(activity, R.string.enable_position_message_map, Toast.LENGTH_SHORT)
.show()
}
// PERMISSIONS REQUESTED AFTER MAP SETUP
}
*/
// Setup close button
rootView.findViewById<View>(R.id.btnClose).setOnClickListener {
hideStopOrBusBottomSheet()
}
Log.d(DEBUG_TAG, "Fragment View Created!")
//TODO: Reshow last open stop when switching back to the map fragment
return rootView
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
//observe change in source of the live positions
livePositionsViewModel.useMQTTPositionsLiveData.observe(viewLifecycleOwner){ useMQTT->
//Log.d(DEBUG_TAG, "Changed MQTT positions, now have to use MQTT: $useMQTT")
if (showBusLayer && isResumed) {
//Log.d(DEBUG_TAG, "Deciding to switch, the current source is using MQTT: $usingMQTTPositions")
if(useMQTT!=usingMQTTPositions){
// we have to switch
val clearPos = PreferenceManager.getDefaultSharedPreferences(requireContext()).getBoolean("positions_clear_on_switch_pref", true)
livePositionsViewModel.clearOldPositionsUpdates()
if(useMQTT){
//switching to MQTT, the GTFS positions are disabled automatically
livePositionsViewModel.requestMatoPosUpdates(MQTTMatoClient.LINES_ALL)
} else{
//switching to GTFS RT: stop Mato, launch first request
livePositionsViewModel.stopMatoUpdates()
livePositionsViewModel.requestGTFSUpdates()
}
Log.d(DEBUG_TAG, "Should clear positions: $clearPos")
if (clearPos) {
livePositionsViewModel.clearAllPositions()
//force clear of the viewed data
- if(vehShowing.isNotEmpty()) hideStopOrBusBottomSheet()
+ if(!vehShowing.isNullOrEmpty()) hideStopOrBusBottomSheet()
clearAllBusPositionsInMap()
}
}
}
usingMQTTPositions = useMQTT
}
mapStateViewModel.locationUserActive.observe(viewLifecycleOwner){
setLocationIconEnabled(it)}
mapStateViewModel.followingUserPosition.observe(viewLifecycleOwner){ updateFollowingIcon(it)}
observeStatusLivePositions()
}
/**
* This method sets up the map and the layers
*/
override fun onMapReady(mapReady: MapLibreMap) {
this.map = mapReady
val context = requireContext()
val mjson = MapLibreStyles.getJsonStyleFromAsset(context, PreferencesHolder.getMapLibreStyleFile(context))
val builder = Style.Builder().fromJson(mjson!!)
mapReady.setStyle(builder) { style ->
mapStyle = style
//setupLayers(style)
addImagesStyle(style)
//init stop layer with this
val stopsInCache = stopsViewModel.stopsToShow.value
if(stopsInCache.isNullOrEmpty())
initStopsLayer(style, null)
else
displayStops(stopsInCache)
if(showBusLayer) setupBusLayer(style, withLabels = true, busIconsScale = 1.2f)
// Start observing data now that everything is set up
observeStops()
checkInitMapLocation(mapReady,style, context)
}
mapReady.addOnCameraIdleListener {
map?.let {
val newBbox = it.projection.visibleRegion.latLngBounds
stopsViewModel.loadStopsInLatLngBounds(newBbox)
lastBBox = newBbox
}
}
mapReady.addOnCameraMoveStartedListener { v->
if(v== MapLibreMap.OnCameraMoveStartedListener.REASON_API_GESTURE){
//the user is moving the map
//isUserMovingCamera = true
updateFollowingIcon(false)
}
}
mapReady.addOnMapClickListener { point ->
onMapClickReact(point)
}
// we start requesting the bus positions now
observeBusPositionUpdates()
//Restoring data
if (initialStopToShow!=null && initialStopToShow?.hasCoords() == true){
val s = initialStopToShow!!
if(s.hasCoords()){
mapReady.cameraPosition = CameraPosition.Builder().target(
LatLng(s.latitude!!, s.longitude!!)
).zoom(DEFAULT_ZOOM).build()
}
restoredMapCamera.set(true)
} else{
var boundsRestored = false
//restore the map state here
map?.let{
boundsRestored = mapStateViewModel.restoreMapState(it)
mapStateViewModel.lastOpenStopID.value?.let{ sID->
val s= stopsViewModel.getStopByID(sID)
if (s==null) {
if(sID.isNotEmpty())
Log.w(DEBUG_TAG,"Wanted to open stop $sID in map but it was not loaded!")
}
else{
openStopInBottomSheet(s) }
}
}
if(!boundsRestored){
// we have not restored the bounds, open normally in target location
// TODO: check that the map is reopened in the same location
val lastLoc = mapStateViewModel.locationToShow
val defaultLoc = LatLng(DEFAULT_CENTER_LAT, DEFAULT_CENTER_LON)
val proposedLoc = lastLoc?.let{ LatLng(lastLoc.latitude, lastLoc.longitude)}
val targetLoc = if(proposedLoc == null || proposedLoc.distanceTo(defaultLoc) > MAX_DIST_KM*1000)
defaultLoc
else proposedLoc
mapReady.cameraPosition = CameraPosition.Builder().target(targetLoc).zoom(DEFAULT_ZOOM).build()
}
restoredMapCamera.set(boundsRestored)
}
mapInitialized = true
//pendingLocationActivation = true
//positionRequestLauncher.launch(Permissions.LOCATION_PERMISSIONS)
}
private fun onMapClickReact(point: LatLng): Boolean{
map?.let { mapReady ->
val screenPoint = mapReady.projection.toScreenLocation(point)
val stopsFeatures = mapReady.queryRenderedFeatures(screenPoint, STOPS_LAYER_ID)
val busNearby = mapReady.queryRenderedFeatures(screenPoint, BUSES_LAYER_ID)
Log.d(DEBUG_TAG, "Clicked on stops: $stopsFeatures \n and buses: $busNearby")
if (stopsFeatures.isNotEmpty()) {
val feature = stopsFeatures[0]
val id = feature.getStringProperty("id")
val name = feature.getStringProperty("name")
//Toast.makeText(requireContext(), "Clicked on $name ($id)", Toast.LENGTH_SHORT).show()
val stop = stopsViewModel.getStopByID(id)
Log.d(DEBUG_TAG, "Decided click is on stop with id $id : $stop")
stop?.let { newstop ->
val sameStopClicked = shownStopInBottomSheet?.let { newstop.ID==it.ID } ?: false
Log.d(DEBUG_TAG, "Hiding clicked stop: $sameStopClicked")
if (isBottomSheetShowing()) {
hideStopOrBusBottomSheet()
}
if(!sameStopClicked){
openStopInBottomSheet(newstop)
//isBottomSheetShowing = true
//move camera
if (newstop.latitude != null && newstop.longitude != null)
//mapReady.cameraPosition = CameraPosition.Builder().target(LatLng(it.latitude!!, it.longitude!!)).build()
mapReady.animateCamera(
CameraUpdateFactory.newLatLng(LatLng(newstop.latitude!!, newstop.longitude!!)),
750
)
}
}
return true
} else if (busNearby.isNotEmpty()) {
val feature = busNearby[0]
val vehid = feature.getStringProperty("veh")
if (isBottomSheetShowing()) hideStopOrBusBottomSheet()
showVehicleTripInBottomSheet(vehid)
//move camera to center on vehicle
updatesByVehDict[vehid]?.let { dat ->
mapReady.animateCamera(
CameraUpdateFactory.newLatLng(LatLng(dat.posUpdate.latitude, dat.posUpdate.longitude)), 750
)
}
return true
}
}
return false
}
override fun showOpenStopWithSymbolLayer(): Boolean {
return false
}
override fun hideStopOrBusBottomSheet(){
if (shownStopInBottomSheet?.ID == initialStopToShow?.ID){
initialStopToShow = null
}
super.hideStopOrBusBottomSheet()
}
override fun onAttach(context: Context) {
super.onAttach(context)
fragmentListener = if (context is CommonFragmentListener) {
context
} else {
throw RuntimeException(
context.toString()
+ " must implement FragmentListenerMain"
)
}
}
override fun onDetach() {
super.onDetach()
fragmentListener = null
}
override fun onStart() {
super.onStart()
}
override fun onResume() {
super.onResume()
//mapView.onResume() handled in GeneralMapLibreFragment
if(showBusLayer) {
//first, clean up all the old positions
livePositionsViewModel.clearOldPositionsUpdates()
if (livePositionsViewModel.useMQTTPositionsLiveData.value!!){
livePositionsViewModel.requestMatoPosUpdates(MQTTMatoClient.LINES_ALL)
usingMQTTPositions = true
}
else {
livePositionsViewModel.requestGTFSUpdates()
usingMQTTPositions = false
}
livePositionsViewModel.isLastWorkResultGood.observe(this) { d: Boolean ->
Log.d(
DEBUG_TAG, "Last trip download result is $d"
)
}
livePositionsViewModel.tripsGtfsIDsToQuery.observe(this) { dat: List<String> ->
Log.i(DEBUG_TAG, "Have these trips IDs missing from the DB, to be queried: $dat")
livePositionsViewModel.downloadTripsFromMato(dat)
}
}
fragmentListener?.readyGUIfor(FragmentKind.MAP)
}
override fun onPause() {
super.onPause()
Log.d(DEBUG_TAG, "Fragment paused")
map?.let{
//if map is initialized
mapStateViewModel.saveMapState(it)
}
try{
//save last location
map?.locationComponent?.let{
if(locationInitialized && it.isLocationComponentActivated){
stopsViewModel.lastUserLocation = it.lastKnownLocation
}
}
}catch (e: Exception){
Log.w(DEBUG_TAG, "Cannot save lastKnowLocation from map location component,error: ${e.message}")
}
mapStateViewModel.lastOpenStopID.postValue(shownStopInBottomSheet?.ID)
if (livePositionsViewModel.useMQTTPositionsLiveData.value!!) livePositionsViewModel.stopMatoUpdates()
}
override fun onStop() {
super.onStop()
Log.d(DEBUG_TAG, "Fragment stopped!")
}
override fun onMapDestroy() {
mapStyle.removeLayer(STOPS_LAYER_ID)
mapStyle.removeSource(STOPS_SOURCE_ID)
mapStyle.removeLayer(BUSES_LAYER_ID)
mapStyle.removeSource(BUSES_SOURCE_ID)
}
override fun getBaseViewForSnackBar(): View? {
return mapView
}
private fun showVehicleTripInBottomSheet(veh: String) {
val data = updatesByVehDict[veh] ?: return
super.showVehicleTripInBottomSheet(veh) { patternCode, _ ->
map?.let { mapStateViewModel.saveMapState(it) }
fragmentListener?.openLineFromVehicle(
data.posUpdate.getLineGTFSFormat(),
patternCode,
mapStateViewModel.savedCameraState?.toBundle()
)
}
}
private fun observeStops() {
// Observe stops
stopsViewModel.stopsToShow.observe(viewLifecycleOwner) { stops ->
stopsShowing = ArrayList(stops)
displayStops(stopsShowing)
initialStopToShow?.let{ s->
//show the stop in the bottom sheet
if(!initialStopShown && (s.ID in stopsShowing.map { it.ID })) {
val stopToShow = stopsShowing.first { it.ID == s.ID }
openStopInBottomSheet(stopToShow)
initialStopShown = true
}
}
}
}
/**
* Add the stops to the layers
*/
private fun displayStops(stops: List<Stop>?) {
if (stops.isNullOrEmpty()) return
if (stops.size==lastStopsSizeShown){
Log.d(DEBUG_TAG, "Not updating, have same number of stops")
return
}
/*if(stops.size> lastStopsSizeShown){
stopsRedrawnTimes = 0
} else{
stopsRedrawnTimes++
}
*/
val features = ArrayList<Feature>()//stops.mapNotNull { stop ->
//stop.latitude?.let { lat ->
// stop.longitude?.let { lon ->
for (s in stops){
if (s.latitude!=null && s.longitude!=null)
features.add(stopToGeoJsonFeature(s))
}
Log.d(DEBUG_TAG,"Displaying ${features.size} stops")
// if the layer is already started, substitute the stops inside, otherwise start it
if (stopsLayerStarted) {
stopsSource.setGeoJson(FeatureCollection.fromFeatures(features))
lastStopsSizeShown = features.size
} else
map?.let {
Log.d(DEBUG_TAG, "Map stop layer is not started yet, init layer")
initStopsLayer(mapStyle, FeatureCollection.fromFeatures(features))
Log.d(DEBUG_TAG,"Started stops layer on map")
lastStopsSizeShown = features.size
stopsLayerStarted = true
}
}
// --------------- BUS LOCATIONS STUFF --------------------------
/**
* Start requesting position updates
*/
private fun observeBusPositionUpdates() {
livePositionsViewModel.updatesWithTripAndPatterns.observe(viewLifecycleOwner) { data: HashMap<String, Pair<LivePositionUpdate, TripAndPatternWithStops?>> ->
Log.d(
DEBUG_TAG,
"Have " + data.size + " trip updates, has Map start finished: $mapInitialized"
)
if (mapInitialized) updateBusPositionsInMap(data, hasVehicleTracking = true) { veh ->
showVehicleTripInBottomSheet(veh)
}
if (!isDetached && !livePositionsViewModel.useMQTTPositionsLiveData.value!!) livePositionsViewModel.requestDelayedGTFSUpdates(
3000
)
}
}
// ------ LOCATION STUFF -----
@SuppressLint("MissingPermission")
override fun onMapLocationComponentInitialized() {
//locationComponent.cameraMode = CameraMode.TRACKING
locationComponent.renderMode = RenderMode.COMPASS
locationComponent.locationEngine?.apply{
// this is only called once
getLastLocation(object : LocationEngineCallback<LocationEngineResult> {
override fun onSuccess(res: LocationEngineResult?) {
Log.d(DEBUG_TAG, "Got the last location, ${res?.lastLocation}")
res?.lastLocation?.let { loc ->
if(mapInitialized){
val newLocation = LatLng(loc.latitude, loc.longitude)
//center the position only if it is close enough
if(newLocation.distanceTo(DEFAULT_LATLNG) < MAX_DIST_KM * 1000)
map?.cameraPosition = CameraPosition.Builder().target(LatLng(loc.latitude, loc.longitude)).build()
}
else
mapStateViewModel.locationToShow = loc
}
}
override fun onFailure(p0: java.lang.Exception) {
if( p0 is MapLibreLocationEngine.NoLocationException)
Log.d(DEBUG_TAG, "Cannot find location: ${p0.message}")
else
Log.w(DEBUG_TAG, "Failed to get the last location, error: ${p0.message}",)
}
})
}
if(locationEnabledOnDevice){
- setFollowUserLocation(true)
+ if(shownStopInBottomSheet == null && vehShowing == null)
+ setFollowUserLocation(true)
}
}
override fun onMapLocationEnabled(active: Boolean) {
//Extra stuff to do
- setFollowUserLocation(active)
+ // this check should always pass
+ if(shownStopInBottomSheet == null && vehShowing == null)
+ setFollowUserLocation(active)
}
@SuppressLint("MissingPermission")
override fun onFirstReceivedLocation(location: Location) {
val it = location
+ val notShowingStopOrVehicle = vehShowing.isNullOrEmpty() && shownStopInBottomSheet != null
if(locationInitialized && !receivedFirstLocation) {
//only zoom if the user position is close enough to the center
val newPoint = LatLng(it.latitude, it.longitude)
if(newPoint.distanceTo(DEFAULT_LATLNG) > MAX_DIST_KM * 1000){
//show Toast
if(!shownToastNoPosition) context?.let{ c->
Toast.makeText(c, R.string.too_far_not_showing_location, Toast.LENGTH_LONG).show()
shownToastNoPosition = true
}
setLocationComponentEnabled(false)
//Update UI Status
mapStateViewModel.locationUserActive.value = false
mapStateViewModel.followingUserPosition.value = false
- } else {
+ } else if(notShowingStopOrVehicle) {
map?.apply {
animateCamera(
CameraUpdateFactory.newCameraPosition(
CameraPosition.Builder().target(LatLng(location.latitude, location.longitude)).build()
),
1000
)
setLocationComponentEnabled(true)
locationComponent.cameraMode = CameraMode.TRACKING
mapStateViewModel.locationUserActive.value = true
}
setFollowUserLocation(true)
+ } else{
+ map?.apply {
+ setLocationComponentEnabled(true)
+ }
}
}
else{
//check for this is when the map is used
mapStateViewModel.locationToShow = location
}
}
override fun setLocationIconEnabled(enabled: Boolean){
if (enabled)
userLocationButton.setImageDrawable(ContextCompat.getDrawable(requireContext(), R.drawable.location_circlew_red))
else
userLocationButton.setImageDrawable(ContextCompat.getDrawable(requireContext(), R.drawable.location_circlew_grey))
}
private fun updateFollowingIcon(enabled: Boolean){
if(enabled)
followUserButton.setImageDrawable(ContextCompat.getDrawable(requireContext(), R.drawable.walk_circle_active))
else
followUserButton.setImageDrawable(ContextCompat.getDrawable(requireContext(), R.drawable.walk_circle_inactive))
}
/**
* This sets both the status on the component if it has been activated and the icon in the Fragment
*/
private fun setFollowUserLocation(enabled: Boolean){
if(locationInitialized) {
if (enabled)
locationComponent.cameraMode = CameraMode.TRACKING
else locationComponent.cameraMode = CameraMode.NONE
}
//update the icon by updating the livedata
mapStateViewModel.followingUserPosition.value = enabled
}
companion object {
private const val STOPS_SOURCE_ID = "stops-source"
private const val STOPS_LAYER_ID = "stops-layer"
private const val LABELS_LAYER_ID = "bus-labels-layer"
private const val LABELS_SOURCE = "labels-source"
private const val STOP_IMAGE_ID ="bus-stop-icon"
const val DEFAULT_CENTER_LAT = 45.0708
const val DEFAULT_CENTER_LON = 7.6858
private val DEFAULT_LATLNG = LatLng(DEFAULT_CENTER_LAT, DEFAULT_CENTER_LON)
private val DEFAULT_ZOOM = 14.3
private const val POSITION_FOUND_ZOOM = 16.5
private const val NO_POSITION_ZOOM = 17.1
private const val DEBUG_TAG = "BusTO-MapLibreFrag"
private const val STOP_ACTIVE_IMG = "Stop-active"
const val FRAGMENT_TAG = "BusTOMapFragment"
private const val LOCATION_PERMISSION_REQUEST_CODE = 981202
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param stop Eventual stop to center the map into
* @return A new instance of fragment MapLibreFragment.
*/
@JvmStatic
fun newInstance(stop: Stop?) =
MapLibreFragment().apply {
arguments = Bundle().let {
// Cannot use Parcelable as it requires higher version of Android
//stop?.let{putParcelable(STOP_TO_SHOW, it)}
stop?.toBundle(it)
}
}
}
}
\ No newline at end of file
diff --git a/app/src/main/java/it/reyboz/bustorino/viewmodels/LivePositionsViewModel.kt b/app/src/main/java/it/reyboz/bustorino/viewmodels/LivePositionsViewModel.kt
index 239c92e..b8703e8 100644
--- a/app/src/main/java/it/reyboz/bustorino/viewmodels/LivePositionsViewModel.kt
+++ b/app/src/main/java/it/reyboz/bustorino/viewmodels/LivePositionsViewModel.kt
@@ -1,484 +1,489 @@
/*
BusTO - ViewModel components
Copyright (C) 2023 Fabio Mazza
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package it.reyboz.bustorino.viewmodels
import android.app.Application
import android.util.Log
import androidx.lifecycle.*
import androidx.preference.PreferenceManager
import androidx.work.WorkInfo
import androidx.work.WorkManager
import com.android.volley.DefaultRetryPolicy
import it.reyboz.bustorino.R
import it.reyboz.bustorino.backend.Fetcher
import it.reyboz.bustorino.backend.LivePositionsServiceStatus
import it.reyboz.bustorino.backend.NetworkVolleyManager
import it.reyboz.bustorino.backend.gtfs.GtfsRtPositionsRequest
import it.reyboz.bustorino.backend.gtfs.GtfsUtils
import it.reyboz.bustorino.backend.gtfs.LivePositionUpdate
import it.reyboz.bustorino.backend.mato.MQTTMatoClient
import it.reyboz.bustorino.backend.mato.PositionsMap
import it.reyboz.bustorino.data.GtfsRepository
import it.reyboz.bustorino.data.MatoPatternsDownloadWorker
import it.reyboz.bustorino.data.MatoTripsDownloadWorker
import it.reyboz.bustorino.data.gtfs.MatoPattern
import it.reyboz.bustorino.data.gtfs.TripAndPatternWithStops
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import java.util.*
import kotlin.collections.ArrayList
import kotlin.collections.HashMap
import kotlin.collections.HashSet
import androidx.core.content.edit
import androidx.lifecycle.MutableLiveData
+import kotlin.text.contains
typealias FullPositionUpdatesMap = HashMap<String, Pair<LivePositionUpdate, TripAndPatternWithStops?>>
typealias FullPositionUpdate = Pair<LivePositionUpdate, TripAndPatternWithStops?>
class LivePositionsViewModel(application: Application): AndroidViewModel(application) {
private val gtfsRepo = GtfsRepository(application)
//chain of LiveData objects: raw positions -> tripsIDs -> tripsAndPatternsInDB -> positions with patterns
//this contains the raw positions updates received from the service
private val positionsToBeMatchedLiveData = MutableLiveData<ArrayList<LivePositionUpdate>>()
private val netVolleyManager = NetworkVolleyManager.getInstance(application)
private var mqttClient = MQTTMatoClient()
private var lineListening = ""
private var lastTimeMQTTUpdatedPositions: Long = 0
private val gtfsRtRequestRunning = MutableLiveData<Boolean>(false)
private val lastFailedTripsRequest = HashMap<String, Date>()
private val workManager = WorkManager.getInstance(application)
private var lastRequestedDownloadTrips = MutableLiveData<List<String>>()
//INPUT FILTER FOR LINE
private var gtfsLineToFilterPos = MutableLiveData<Pair<String,MatoPattern?>>()
var serviceStatus = MutableLiveData(LivePositionsServiceStatus.CONNECTING)
private val sharedPrefs = PreferenceManager.getDefaultSharedPreferences(application)
private val keySourcePositions = application.getString(R.string.pref_positions_source)
private val LIVE_POS_PREF_MQTT : String
private val LIVE_POS_PREF_GTFSRT :String
val useMQTTPositionsLiveData: MutableLiveData<Boolean>
init {
sharedPrefs.registerOnSharedPreferenceChangeListener { shp, key ->
if(key == keySourcePositions) {
val newV = shp.getString(keySourcePositions, LIVE_POS_PREF_MQTT)
useMQTTPositionsLiveData.postValue(newV.equals(LIVE_POS_PREF_MQTT))
Log.d(DEBUG_TI, "Changed position source to: $newV")
}
}
LIVE_POS_PREF_MQTT = application.getString(R.string.positions_source_mqtt)
LIVE_POS_PREF_GTFSRT = application.getString(R.string.positions_source_gtfsrt)
useMQTTPositionsLiveData = MutableLiveData(isMQTTPositionsSelected())
}
private fun isMQTTPositionsSelected(): Boolean{
val source = sharedPrefs.getString(keySourcePositions, LIVE_POS_PREF_MQTT)
val useMQTT=source == LIVE_POS_PREF_MQTT
Log.d(DEBUG_TI, "Init positions, source: $source, isMQTT: $useMQTT")
return useMQTT
}
/**
* Switch provider of live positions from MQTT to GTFSRT and viceversa
*/
fun switchPositionsSource(){
val usingMQTT = useMQTTPositionsLiveData.value!!
//code that was in the MapLibreFragment
useMQTTPositionsLiveData.value = !usingMQTT
sharedPrefs.edit(commit = true) {
putString(
keySourcePositions,
if (usingMQTT) LIVE_POS_PREF_GTFSRT else LIVE_POS_PREF_MQTT
)
}
Log.d(DEBUG_TI, "Switched positions source in ViewModel, now using MQTT: ${!usingMQTT}")
serviceStatus.value = LivePositionsServiceStatus.CONNECTING
}
var isLastWorkResultGood = workManager
.getWorkInfosForUniqueWorkLiveData(MatoTripsDownloadWorker.TAG_TRIPS).map { it ->
if (it.isEmpty()) return@map false
var res = true
if(it[0].state == WorkInfo.State.FAILED){
val currDate = Date()
res = false
lastRequestedDownloadTrips.value?.let { trips->
for(tr in trips){
lastFailedTripsRequest[tr] = currDate
}
}
}
return@map res
}
/**
* Responder to the MQTT Client
*/
private val matoPositionListener = object: MQTTMatoClient.Companion.MQTTMatoListener{
override fun onUpdateReceived(it: PositionsMap) {
val mupds = ArrayList<LivePositionUpdate>()
if(lineListening==MQTTMatoClient.LINES_ALL){
for(sdic in it.values){
for(update in sdic.values){
mupds.add(update)
}
}
} else{
//we're listening to one
if (it.containsKey(lineListening.trim()) ){
for(up in it[lineListening]?.values!!){
mupds.add(up)
}
}
}
//avoid updating the positions too often (limit to 0.5 seconds)
val time = System.currentTimeMillis()
if(lastTimeMQTTUpdatedPositions == (0.toLong()) || (time-lastTimeMQTTUpdatedPositions)>500){
positionsToBeMatchedLiveData.postValue(mupds)
lastTimeMQTTUpdatedPositions = time
}
//we have received an update, so set the status to OK
serviceStatus.postValue(LivePositionsServiceStatus.OK)
}
override fun onStatusUpdate(status: LivePositionsServiceStatus) {
serviceStatus.postValue(status)
}
}
//find the trip IDs in the updates
private val tripsIDsInUpdates = positionsToBeMatchedLiveData.map { it ->
//Log.d(DEBUG_TI, "Updates map has keys ${upMap.keys}")
- it.map { pos -> "gtt:"+pos.tripID }
+ it.map { pos -> "gtt:"+pos.tripID }.filter{ s->
+ !(s.contains("null") || s.trim() =="gtt:")
+ }
}
// get the trip IDs in the DB
private val gtfsTripsPatternsInDB = tripsIDsInUpdates.switchMap {
//Log.i(DEBUG_TI, "tripsIds in updates: ${it.size}")
gtfsRepo.gtfsDao.getTripPatternStops(it)
}
//trip IDs to query, which are not present in the DB
//REMEMBER TO OBSERVE THIS IN THE MAP
val tripsGtfsIDsToQuery: LiveData<List<String>> = gtfsTripsPatternsInDB.map { tripswithPatterns ->
val tripNames=tripswithPatterns.map { twp-> twp.trip.tripID }
Log.i(DEBUG_TI, "Have ${tripswithPatterns.size} trips in the DB")
if (tripsIDsInUpdates.value!=null)
- return@map tripsIDsInUpdates.value!!.filter { !(tripNames.contains(it) || it.contains("null"))}
+ return@map tripsIDsInUpdates.value!!.filter { !(
+ tripNames.contains(it) || it.contains("null") || it =="gtt:"
+ )}.distinct()
else {
Log.e(DEBUG_TI,"Got results for gtfsTripsInDB but not tripsIDsInUpdates??")
return@map ArrayList<String>()
}
}
/**
* This livedata object contains the final updates with patterns present in the DB
*/
val updatesWithTripAndPatterns = gtfsTripsPatternsInDB.map { tripPatterns->
//TODO: Change the mapping in the final updates, I don't know why the key is the tripID and not the vehicle ID
Log.i(DEBUG_TI, "Mapping trips and patterns")
val mdict = HashMap<String,FullPositionUpdate>()
//missing patterns
val routesToDownload = HashSet<String>()
if(positionsToBeMatchedLiveData.value!=null)
for(update in positionsToBeMatchedLiveData.value!!){
val trID:String = update.tripID
var found = false
for(trip in tripPatterns){
if (trip.pattern == null){
//pattern is null, which means we have to download
// the pattern data from MaTO
routesToDownload.add(trip.trip.routeID)
}
if (trip.trip.tripID == "gtt:$trID"){
found = true
//insert directly
mdict[trID] = Pair(update,trip)
break
}
}
if (!found){
//Log.d(DEBUG_TI, "Cannot find pattern ${tr}")
//give the update anyway
mdict[trID] = Pair(update,null)
}
}
//have to request download of missing Patterns
if (routesToDownload.isNotEmpty()){
Log.d(DEBUG_TI, "Have ${routesToDownload.size} missing patterns from the DB: $routesToDownload")
//downloadMissingPatterns (ArrayList(routesToDownload))
MatoPatternsDownloadWorker.downloadPatternsForRoutes(routesToDownload.toList(), getApplication())
}
return@map mdict
}
fun clearOldPositionsUpdates(){
//RETURN if the map is null
val positionsOld = positionsToBeMatchedLiveData.value ?: return
val currentTimeSecs = (System.currentTimeMillis() / 1000 )
val updatedList = ArrayList<LivePositionUpdate>()
for (up in positionsOld){
//If the time has passed, remove it
if (currentTimeSecs - up.timestamp <= MAX_MINUTES_CLEAR_POSITIONS*60) //TODO decide time limit in minutes
updatedList.add(up)
}
val diff = positionsOld.size - updatedList.size
Log.d(DEBUG_TI, "Removed ${diff} positions marked as old")
// Re-trigger all the LiveData chain
positionsToBeMatchedLiveData.value = updatedList
}
fun clearAllPositions(){
positionsToBeMatchedLiveData.postValue(ArrayList())
Log.d(DEBUG_TI, "Cleared all positions in LivePositionsViewModel")
}
//OBSERVE THIS TO GET THE LOCATION UPDATES FILTERED
val filteredLocationUpdates = MediatorLiveData<Pair<FullPositionUpdatesMap, List<String>>>()
init {
filteredLocationUpdates.addSource(updatesWithTripAndPatterns){
filteredLocationUpdates.postValue(filterUpdatesForGtfsLine(it, gtfsLineToFilterPos.value!!))
}
filteredLocationUpdates.addSource(gtfsLineToFilterPos){
Log.d(DEBUG_TI, "line to filter change to: ${gtfsLineToFilterPos.value}")
updatesWithTripAndPatterns.value?.let{
ups-> filteredLocationUpdates.postValue(filterUpdatesForGtfsLine(ups, it))
//Log.d(DEBUG_TI, "Set ${ups.size} updates as new value for filteredLocation")
}
}
}
private fun clearFilteredPositions(){
filteredLocationUpdates.postValue(Pair(HashMap(), ArrayList<String>()))
}
fun setGtfsLineToFilterPos(line: String, pattern: MatoPattern?){
clearFilteredPositions()
gtfsLineToFilterPos.value = Pair(line, pattern)
}
private fun filterUpdatesForGtfsLine(updates: FullPositionUpdatesMap,
linePatt: Pair<String, MatoPattern?>):
Pair<HashMap<String,FullPositionUpdate>, List<String>>{
val gtfsLineId = linePatt.first
val pattern = linePatt.second
val updsForTripId = HashMap<String, Pair<LivePositionUpdate, TripAndPatternWithStops?>>()
val vehicleOnWrongDirection = mutableListOf<String>()
//supporting the eventual null case when there is no need to filter
if (gtfsLineId == "ALL"){
//copy the dict
for ((tripId, pair) in updates.entries) {
updsForTripId[tripId] = pair
}
} else {
val filtdLineID = GtfsUtils.stripGtfsPrefix(gtfsLineId)
//filter buses with direction, show those only with the same direction
val directionId = pattern?.directionId ?: -100
val numUpds = updates.entries.size
Log.d(
DEBUG_TI,
"Got $numUpds updates, using MQTT: ${useMQTTPositionsLiveData.value}, pattern ${pattern?.name}"
)
// cannot understand where this is used
//val patternsDirections = HashMap<String,Int>()
for ((tripId, pair) in updates.entries) {
//remove trips with wrong line
val posUp = pair.first
val vehicle = pair.first.vehicle
if (pair.first.routeID != filtdLineID)
continue
if (directionId != -100 && pair.second != null && pair.second?.pattern != null) {
val dir = pair.second!!.pattern!!.directionId
if (dir == directionId) {
//add the trip
updsForTripId[tripId] = pair
//Log.d(DEBUG_TI, "Add vehicle ${pair.first.vehicle}, route ${pair.first.routeID}")
} else {
vehicleOnWrongDirection.add(vehicle)
}
//patternsDirections[tripId] = dir ?: -10
} else {
updsForTripId[tripId] = pair
//Log.d(DEBUG_TAG, "No pattern for tripID: $tripId")
//patternsDirections[tripId] = -10
}
}
}
Log.d(DEBUG_TI, "Filtered updates are ${updsForTripId.keys.size}") // Original updates directs: $patternsDirections\n
return Pair(updsForTripId, vehicleOnWrongDirection)
}
fun requestMatoPosUpdates(line: String){
lineListening = line
viewModelScope.launch {
mqttClient.startAndSubscribe(line,matoPositionListener, getApplication())
//clear old positions (useful when we are coming back to the map after some time)
mqttClient.clearOldPositions(MAX_MINUTES_CLEAR_POSITIONS)
}
//updatePositions(1000)
}
fun stopMatoUpdates(){
viewModelScope.launch {
val tt = System.currentTimeMillis()
mqttClient.stopMatoRequests(matoPositionListener)
val time = System.currentTimeMillis() -tt
Log.d(DEBUG_TI, "Took $time ms to unsubscribe")
}
}
fun retriggerPositionUpdate(){
if(positionsToBeMatchedLiveData.value!=null){
positionsToBeMatchedLiveData.postValue(positionsToBeMatchedLiveData.value)
}
}
//Gtfs Real time
private val gtfsPositionsReqListener = object: GtfsRtPositionsRequest.Companion.RequestListener{
override fun onResponse(response: ArrayList<LivePositionUpdate>?) {
Log.i(DEBUG_TI,"Got response from the GTFS RT server")
if (response == null){
serviceStatus.postValue(LivePositionsServiceStatus.ERROR_CONNECTION)
}
else response.let { it:ArrayList<LivePositionUpdate> ->
val ss: LivePositionsServiceStatus
if (it.size == 0) {
Log.w(DEBUG_TI,"No position updates from the GTFS RT server")
ss = LivePositionsServiceStatus.NO_POSITIONS
} else {
//Log.i(DEBUG_TI, "Posting value to positionsLiveData")
viewModelScope.launch { positionsToBeMatchedLiveData.postValue(it) }
ss = LivePositionsServiceStatus.OK
}
serviceStatus.postValue(ss)
}
gtfsRtRequestRunning.postValue(false)
}
}
/**
* Listener for the errors in downloading positions from GTFS RT
*/
private val positionRequestErrorListener = GtfsRtPositionsRequest.Companion.ErrorListener {
Log.e(DEBUG_TI, "Could not download the update", it)
gtfsRtRequestRunning.postValue(false)
if(it is GtfsRtPositionsRequest.RequestError){
val status = when(it.result) {
Fetcher.Result.OK -> LivePositionsServiceStatus.OK
Fetcher.Result.PARSER_ERROR -> LivePositionsServiceStatus.ERROR_PARSING_RESPONSE
Fetcher.Result.SERVER_ERROR_404 -> LivePositionsServiceStatus.ERROR_NETWORK_RESPONSE
Fetcher.Result.SERVER_ERROR -> LivePositionsServiceStatus.ERROR_NETWORK_RESPONSE
Fetcher.Result.CONNECTION_ERROR -> LivePositionsServiceStatus.ERROR_CONNECTION
else -> LivePositionsServiceStatus.ERROR_CONNECTION
}
serviceStatus.postValue(status)
} else
serviceStatus.postValue(LivePositionsServiceStatus.ERROR_NETWORK_RESPONSE)
}
fun requestGTFSUpdates(){
if(gtfsRtRequestRunning.value == null || !gtfsRtRequestRunning.value!!) {
val request = GtfsRtPositionsRequest(positionRequestErrorListener, gtfsPositionsReqListener)
request.setRetryPolicy(
DefaultRetryPolicy(1000,10,DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)
)
netVolleyManager.requestQueue.add(request)
Log.i(DEBUG_TI, "Requested GTFS realtime position updates")
gtfsRtRequestRunning.value = true
}
}
fun requestDelayedGTFSUpdates(timems: Long){
viewModelScope.launch {
delay(timems)
requestGTFSUpdates()
}
}
override fun onCleared() {
//stop the MQTT Service
Log.d(DEBUG_TI, "Clearing the live positions view model, stopping the mqttClient")
mqttClient.disconnect()
super.onCleared()
}
//Request trips download
fun downloadTripsFromMato(trips: List<String>): Boolean{
if(trips.isEmpty())
return false
var shouldContinue = false
val currentDateTime = Date().time
for (tr in trips){
if (!lastFailedTripsRequest.containsKey(tr)){
shouldContinue = true
break
} else{
//Log.i(DEBUG_TI, "Last time the trip has failed is ${lastFailedTripsRequest[tr]}")
if ((lastFailedTripsRequest[tr]!!.time - currentDateTime) > MAX_TIME_RETRY){
shouldContinue =true
break
}
}
}
if (shouldContinue) {
//if one trip
val workRequ =MatoTripsDownloadWorker.requestMatoTripsDownload(trips, getApplication(), "BusTO-MatoTripsDown")
workRequ?.let { req ->
Log.d(DEBUG_TI, "Enqueueing new work, saving work info")
lastRequestedDownloadTrips.postValue(trips)
//isLastWorkResultGood =
}
} else{
Log.w(DEBUG_TI, "Requested to fetch data for ${trips.size} trips but they all have failed before in the last $MAX_MINUTES_RETRY mins")
}
return shouldContinue
}
companion object{
private const val DEBUG_TI = "BusTO-LivePosViewModel"
private const val MAX_MINUTES_RETRY = 3
private const val MAX_TIME_RETRY = MAX_MINUTES_RETRY*60*1000 //3 minutes (in milliseconds)
public const val MAX_MINUTES_CLEAR_POSITIONS = 10
}
}
\ No newline at end of file

File Metadata

Mime Type
text/x-diff
Expires
Thu, Nov 19, 02:07 (11 h, 54 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2263771
Default Alt Text
(161 KB)

Event Timeline