Fixed satellite track exception crash

This commit is contained in:
Arty Bishop committed 2022-01-09 18:03:20 +00:00
1 parent 6926f87747
commit eebe691419
4 files changed
+44 -7

No files matched your search

@@ -1,3 +1,20 @@
/*
* Look4Sat. Amateur radio satellite tracker and pass predictor.
* Copyright (C) 2019-2021 Arty Bishop (bishop.arty@gmail.com)
*
* 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 <https://www.gnu.org/licenses/>.
*/
package com.rtbishop.look4sat.framework package com.rtbishop.look4sat.framework
import android.Manifest import android.Manifest
@@ -68,6 +68,11 @@ class PreferencesSource @Inject constructor(
} }
} }
fun updatePosition(latitude: Double, longitude: Double) {
val stationPosition = GeoPos(latitude, longitude)
saveStationPosition(stationPosition)
}
fun updatePositionFromGPS(): Boolean { fun updatePositionFromGPS(): Boolean {
return try { return try {
val location = locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER) val location = locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER)
@@ -118,7 +118,7 @@ class MapFragment : Fragment(R.layout.fragment_map) {
textLabelFontSize = 24 textLabelFontSize = 24
textLabelBackgroundColor = Color.TRANSPARENT textLabelBackgroundColor = Color.TRANSPARENT
textLabelForegroundColor = textLabelForegroundColor =
ContextCompat.getColor(requireContext(), R.color.themeLight) ContextCompat.getColor(requireContext(), R.color.themeAccent)
setTextIcon(it.key.params.name) setTextIcon(it.key.params.name)
setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_CENTER) setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_CENTER)
try { try {
@@ -18,8 +18,12 @@
package com.rtbishop.look4sat.presentation.settingsScreen package com.rtbishop.look4sat.presentation.settingsScreen
import android.Manifest import android.Manifest
import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.pm.PackageManager import android.content.pm.PackageManager
import android.location.Location
import android.location.LocationListener
import android.location.LocationManager
import android.net.Uri import android.net.Uri
import android.os.Bundle import android.os.Bundle
import android.text.method.LinkMovementMethod import android.text.method.LinkMovementMethod
@@ -42,7 +46,7 @@ import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject import javax.inject.Inject
@AndroidEntryPoint @AndroidEntryPoint
class SettingsFragment : Fragment(R.layout.fragment_settings) { class SettingsFragment : Fragment(R.layout.fragment_settings), LocationListener {
@Inject @Inject
lateinit var preferences: PreferencesSource lateinit var preferences: PreferencesSource
@@ -173,11 +177,13 @@ class SettingsFragment : Fragment(R.layout.fragment_settings) {
} }
private fun updatePositionFromGPS() { private fun updatePositionFromGPS() {
val locPermString = Manifest.permission.ACCESS_FINE_LOCATION val manager = requireContext().getSystemService(Context.LOCATION_SERVICE) as LocationManager
val locPermResult = ContextCompat.checkSelfPermission(requireContext(), locPermString) val provider = LocationManager.NETWORK_PROVIDER
if (locPermResult == PackageManager.PERMISSION_GRANTED) { val permission = Manifest.permission.ACCESS_COARSE_LOCATION
if (preferences.updatePositionFromGPS()) { val result = ContextCompat.checkSelfPermission(requireContext(), permission)
showSnack(getString(R.string.pref_pos_success)) if (result == PackageManager.PERMISSION_GRANTED) {
if (manager.isProviderEnabled(provider)) {
manager.requestLocationUpdates(provider, 0L, 0f, this)
} else { } else {
showSnack(getString(R.string.pref_pos_gps_null)) showSnack(getString(R.string.pref_pos_gps_null))
} }
@@ -204,4 +210,13 @@ class SettingsFragment : Fragment(R.layout.fragment_settings) {
_binding = null _binding = null
super.onDestroyView() super.onDestroyView()
} }
override fun onLocationChanged(location: Location) {
val manager = requireContext().getSystemService(Context.LOCATION_SERVICE) as LocationManager
manager.removeUpdates(this)
val latitude = location.latitude.round(4)
val longitude = location.longitude.round(4)
preferences.updatePosition(latitude, longitude)
showSnack(getString(R.string.pref_pos_success))
}
} }