|
| 1 | +package com.kernel.finch.core.manager |
| 2 | + |
| 3 | +import android.hardware.Sensor |
| 4 | +import android.hardware.SensorEvent |
| 5 | +import android.hardware.SensorEventListener |
| 6 | +import android.hardware.SensorManager |
| 7 | +import java.util.* |
| 8 | + |
| 9 | +private const val SENSITIVITY_LIGHT = 11 |
| 10 | +private const val SENSITIVITY_MEDIUM = 13 |
| 11 | +private const val SENSITIVITY_HARD = 15 |
| 12 | +private const val DEFAULT_ACCELERATION_THRESHOLD = SENSITIVITY_MEDIUM |
| 13 | + |
| 14 | +/** |
| 15 | + * Detects phone shaking. If more than 75% of the samples taken in the past 0.5s are |
| 16 | + * accelerating, the device is a) shaking, or b) free falling 1.84m (h = |
| 17 | + * 1/2*g*t^2*3/4) |
| 18 | + */ |
| 19 | +internal class ShakeDetectorManager(private val listener: () -> Unit) : SensorEventListener { |
| 20 | + /** |
| 21 | + * When the magnitude of total acceleration exceeds this |
| 22 | + * value, the phone is accelerating. |
| 23 | + */ |
| 24 | + private var accelerationThreshold = DEFAULT_ACCELERATION_THRESHOLD |
| 25 | + |
| 26 | + private val queue = SampleQueue() |
| 27 | + private var sensorManager: SensorManager? = null |
| 28 | + private var accelerometer: Sensor? = null |
| 29 | + |
| 30 | + /** |
| 31 | + * Starts listening for shakes on devices with appropriate hardware. |
| 32 | + * |
| 33 | + * @return true if the device supports shake detection. |
| 34 | + */ |
| 35 | + fun start(sensorManager: SensorManager): Boolean { |
| 36 | + // Already started? |
| 37 | + if (accelerometer != null) { |
| 38 | + return true |
| 39 | + } |
| 40 | + accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER) |
| 41 | + |
| 42 | + // If this phone has an accelerometer, listen to it. |
| 43 | + if (accelerometer != null) { |
| 44 | + this.sensorManager = sensorManager |
| 45 | + sensorManager.registerListener( |
| 46 | + this, accelerometer, |
| 47 | + SensorManager.SENSOR_DELAY_NORMAL |
| 48 | + ) |
| 49 | + } |
| 50 | + return accelerometer != null |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Stops listening. Safe to call when already stopped. Ignored on devices |
| 55 | + * without appropriate hardware. |
| 56 | + */ |
| 57 | + fun stop() { |
| 58 | + accelerometer?.let { |
| 59 | + queue.clear() |
| 60 | + sensorManager?.unregisterListener(this, it) |
| 61 | + sensorManager = null |
| 62 | + accelerometer = null |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + override fun onSensorChanged(event: SensorEvent) { |
| 67 | + val accelerating = isAccelerating(event) |
| 68 | + val timestamp = event.timestamp |
| 69 | + queue.add(timestamp, accelerating) |
| 70 | + if (queue.isShaking) { |
| 71 | + queue.clear() |
| 72 | + listener() |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + /** Returns true if the device is currently accelerating. */ |
| 77 | + private fun isAccelerating(event: SensorEvent): Boolean { |
| 78 | + val ax = event.values[0] |
| 79 | + val ay = event.values[1] |
| 80 | + val az = event.values[2] |
| 81 | + |
| 82 | + // Instead of comparing magnitude to ACCELERATION_THRESHOLD, |
| 83 | + // compare their squares. This is equivalent and doesn't need the |
| 84 | + // actual magnitude, which would be computed using (expensive) Math.sqrt(). |
| 85 | + val magnitudeSquared = (ax * ax + ay * ay + az * az).toDouble() |
| 86 | + return magnitudeSquared > accelerationThreshold * accelerationThreshold |
| 87 | + } |
| 88 | + |
| 89 | + /** Sets the acceleration threshold sensitivity. */ |
| 90 | + fun setSensitivity(accelerationThreshold: Int) { |
| 91 | + this.accelerationThreshold = accelerationThreshold |
| 92 | + } |
| 93 | + |
| 94 | + /** Queue of samples. Keeps a running average. */ |
| 95 | + internal class SampleQueue { |
| 96 | + private val pool = SamplePool() |
| 97 | + private var oldest: Sample? = null |
| 98 | + private var newest: Sample? = null |
| 99 | + private var sampleCount = 0 |
| 100 | + private var acceleratingCount = 0 |
| 101 | + |
| 102 | + /** |
| 103 | + * Adds a sample. |
| 104 | + * |
| 105 | + * @param timestamp in nanoseconds of sample |
| 106 | + * @param accelerating true if > [.accelerationThreshold]. |
| 107 | + */ |
| 108 | + fun add(timestamp: Long, accelerating: Boolean) { |
| 109 | + // Purge samples that proceed window. |
| 110 | + purge(timestamp - MAX_WINDOW_SIZE) |
| 111 | + |
| 112 | + // Add the sample to the queue. |
| 113 | + val added = pool.acquire() |
| 114 | + added.timestamp = timestamp |
| 115 | + added.accelerating = accelerating |
| 116 | + added.next = null |
| 117 | + newest?.let { |
| 118 | + it.next = added |
| 119 | + } |
| 120 | + newest = added |
| 121 | + if (oldest == null) { |
| 122 | + oldest = added |
| 123 | + } |
| 124 | + |
| 125 | + // Update running average. |
| 126 | + sampleCount++ |
| 127 | + if (accelerating) { |
| 128 | + acceleratingCount++ |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + /** Removes all samples from this queue. */ |
| 133 | + fun clear() { |
| 134 | + while (oldest != null) { |
| 135 | + val removed = oldest ?: continue |
| 136 | + oldest = removed.next |
| 137 | + pool.release(removed) |
| 138 | + } |
| 139 | + newest = null |
| 140 | + sampleCount = 0 |
| 141 | + acceleratingCount = 0 |
| 142 | + } |
| 143 | + |
| 144 | + /** Purges samples with timestamps older than cutoff. */ |
| 145 | + private fun purge(cutoff: Long) { |
| 146 | + while (sampleCount >= MIN_QUEUE_SIZE && oldest != null && cutoff - oldest!!.timestamp > 0) { |
| 147 | + // Remove sample. |
| 148 | + val removed = oldest ?: continue |
| 149 | + if (removed.accelerating) { |
| 150 | + acceleratingCount-- |
| 151 | + } |
| 152 | + sampleCount-- |
| 153 | + oldest = removed.next |
| 154 | + if (oldest == null) { |
| 155 | + newest = null |
| 156 | + } |
| 157 | + pool.release(removed) |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + /** Copies the samples into a list, with the oldest entry at index 0. */ |
| 162 | + fun asList(): List<Sample> { |
| 163 | + val list: MutableList<Sample> = ArrayList() |
| 164 | + var s = oldest |
| 165 | + while (s != null) { |
| 166 | + list.add(s) |
| 167 | + s = s.next |
| 168 | + } |
| 169 | + return list |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * Returns true if we have enough samples and more than 3/4 of those samples |
| 174 | + * are accelerating. |
| 175 | + */ |
| 176 | + val isShaking: Boolean |
| 177 | + get() = newest != null && oldest != null && newest!!.timestamp - oldest!!.timestamp >= MIN_WINDOW_SIZE && acceleratingCount >= (sampleCount shr 1) + (sampleCount shr 2) |
| 178 | + |
| 179 | + companion object { |
| 180 | + /** Window size in ns. Used to compute the average. */ |
| 181 | + private const val MAX_WINDOW_SIZE: Long = 500000000 // 0.5s |
| 182 | + private const val MIN_WINDOW_SIZE = MAX_WINDOW_SIZE shr 1 // 0.25s |
| 183 | + |
| 184 | + /** |
| 185 | + * Ensure the queue size never falls below this size, even if the device |
| 186 | + * fails to deliver this many events during the time window. The LG Ally |
| 187 | + * is one such device. |
| 188 | + */ |
| 189 | + private const val MIN_QUEUE_SIZE = 4 |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + /** An accelerometer sample. */ |
| 194 | + internal class Sample { |
| 195 | + /** Time sample was taken. */ |
| 196 | + var timestamp: Long = 0 |
| 197 | + |
| 198 | + /** If acceleration > [.accelerationThreshold]. */ |
| 199 | + var accelerating = false |
| 200 | + |
| 201 | + /** Next sample in the queue or pool. */ |
| 202 | + var next: Sample? = null |
| 203 | + } |
| 204 | + |
| 205 | + /** Pools samples. Avoids garbage collection. */ |
| 206 | + internal class SamplePool { |
| 207 | + private var head: Sample? = null |
| 208 | + |
| 209 | + /** Acquires a sample from the pool. */ |
| 210 | + fun acquire(): Sample { |
| 211 | + var acquired = head |
| 212 | + if (acquired == null) { |
| 213 | + acquired = Sample() |
| 214 | + } else { |
| 215 | + // Remove instance from pool. |
| 216 | + head = acquired.next |
| 217 | + } |
| 218 | + return acquired |
| 219 | + } |
| 220 | + |
| 221 | + /** Returns a sample to the pool. */ |
| 222 | + fun release(sample: Sample) { |
| 223 | + sample.next = head |
| 224 | + head = sample |
| 225 | + } |
| 226 | + } |
| 227 | + |
| 228 | + override fun onAccuracyChanged(sensor: Sensor, accuracy: Int) {} |
| 229 | +} |
0 commit comments