From eaec494bc5e023045a4e844d64977324078255fb Mon Sep 17 00:00:00 2001 From: Darcy Wong Date: Tue, 27 Aug 2024 08:34:19 +0700 Subject: [PATCH] fix(android): Add gating to setLongpressDelay() --- .../kmapro/AdjustLongpressDelayActivity.java | 6 ++---- .../main/java/com/keyman/engine/KMManager.java | 15 +++++++++++++-- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/android/KMAPro/kMAPro/src/main/java/com/tavultesoft/kmapro/AdjustLongpressDelayActivity.java b/android/KMAPro/kMAPro/src/main/java/com/tavultesoft/kmapro/AdjustLongpressDelayActivity.java index 2ecf62fe099..e79cb917ff3 100644 --- a/android/KMAPro/kMAPro/src/main/java/com/tavultesoft/kmapro/AdjustLongpressDelayActivity.java +++ b/android/KMAPro/kMAPro/src/main/java/com/tavultesoft/kmapro/AdjustLongpressDelayActivity.java @@ -31,8 +31,6 @@ public class AdjustLongpressDelayActivity extends BaseActivity { // Keeps track of the adjusted longpress delay time for saving. // Internally use milliseconds, but GUI displays seconds private static int currentDelayTimeMS = KMManager.KMDefault_LongpressDelay; // ms - private static int minLongpressTime = 300; // ms - private static int maxLongpressTime = 1500; // ms private static int delayTimeIncrement = 200; // ms /** @@ -112,7 +110,7 @@ public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { findViewById(R.id.delayTimeDownButton).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - if (currentDelayTimeMS > minLongpressTime) { + if (currentDelayTimeMS > KMManager.KMMinimum_LongpressDelay) { currentDelayTimeMS -= delayTimeIncrement; seekBar.setProgress(delayTimeToProgress()); } @@ -122,7 +120,7 @@ public void onClick(View v) { findViewById(R.id.delayTimeUpButton).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - if (currentDelayTimeMS < maxLongpressTime) { + if (currentDelayTimeMS < KMManager.KMMaximum_LongpressDelay) { currentDelayTimeMS += delayTimeIncrement; seekBar.setProgress(delayTimeToProgress()); } diff --git a/android/KMEA/app/src/main/java/com/keyman/engine/KMManager.java b/android/KMEA/app/src/main/java/com/keyman/engine/KMManager.java index 89690dffec0..592f4be2a15 100644 --- a/android/KMEA/app/src/main/java/com/keyman/engine/KMManager.java +++ b/android/KMEA/app/src/main/java/com/keyman/engine/KMManager.java @@ -310,8 +310,11 @@ public enum EnterModeType { public static final String KMDefault_DictionaryVersion = "0.1.4"; public static final String KMDefault_DictionaryKMP = KMDefault_DictionaryPackageID + FileUtils.MODELPACKAGE; - // Default KeymanWeb longpress delay in milliseconds + // Default KeymanWeb longpress delay constants in milliseconds public static final int KMDefault_LongpressDelay = 500; + public static final int KMMinimum_LongpressDelay = 300; + public static final int KMMaximum_LongpressDelay = 1500; + // Keyman files protected static final String KMFilename_KeyboardHtml = "keyboard.html"; @@ -2097,14 +2100,22 @@ public static int getLongpressDelay() { /** * Set the longpress delay (in milliseconds) as a stored preference. + * Valid range is 300 ms to 1500 ms. Returns true if the preference is successfully stored. * @param longpressDelay - int longpress delay in milliseconds + * @return boolean */ - public static void setLongpressDelay(int longpressDelay) { + public static boolean setLongpressDelay(int longpressDelay) { + if (longpressDelay < KMMinimum_LongpressDelay || longpressDelay > KMMaximum_LongpressDelay) { + return false; + } + SharedPreferences prefs = appContext.getSharedPreferences( appContext.getString(R.string.kma_prefs_name), Context.MODE_PRIVATE); SharedPreferences.Editor editor = prefs.edit(); editor.putInt(KMKey_LongpressDelay, longpressDelay); editor.commit(); + + return true; } /**