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 @@ -53,6 +53,7 @@ import it.reyboz.bustorino.data.gtfs.TripAndPatternWithStops import it.reyboz.bustorino.map.* import it.reyboz.bustorino.map.CustomInfoWindow.TouchResponder +import it.reyboz.bustorino.middleware.LocationUtils import it.reyboz.bustorino.viewmodels.LinesViewModel import it.reyboz.bustorino.viewmodels.LivePositionsViewModel import kotlinx.coroutines.delay @@ -88,7 +89,7 @@ private lateinit var switchButton: ImageButton private var favoritesButton: ImageButton? = null - private lateinit var locationIcon: ImageButton + private var locationIcon: ImageButton? = null private var isLineInFavorite = false private var appContext: Context? = null private val lineSharedPrefMonitor = SharedPreferences.OnSharedPreferenceChangeListener { pref, keychanged -> @@ -146,6 +147,18 @@ private lateinit var stopsOverlay: FolderOverlay private lateinit var locationOverlay: LocationOverlay + private val locationOverlayResponder = object : LocationOverlay.OverlayCallbacks{ + override fun onDisableFollowMyLocation() { + Log.d(DEBUG_TAG, "Follow location disabled") + + } + + override fun onEnableFollowMyLocation() { + Log.d(DEBUG_TAG, "Follow location enabled") + //set image on respective button + locationIcon?.setImageDrawable(ContextCompat.getDrawable(requireContext(), R.drawable.location_circlew_red)) + } + } //fragment actions private lateinit var fragmentListener: CommonFragmentListener @@ -209,7 +222,7 @@ if(map.visibility == View.VISIBLE){ map.visibility = View.GONE stopsRecyclerView.visibility = View.VISIBLE - locationIcon.visibility = View.GONE + locationIcon?.visibility = View.GONE viewModel.setMapShowing(false) liveBusViewModel.stopMatoUpdates() @@ -219,7 +232,7 @@ } else{ stopsRecyclerView.visibility = View.GONE map.visibility = View.VISIBLE - locationIcon.visibility = View.VISIBLE + locationIcon?.visibility = View.VISIBLE viewModel.setMapShowing(true) //map.overlayManager.add(busPositionsOverlay) @@ -232,19 +245,33 @@ switchButton.setImageDrawable(AppCompatResources.getDrawable(requireContext(), R.drawable.ic_list_30)) } } - locationIcon.setOnClickListener { + locationIcon?.let { + if(!LocationUtils.isLocationEnabled(requireContext())) + setLocationIconEnabled(false) + } + 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() + //set image on respective button + setLocationIconEnabled(false) + if(context!=null) { + if (LocationUtils.isLocationEnabled(context)) { + //show message + Toast.makeText(context, 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() + if(context!=null) { + if (LocationUtils.isLocationEnabled(context)) { + //set image on button + setLocationIconEnabled(true) + //show message + Toast.makeText(context, R.string.location_enabled, Toast.LENGTH_SHORT).show() + } + } } } viewModel.setRouteIDQuery(lineID) @@ -360,6 +387,13 @@ return rootView } + private fun setLocationIconEnabled(setTrue: Boolean){ + if(setTrue) + locationIcon?.setImageDrawable(ContextCompat.getDrawable(requireContext(), R.drawable.location_circlew_red)) + else + locationIcon?.setImageDrawable(ContextCompat.getDrawable(requireContext(), R.drawable.location_circlew_grey)) + } + private fun initializeMap(rootView : View){ val ctx = requireContext().applicationContext Configuration.getInstance().load(ctx, PreferenceManager.getDefaultSharedPreferences(ctx)) @@ -368,16 +402,7 @@ map.let { it.setTileSource(TileSourceFactory.MAPNIK) - locationOverlay = LocationOverlay.createLocationOverlay(true, it, requireContext(), object : LocationOverlay.OverlayCallbacks{ - override fun onDisableFollowMyLocation() { - Log.d(DEBUG_TAG, "Follow location disabled") - } - - override fun onEnableFollowMyLocation() { - Log.d(DEBUG_TAG, "Follow location enabled") - } - - }) + locationOverlay = LocationOverlay.createLocationOverlay(true, it, requireContext(), locationOverlayResponder) locationOverlay.disableFollowLocation() stopsOverlay = FolderOverlay() diff --git a/app/src/main/java/it/reyboz/bustorino/fragments/ScreenBaseFragment.java b/app/src/main/java/it/reyboz/bustorino/fragments/ScreenBaseFragment.java --- a/app/src/main/java/it/reyboz/bustorino/fragments/ScreenBaseFragment.java +++ b/app/src/main/java/it/reyboz/bustorino/fragments/ScreenBaseFragment.java @@ -9,7 +9,6 @@ import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; -import com.google.android.material.floatingactionbutton.FloatingActionButton; import it.reyboz.bustorino.BuildConfig; import static android.content.Context.MODE_PRIVATE; diff --git a/app/src/main/java/it/reyboz/bustorino/middleware/AppLocationManager.kt b/app/src/main/java/it/reyboz/bustorino/middleware/AppLocationManager.kt --- a/app/src/main/java/it/reyboz/bustorino/middleware/AppLocationManager.kt +++ b/app/src/main/java/it/reyboz/bustorino/middleware/AppLocationManager.kt @@ -31,6 +31,8 @@ /** * Singleton class used to access location. Possibly extended with other location sources. + * + * 2024: This is far too much. We need to simplify the whole mechanism (no more singleton) */ class AppLocationManager private constructor(context: Context) : LocationListener { private val appContext: Context diff --git a/app/src/main/java/it/reyboz/bustorino/middleware/LocationUtils.java b/app/src/main/java/it/reyboz/bustorino/middleware/LocationUtils.java new file mode 100644 --- /dev/null +++ b/app/src/main/java/it/reyboz/bustorino/middleware/LocationUtils.java @@ -0,0 +1,28 @@ +package it.reyboz.bustorino.middleware; + +import android.content.Context; +import android.location.LocationManager; +import android.os.Build; +import android.provider.Settings; +import androidx.core.content.ContextCompat; + +public class LocationUtils { + + public static LocationManager getSystemLocationManager(Context context){ + return ContextCompat.getSystemService(context, LocationManager.class); + } + + //thanks to https://stackoverflow.com/questions/10311834/how-to-check-if-location-services-are-enabled + public static Boolean isLocationEnabled(Context context) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { + // This is a new method provided in API 28 + LocationManager lm = getSystemLocationManager(context); + return lm.isLocationEnabled(); + } else { + // This was deprecated in API 28 + int mode = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE, + Settings.Secure.LOCATION_MODE_OFF); + return (mode != Settings.Secure.LOCATION_MODE_OFF); + } + } +}