diff --git a/app/src/main/java/it/reyboz/bustorino/ActivityPrincipal.java b/app/src/main/java/it/reyboz/bustorino/ActivityPrincipal.java --- a/app/src/main/java/it/reyboz/bustorino/ActivityPrincipal.java +++ b/app/src/main/java/it/reyboz/bustorino/ActivityPrincipal.java @@ -769,6 +769,14 @@ editor.putStringSet(SettingsFragment.KEY_ARRIVALS_FETCHERS_USE, utils.convertArrayToSet(defaultVals)); edit=true; } + //Live bus positions + final String keySourcePositions=getString(R.string.pref_positions_source); + final String positionsSource = mainSharedPref.getString(keySourcePositions, ""); + if(positionsSource.isEmpty()){ + String[] defaultVals = getResources().getStringArray(R.array.positions_source_values); + editor.putString(keySourcePositions, defaultVals[0]); + edit=true; + } if (edit){ editor.commit(); } diff --git a/app/src/main/java/it/reyboz/bustorino/fragments/IntroFragment.kt b/app/src/main/java/it/reyboz/bustorino/fragments/IntroFragment.kt --- a/app/src/main/java/it/reyboz/bustorino/fragments/IntroFragment.kt +++ b/app/src/main/java/it/reyboz/bustorino/fragments/IntroFragment.kt @@ -87,14 +87,17 @@ } 4 ->{ setImageBitmap(imageHolder, R.drawable.tuto_map) - val permGranted = (Permissions.anyLocationPermissionsGranted(requireContext())) - setInteractButtonState(ButtonState.LOCATION, !permGranted) textView.text = utils.convertHtml(getString(R.string.tutorial_map)) - interactButton.setOnClickListener { - //ask location permission - locationRequestResLauncher.launch(Permissions.LOCATION_PERMISSIONS) + if (Build.VERSION.SDK_INT >= 23) { + //only show if running on Android M or above + val permGranted = (Permissions.anyLocationPermissionsGranted(requireContext())) + setInteractButtonState(ButtonState.LOCATION, !permGranted) + interactButton.setOnClickListener { + //ask location permission + locationRequestResLauncher.launch(Permissions.LOCATION_PERMISSIONS) + } + interactButton.visibility = View.VISIBLE } - interactButton.visibility = View.VISIBLE } 5 ->{ diff --git a/app/src/main/java/it/reyboz/bustorino/fragments/LinesDetailFragment.kt b/app/src/main/java/it/reyboz/bustorino/fragments/LinesDetailFragment.kt --- a/app/src/main/java/it/reyboz/bustorino/fragments/LinesDetailFragment.kt +++ b/app/src/main/java/it/reyboz/bustorino/fragments/LinesDetailFragment.kt @@ -88,6 +88,7 @@ private lateinit var switchButton: ImageButton private var favoritesButton: ImageButton? = null + private lateinit var locationIcon: ImageButton private var isLineInFavorite = false private var appContext: Context? = null private val lineSharedPrefMonitor = SharedPreferences.OnSharedPreferenceChangeListener { pref, keychanged -> @@ -97,6 +98,8 @@ 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()} @@ -170,6 +173,7 @@ val rootView = inflater.inflate(R.layout.fragment_lines_detail, container, false) lineID = requireArguments().getString(LINEID_KEY, "") switchButton = rootView.findViewById(R.id.switchImageButton) + locationIcon = rootView.findViewById(R.id.locationEnableIcon) favoritesButton = rootView.findViewById(R.id.favoritesButton) stopsRecyclerView = rootView.findViewById(R.id.patternStopsRecyclerView) descripTextView = rootView.findViewById(R.id.lineDescripTextView) @@ -205,21 +209,44 @@ if(map.visibility == View.VISIBLE){ map.visibility = View.GONE stopsRecyclerView.visibility = View.VISIBLE + locationIcon.visibility = View.GONE viewModel.setMapShowing(false) liveBusViewModel.stopMatoUpdates() + //map.overlayManager.remove(busPositionsOverlay) + switchButton.setImageDrawable(AppCompatResources.getDrawable(requireContext(), R.drawable.ic_map_white_30)) } else{ stopsRecyclerView.visibility = View.GONE map.visibility = View.VISIBLE + locationIcon.visibility = View.VISIBLE viewModel.setMapShowing(true) + + //map.overlayManager.add(busPositionsOverlay) + //map. if(useMQTTPositions) - liveBusViewModel.requestMatoPosUpdates(lineID) + liveBusViewModel.requestMatoPosUpdates(GtfsUtils.getLineNameFromGtfsID(lineID)) else liveBusViewModel.requestGTFSUpdates() + switchButton.setImageDrawable(AppCompatResources.getDrawable(requireContext(), R.drawable.ic_list_30)) } } + locationIcon.setOnClickListener { + if(locationOverlay.isMyLocationEnabled){ + //switch off + locationOverlay.disableMyLocation() + locationIcon.setImageDrawable(ContextCompat.getDrawable(requireContext(), R.drawable.location_circlew_grey)) + //show message + Toast.makeText(requireContext(),R.string.location_disabled,Toast.LENGTH_SHORT).show() + } else{ + //switch on + locationOverlay.enableMyLocation() + locationIcon.setImageDrawable(ContextCompat.getDrawable(requireContext(), R.drawable.location_circlew_red)) + //show message + Toast.makeText(requireContext(),R.string.location_enabled,Toast.LENGTH_SHORT).show() + } + } viewModel.setRouteIDQuery(lineID) val keySourcePositions = getString(R.string.pref_positions_source) @@ -659,6 +686,17 @@ ) { //Log.d(MapFragment.DEBUG_TAG, "Updating positions of the buses") //if(busPositionsOverlay == null) busPositionsOverlay = new FolderOverlay(); + // cleanup the patterns + // at first run, the buses which have no direction are still displayed. If those become missing in the data, + // it becomes clear that they don't have the same direction + val currentBusesTripsIds = HashSet(busPositionMarkersByTrip.keys) + for (tripID in currentBusesTripsIds){ + if (!tripsPatterns.keys.contains(tripID)){ + //the tripId is not in the updates anymore, remove it + removeBusMarker(tripID) + } + } + val noPatternsTrips = ArrayList() for (tripID in tripsPatterns.keys) { val (update, tripWithPatternStops) = tripsPatterns[tripID] ?: continue @@ -718,6 +756,8 @@ } } } + + if (noPatternsTrips.size > 0) { Log.i(DEBUG_TAG, "These trips have no matching pattern: $noPatternsTrips") } diff --git a/app/src/main/java/it/reyboz/bustorino/fragments/MapFragment.java b/app/src/main/java/it/reyboz/bustorino/fragments/MapFragment.java --- a/app/src/main/java/it/reyboz/bustorino/fragments/MapFragment.java +++ b/app/src/main/java/it/reyboz/bustorino/fragments/MapFragment.java @@ -76,6 +76,8 @@ import it.reyboz.bustorino.middleware.GeneralActivity; import it.reyboz.bustorino.util.Permissions; +import static it.reyboz.bustorino.fragments.SettingsFragment.LIVE_POSITIONS_PREF_MQTT_VALUE; + public class MapFragment extends ScreenBaseFragment { //private static final String TAG = "Busto-MapActivity"; @@ -234,7 +236,7 @@ String keySourcePositions=getString(R.string.pref_positions_source); useMQTTViewModel = ( PreferenceManager.getDefaultSharedPreferences(requireContext()) - .getString(keySourcePositions,"mqtt").contentEquals("mqtt")); + .getString(keySourcePositions,LIVE_POSITIONS_PREF_MQTT_VALUE).contentEquals(LIVE_POSITIONS_PREF_MQTT_VALUE)); //Start map from bundle @@ -363,7 +365,8 @@ /// choose which to use String keySourcePositions=getString(R.string.pref_positions_source); useMQTTViewModel = PreferenceManager.getDefaultSharedPreferences(requireContext()) - .getString(keySourcePositions,"mqtt").contentEquals("mqtt"); + .getString(keySourcePositions,LIVE_POSITIONS_PREF_MQTT_VALUE).contentEquals( + LIVE_POSITIONS_PREF_MQTT_VALUE); if(livePositionsViewModel !=null) { //gtfsPosViewModel.requestUpdates(); if(useMQTTViewModel) diff --git a/app/src/main/java/it/reyboz/bustorino/fragments/SettingsFragment.java b/app/src/main/java/it/reyboz/bustorino/fragments/SettingsFragment.java --- a/app/src/main/java/it/reyboz/bustorino/fragments/SettingsFragment.java +++ b/app/src/main/java/it/reyboz/bustorino/fragments/SettingsFragment.java @@ -51,6 +51,7 @@ Handler mHandler; public final static String PREF_KEY_STARTUP_SCREEN="startup_screen_to_show"; public final static String KEY_ARRIVALS_FETCHERS_USE = "arrivals_fetchers_use_setting"; + public final static String LIVE_POSITIONS_PREF_MQTT_VALUE="mqtt"; private boolean setSummaryStartupPref = false; diff --git a/app/src/main/java/it/reyboz/bustorino/viewmodels/LivePositionsViewModel.kt b/app/src/main/java/it/reyboz/bustorino/viewmodels/LivePositionsViewModel.kt --- a/app/src/main/java/it/reyboz/bustorino/viewmodels/LivePositionsViewModel.kt +++ b/app/src/main/java/it/reyboz/bustorino/viewmodels/LivePositionsViewModel.kt @@ -85,7 +85,7 @@ } // get the trip IDs in the DB private val gtfsTripsPatternsInDB = tripsIDsInUpdates.switchMap { - Log.i(DEBUG_TI, "tripsIds in updates changed: ${it.size}") + //Log.i(DEBUG_TI, "tripsIds in updates: ${it.size}") gtfsRepo.gtfsDao.getTripPatternStops(it) } //trip IDs to query, which are not present in the DB diff --git a/app/src/main/res/drawable/location_circlew_grey.xml b/app/src/main/res/drawable/location_circlew_grey.xml new file mode 100644 --- /dev/null +++ b/app/src/main/res/drawable/location_circlew_grey.xml @@ -0,0 +1,13 @@ + + + + diff --git a/app/src/main/res/drawable/location_circlew_red.xml b/app/src/main/res/drawable/location_circlew_red.xml new file mode 100644 --- /dev/null +++ b/app/src/main/res/drawable/location_circlew_red.xml @@ -0,0 +1,13 @@ + + + + diff --git a/app/src/main/res/layout/fragment_intro.xml b/app/src/main/res/layout/fragment_intro.xml --- a/app/src/main/res/layout/fragment_intro.xml +++ b/app/src/main/res/layout/fragment_intro.xml @@ -37,14 +37,16 @@ android:textColor="@color/grey_900" android:fontFamily="@font/pitagon_medium" app:layout_constraintBottom_toTopOf="@id/closeAllButton" app:layout_constraintEnd_toEndOf="parent" - app:layout_constraintStart_toStartOf="parent"/> + app:layout_constraintStart_toStartOf="parent" + android:layout_marginBottom="5dp" + />