Skip to content

Added debouncing to prevent from frequent triggering #20879

Merged
BrayanDSO merged 1 commit into
ankidroid:mainfrom
jatinkumar2409:main
May 7, 2026
Merged

Added debouncing to prevent from frequent triggering #20879
BrayanDSO merged 1 commit into
ankidroid:mainfrom
jatinkumar2409:main

Conversation

@jatinkumar2409

@jatinkumar2409 jatinkumar2409 commented Apr 28, 2026

Copy link
Copy Markdown
Contributor

Purpose / Description

The shake gesture was triggering multiple times on frequent shakes

Fixes

Approach

Added debouncing so that there should be atleast 2500ms between two shake gestures to prevent frequent triggering

How Has This Been Tested?

ankidroid_shake_fix.mp4

You can set the shake gesture to any of the action specifically "set due date" and try to shake frequently on old screen and you can test with adding or editing notes in new study screen

Learning (optional, can help others)

Describe the research stage

Links to blog posts, patterns, libraries or addons used to solve this problem

Checklist

Please, go through these checks before submitting the PR.

  • You have a descriptive commit message with a short title (first line, max 50 chars).
  • You have commented your code, particularly in hard-to-understand areas
  • You have performed a self-review of your own code
  • UI changes: include screenshots of all affected screens (in particular showing any new or changed strings)
  • UI Changes: You have tested your change using the Google Accessibility Scanner

@david-allison david-allison left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!!

Could you clean the diff (files changed tab) so only relevant changes are visible

Feel free to squash + force push, the unnecessary changes make it hard to understand the intent of the change


From the "files changed", this does not appear to impact the new study screen (ReviewerFragment)

@jatinkumar2409

jatinkumar2409 commented Apr 28, 2026

Copy link
Copy Markdown
Contributor Author

Thank you @david-allison for guiding me , I am working on this . Will update it soon

@ZornHadNoChoice

Copy link
Copy Markdown
Collaborator

3000ms feels too much, doesn't it? Is 500ms or even 1000ms not enough to prevent the bug?

@jatinkumar2409

Copy link
Copy Markdown
Contributor Author

@ZornHadNoChoice Yeah in new devices 1000ms is enough but i tested in my 2gb realme c30s and it was easily crossing 1000ms that too on frequent shakes. So , for keeping older and low ram devices in mind i changed it to 2500ms and its working fine now

@BrayanDSO BrayanDSO left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Subclassing ShakeDetector would be cleaner IMO

@BrayanDSO BrayanDSO added the Needs Author Reply Waiting for a reply from the original author label Apr 28, 2026

@david-allison david-allison left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!!

Architecturally, this would be better handled inside a custom ShakeDetector. See how we construct it here:

shakeDetector = ShakeDetector(this)
shakeDetector?.start(sensorManager, SensorManager.SENSOR_DELAY_UI)

This also opens us up for future refactorings: removing sensorManager from ReviewerFragment and moving it into the custom class.

I'll need to test the debounce threshold on a physical phone. Regardless of the outcome, the constant should be extracted, and the reason that you selected the value should be documented.

Use a Duration for the time: val cooldown = 2500.milliseconds or val cooldown = 2.5.seconds

@BrayanDSO

Copy link
Copy Markdown
Member

probably 1500ms or 2000ms is good enough

@jatinkumar2409

Copy link
Copy Markdown
Contributor Author

@BrayanDSO yeah 1500ms or 2000ms can be good but 2500 is for old and laggy phones

@jatinkumar2409

Copy link
Copy Markdown
Contributor Author

@david-allison i am creating the wrapper in com.ichi2.anki.utils directory

@jatinkumar2409

Copy link
Copy Markdown
Contributor Author

@david-allison I have created the custom class for sensorManager. Requested for your review

Comment thread AnkiDroid/src/main/java/com/ichi2/anki/utils/AnkiShakeDetector.kt Outdated

@david-allison david-allison left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! Implementers choice on these nitpicks.

Only request is knowing where the 2500 came from

Comment thread AnkiDroid/src/main/java/com/ichi2/anki/ui/windows/reviewer/ReviewerFragment.kt Outdated
Comment thread AnkiDroid/src/main/java/com/ichi2/anki/utils/AnkiShakeDetector.kt Outdated
private val sensorManager: SensorManager?,
private val sensorDelay: Int,
private val listener: ShakeDetector.Listener,
// The minimum time between two shakes in milliseconds . 2500 is used as default value

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you select this value? Due to your testing on what device?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I chose this value because the setduedate dialog of AbstractFlashCardViewer (legacy screen) was appearing multiple times during frequent shaking in my realme c30s . It is the older 2gb ram phone , so i thought this problem can also arise in more older phones. That's why to be safe , i chose higher threshold

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the time logic should work the same independently of how slow the phone is

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a comment in the code.

As Brayan said, timing should also be phone-independent, select a value which works for you here, don't make assumptions we'll need to be safe for legacy hardware.

Comment thread AnkiDroid/src/main/java/com/ichi2/anki/utils/AnkiShakeDetector.kt Outdated
Comment thread AnkiDroid/src/main/java/com/ichi2/anki/utils/AnkiShakeDetector.kt Outdated
Comment thread AnkiDroid/src/main/java/com/ichi2/anki/AbstractFlashcardViewer.kt
Comment thread AnkiDroid/src/main/java/com/ichi2/anki/utils/AnkiShakeDetector.kt Outdated
Comment thread AnkiDroid/src/main/java/com/ichi2/anki/utils/AnkiShakeDetector.kt Outdated
@jatinkumar2409

Copy link
Copy Markdown
Contributor Author

@david-allison
I chose this 2500 as threshold because the setduedate dialog of AbstractFlashCardViewer (legacy screen) was appearing multiple times during frequent shaking in my realme c30s . It is the older 2gb ram phone , so i thought this problem can also arise in more older phones. That's why to be safe , i chose higher threshold

@jatinkumar2409

Copy link
Copy Markdown
Contributor Author

@david-allison fixed the requested changes . Require your review

* in rapid succession during a single physical shake event.
*/
class AnkiShakeDetector(
// The sensor manager to use for the shake detection

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// The sensor manager to use for the shake detection

Comment thread AnkiDroid/src/main/java/com/ichi2/anki/utils/AnkiShakeDetector.kt Outdated
private val sensorManager: SensorManager?,
private val sensorDelay: Int,
private val listener: ShakeDetector.Listener,
// The minimum time between two shakes in milliseconds . 2500 is used as default value

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a comment in the code.

As Brayan said, timing should also be phone-independent, select a value which works for you here, don't make assumptions we'll need to be safe for legacy hardware.

@jatinkumar2409

jatinkumar2409 commented Apr 30, 2026

Copy link
Copy Markdown
Contributor Author

@david-allison your requested changes have already fixed . You reviewed the older code as your comments have outdated . Can you please recheck it . As i moved the AnkiShakeDetector from utils directory to android

@david-allison

Copy link
Copy Markdown
Member

Thanks! #20879 (comment) is still pending

@jatinkumar2409

Copy link
Copy Markdown
Contributor Author

@BrayanDSO I updated the code . Needs your review

@BrayanDSO BrayanDSO left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks

import kotlin.time.Duration.Companion.milliseconds

/*
* Wrapper for a [ShakeDetector] to provide a cooldown mechanism.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Wrapper for a [ShakeDetector] to provide a cooldown mechanism.
* Wrapper for a [ShakeDetector] to provide a cooldown mechanism.

@BrayanDSO BrayanDSO added Pending Merge Things with approval that are waiting future merge (e.g. targets a future release, CI wait, etc) and removed Needs Review Needs Second Approval Has one approval, one more approval to merge labels May 5, 2026
@jatinkumar2409

Copy link
Copy Markdown
Contributor Author

@david-allison @BrayanDSO Thank you for mentoring me in this issue . As this was my first open source contribution . Can i get the short feedback so that i can improve for future contributions.
Thank you

@jatinkumar2409 jatinkumar2409 force-pushed the main branch 2 times, most recently from 1dad002 to be22fef Compare May 6, 2026 09:49

@BrayanDSO BrayanDSO left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mixed your commit with another one. Please rebase your branch with only our changes

@BrayanDSO BrayanDSO added Needs Author Reply Waiting for a reply from the original author and removed Pending Merge Things with approval that are waiting future merge (e.g. targets a future release, CI wait, etc) labels May 6, 2026
@jatinkumar2409

Copy link
Copy Markdown
Contributor Author

@BrayanDSO I fixed that

@jatinkumar2409 jatinkumar2409 requested a review from BrayanDSO May 6, 2026 10:44
@BrayanDSO

Copy link
Copy Markdown
Member

address David's comment about the copyright header

@jatinkumar2409

Copy link
Copy Markdown
Contributor Author

@BrayanDSO It was just the small formatting . I already fixed that

@BrayanDSO

Copy link
Copy Markdown
Member

so push the changes. It looks the same here.

@jatinkumar2409

jatinkumar2409 commented May 6, 2026

Copy link
Copy Markdown
Contributor Author

@BrayanDSO Fixed that

@BrayanDSO BrayanDSO left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

other cleanups can be made later. LGTM

@jatinkumar2409

Copy link
Copy Markdown
Contributor Author

@BrayanDSO Can this be merged now

@BrayanDSO BrayanDSO added this pull request to the merge queue May 7, 2026
@BrayanDSO BrayanDSO added Pending Merge Things with approval that are waiting future merge (e.g. targets a future release, CI wait, etc) and removed Needs Author Reply Waiting for a reply from the original author labels May 7, 2026
Merged via the queue into ankidroid:main with commit 23b04d9 May 7, 2026
19 checks passed
@github-actions github-actions Bot removed the Pending Merge Things with approval that are waiting future merge (e.g. targets a future release, CI wait, etc) label May 7, 2026
@github-actions github-actions Bot added this to the 2.25 release milestone May 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Shake gesture shouldn't trigger repeatedly

4 participants