diff --git a/app/src/main/java/com/rtbishop/look4sat/framework/LocationSource.kt b/app/src/main/java/com/rtbishop/look4sat/framework/LocationSource.kt index bce5121a..0766904d 100644 --- a/app/src/main/java/com/rtbishop/look4sat/framework/LocationSource.kt +++ b/app/src/main/java/com/rtbishop/look4sat/framework/LocationSource.kt @@ -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 . + */ package com.rtbishop.look4sat.framework import android.Manifest diff --git a/app/src/main/java/com/rtbishop/look4sat/framework/PreferencesSource.kt b/app/src/main/java/com/rtbishop/look4sat/framework/PreferencesSource.kt index 2737a8db..b69dd601 100644 --- a/app/src/main/java/com/rtbishop/look4sat/framework/PreferencesSource.kt +++ b/app/src/main/java/com/rtbishop/look4sat/framework/PreferencesSource.kt @@ -68,6 +68,11 @@ class PreferencesSource @Inject constructor( } } + fun updatePosition(latitude: Double, longitude: Double) { + val stationPosition = GeoPos(latitude, longitude) + saveStationPosition(stationPosition) + } + fun updatePositionFromGPS(): Boolean { return try { val location = locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER) diff --git a/app/src/main/java/com/rtbishop/look4sat/presentation/mapScreen/MapFragment.kt b/app/src/main/java/com/rtbishop/look4sat/presentation/mapScreen/MapFragment.kt index 668d6157..90df824d 100644 --- a/app/src/main/java/com/rtbishop/look4sat/presentation/mapScreen/MapFragment.kt +++ b/app/src/main/java/com/rtbishop/look4sat/presentation/mapScreen/MapFragment.kt @@ -118,7 +118,7 @@ class MapFragment : Fragment(R.layout.fragment_map) { textLabelFontSize = 24 textLabelBackgroundColor = Color.TRANSPARENT textLabelForegroundColor = - ContextCompat.getColor(requireContext(), R.color.themeLight) + ContextCompat.getColor(requireContext(), R.color.themeAccent) setTextIcon(it.key.params.name) setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_CENTER) try { diff --git a/app/src/main/java/com/rtbishop/look4sat/presentation/settingsScreen/SettingsFragment.kt b/app/src/main/java/com/rtbishop/look4sat/presentation/settingsScreen/SettingsFragment.kt index c2676f23..03134d11 100644 --- a/app/src/main/java/com/rtbishop/look4sat/presentation/settingsScreen/SettingsFragment.kt +++ b/app/src/main/java/com/rtbishop/look4sat/presentation/settingsScreen/SettingsFragment.kt @@ -18,8 +18,12 @@ package com.rtbishop.look4sat.presentation.settingsScreen import android.Manifest +import android.content.Context import android.content.Intent import android.content.pm.PackageManager +import android.location.Location +import android.location.LocationListener +import android.location.LocationManager import android.net.Uri import android.os.Bundle import android.text.method.LinkMovementMethod @@ -42,7 +46,7 @@ import dagger.hilt.android.AndroidEntryPoint import javax.inject.Inject @AndroidEntryPoint -class SettingsFragment : Fragment(R.layout.fragment_settings) { +class SettingsFragment : Fragment(R.layout.fragment_settings), LocationListener { @Inject lateinit var preferences: PreferencesSource @@ -173,11 +177,13 @@ class SettingsFragment : Fragment(R.layout.fragment_settings) { } private fun updatePositionFromGPS() { - val locPermString = Manifest.permission.ACCESS_FINE_LOCATION - val locPermResult = ContextCompat.checkSelfPermission(requireContext(), locPermString) - if (locPermResult == PackageManager.PERMISSION_GRANTED) { - if (preferences.updatePositionFromGPS()) { - showSnack(getString(R.string.pref_pos_success)) + val manager = requireContext().getSystemService(Context.LOCATION_SERVICE) as LocationManager + val provider = LocationManager.NETWORK_PROVIDER + val permission = Manifest.permission.ACCESS_COARSE_LOCATION + val result = ContextCompat.checkSelfPermission(requireContext(), permission) + if (result == PackageManager.PERMISSION_GRANTED) { + if (manager.isProviderEnabled(provider)) { + manager.requestLocationUpdates(provider, 0L, 0f, this) } else { showSnack(getString(R.string.pref_pos_gps_null)) } @@ -204,4 +210,13 @@ class SettingsFragment : Fragment(R.layout.fragment_settings) { _binding = null 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)) + } }