|
| 1 | +/* |
| 2 | + Copyright (c) 2026 Jatin Kumar <jnkr2409@gmail.com> |
| 3 | +* This program is free software; you can redistribute it and/or modify it under * |
| 4 | +* the terms of the GNU General Public License as published by the Free Software * |
| 5 | +* Foundation; either version 3 of the License, or (at your option) any later * |
| 6 | +* version. * |
| 7 | +* * |
| 8 | +* This program is distributed in the hope that it will be useful, but WITHOUT ANY * |
| 9 | +* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A * |
| 10 | +* PARTICULAR PURPOSE. See the GNU General Public License for more details. * |
| 11 | +* * |
| 12 | +* You should have received a copy of the GNU General Public License along with * |
| 13 | +* this program. If not, see <http://www.gnu.org/licenses/>. |
| 14 | + */ |
| 15 | + |
| 16 | +package com.ichi2.anki.android |
| 17 | +import android.content.Context |
| 18 | +import android.hardware.SensorManager |
| 19 | +import android.os.SystemClock |
| 20 | +import androidx.core.content.ContextCompat |
| 21 | +import androidx.core.content.getSystemService |
| 22 | +import com.squareup.seismic.ShakeDetector |
| 23 | +import timber.log.Timber |
| 24 | +import kotlin.time.Duration |
| 25 | +import kotlin.time.Duration.Companion.milliseconds |
| 26 | + |
| 27 | +/* |
| 28 | + * Wrapper for a [ShakeDetector] to provide a cooldown mechanism. |
| 29 | + * This prevents the "Undo" action or other gestures from triggering multiple times |
| 30 | + * in rapid succession during a single physical shake event. |
| 31 | + */ |
| 32 | +class AnkiShakeDetector( |
| 33 | + private val sensorManager: SensorManager, |
| 34 | + /* |
| 35 | + * Sensor Delay tells how often the app should check for phone movement. |
| 36 | + * |
| 37 | + * We use [SensorManager.SENSOR_DELAY_UI] because: |
| 38 | + * - Enough Speed : It is fast enough to catch a normal shake. |
| 39 | + * - Battery Friendly: It uses less power than "Game" or "Fastest" settings. |
| 40 | + */ |
| 41 | + private val sensorDelay: Int = SensorManager.SENSOR_DELAY_UI, |
| 42 | + private val listener: ShakeDetector.Listener, |
| 43 | + /* |
| 44 | + * The minimum time between shake events to prevent accidental double-triggers. |
| 45 | + * |
| 46 | + * Through trial and error, 800ms was determined to be the optimal 'sweet spot': |
| 47 | + * - 500ms : A single physical shake often registered as two distinct events, |
| 48 | + * causing the flag to toggle on and immediately off again. |
| 49 | + * |
| 50 | + * - 1000ms+ : Felt unresponsive when the user wanted to quickly flag/unflag |
| 51 | + * multiple cards in a row. |
| 52 | + * |
| 53 | + * - 800ms : Consistently filters out the "rebound" of a single shake while |
| 54 | + * remaining responsive for intentional back-to-back actions. |
| 55 | + */ |
| 56 | + private val cooldown: Duration = 800.milliseconds, |
| 57 | +) : ShakeDetector.Listener { |
| 58 | + private val shakeDetector = ShakeDetector(this) |
| 59 | + private var lastShakeTime = 0L |
| 60 | + |
| 61 | + fun start() { |
| 62 | + sensorManager.let { |
| 63 | + shakeDetector.start(it, sensorDelay) |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + fun stop() { |
| 68 | + shakeDetector.stop() |
| 69 | + } |
| 70 | + |
| 71 | + override fun hearShake() { |
| 72 | + Timber.d("The time since the last shake was: ${SystemClock.elapsedRealtime() - lastShakeTime}") |
| 73 | + val currentTime = SystemClock.elapsedRealtime() |
| 74 | + if (currentTime - lastShakeTime < cooldown.inWholeMilliseconds) { |
| 75 | + return |
| 76 | + } |
| 77 | + try { |
| 78 | + listener.hearShake() |
| 79 | + } finally { |
| 80 | + lastShakeTime = SystemClock.elapsedRealtime() |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + companion object { |
| 85 | + fun createInstance( |
| 86 | + context: Context, |
| 87 | + listener: ShakeDetector.Listener, |
| 88 | + ): AnkiShakeDetector? { |
| 89 | + val sensorManager = context.getSystemService<SensorManager>() |
| 90 | + return sensorManager?.let { |
| 91 | + AnkiShakeDetector( |
| 92 | + sensorManager = sensorManager, |
| 93 | + listener = listener, |
| 94 | + ) |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments