diff --git a/app/src/main/java/com/rtbishop/look4sat/di/ApplicationModule.kt b/app/src/main/java/com/rtbishop/look4sat/di/ApplicationModule.kt index 6da7115a..30307255 100644 --- a/app/src/main/java/com/rtbishop/look4sat/di/ApplicationModule.kt +++ b/app/src/main/java/com/rtbishop/look4sat/di/ApplicationModule.kt @@ -22,7 +22,6 @@ import android.content.Context import android.content.SharedPreferences import android.hardware.SensorManager import android.location.LocationManager -import android.view.WindowManager import androidx.preference.PreferenceManager import dagger.Module import dagger.Provides @@ -49,11 +48,6 @@ object ApplicationModule { return context.getSystemService(Context.SENSOR_SERVICE) as SensorManager } - @Provides - fun provideWindowManager(@ApplicationContext context: Context): WindowManager { - return context.getSystemService(Context.WINDOW_SERVICE) as WindowManager - } - @Provides fun provideSharedPreferences(@ApplicationContext context: Context): SharedPreferences { return PreferenceManager.getDefaultSharedPreferences(context) diff --git a/app/src/main/java/com/rtbishop/look4sat/ui/entriesScreen/EntriesFragment.kt b/app/src/main/java/com/rtbishop/look4sat/ui/entriesScreen/EntriesFragment.kt index f4754b24..0afa24ae 100644 --- a/app/src/main/java/com/rtbishop/look4sat/ui/entriesScreen/EntriesFragment.kt +++ b/app/src/main/java/com/rtbishop/look4sat/ui/entriesScreen/EntriesFragment.kt @@ -33,6 +33,7 @@ import com.rtbishop.look4sat.data.model.TleSource import com.rtbishop.look4sat.databinding.FragmentEntriesBinding import com.rtbishop.look4sat.utility.RecyclerDivider import com.rtbishop.look4sat.utility.getNavResult +import com.rtbishop.look4sat.utility.navigateSafe import dagger.hilt.android.AndroidEntryPoint @AndroidEntryPoint @@ -61,7 +62,9 @@ class EntriesFragment : Fragment(R.layout.fragment_entries) { (itemAnimator as SimpleItemAnimator).supportsChangeAnimations = false addItemDecoration(RecyclerDivider(R.drawable.rec_divider_light)) } - importWeb.setOnClickListener { findNavController().navigate(R.id.nav_dialog_sources) } + importWeb.setOnClickListener { + findNavController().navigateSafe(R.id.action_entries_to_sources) + } importFile.setOnClickListener { filePicker.launch("*/*") } selectMode.setOnClickListener { viewModel.createModesDialog(requireContext()).show() } selectAll.setOnClickListener { viewModel.selectCurrentItems() } diff --git a/app/src/main/java/com/rtbishop/look4sat/ui/mapScreen/MapViewModel.kt b/app/src/main/java/com/rtbishop/look4sat/ui/mapScreen/MapViewModel.kt index f9878e43..ff585913 100644 --- a/app/src/main/java/com/rtbishop/look4sat/ui/mapScreen/MapViewModel.kt +++ b/app/src/main/java/com/rtbishop/look4sat/ui/mapScreen/MapViewModel.kt @@ -24,7 +24,6 @@ import com.github.amsacode.predict4java.Position import com.github.amsacode.predict4java.SatPos import com.github.amsacode.predict4java.Satellite import com.rtbishop.look4sat.data.model.SelectedSat -import com.rtbishop.look4sat.utility.PrefsManager import com.rtbishop.look4sat.data.repository.SatelliteRepo import com.rtbishop.look4sat.utility.* import dagger.hilt.android.lifecycle.HiltViewModel @@ -35,7 +34,6 @@ import org.osmdroid.views.overlay.Overlay import org.osmdroid.views.overlay.Polygon import org.osmdroid.views.overlay.Polyline import timber.log.Timber -import java.lang.IllegalArgumentException import java.util.* import javax.inject.Inject @@ -142,7 +140,7 @@ class MapViewModel @Inject constructor( } private fun getSatTrack(satellite: Satellite): Overlay { - val positions = satellite.getPredictor(gsp).getPositions(dateNow, 15, 3, 3.2) + val positions = satellite.getPredictor(gsp).getPositions(dateNow, 15, 0, 3.2) val trackOverlay = FolderOverlay() val trackPoints = mutableListOf() var oldLon = 0.0 diff --git a/app/src/main/java/com/rtbishop/look4sat/ui/polarScreen/Orientation.kt b/app/src/main/java/com/rtbishop/look4sat/ui/polarScreen/Orientation.kt deleted file mode 100644 index 9d647663..00000000 --- a/app/src/main/java/com/rtbishop/look4sat/ui/polarScreen/Orientation.kt +++ /dev/null @@ -1,85 +0,0 @@ -/* - * 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.ui.polarScreen - -import android.hardware.Sensor -import android.hardware.SensorEvent -import android.hardware.SensorEventListener -import android.hardware.SensorManager -import android.view.Surface -import android.view.WindowManager -import javax.inject.Inject -import kotlin.math.atan2 -import kotlin.math.round - -class Orientation @Inject constructor( - private val mSensorManager: SensorManager, - private val mWindowManager: WindowManager -) : SensorEventListener { - - interface OrientationListener { - fun onOrientationChanged(azimuth: Float, pitch: Float, roll: Float) - } - - private val mRotationSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR) - private val mRotationMatrix = FloatArray(9) - private val mOrientationValues = FloatArray(3) - private val mOneRadDegrees = 57.295779513f - private var mListener: OrientationListener? = null - - fun startListening(listener: OrientationListener) { - if (mListener !== listener) { - mListener = listener - mSensorManager.registerListener(this, mRotationSensor, 16000) - } else return - } - - fun stopListening() { - mSensorManager.unregisterListener(this) - mListener = null - } - - override fun onAccuracyChanged(sensor: Sensor, accuracy: Int) {} - - override fun onSensorChanged(event: SensorEvent) { - if (mListener == null) return - else if (event.sensor == mRotationSensor) updateOrientation(event.values) - } - - @Suppress("DEPRECATION") - private fun updateOrientation(rotationVector: FloatArray) { - SensorManager.getRotationMatrixFromVector(mRotationMatrix, rotationVector) - val (matrixColumn, sense) = when (mWindowManager.defaultDisplay.rotation) { - Surface.ROTATION_0 -> Pair(0, 1) - Surface.ROTATION_90 -> Pair(1, -1) - Surface.ROTATION_180 -> Pair(0, -1) - Surface.ROTATION_270 -> Pair(1, 1) - else -> error("Invalid screen rotation value") - } - val x = sense * mRotationMatrix[matrixColumn] - val y = sense * mRotationMatrix[matrixColumn + 3] - val azimuth = -atan2(y, x) * mOneRadDegrees -// SensorManager.getOrientation(mRotationMatrix, mOrientationValues) -// val azimuth = mOrientationValues[0] * mOneRadDegrees - val pitch = mOrientationValues[1] * mOneRadDegrees - val roll = mOrientationValues[2] * mOneRadDegrees - val magneticAzimuth = (azimuth + 360f) % 360f - val roundedAzimuth = round(magneticAzimuth * 10) / 10 - mListener?.onOrientationChanged(roundedAzimuth, pitch, roll) - } -} \ No newline at end of file diff --git a/app/src/main/java/com/rtbishop/look4sat/ui/polarScreen/OrientationProvider.kt b/app/src/main/java/com/rtbishop/look4sat/ui/polarScreen/OrientationProvider.kt new file mode 100644 index 00000000..4c1adf42 --- /dev/null +++ b/app/src/main/java/com/rtbishop/look4sat/ui/polarScreen/OrientationProvider.kt @@ -0,0 +1,77 @@ +/* + * 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.ui.polarScreen + +import android.hardware.Sensor +import android.hardware.SensorEvent +import android.hardware.SensorEventListener +import android.hardware.SensorManager +import javax.inject.Inject +import kotlin.math.round + +class OrientationProvider @Inject constructor(private val sensorManager: SensorManager) : + SensorEventListener { + + interface OrientationListener { + fun onOrientationChanged(azimuth: Float, pitch: Float, roll: Float) + } + + private val sensor: Sensor? = sensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR) + private val rotationMatrix = FloatArray(9) + private val orientationValues = FloatArray(3) + private val oneRadDegrees = 57.295779513f + private var sensorAccuracy: Int = SensorManager.SENSOR_STATUS_UNRELIABLE + private var orientationListener: OrientationListener? = null + + fun startListening(listener: OrientationListener) { + if (sensor != null && orientationListener !== listener) { + orientationListener = listener + sensorManager.registerListener(this, sensor, 16000) + } else return + } + + fun stopListening() { + sensorManager.unregisterListener(this) + orientationListener = null + } + + override fun onAccuracyChanged(sensor: Sensor, accuracy: Int) { + sensorAccuracy = accuracy + } + + override fun onSensorChanged(event: SensorEvent) { + when { + orientationListener == null -> return + sensorAccuracy == SensorManager.SENSOR_STATUS_UNRELIABLE -> return + event.sensor == sensor -> updateOrientation(event.values) + } + } + + private fun updateOrientation(rotationVector: FloatArray) { + SensorManager.getRotationMatrixFromVector(rotationMatrix, rotationVector) + SensorManager.getOrientation(rotationMatrix, orientationValues) + val azimuth = orientationValues[0] * oneRadDegrees + val pitch = orientationValues[1] * oneRadDegrees + val roll = orientationValues[2] * oneRadDegrees + val magneticAzimuth = (azimuth + 360f) % 360f + val roundedAzimuth = round(magneticAzimuth * 10) / 10 + val roundedPitch = round(pitch * 10) / 10 + val roundedRoll = round(roll * 10) / 10 + orientationListener?.onOrientationChanged(roundedAzimuth, roundedPitch, roundedRoll) + } +} diff --git a/app/src/main/java/com/rtbishop/look4sat/ui/polarScreen/PolarFragment.kt b/app/src/main/java/com/rtbishop/look4sat/ui/polarScreen/PolarFragment.kt index b1b2c51a..08703a86 100644 --- a/app/src/main/java/com/rtbishop/look4sat/ui/polarScreen/PolarFragment.kt +++ b/app/src/main/java/com/rtbishop/look4sat/ui/polarScreen/PolarFragment.kt @@ -29,6 +29,7 @@ import com.rtbishop.look4sat.data.model.SatPass import com.rtbishop.look4sat.databinding.FragmentPolarBinding import com.rtbishop.look4sat.utility.RecyclerDivider import com.rtbishop.look4sat.utility.formatForTimer +import com.rtbishop.look4sat.utility.navigateSafe import dagger.hilt.android.AndroidEntryPoint import java.util.* @@ -119,7 +120,7 @@ class PolarFragment : Fragment(R.layout.fragment_polar) { binding.polarTimer.text = millisBeforeEnd.formatForTimer() if (dateNow.after(satPass.losDate)) { binding.polarTimer.text = 0L.formatForTimer() - findNavController().popBackStack() + findNavController().navigateSafe(R.id.action_polar_to_passes) } } } else { diff --git a/app/src/main/java/com/rtbishop/look4sat/ui/polarScreen/PolarViewModel.kt b/app/src/main/java/com/rtbishop/look4sat/ui/polarScreen/PolarViewModel.kt index 9b192fdd..8d05495d 100644 --- a/app/src/main/java/com/rtbishop/look4sat/ui/polarScreen/PolarViewModel.kt +++ b/app/src/main/java/com/rtbishop/look4sat/ui/polarScreen/PolarViewModel.kt @@ -34,12 +34,12 @@ import javax.inject.Inject @HiltViewModel class PolarViewModel @Inject constructor( - private val orientation: Orientation, + private val orientationProvider: OrientationProvider, private val prefsManager: PrefsManager, private val passesRepo: PassesRepo, private val satelliteRepo: SatelliteRepo, @IoDispatcher private val ioDispatcher: CoroutineDispatcher -) : ViewModel(), Orientation.OrientationListener { +) : ViewModel(), OrientationProvider.OrientationListener { private val magDeclination = prefsManager.getMagDeclination() private val _transmitters = MutableLiveData>() @@ -59,11 +59,11 @@ class PolarViewModel @Inject constructor( } fun enableSensor() { - if (prefsManager.shouldUseCompass()) orientation.startListening(this) + if (prefsManager.shouldUseCompass()) orientationProvider.startListening(this) } fun disableSensor() { - if (prefsManager.shouldUseCompass()) orientation.stopListening() + if (prefsManager.shouldUseCompass()) orientationProvider.stopListening() } override fun onOrientationChanged(azimuth: Float, pitch: Float, roll: Float) { diff --git a/app/src/main/res/layout/fragment_polar.xml b/app/src/main/res/layout/fragment_polar.xml index 27b6af97..68cd0afe 100644 --- a/app/src/main/res/layout/fragment_polar.xml +++ b/app/src/main/res/layout/fragment_polar.xml @@ -78,25 +78,23 @@ android:id="@+id/recycler" android:layout_width="0dp" android:layout_height="0dp" - android:layout_marginTop="23dp" android:padding="2dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" - app:layout_constraintTop_toBottomOf="@+id/frame" /> + app:layout_constraintTop_toBottomOf="@+id/sat_name" /> + app:layout_constraintTop_toTopOf="@+id/recycler" /> + + . - */ -package com.rtbishop.look4sat - -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.ExperimentalCoroutinesApi -import kotlinx.coroutines.test.TestCoroutineDispatcher -import kotlinx.coroutines.test.resetMain -import kotlinx.coroutines.test.setMain -import org.junit.rules.TestWatcher -import org.junit.runner.Description - -@ExperimentalCoroutinesApi -class CoroutineTestRule(private val dispatcher: TestCoroutineDispatcher = TestCoroutineDispatcher()) : - TestWatcher() { - - override fun starting(description: Description?) { - super.starting(description) - Dispatchers.setMain(dispatcher) - } - - override fun finished(description: Description?) { - super.finished(description) - Dispatchers.resetMain() - dispatcher.cleanupTestCoroutines() - } -}