Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion app/src/main/java/com/bobek/compass/CompassFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ import com.bobek.compass.view.CompassViewModel
import com.google.android.material.color.MaterialColors
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import java.util.concurrent.Executor
import kotlin.math.sqrt

const val OPTION_INSTRUMENTED_TEST = "INSTRUMENTED_TEST"

Expand Down Expand Up @@ -443,7 +444,7 @@ class CompassFragment : Fragment() {
override fun onSensorChanged(event: SensorEvent) {
when (event.sensor.type) {
Sensor.TYPE_ROTATION_VECTOR -> updateCompass(event)
Sensor.TYPE_MAGNETIC_FIELD -> Log.v(TAG, "Received magnetic field sensor event ${event.values}")
Sensor.TYPE_MAGNETIC_FIELD -> updateMagneticFieldStrength(event)
else -> Log.w(TAG, "Unexpected sensor changed event of type ${event.sensor.type}")
}
}
Expand Down Expand Up @@ -485,6 +486,15 @@ class CompassFragment : Fragment() {
?.let(MathUtils::getMagneticDeclination)
?: 0.0f
}

private fun updateMagneticFieldStrength(event: SensorEvent) {
val x = event.values[0]
val y = event.values[1]
val z = event.values[2]
val strength = sqrt(x * x + y * y + z * z)
Log.v(TAG, "Magnetic field strength $strength")
compassViewModel.magneticFieldStrength.value = strength
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ import com.bobek.compass.model.LocationStatus
import com.bobek.compass.model.SensorAccuracy

class CompassViewModel : ViewModel() {

val azimuth = MutableLiveData<Azimuth>()
val sensorAccuracy = MutableLiveData(SensorAccuracy.NO_CONTACT)
val trueNorth = MutableLiveData(false)
val hapticFeedback = MutableLiveData(true)
val location = MutableLiveData<Location>()
val locationStatus = MutableLiveData(LocationStatus.NOT_PRESENT)
val magneticFieldStrength = MutableLiveData<Float>()
}
39 changes: 31 additions & 8 deletions app/src/main/res/layout-land/fragment_compass.xml
Original file line number Diff line number Diff line change
Expand Up @@ -137,17 +137,40 @@

</androidx.constraintlayout.widget.ConstraintLayout>

<com.google.android.material.textview.MaterialTextView
android:id="@+id/declination_text"
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/declination_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTint="?android:attr/textColorHint"
android:text="@{model.trueNorth == true &amp;&amp; model.locationStatus == LocationStatus.PRESENT ? @string/true_north : @string/magnetic_north}"
android:textAppearance="@android:style/TextAppearance.Material.Small"
android:textColor="?android:attr/textColorHint"
app:drawableStartCompat="@drawable/ic_compass"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
app:layout_constraintStart_toStartOf="parent">

<com.google.android.material.textview.MaterialTextView
android:id="@+id/magnetic_field_strength_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:text="@{model.magneticFieldStrength != null ? @string/magnetic_field_strength(model.magneticFieldStrength) : ``}"
android:textAppearance="@android:style/TextAppearance.Material.Small"
android:textColor="?android:attr/textColorHint"
android:visibility="@{model.trueNorth ? View.GONE : View.VISIBLE}"
app:layout_constraintBottom_toBottomOf="@id/declination_text"
app:layout_constraintEnd_toStartOf="@id/declination_text"
app:layout_constraintTop_toTopOf="@id/declination_text" />

<com.google.android.material.textview.MaterialTextView
android:id="@+id/declination_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTint="?android:attr/textColorHint"
android:text="@{model.trueNorth == true &amp;&amp; model.locationStatus == LocationStatus.PRESENT ? @string/true_north : @string/magnetic_north}"
android:textAppearance="@android:style/TextAppearance.Material.Small"
android:textColor="?android:attr/textColorHint"
app:drawableStartCompat="@drawable/ic_compass"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

Expand Down
38 changes: 30 additions & 8 deletions app/src/main/res/layout/fragment_compass.xml
Original file line number Diff line number Diff line change
Expand Up @@ -133,18 +133,40 @@

</androidx.constraintlayout.widget.ConstraintLayout>

<com.google.android.material.textview.MaterialTextView
android:id="@+id/declination_text"
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/declination_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTint="?android:attr/textColorHint"
android:text="@{model.trueNorth == true &amp;&amp; model.locationStatus == LocationStatus.PRESENT ? @string/true_north : @string/magnetic_north}"
android:textAppearance="@android:style/TextAppearance.Material.Small"
android:textColor="?android:attr/textColorHint"
app:drawableStartCompat="@drawable/ic_compass"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
app:layout_constraintStart_toStartOf="parent">

<com.google.android.material.textview.MaterialTextView
android:id="@+id/magnetic_field_strength_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:text="@{model.magneticFieldStrength != null ? @string/magnetic_field_strength(model.magneticFieldStrength) : ``}"
android:textAppearance="@android:style/TextAppearance.Material.Small"
android:textColor="?android:attr/textColorHint"
app:layout_constraintBottom_toTopOf="@id/declination_text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />

<com.google.android.material.textview.MaterialTextView
android:id="@+id/declination_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTint="?android:attr/textColorHint"
android:text="@{model.trueNorth == true &amp;&amp; model.locationStatus == LocationStatus.PRESENT ? @string/true_north : @string/magnetic_north}"
android:textAppearance="@android:style/TextAppearance.Material.Small"
android:textColor="?android:attr/textColorHint"
app:drawableStartCompat="@drawable/ic_compass"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-zh/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<string name="access_location_permission_denied">缺少访问位置信息的权限,指针将回归到默认的北方。</string>
<string name="settings">设置</string>
<string name="true_north">GPS定位下的北方</string>
<string name="magnetic_field_strength">磁场强度: %1$.1f µT</string>
<string name="true_north_summary">需要获得访问您位置信息的权限来修正磁偏角并指向正确的北方。</string>
<string name="magnetic_north">地磁北极</string>
<string name="haptic_feedback">触觉反馈</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<string name="access_location_permission_denied">Missing permission to access location. Falling back to magnetic north.</string>
<string name="settings">Settings</string>
<string name="true_north">True North</string>
<string name="magnetic_field_strength">Magnetic field: %1$.1f µT</string>
<string name="true_north_summary">Correct the magnetic declination and show to true north. Requires access to your location.</string>
<string name="magnetic_north">Magnetic North</string>
<string name="haptic_feedback">Haptic feedback</string>
Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[versions]
agp = "8.9.2"
agp = "8.10.1"
appcompat = "1.7.0"
espressoCore = "3.6.1"
junit = "4.13.2"
Expand Down