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 1b760ec..11e178b 100644 --- a/app/src/main/java/it/reyboz/bustorino/fragments/GeneralMapLibreFragment.kt +++ b/app/src/main/java/it/reyboz/bustorino/fragments/GeneralMapLibreFragment.kt @@ -1,1303 +1,1311 @@ /* 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 . */ 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.Gravity 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.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.VehicleClassInfo 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.camera.CameraUpdateFactory 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.math.absoluteValue 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 + /** + * Stop to show in Bottom Sheet, if it is null, no stop is shown + */ + protected var shownStopInBottomSheet : Stop? = 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 protected var locationEngine: MapLibreLocationEngine? = null 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 linesBottomTextView: 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 lateinit var warningTripIcon: ImageView private lateinit var loadingTripIcon: ImageView protected lateinit var centerBottomImage: ImageView protected var lastLocation : Location? = null private var lastMapStyle ="" //BUS POSITIONS protected val updatesByVehDict = HashMap(5) protected val animatorsByVeh = HashMap() + /** + * Label of the vehicle to show in Bottom Sheet, if it is null, no vehicle is shown + */ protected var vehShowing: String? = null protected var lastUpdateTime:Long = -2 protected var jobUpdate: Job? = null //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 { 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(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) loadingTripIcon = view.findViewById(R.id.downloadingIcon) warningTripIcon = view.findViewById(R.id.warningIconTrip) linesBottomTextView = view.findViewById(R.id.linesBottomTextView) linesBottomTextView.text = getString(R.string.lines_fill, "") bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet) bottomSheetBehavior.state = BottomSheetBehavior.STATE_HIDDEN centerBottomImage = view.findViewById(R.id.centerBottomLayoutImage) centerBottomImage.setOnClickListener(){ centerOnOpenItemInBottomSheet() } //set onclick listener for warning trip icon warningTripIcon.setOnClickListener { showToastMessage(R.string.no_trip_info_warning, true) } loadingTripIcon.setOnClickListener { showToastMessage(R.string.downloading_trip_info, true) } } 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 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){ 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())) } bottomSheetBehavior.state = BottomSheetBehavior.STATE_HIDDEN //isBottomSheetShowing = false //reset states shownStopInBottomSheet = null if (vehShowing!=null){ //we are hiding a vehicle 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 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) } protected fun showVehClassInfo(vehInfo: VehicleClassInfo){ val print = "${vehInfo.type.getName()}: ${vehInfo.name}" makeToast(print) } /** * 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>, hasVehicleTracking: Boolean = true, trackVehicleCallback: ((String) -> Unit)? = null ) { //TODO: Eventually change this (incomingData should be keyed by vehicle) 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() == 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 val update = data.posUpdate if (pat != null) { stopTitleTextView.text = pat.headsign stopTitleTextView.visibility = View.VISIBLE stopNumberTextView.text = getString(R.string.line_fill_towards, lineName) loadingTripIcon.visibility = View.GONE } else { stopTitleTextView.visibility = View.GONE stopNumberTextView.text = getString(R.string.line_fill, lineName) loadingTripIcon.visibility = if (update.hasTripId()) View.VISIBLE else View.GONE } 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.bus_marker_color_selected, activity?.theme) ViewCompat.setBackgroundTintList(directionsCard, ColorStateList.valueOf(colorBlue)) linesPassingTextView.text = getString(R.string.vehicle_fill, data.posUpdate.vehicle) linesPassingTextView.gravity = Gravity.CENTER_VERTICAL linesBottomTextView.visibility = View.GONE arrivalsCard.visibility = View.GONE extraBottomTextView.text = getString(R.string.updated_fill, utils.unixTimestampToLocalTime(data.posUpdate.timestamp)) extraBottomTextView.visibility = View.VISIBLE val vehInfo = VehicleUtils.getTypeForLabel(update.vehicle) if(vehInfo == null){ vehicleIcon.visibility = View.GONE linesPassingTextView.setOnClickListener { } //empty click listener } else{ val ico = when(vehInfo.type){ VehicleUtils.VehicleType.BUS -> R.drawable.ic_bus VehicleUtils.VehicleType.ELECTRIC_BUS -> R.drawable.ic_bus_electric_filled VehicleUtils.VehicleType.TRAM -> R.drawable.ic_tram_material } vehicleIcon.setImageDrawable(ResourcesCompat.getDrawable(resources, ico, activity?.theme)) vehicleIcon.visibility = View.VISIBLE vehicleIcon.setOnClickListener { showVehClassInfo(vehInfo) } linesPassingTextView.setOnClickListener { showVehClassInfo(vehInfo) } } if (!update.hasTripId()){ warningTripIcon.visibility = View.VISIBLE } else{ warningTripIcon.visibility = View.GONE } } vehShowing = veh 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 100ms throttle */ protected fun updatePositionsIcons(forced: Boolean) { // Avoid frequent updates - throttle to max once per 60ms val currentTime = System.currentTimeMillis() val isStarted = (lifecycle.currentState.isAtLeast(Lifecycle.State.STARTED)) if(forced){ // if we're running a forced update, cancel the pending one jobUpdate?.apply{ cancel() //Log.d(DEBUG_TAG, "Cancelled update") } } else if (currentTime - lastUpdateTime < 100) { // Schedule delayed update if(viewLifecycleOwnerLiveData.value != null) { jobUpdate?.cancel() jobUpdate = viewLifecycleOwner.lifecycleScope.launch { delay(100.milliseconds) //Log.d(DEBUG_TAG, "Running update from delayed") updatePositionsIcons(false) } //Log.d(DEBUG_TAG, "Cancelled previous job, delaying update") } return } if(!isStarted){ Log.w(DEBUG_TAG, "fragment is not started, ") } val busFeatures = ArrayList() val selectedBusFeatures = ArrayList() 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() == true && vehShowing == dat.posUpdate.vehicle) { selectedBusFeatures.add(newFeature) //Log.d(DEBUG_TAG, "Update position for bus $vehShowing") //TODO: Recenter the map on the vehicle } 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(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 stop.routesThatStopHereToString() //requireContext().getString(R.string.lines_fill, stop.routesThatStopHereToString()) linesPassingTextView.text = string_show linesPassingTextView.visibility = View.VISIBLE linesPassingTextView.setOnClickListener { } //empty click listener (needed when switching from vehicle) linesPassingTextView.gravity = Gravity.TOP linesBottomTextView.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, 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)) // icons for the vehicles vehicleIcon.visibility = View.GONE warningTripIcon.visibility = View.GONE loadingTripIcon.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() 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 val layerAbove = if (lastMapStyle == MapLibreUtils.STYLE_OSM_RASTER){ "osm-raster" } else// if (lastMapStyle == MapLibreUtils.STYLE_VERSATILES_ECLIPSE_JSON){ "symbol-transit-airfield" /*} else { // "poi_park" } */ initStopsLayer(style, stopsFeatures, layerAbove) } protected fun initStopsLayer(style: Style, stopsFeatures: FeatureCollection?, stopsLayerAbove: String){ stopsSource = GeoJsonSource(STOPS_SOURCE_ID,stopsFeatures ?: FeatureCollection.fromFeatures(ArrayList())) 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())) 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) } } } + protected fun showingBusStopOrVehicle(): Boolean{ + return !(shownStopInBottomSheet == null && vehShowing == null) + } /** * 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() } } protected fun centerOnOpenItemInBottomSheet(){ var latlng: LatLng? = null var found = 0 if(shownStopInBottomSheet!=null){ val stop = shownStopInBottomSheet!! latlng = LatLng(stop.latitude!!, stop.longitude!!) found = 1 }else if(vehShowing!=null){ val update = updatesByVehDict.getOrDefault(vehShowing!!, null) update?.let{ latlng = LatLng(it.posUpdate.latitude, it.posUpdate.longitude) found = 2 } } if (latlng!=null) { map?.apply{ //complicated part: make toast if the position is the same //val newBbox = projection.visibleRegion.latLngBounds val ll = cameraPosition.target if(ll!=null && (ll.latitude - latlng.latitude).absoluteValue < 1e-4 && (ll.longitude-latlng.longitude).absoluteValue < 1e-4){ Toast.makeText(requireContext(), if(found==1) R.string.already_center_bottom_sheet_stop else R.string.already_center_bottom_sheet_vehicle, Toast.LENGTH_SHORT ).show() } else { animateCamera( CameraUpdateFactory.newLatLng(latlng), 700 ) } } }else { Log.d(DEBUG_TAG, "Asked to center on open thing in bottom sheet but no position found") } } 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/MapLibreFragment.kt b/app/src/main/java/it/reyboz/bustorino/fragments/MapLibreFragment.kt index 6309490..367d933 100644 --- a/app/src/main/java/it/reyboz/bustorino/fragments/MapLibreFragment.kt +++ b/app/src/main/java/it/reyboz/bustorino/fragments/MapLibreFragment.kt @@ -1,756 +1,749 @@ /* 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 . */ 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(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() 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(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.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(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 lastLoc = mapStateViewModel.userLocationToShow 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 -> 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 hasToShowInitialStop(): Boolean{ + return if(initialStopToShow==null) false + else !initialStopShown + } 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?) { 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()//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> -> 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 { 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) + //center the position only if it is close enough and we are not showing a stop + if(!showingBusStopOrVehicle() && newLocation.distanceTo(DEFAULT_LATLNG) < MAX_DIST_KM * 1000) map?.cameraPosition = CameraPosition.Builder().target(LatLng(loc.latitude, loc.longitude)).build() - } else - mapStateViewModel.locationToShow = loc + mapStateViewModel.userLocationToShow = 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){ - if(shownStopInBottomSheet == null && vehShowing == null) + if(!showingBusStopOrVehicle() && !hasToShowInitialStop()) setFollowUserLocation(true) } } override fun onMapLocationEnabled(active: Boolean) { //Extra stuff to do // this check should always pass - if(shownStopInBottomSheet == null && vehShowing == null) + if(!(showingBusStopOrVehicle()) || hasToShowInitialStop()) setFollowUserLocation(active) } @SuppressLint("MissingPermission") override fun onFirstReceivedLocation(location: Location) { val it = location - val notShowingStopOrVehicle = vehShowing.isNullOrEmpty() && shownStopInBottomSheet != null + //val notShowingStopOrVehicle = s//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 if(notShowingStopOrVehicle) { + } else if(!(showingBusStopOrVehicle()||hasToShowInitialStop())) { 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 + mapStateViewModel.userLocationToShow = 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/MapStateViewModel.kt b/app/src/main/java/it/reyboz/bustorino/viewmodels/MapStateViewModel.kt index 3a53046..dd11e50 100644 --- a/app/src/main/java/it/reyboz/bustorino/viewmodels/MapStateViewModel.kt +++ b/app/src/main/java/it/reyboz/bustorino/viewmodels/MapStateViewModel.kt @@ -1,54 +1,54 @@ package it.reyboz.bustorino.viewmodels import android.location.Location import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel import it.reyboz.bustorino.map.MapCameraState import org.maplibre.android.camera.CameraPosition import org.maplibre.android.geometry.LatLng import org.maplibre.android.maps.MapLibreMap class MapStateViewModel : ViewModel() { var savedCameraState: MapCameraState? = null private set val lastOpenStopID = MutableLiveData() fun saveMapState(map: MapLibreMap){ val cp = map.cameraPosition val newBbox = map.projection.visibleRegion.latLngBounds val cameraState = MapCameraState( latitude = newBbox.center.latitude, longitude = newBbox.center.longitude, zoom = cp.zoom, bearing = cp.bearing, tilt = cp.tilt ) savedCameraState = cameraState } fun restoreMapState(map: MapLibreMap): Boolean { return restoreMapState(map, this.savedCameraState) } - var locationToShow: Location? = null + var userLocationToShow: Location? = null val locationUserActive = MutableLiveData(false) val followingUserPosition = MutableLiveData(false) val locationDeviceEnabled = MutableLiveData(false) companion object{ fun restoreMapState(map: MapLibreMap, savedCameraState: MapCameraState?): Boolean { val state = savedCameraState ?: return false map.cameraPosition = CameraPosition.Builder() .target(LatLng(state.latitude, state.longitude)) .zoom(state.zoom) .bearing(state.bearing) .tilt(state.tilt) .build() return true } } } \ No newline at end of file