Cleaned up preference.xml, added setPositionFromGPS button
This commit is contained in:
1 parent
cfdba41ed5
commit
e06b1bcb11
10 files changed
+88
-143
No files matched your search
+16
-9
@@ -17,23 +17,27 @@
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
apply plugin: "com.android.application"
|
||||
apply plugin: "kotlin-android"
|
||||
apply plugin: "kotlin-kapt"
|
||||
apply plugin: "kotlin-android-extensions"
|
||||
apply plugin: "dagger.hilt.android.plugin"
|
||||
plugins {
|
||||
id "com.android.application"
|
||||
id "kotlin-android"
|
||||
id "kotlin-kapt"
|
||||
id "kotlin-android-extensions"
|
||||
id "dagger.hilt.android.plugin"
|
||||
}
|
||||
|
||||
android {
|
||||
compileSdkVersion 30
|
||||
buildToolsVersion "30.0.2"
|
||||
|
||||
defaultConfig {
|
||||
applicationId "com.rtbishop.look4sat"
|
||||
minSdkVersion 21
|
||||
targetSdkVersion 30
|
||||
versionCode 18
|
||||
versionName '1.4.8'
|
||||
versionName "1.4.8"
|
||||
resConfigs "en", "ru"
|
||||
}
|
||||
|
||||
buildTypes {
|
||||
release {
|
||||
postprocessing {
|
||||
@@ -44,14 +48,15 @@ android {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
configurations {
|
||||
all {
|
||||
exclude module: 'commons-logging'
|
||||
exclude module: "commons-logging"
|
||||
}
|
||||
}
|
||||
compileOptions {
|
||||
sourceCompatibility = 1.8
|
||||
targetCompatibility = 1.8
|
||||
sourceCompatibility JavaVersion.VERSION_1_8
|
||||
targetCompatibility JavaVersion.VERSION_1_8
|
||||
}
|
||||
kotlinOptions {
|
||||
jvmTarget = "1.8"
|
||||
@@ -81,4 +86,6 @@ dependencies {
|
||||
|
||||
implementation "org.osmdroid:osmdroid-android:$osmdroid_version"
|
||||
implementation "com.github.davidmoten:predict4java:$predict4java_version"
|
||||
|
||||
testImplementation "junit:junit:$junit_version"
|
||||
}
|
||||
@@ -35,9 +35,7 @@ import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.launch
|
||||
import java.util.*
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Singleton
|
||||
class SharedViewModel @ViewModelInject constructor(
|
||||
private val prefsManager: PrefsManager,
|
||||
private val sourcesRepo: SourcesRepo,
|
||||
|
||||
@@ -23,7 +23,6 @@ import android.content.Context
|
||||
import android.content.SharedPreferences
|
||||
import android.location.LocationManager
|
||||
import androidx.preference.PreferenceManager
|
||||
import com.rtbishop.look4sat.utility.PrefsManager
|
||||
import dagger.Module
|
||||
import dagger.Provides
|
||||
import dagger.hilt.InstallIn
|
||||
@@ -43,9 +42,4 @@ object UtilityModule {
|
||||
fun getLocationManager(@ActivityContext context: Context): LocationManager {
|
||||
return context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
|
||||
}
|
||||
|
||||
@Provides
|
||||
fun getPrefsManager(prefs: SharedPreferences, locationManager: LocationManager): PrefsManager {
|
||||
return PrefsManager(prefs, locationManager)
|
||||
}
|
||||
}
|
||||
@@ -19,81 +19,56 @@
|
||||
|
||||
package com.rtbishop.look4sat.ui.fragments
|
||||
|
||||
import android.content.SharedPreferences
|
||||
import android.location.LocationManager
|
||||
import android.os.Bundle
|
||||
import android.text.InputType
|
||||
import androidx.preference.EditTextPreference
|
||||
import android.view.View
|
||||
import androidx.preference.Preference
|
||||
import androidx.preference.PreferenceFragmentCompat
|
||||
import com.rtbishop.look4sat.R
|
||||
import com.rtbishop.look4sat.utility.PrefsManager
|
||||
import com.rtbishop.look4sat.utility.Utilities.round
|
||||
import com.rtbishop.look4sat.utility.Utilities.snack
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import javax.inject.Inject
|
||||
|
||||
@AndroidEntryPoint
|
||||
class PrefsFragment : PreferenceFragmentCompat(),
|
||||
SharedPreferences.OnSharedPreferenceChangeListener {
|
||||
class PrefsFragment : PreferenceFragmentCompat() {
|
||||
|
||||
@Inject
|
||||
lateinit var locationManager: LocationManager
|
||||
|
||||
@Inject
|
||||
lateinit var prefsManager: PrefsManager
|
||||
|
||||
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
|
||||
setPreferencesFromResource(R.xml.preference, rootKey)
|
||||
}
|
||||
|
||||
findPreference<EditTextPreference>(PrefsManager.keyLatitude)?.apply {
|
||||
setOnBindEditTextListener {
|
||||
it.inputType = InputType.TYPE_CLASS_NUMBER or
|
||||
InputType.TYPE_NUMBER_FLAG_DECIMAL or
|
||||
InputType.TYPE_NUMBER_FLAG_SIGNED
|
||||
}
|
||||
setOnPreferenceChangeListener { _, newValue ->
|
||||
val valueStr = newValue.toString()
|
||||
if (valueStr.isEmpty() || valueStr == "-" || valueStr.toDouble() < -90.0 || valueStr.toDouble() > 90.0) {
|
||||
getString(R.string.pref_lat_input_error).snack(requireView())
|
||||
return@setOnPreferenceChangeListener false
|
||||
}
|
||||
return@setOnPreferenceChangeListener true
|
||||
}
|
||||
}
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
|
||||
findPreference<EditTextPreference>(PrefsManager.keyLongitude)?.apply {
|
||||
setOnBindEditTextListener {
|
||||
it.inputType = InputType.TYPE_CLASS_NUMBER or
|
||||
InputType.TYPE_NUMBER_FLAG_DECIMAL or
|
||||
InputType.TYPE_NUMBER_FLAG_SIGNED
|
||||
}
|
||||
setOnPreferenceChangeListener { _, newValue ->
|
||||
val valueStr = newValue.toString()
|
||||
if (valueStr.isEmpty() || valueStr == "-" || valueStr.toDouble() < -180.0 || valueStr.toDouble() > 180.0) {
|
||||
getString(R.string.pref_lon_input_error).snack(requireView())
|
||||
return@setOnPreferenceChangeListener false
|
||||
}
|
||||
return@setOnPreferenceChangeListener true
|
||||
}
|
||||
}
|
||||
|
||||
findPreference<EditTextPreference>(PrefsManager.keyAltitude)?.apply {
|
||||
setOnBindEditTextListener {
|
||||
it.inputType = InputType.TYPE_CLASS_NUMBER or
|
||||
InputType.TYPE_NUMBER_FLAG_DECIMAL or
|
||||
InputType.TYPE_NUMBER_FLAG_SIGNED
|
||||
}
|
||||
setOnPreferenceChangeListener { _, newValue ->
|
||||
val valueStr = newValue.toString()
|
||||
if (valueStr.isEmpty() || valueStr == "-" || valueStr.toDouble() < -413.0 || valueStr.toDouble() > 8850.0) {
|
||||
getString(R.string.pref_alt_input_error).snack(requireView())
|
||||
return@setOnPreferenceChangeListener false
|
||||
}
|
||||
return@setOnPreferenceChangeListener true
|
||||
findPreference<Preference>(PrefsManager.keyPositionGPS)?.apply {
|
||||
setOnPreferenceClickListener {
|
||||
setPositionFromGPS()
|
||||
return@setOnPreferenceClickListener true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) {}
|
||||
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
preferenceManager.sharedPreferences.registerOnSharedPreferenceChangeListener(this)
|
||||
}
|
||||
|
||||
override fun onPause() {
|
||||
super.onPause()
|
||||
preferenceManager.sharedPreferences.unregisterOnSharedPreferenceChangeListener(this)
|
||||
private fun setPositionFromGPS() {
|
||||
try {
|
||||
val location = locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER)
|
||||
if (location != null) {
|
||||
val latitude = location.latitude.round(4)
|
||||
val longitude = location.longitude.round(4)
|
||||
val altitude = location.altitude.round(1)
|
||||
prefsManager.setStationPosition(latitude, longitude, altitude)
|
||||
getString(R.string.pref_pos_gps_success).snack(requireView())
|
||||
} else {
|
||||
getString(R.string.pref_pos_gps_null).snack(requireView())
|
||||
}
|
||||
} catch (e: SecurityException) {
|
||||
getString(R.string.pref_pos_gps_error).snack(requireView())
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -21,42 +21,47 @@ package com.rtbishop.look4sat.utility
|
||||
|
||||
import android.content.SharedPreferences
|
||||
import android.hardware.GeomagneticField
|
||||
import android.location.LocationManager
|
||||
import androidx.core.content.edit
|
||||
import com.github.amsacode.predict4java.GroundStationPosition
|
||||
import com.rtbishop.look4sat.data.PassPrefs
|
||||
import com.rtbishop.look4sat.utility.Utilities.round
|
||||
import javax.inject.Inject
|
||||
|
||||
class PrefsManager @Inject constructor(
|
||||
private val preferences: SharedPreferences,
|
||||
private val locationManager: LocationManager
|
||||
) {
|
||||
class PrefsManager @Inject constructor(private val preferences: SharedPreferences) {
|
||||
companion object {
|
||||
const val hoursAhead = "hoursAhead"
|
||||
const val minimalElevation = "minimalElevation"
|
||||
const val keyLatitude = "latitude"
|
||||
const val keyLongitude = "longitude"
|
||||
const val keyAltitude = "altitude"
|
||||
const val keyCompass = "compass"
|
||||
const val keyTimeUtc = "timeUTC"
|
||||
const val defaultGSP = "0.0"
|
||||
const val keyIsFirstLaunch = "keyIsFirstLaunch"
|
||||
const val keyTimeUTC = "timeUTC"
|
||||
const val keyHoursAhead = "hoursAhead"
|
||||
const val keyMinElevation = "minElevation"
|
||||
const val keyPositionGPS = "setPositionGPS"
|
||||
const val keyIsFirstLaunch = "isFirstLaunch"
|
||||
}
|
||||
|
||||
fun getPassPrefs(): PassPrefs {
|
||||
val hoursAhead = preferences.getInt(hoursAhead, 8)
|
||||
val minEl = preferences.getInt(minimalElevation, 16).toDouble()
|
||||
val hoursAhead = preferences.getInt(keyHoursAhead, 8)
|
||||
val minEl = preferences.getInt(keyMinElevation, 16).toDouble()
|
||||
return PassPrefs(hoursAhead, minEl)
|
||||
}
|
||||
|
||||
fun getStationPosition(): GroundStationPosition {
|
||||
val defaultGSP = "0.0"
|
||||
val lat = preferences.getString(keyLatitude, defaultGSP)!!.toDouble()
|
||||
val lon = preferences.getString(keyLongitude, defaultGSP)!!.toDouble()
|
||||
val alt = preferences.getString(keyAltitude, defaultGSP)!!.toDouble()
|
||||
return GroundStationPosition(lat, lon, alt)
|
||||
}
|
||||
|
||||
fun setStationPosition(latitude: Double, longitude: Double, altitude: Double) {
|
||||
preferences.edit {
|
||||
putString(keyLatitude, latitude.toString())
|
||||
putString(keyLongitude, longitude.toString())
|
||||
putString(keyAltitude, altitude.toString())
|
||||
apply()
|
||||
}
|
||||
}
|
||||
|
||||
fun getMagDeclination(): Float {
|
||||
val stationPosition = getStationPosition()
|
||||
val lat = stationPosition.latitude.toFloat()
|
||||
@@ -65,25 +70,8 @@ class PrefsManager @Inject constructor(
|
||||
return GeomagneticField(lat, lon, alt, System.currentTimeMillis()).declination
|
||||
}
|
||||
|
||||
fun setStationPositionFromGPS() {
|
||||
try {
|
||||
locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER)?.let {
|
||||
val lat = it.latitude.round(4)
|
||||
val lon = it.longitude.round(4)
|
||||
val alt = it.altitude.round(1)
|
||||
preferences.edit {
|
||||
putString(keyLatitude, lat.toString())
|
||||
putString(keyLongitude, lon.toString())
|
||||
putString(keyAltitude, alt.toString())
|
||||
apply()
|
||||
}
|
||||
}
|
||||
} catch (e: SecurityException) {
|
||||
}
|
||||
}
|
||||
|
||||
fun shouldUseUTC(): Boolean {
|
||||
return preferences.getBoolean(keyTimeUtc, false)
|
||||
return preferences.getBoolean(keyTimeUTC, false)
|
||||
}
|
||||
|
||||
fun shouldUseCompass(): Boolean {
|
||||
|
||||
@@ -38,7 +38,7 @@
|
||||
android:gravity="center"
|
||||
android:maxLines="1"
|
||||
android:text="@string/trans_description"
|
||||
android:textSize="16sp"
|
||||
android:textSize="15sp"
|
||||
app:layout_constraintEnd_toStartOf="@+id/img_uplink"
|
||||
app:layout_constraintHorizontal_chainStyle="spread_inside"
|
||||
app:layout_constraintStart_toEndOf="@+id/img_downlink"
|
||||
@@ -77,7 +77,7 @@
|
||||
android:layout_marginTop="4dp"
|
||||
android:layout_marginBottom="2dp"
|
||||
android:text="@string/trans_mode"
|
||||
android:textSize="16sp"
|
||||
android:textSize="15sp"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toStartOf="@+id/isInverted"
|
||||
app:layout_constraintHorizontal_chainStyle="spread_inside"
|
||||
@@ -90,7 +90,7 @@
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginEnd="6dp"
|
||||
android:text="@string/trans_inverted"
|
||||
android:textSize="16sp"
|
||||
android:textSize="15sp"
|
||||
app:layout_constraintBaseline_toBaselineOf="@+id/mode"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toEndOf="@+id/mode" />
|
||||
|
||||
@@ -52,16 +52,14 @@
|
||||
<string name="pat_osm_latLon">Шир: %.1f°, Дол: %.1f°</string>
|
||||
<string name="pat_osm_vel">Скор: %.2f км/с</string>
|
||||
|
||||
<string name="pref_gsp_title">Местоположение наземной станции</string>
|
||||
<string name="pref_pos_gps">Установить позицию по данным GPS</string>
|
||||
<string name="pref_pos_gps_success">Позиция успешно обновлена</string>
|
||||
<string name="pref_pos_gps_null">Местоположение отключено</string>
|
||||
<string name="pref_pos_gps_error">Нет разрешения использовать геоданные</string>
|
||||
<string name="pref_pass_title">Настройки пролетов спутников</string>
|
||||
<string name="pref_hours_title">Количество часов</string>
|
||||
<string name="pref_elev_title">Минимальная элевация</string>
|
||||
<string name="pref_gsp_title">Местоположение наземной станции</string>
|
||||
<string name="pref_lat_title">Широта, град</string>
|
||||
<string name="pref_lat_input_error">Введите значение между -90° и 90°</string>
|
||||
<string name="pref_lon_title">Долгота, град</string>
|
||||
<string name="pref_lon_input_error">Введите значение между -180° и 180°</string>
|
||||
<string name="pref_alt_title">Высота, м</string>
|
||||
<string name="pref_alt_input_error">Введите значение между -413м и 8850м</string>
|
||||
<string name="pref_other_title">Другие настройки</string>
|
||||
<string name="pref_compass_title">Использовать компас</string>
|
||||
<string name="pref_time_utc_title">Показывать время пролета по UTC</string>
|
||||
|
||||
@@ -71,16 +71,14 @@
|
||||
<string name="pat_osm_latLon">Lat: %.1f°, Lon: %.1f°</string>
|
||||
<string name="pat_osm_vel">Vel: %.2f km/s</string>
|
||||
|
||||
<string name="pref_gsp_title">Ground station position</string>
|
||||
<string name="pref_pos_gps">Set position from GPS</string>
|
||||
<string name="pref_pos_gps_success">Position updated successfully</string>
|
||||
<string name="pref_pos_gps_null">Location is switched off</string>
|
||||
<string name="pref_pos_gps_error">Check your location permissions</string>
|
||||
<string name="pref_pass_title">Satellite pass preferences</string>
|
||||
<string name="pref_hours_title">Hours ahead</string>
|
||||
<string name="pref_elev_title">Minimal elevation</string>
|
||||
<string name="pref_gsp_title">Ground station position</string>
|
||||
<string name="pref_lat_title">Latitude, deg</string>
|
||||
<string name="pref_lat_input_error">Set the value between -90° and 90°</string>
|
||||
<string name="pref_lon_title">Longitude, deg</string>
|
||||
<string name="pref_lon_input_error">Set the value between -180° and 180°</string>
|
||||
<string name="pref_alt_title">Altitude, m</string>
|
||||
<string name="pref_alt_input_error">Set the value between -413m and 8850m</string>
|
||||
<string name="pref_other_title">Other</string>
|
||||
<string name="pref_time_utc_title">Show pass time in UTC</string>
|
||||
<string name="pref_compass_title">Use sensors to rotate PolarView</string>
|
||||
|
||||
@@ -22,24 +22,10 @@
|
||||
<PreferenceCategory
|
||||
android:title="@string/pref_gsp_title"
|
||||
app:iconSpaceReserved="false">
|
||||
<EditTextPreference
|
||||
android:defaultValue="0.0"
|
||||
android:key="latitude"
|
||||
android:title="@string/pref_lat_title"
|
||||
app:iconSpaceReserved="false"
|
||||
app:useSimpleSummaryProvider="true" />
|
||||
<EditTextPreference
|
||||
android:defaultValue="0.0"
|
||||
android:key="longitude"
|
||||
android:title="@string/pref_lon_title"
|
||||
app:iconSpaceReserved="false"
|
||||
app:useSimpleSummaryProvider="true" />
|
||||
<EditTextPreference
|
||||
android:defaultValue="0.0"
|
||||
android:key="altitude"
|
||||
android:title="@string/pref_alt_title"
|
||||
app:iconSpaceReserved="false"
|
||||
app:useSimpleSummaryProvider="true" />
|
||||
<Preference
|
||||
android:key="setPositionGPS"
|
||||
android:title="@string/pref_pos_gps"
|
||||
app:iconSpaceReserved="false" />
|
||||
</PreferenceCategory>
|
||||
<PreferenceCategory
|
||||
android:title="@string/pref_pass_title"
|
||||
@@ -53,7 +39,7 @@
|
||||
app:min="1"
|
||||
app:showSeekBarValue="true" />
|
||||
<SeekBarPreference
|
||||
android:key="minimalElevation"
|
||||
android:key="minElevation"
|
||||
android:max="72"
|
||||
android:title="@string/pref_elev_title"
|
||||
app:adjustable="false"
|
||||
|
||||
@@ -31,6 +31,7 @@ buildscript {
|
||||
retrofit_version = '2.9.0'
|
||||
osmdroid_version = '6.1.8'
|
||||
predict4java_version = '1.3.1'
|
||||
junit_version = '4.13.1'
|
||||
}
|
||||
repositories {
|
||||
google()
|
||||
|
||||
Reference in new issue
Block a user