Skip to content

RF Sentinel (RFSentinel): free, open-source police detector app for Android. Spots police body cams, Flock ALPR cameras, AirTag trackers, Remote ID drones, smart glasses and fake cell towers from Bluetooth LE and WiFi. No root.

Locate: camera view (ARCore, full build only) tracks the phone while you sweep and walk

CIS-C0b1bc67c40114
diff --git a/app/build.gradle.kts b/app/build.gradle.kts
index eb37e81..43baf1a 100644
--- a/app/build.gradle.kts
+++ b/app/build.gradle.kts
@@ -153,6 +153,8 @@ dependencies {
implementation("org.maplibre.gl:android-sdk-vulkan-opengl:13.6.1")
// Optional Mapbox map (Settings > Map > Map provider), full build only; needs MAPBOX_ACCESS_TOKEN in local.properties.
if (!fossOnly) "fullImplementation"("com.mapbox.maps:android-ndk27:11.32.0")
+ // Locate's camera view (ARCore: where the phone is, to the centimetre, while it looks around), full build only.
+ if (!fossOnly) "fullImplementation"("com.google.ar:core:1.57.0")
// Android Auto (Car App Library, template-based UI; "projected" = phone-driven Android Auto)
implementation("androidx.car.app:app:1.7.0")
diff --git a/app/src/foss/java/com/rfsentinel/app/ui/ArSupport.kt b/app/src/foss/java/com/rfsentinel/app/ui/ArSupport.kt
new file mode 100644
index 0000000..c25661d
--- /dev/null
+++ b/app/src/foss/java/com/rfsentinel/app/ui/ArSupport.kt
@@ -0,0 +1,10 @@
+package com.rfsentinel.app.ui
+
+import android.app.Activity
+
+/** The FOSS build has no ARCore (Google's): Locate is the chest sweep only. */
+internal object ArSupport {
+ fun available(activity: Activity) = false
+
+ fun open(activity: Activity) {}
+}
diff --git a/app/src/full/AndroidManifest.xml b/app/src/full/AndroidManifest.xml
new file mode 100644
index 0000000..4922a3c
--- /dev/null
+++ b/app/src/full/AndroidManifest.xml
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="utf-8"?>
+<manifest xmlns:android="http://schemas.android.com/apk/res/android">
+
+ <!-- Locate's camera view (full build): asked the first time you open it. The picture stays on the phone. -->
+ <uses-permission android:name="android.permission.CAMERA" />
+ <uses-feature android:name="android.hardware.camera.ar" android:required="false" />
+ <uses-feature android:name="android.hardware.camera" android:required="false" />
+
+ <application>
+ <!-- Optional: the app runs without ARCore; the camera view offers to install it. -->
+ <meta-data android:name="com.google.ar.core" android:value="optional" />
+ <activity
+ android:name=".ui.ArLocateActivity"
+ android:exported="false"
+ android:screenOrientation="portrait" />
+ </application>
+</manifest>
diff --git a/app/src/full/java/com/rfsentinel/app/ui/ArLocateActivity.kt b/app/src/full/java/com/rfsentinel/app/ui/ArLocateActivity.kt
new file mode 100644
index 0000000..9112e2f
--- /dev/null
+++ b/app/src/full/java/com/rfsentinel/app/ui/ArLocateActivity.kt
@@ -0,0 +1,363 @@
+package com.rfsentinel.app.ui
+
+import android.Manifest
+import android.content.Context
+import android.content.pm.PackageManager
+import android.graphics.Canvas
+import android.graphics.Color
+import android.graphics.Paint
+import android.graphics.Path
+import android.opengl.GLES11Ext
+import android.opengl.GLES20
+import android.opengl.GLSurfaceView
+import android.os.Bundle
+import android.os.Handler
+import android.os.Looper
+import android.view.Gravity
+import android.view.View
+import android.view.ViewGroup
+import android.widget.FrameLayout
+import android.widget.ImageButton
+import android.widget.LinearLayout
+import android.widget.TextView
+import android.widget.Toast
+import androidx.activity.result.contract.ActivityResultContracts
+import androidx.appcompat.app.AppCompatActivity
+import com.google.android.material.button.MaterialButton
+import com.google.android.material.card.MaterialCardView
+import com.google.android.material.color.MaterialColors
+import com.google.ar.core.ArCoreApk
+import com.google.ar.core.Coordinates2d
+import com.google.ar.core.Session
+import com.google.ar.core.TrackingFailureReason
+import com.google.ar.core.TrackingState
+import com.rfsentinel.app.R
+import com.rfsentinel.app.detect.DeviceIntel
+import com.rfsentinel.app.util.applySystemBarInsets
+import java.nio.ByteBuffer
+import java.nio.ByteOrder
+import java.nio.FloatBuffer
+import javax.microedition.khronos.egl.EGLConfig
+import javax.microedition.khronos.opengles.GL10
+import kotlin.math.abs
+import kotlin.math.hypot
+import kotlin.math.min
+import kotlin.math.tan
+
+/**
+ * Locate through the camera: the same sweep (phone upright in front of you, turn round), with ARCore tracking where
+ * the phone is to the centimetre while it looks around, so walking toward the device keeps its direction and gives
+ * its distance. A marker floats where it is when it's ahead; the arrow points the way otherwise. Locate goes on
+ * when this closes. Main thread, except the GL renderer.
+ */
+class ArLocateActivity : AppCompatActivity(), GLSurfaceView.Renderer {
+
+ /** What the camera saw last frame (GL thread to main thread). */
+ private class Shot(val tracking: Boolean, val failure: TrackingFailureReason, val x: Float, val z: Float,
+ val arHeading: Float, val level: Boolean, val focal: Float)
+
+ private val main = Handler(Looper.getMainLooper())
+ private var session: Session? = null
+ private var boundTo: Session? = null
+ private var installAsked = false
+ private var cameraAsked = false
+ @Volatile private var shot: Shot? = null
+ private val frame = ArFrame()
+ private val background = Background()
+ private lateinit var surface: GLSurfaceView
+ private lateinit var marker: Marker
+ private lateinit var picture: SweepGuide.SweepView
+ private lateinit var chip: TextView
+ private lateinit var prompt: TextView
+ private lateinit var info: TextView
+
+ private val askCamera = registerForActivityResult(ActivityResultContracts.RequestPermission()) { ok ->
+ if (!ok) { Toast.makeText(this, "The camera view needs the camera", Toast.LENGTH_SHORT).show(); finish() }
+ }
+
+ override fun onCreate(savedInstanceState: Bundle?) {
+ super.onCreate(savedInstanceState)
+ supportActionBar?.hide()
+ val d = resources.displayMetrics.density
+ val pad = (16 * d).toInt()
+ val muted = MaterialColors.getColor(this, com.google.android.material.R.attr.colorOnSurfaceVariant, 0xFF9AA5AD.toInt())
+ surface = GLSurfaceView(this).apply {
+ preserveEGLContextOnPause = true
+ setEGLContextClientVersion(2)
+ setEGLConfigChooser(8, 8, 8, 8, 16, 0)
+ setRenderer(this@ArLocateActivity)
+ renderMode = GLSurfaceView.RENDERMODE_CONTINUOUSLY
+ }
+ marker = Marker(this)
+ picture = SweepGuide.SweepView(this)
+ chip = text(14f, null)
+ prompt = text(18f, null).apply { setTypeface(typeface, android.graphics.Typeface.BOLD) }
+ info = text(14f, muted)
+ val top = MaterialCardView(this).apply {
+ addView(LinearLayout(context).apply {
+ orientation = LinearLayout.HORIZONTAL
+ gravity = Gravity.CENTER_VERTICAL
+ setPadding(pad, 0, 0, 0)
+ addView(chip, LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1f))
+ addView(ImageButton(context).apply {
+ setImageResource(R.drawable.ic_nav_close)
+ background = null
+ imageTintList = android.content.res.ColorStateList.valueOf(
+ MaterialColors.getColor(context, com.google.android.material.R.attr.colorOnSurface, Color.WHITE))
+ contentDescription = "Close the camera view"
+ setPadding(pad, pad, pad, pad)
+ setOnClickListener { finish() }
+ })
+ })
+ }
+ val side = min((140 * d).toInt(), (resources.displayMetrics.widthPixels * 0.4f).toInt())
+ val bottom = MaterialCardView(this).apply {
+ addView(LinearLayout(context).apply {
+ orientation = LinearLayout.VERTICAL
+ gravity = Gravity.CENTER_HORIZONTAL
+ setPadding(pad, pad, pad, pad / 2)
+ addView(picture, LinearLayout.LayoutParams(side, side))
+ addView(prompt); addView(info)
+ addView(MaterialButton(context, null, android.R.attr.borderlessButtonStyle).apply {
+ text = "Sweep again"
+ setOnClickListener { LiveRead.sweepAgain() }
+ })
+ })
+ }
+ val overlay = FrameLayout(this).apply {
+ setPadding(pad, pad, pad, pad)
+ addView(top, FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.TOP))
+ addView(bottom, FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.BOTTOM))
+ }
+ overlay.applySystemBarInsets()
+ setContentView(FrameLayout(this).apply {
+ setBackgroundColor(Color.BLACK)
+ addView(surface); addView(marker); addView(overlay)
+ })
+ }
+
+ private fun text(sp: Float, color: Int?) = TextView(this).apply {
+ textSize = sp
+ gravity = Gravity.CENTER
+ color?.let { setTextColor(it) }
+ setPadding(0, (4 * resources.displayMetrics.density).toInt(), 0, 0)
+ }
+
+ override fun onResume() {
+ super.onResume()
+ if (LiveRead.locating == null) { finish(); return }
+ if (checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
+ if (!cameraAsked) { cameraAsked = true; askCamera.launch(Manifest.permission.CAMERA) }
+ return
+ }
+ val s = session ?: try {
+ // Google Play Services for AR: offered once, then back here.
+ if (ArCoreApk.getInstance().requestInstall(this, !installAsked) == ArCoreApk.InstallStatus.INSTALL_REQUESTED) {
+ installAsked = true; return
+ }
+ Session(this).also { session = it }
+ } catch (e: Exception) {
+ Toast.makeText(this, "The camera view doesn't work on this phone", Toast.LENGTH_LONG).show()
+ finish(); return
+ }
+ try { s.resume() } catch (e: Exception) {
+ Toast.makeText(this, "The camera is busy", Toast.LENGTH_SHORT).show()
+ finish(); return
+ }
+ frame.reset()
+ surface.onResume()
+ main.post(update)
+ }
+
+ override fun onPause() {
+ super.onPause()
+ main.removeCallbacks(update)
+ // The renderer stops before the session it draws from.
+ surface.onPause()
+ session?.pause()
+ shot = null
+ }
+
+ override fun onDestroy() {
+ session?.close(); session = null
+ super.onDestroy()
+ }
+
+ // ---- GL thread: the camera picture, and where the camera is ----
+
+ override fun onSurfaceCreated(gl: GL10?, config: EGLConfig?) {
+ background.create()
+ boundTo = null
+ }
+
+ override fun onSurfaceChanged(gl: GL10?, width: Int, height: Int) {
+ GLES20.glViewport(0, 0, width, height)
+ @Suppress("DEPRECATION")
+ session?.setDisplayGeometry(windowManager.defaultDisplay.rotation, width, height)
+ }
+
+ private val projection = FloatArray(16)
+
+ override fun onDrawFrame(gl: GL10?) {
+ GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT or GLES20.GL_DEPTH_BUFFER_BIT)
+ val s = session ?: return
+ if (boundTo !== s) { s.setCameraTextureName(background.texture); boundTo = s }
+ val f = try { s.update() } catch (e: Exception) { return }
+ background.draw(f)
+ val c = f.camera
+ val pose = c.pose
+ val z = pose.zAxis
+ // The camera looks along its -z.
+ val fx = -z[0]; val fy = -z[1]; val fz = -z[2]
+ c.getProjectionMatrix(projection, 0, 0.1f, 100f)
+ shot = Shot(c.trackingState == TrackingState.TRACKING, c.trackingFailureReason, pose.tx(), pose.tz(),
+ ArFrame.arHeading(fx, fz), abs(fy) < LEVEL && hypot(fx, fz) > 0.1f, projection[0])
+ }
+
+ // ---- Main thread: tie the camera to Motion, and the words ----
+
+ private val update = object : Runnable {
+ override fun run() {
+ main.postDelayed(this, UPDATE_MS)
+ val st = LiveRead.sweepState() ?: run { finish(); return } // Locate ended
+ val sh = shot
+ val tracking = sh?.tracking == true
+ if (sh != null && !tracking) frame.reset()
+ val here = if (tracking) frame.place(sh!!.x, sh.z, sh.arHeading, LiveRead.heading.takeIf { sh.level && st.upright }, Motion.position) else null
+ here?.let { Motion.ar(it.x, it.y) }
+ val heading = sh?.takeIf { tracking }?.let { frame.heading(it.arHeading) } ?: st.heading
+
+ val r = LiveRead.current()
+ chip.text = (r?.title ?: "Device") + " · " + (r?.rssi?.let { "$it dBm" } ?: "not heard")
+ val l = st.locked
+ picture.step = if (l != null) 2 else 1
+ val now = System.currentTimeMillis()
+ val place = LiveRead.locating?.let { RadarTrack.place(it, now) }
+ marker.show(if (l != null && heading != null && sh != null) Sweep.turn(heading, l.bearing).toFloat() else null,
+ sh?.focal ?: 0f, place?.distanceM)
+ prompt.text = when {
+ sh == null -> "Starting the camera"
+ !tracking -> when (sh.failure) {
+ TrackingFailureReason.INSUFFICIENT_LIGHT -> "More light"
+ TrackingFailureReason.EXCESSIVE_MOTION -> "Slower"
+ TrackingFailureReason.INSUFFICIENT_FEATURES -> "Point it at something with detail"
+ TrackingFailureReason.CAMERA_UNAVAILABLE -> "The camera is busy"
+ else -> "Move the phone side to side"
+ }
+ !st.upright -> "Hold it upright, camera ahead"
+ l == null && st.covered >= Sweep.MIN_SECTORS -> "Once more round, a little slower"
+ l == null -> "Turn round slowly"
+ heading != null -> LiveRead.turnText(Sweep.turn(heading, l.bearing)).replaceFirstChar { it.uppercase() }
+ else -> "Locked"
+ }
+ info.text = if (l == null) {
+ "${st.covered * 360 / Sweep.SECTORS}° of 360°" + (st.estimate?.let { " · ${(it.confidence * 100).toInt()}% sure" } ?: "")
+ } else {
+ Sweep.compassPoint(l.bearing) + (place?.let { " · ~" + DeviceIntel.formatDistance(it.distanceM.toDouble()) } ?: "") +
+ " · ${(l.confidence * 100).toInt()}% sure" + (if (place == null) "\nWalk a few steps for its distance" else "")
+ }
+ }
+ }
+
+ /** The device where it is in the picture, while it's ahead: a diamond and its distance. */
+ private class Marker(context: Context) : View(context) {
+ private val d = resources.displayMetrics.density
+ private val you = MaterialColors.getColor(context, R.attr.rfTraceColor, 0xFFE0622D.toInt())
+ private val fill = Paint(Paint.ANTI_ALIAS_FLAG).apply { color = you }
+ private val edge = Paint(Paint.ANTI_ALIAS_FLAG).apply { style = Paint.Style.STROKE; color = Color.WHITE; strokeWidth = 2 * d }
+ private val text = Paint(Paint.ANTI_ALIAS_FLAG).apply {
+ textAlign = Paint.Align.CENTER; color = Color.WHITE; textSize = 15 * d; setShadowLayer(4 * d, 0f, 0f, Color.BLACK)
+ }
+ private val path = Path()
+ private var turn: Float? = null
+ private var focal = 0f
+ private var distance: Float? = null
+
+ /** [turn]: degrees from straight ahead (right positive), null to hide; [focal]: the projection's x scale. */
+ fun show(turn: Float?, focal: Float, distance: Float?) {
+ this.turn = turn; this.focal = focal; this.distance = distance
+ invalidate()
+ }
+
+ override fun onDraw(canvas: Canvas) {
+ val t = turn ?: return
+ if (abs(t) >= 80f || focal <= 0f) return
+ val ndc = focal * tan(Math.toRadians(t.toDouble())).toFloat()
+ if (abs(ndc) > 1f) return
+ // shortcut: drawn at the picture's middle height (the device's height isn't known), and phone roll ignored.
+ val x = (ndc + 1f) / 2f * width
+ val y = height * 0.42f
+ val s = 14 * d
+ path.reset()
+ path.moveTo(x, y - s); path.lineTo(x + s, y); path.lineTo(x, y + s); path.lineTo(x - s, y); path.close()
+ canvas.drawPath(path, fill); canvas.drawPath(path, edge)
+ distance?.let { canvas.drawText("~" + DeviceIntel.formatDistance(it.toDouble()), x, y + s + 20 * d, text) }
+ }
+ }
+
+ /** The camera picture behind everything: ARCore's external texture on a full-screen quad. GL thread. */
+ private class Background {
+ var texture = 0
+ private set
+ private var program = 0
+ private val quad = floats(-1f, -1f, 1f, -1f, -1f, 1f, 1f, 1f)
+ private val uv = floats(0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f)
+
+ fun create() {
+ val t = IntArray(1)
+ GLES20.glGenTextures(1, t, 0)
+ texture = t[0]
+ GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, texture)
+ GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE)
+ GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE)
+ GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR)
+ GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR)
+ program = GLES20.glCreateProgram().also { p ->
+ GLES20.glAttachShader(p, shader(GLES20.GL_VERTEX_SHADER, VERTEX))
+ GLES20.glAttachShader(p, shader(GLES20.GL_FRAGMENT_SHADER, FRAGMENT))
+ GLES20.glLinkProgram(p)
+ }
+ }
+
+ fun draw(f: com.google.ar.core.Frame) {
+ if (f.hasDisplayGeometryChanged()) {
+ quad.position(0); uv.position(0)
+ f.transformCoordinates2d(Coordinates2d.OPENGL_NORMALIZED_DEVICE_COORDINATES, quad, Coordinates2d.TEXTURE_NORMALIZED, uv)
+ }
+ if (f.timestamp == 0L) return
+ quad.position(0); uv.position(0)
+ GLES20.glDisable(GLES20.GL_DEPTH_TEST)
+ GLES20.glUseProgram(program)
+ GLES20.glActiveTexture(GLES20.GL_TEXTURE0)
+ GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, texture)
+ val pos = GLES20.glGetAttribLocation(program, "a_pos")
+ val tex = GLES20.glGetAttribLocation(program, "a_uv")
+ GLES20.glVertexAttribPointer(pos, 2, GLES20.GL_FLOAT, false, 0, quad)
+ GLES20.glVertexAttribPointer(tex, 2, GLES20.GL_FLOAT, false, 0, uv)
+ GLES20.glEnableVertexAttribArray(pos); GLES20.glEnableVertexAttribArray(tex)
+ GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4)
+ GLES20.glDisableVertexAttribArray(pos); GLES20.glDisableVertexAttribArray(tex)
+ }
+
+ private fun shader(type: Int, src: String) = GLES20.glCreateShader(type).also {
+ GLES20.glShaderSource(it, src); GLES20.glCompileShader(it)
+ }
+
+ private companion object {
+ fun floats(vararg v: Float): FloatBuffer =
+ ByteBuffer.allocateDirect(v.size * 4).order(ByteOrder.nativeOrder()).asFloatBuffer().apply { put(v); position(0) }
+
+ const val VERTEX = "attribute vec4 a_pos; attribute vec2 a_uv; varying vec2 v_uv;\n" +
+ "void main() { gl_Position = a_pos; v_uv = a_uv; }"
+ const val FRAGMENT = "#extension GL_OES_EGL_image_external : require\n" +
+ "precision mediump float; varying vec2 v_uv; uniform samplerExternalOES u_tex;\n" +
+ "void main() { gl_FragColor = texture2D(u_tex, v_uv); }"
+ }
+ }
+
+ private companion object {
+ const val UPDATE_MS = 50L
+ /** The camera's forward this close to level (sine of the tilt) counts as held upright. */
+ const val LEVEL = 0.4f
+ }
+}
diff --git a/app/src/full/java/com/rfsentinel/app/ui/ArSupport.kt b/app/src/full/java/com/rfsentinel/app/ui/ArSupport.kt
new file mode 100644
index 0000000..35de33c
--- /dev/null
+++ b/app/src/full/java/com/rfsentinel/app/ui/ArSupport.kt
@@ -0,0 +1,13 @@
+package com.rfsentinel.app.ui
+
+import android.app.Activity
+import android.content.Intent
+import android.content.pm.PackageManager
+
+/** The full build: Locate's camera view (ARCore, Google Play Services for AR, which it offers to install). */
+internal object ArSupport {
+ /** A back camera: whether ARCore runs on it is only asked when you open the view (no query to Google before). */
+ fun available(activity: Activity) = activity.packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA)
+
+ fun open(activity: Activity) = activity.startActivity(Intent(activity, ArLocateActivity::class.java))
+}
diff --git a/app/src/main/java/com/rfsentinel/app/ui/ArFrame.kt b/app/src/main/java/com/rfsentinel/app/ui/ArFrame.kt
new file mode 100644
index 0000000..66f56e7
--- /dev/null
+++ b/app/src/main/java/com/rfsentinel/app/ui/ArFrame.kt
@@ -0,0 +1,62 @@
+package com.rfsentinel.app.ui
+
+import kotlin.math.atan2
+import kotlin.math.cos
+import kotlin.math.sin
+
+/**
+ * Ties the AR camera's world (metres from where it started, its own "north") to [Motion]'s east / north: the
+ * compass gives the turn between the two, averaged over the first [ALIGN_FRAMES] level frames, and where Motion had
+ * you then gives the shift. After that the camera alone says where you are and which way you face. Pure (unit-tested).
+ */
+class ArFrame {
+ private var sumSin = 0.0
+ private var sumCos = 0.0
+ private var frames = 0
+ /** AR north to true north, radians clockwise; null until aligned. */
+ private var offset: Double? = null
+ private var originX = 0.0
+ private var originY = 0.0
+
+ val aligned get() = offset != null
+
+ /** Tracking was lost or paused: the camera's world may have moved, align again. */
+ fun reset() { sumSin = 0.0; sumCos = 0.0; frames = 0; offset = null }
+
+ /**
+ * One tracked frame: the camera at AR ([x], [z]) looking [arHeading] (degrees clockwise from AR north, -z);
+ * [compass] (null unless held level) and [here] (Motion's place) align it. Where you are, once aligned.
+ */
+ fun place(x: Float, z: Float, arHeading: Float, compass: Float?, here: Motion.Pos?): Motion.Pos? {
+ val o = offset ?: run {
+ if (compass == null || here == null) return null
+ val d = Math.toRadians(HeadingFusion.diff(compass, arHeading).toDouble())
+ sumSin += sin(d); sumCos += cos(d)
+ if (++frames < ALIGN_FRAMES) return null
+ val o = atan2(sumSin, sumCos)
+ val (e, n) = turn(x, z, o)
+ originX = here.x - e; originY = here.y - n
+ offset = o
+ o
+ }
+ val (e, n) = turn(x, z, o)
+ return Motion.Pos(originX + e, originY + n)
+ }
+
+ /** True compass degrees for an AR heading, null until aligned. */
+ fun heading(arHeading: Float): Float? = offset?.let { HeadingFusion.norm(arHeading + Math.toDegrees(it).toFloat()) }
+
+ /** AR (x, z) as east / north turned [o] clockwise. */
+ private fun turn(x: Float, z: Float, o: Double): Pair<Double, Double> {
+ val e = x.toDouble(); val n = -z.toDouble()
+ return (e * cos(o) + n * sin(o)) to (-e * sin(o) + n * cos(o))
+ }
+
+ companion object {
+ /** About a second and a half of updates held level. */
+ const val ALIGN_FRAMES = 30
+
+ /** Degrees clockwise from AR north (-z) of the way the camera looks ([fx], [fz]: its forward, flattened). */
+ fun arHeading(fx: Float, fz: Float) = HeadingFusion.norm(Math.toDegrees(atan2(fx.toDouble(), -fz.toDouble())).toFloat())
+ }
+}
diff --git a/app/src/main/java/com/rfsentinel/app/ui/Motion.kt b/app/src/main/java/com/rfsentinel/app/ui/Motion.kt
index 396898b..7f6db45 100644
--- a/app/src/main/java/com/rfsentinel/app/ui/Motion.kt
+++ b/app/src/main/java/com/rfsentinel/app/ui/Motion.kt
@@ -33,6 +33,8 @@ object Motion : SensorEventListener {
private var stale = false
private var staleSince = 0L
private var fixTime = 0L
+ /** When Locate's camera view last placed you ([ar]): its tracking beats steps and GPS while it lasts. */
+ private var arAt = 0L
/** Where you are now; null while waiting for a GPS fix to place you again after a pause. */
val position: Pos? get() = if (users == 0 || stale) null else Pos(fusion.x, fusion.y)
@@ -53,6 +55,13 @@ object Motion : SensorEventListener {
compass?.stop(); compass = null
}
+ /** Locate's camera view tracks you at [x], [y] (this frame's metres). Main thread. */
+ fun ar(x: Double, y: Double) {
+ if (users == 0 || stale) return
+ fusion.set(x, y)
+ arAt = System.currentTimeMillis()
+ }
+
private fun newFrame() { generation++; fusion.reset(); stale = false }
override fun onSensorChanged(e: SensorEvent) {
@@ -60,14 +69,14 @@ object Motion : SensorEventListener {
ScanForegroundService.lastFix?.takeIf { it.time != fixTime }?.let { f ->
fixTime = f.time
compass?.setLocation(f.latitude, f.longitude, f.altitude)
- if (f.hasAccuracy() && f.accuracy <= MAX_ACCURACY_M && now - f.time < FIX_FRESH_MS) {
+ if (f.hasAccuracy() && f.accuracy <= MAX_ACCURACY_M && now - f.time < FIX_FRESH_MS && now - arAt > AR_HOLD_MS) {
fusion.gps(f.latitude, f.longitude, f.accuracy.toDouble(), snap = stale)
stale = false
}
}
if (stale && now - staleSince > STALE_MS) newFrame()
val v = e.values
- if (!steps.add(e.timestamp / 1_000_000, sqrt((v[0] * v[0] + v[1] * v[1] + v[2] * v[2]).toDouble()))) return
+ if (!steps.add(e.timestamp / 1_000_000, sqrt((v[0] * v[0] + v[1] * v[1] + v[2] * v[2]).toDouble())) || now - arAt <= AR_HOLD_MS) return
compass?.degrees?.let { fusion.step(it) }
}
@@ -90,6 +99,9 @@ object Motion : SensorEventListener {
fun reset() { x = 0.0; y = 0.0; variance = 0.0; lat0 = Double.NaN; lon0 = Double.NaN; lastHeading = null }
+ /** Placed by the AR camera: centimetres, so little doubt for the next GPS fix to pull on. */
+ fun set(x: Double, y: Double) { this.x = x; this.y = y; variance = AR_VARIANCE }
+
/** One step while facing [heading]. Shuffling round on the spot (a sweep) is steps too, but barely moves you. */
fun step(heading: Float) {
val turned = lastHeading?.let { abs(Sweep.turn(it, heading)) > TURN_DEG } ?: false
@@ -146,6 +158,9 @@ object Motion : SensorEventListener {
/** Doubt each step adds (m²): length and heading errors. */
private const val STEP_VARIANCE = 0.25
private const val MIN_ACCURACY_M = 3.0
+ /** The camera view lost tracking this long: steps and GPS take over again. */
+ private const val AR_HOLD_MS = 1_000L
+ private const val AR_VARIANCE = 0.01
private const val MAX_ACCURACY_M = 50f
private const val FIX_FRESH_MS = 5_000L
/** Resumed and no fix this long: start over. */
diff --git a/app/src/main/java/com/rfsentinel/app/ui/SweepGuide.kt b/app/src/main/java/com/rfsentinel/app/ui/SweepGuide.kt
index d3fbfe3..41a49de 100644
--- a/app/src/main/java/com/rfsentinel/app/ui/SweepGuide.kt
+++ b/app/src/main/java/com/rfsentinel/app/ui/SweepGuide.kt
@@ -69,7 +69,10 @@ class SweepGuide(private val activity: Activity) {
d.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener {
if (step == Step.HOW) { LiveRead.sweepAgain(); go(Step.SWEEP) } else d.dismiss()
}
- d.getButton(AlertDialog.BUTTON_NEUTRAL).setOnClickListener { LiveRead.sweepAgain(); go(Step.SWEEP) }
+ d.getButton(AlertDialog.BUTTON_NEUTRAL).setOnClickListener {
+ LiveRead.sweepAgain()
+ if (step == Step.HOW) { ArSupport.open(activity); d.dismiss() } else go(Step.SWEEP)
+ }
go(Step.HOW)
}
d.show()
@@ -89,8 +92,8 @@ class SweepGuide(private val activity: Activity) {
d.getButton(AlertDialog.BUTTON_NEGATIVE).text = if (s == Step.SWEEP) "Cancel" else "Not now"
d.getButton(AlertDialog.BUTTON_NEGATIVE).visibility = if (s == Step.LOCKED) View.GONE else View.VISIBLE
d.getButton(AlertDialog.BUTTON_NEUTRAL).apply {
- text = "Sweep again"
- visibility = if (s == Step.LOCKED) View.VISIBLE else View.GONE
+ text = if (s == Step.HOW) "Use camera" else "Sweep again"
+ visibility = if (s == Step.LOCKED || (s == Step.HOW && ArSupport.available(activity))) View.VISIBLE else View.GONE
}
main.removeCallbacks(update)
update.run()
@@ -134,8 +137,8 @@ class SweepGuide(private val activity: Activity) {
}
}
- /** The picture: the demo, the filling ring, or the arrow. Redraws every frame while shown. */
- private class SweepView(context: Context) : View(context) {
+ /** The picture: the demo, the filling ring, or the arrow (also Locate's camera view). Redraws every frame while shown. */
+ class SweepView(context: Context) : View(context) {
var step = 0
private val d = resources.displayMetrics.density
private val scope = MaterialColors.getColor(context, R.attr.rfScopeColor, 0xFF0B5C63.toInt())
diff --git a/app/src/test/java/com/rfsentinel/app/ui/ArFrameTest.kt b/app/src/test/java/com/rfsentinel/app/ui/ArFrameTest.kt
new file mode 100644
index 0000000..5ecf82b
--- /dev/null
+++ b/app/src/test/java/com/rfsentinel/app/ui/ArFrameTest.kt
@@ -0,0 +1,34 @@
+package com.rfsentinel.app.ui
+
+import org.junit.Assert.assertEquals
+import org.junit.Assert.assertNull
+import org.junit.Test
+
+class ArFrameTest {
+
+ @Test
+ fun walkingAheadInTheCameraWorldIsWalkingTheCompassWay() {
+ val f = ArFrame()
+ val here = Motion.Pos(10.0, 5.0)
+ // Facing AR north, the compass says east.
+ repeat(ArFrame.ALIGN_FRAMES - 1) { assertNull(f.place(0f, 0f, 0f, 90f, here)) }
+ val p0 = f.place(0f, 0f, 0f, 90f, here)!!
+ assertEquals(10.0, p0.x, 1e-6); assertEquals(5.0, p0.y, 1e-6)
+ // Two metres ahead (AR -z) is two metres east.
+ val p = f.place(0f, -2f, 0f, null, null)!!
+ assertEquals(12.0, p.x, 1e-6); assertEquals(5.0, p.y, 1e-6)
+ assertEquals(90f, f.heading(0f)!!, 1e-3f)
+ assertEquals(180f, f.heading(ArFrame.arHeading(1f, 0f))!!, 1e-3f)
+ }
+
+ @Test
+ fun alignsOnlyWhileHeldLevelAndPlaced() {
+ val f = ArFrame()
+ repeat(100) { f.place(0f, 0f, 0f, null, Motion.Pos(0.0, 0.0)) }
+ assertNull(f.heading(0f))
+ f.reset()
+ repeat(ArFrame.ALIGN_FRAMES) { f.place(0f, 0f, 350f, 10f, Motion.Pos(0.0, 0.0)) }
+ // The turn averages the short way round through north.
+ assertEquals(10f, f.heading(350f)!!, 1e-3f)
+ }
+}