diff options
| author | Jules Aguillon | 2023-06-03 11:08:22 +0200 |
|---|---|---|
| committer | Jules Aguillon | 2023-06-03 11:15:19 +0200 |
| commit | 69e0b4c2a2424b83ce0ec876938ec3e73dd5b612 (patch) | |
| tree | 75c002dcf243d440497a6ce11b30c50a50b4bd8f /srcs | |
| parent | 6f418727cfcdd9377a345c8a40e200f701aa7ac7 (diff) | |
| download | unexpected-keyboard-69e0b4c2a2424b83ce0ec876938ec3e73dd5b612.tar.gz unexpected-keyboard-69e0b4c2a2424b83ce0ec876938ec3e73dd5b612.zip | |
More control over vibration
The newer haptic feedback API that is used instead of the vibrator
service since ef03dfe doesn't work for everyone.
The new vibration option allow to choose both the newer API ("system")
and the older API ("strong", "medium", "light").
Diffstat (limited to 'srcs')
| -rw-r--r-- | srcs/juloo.keyboard2/Config.java | 5 | ||||
| -rw-r--r-- | srcs/juloo.keyboard2/Keyboard2View.java | 9 | ||||
| -rw-r--r-- | srcs/juloo.keyboard2/VibratorCompat.java | 81 |
3 files changed, 85 insertions, 10 deletions
diff --git a/srcs/juloo.keyboard2/Config.java b/srcs/juloo.keyboard2/Config.java index 1abef6b..9fa6593 100644 --- a/srcs/juloo.keyboard2/Config.java +++ b/srcs/juloo.keyboard2/Config.java @@ -31,7 +31,7 @@ final class Config public boolean number_row; public float swipe_dist_px; public float slide_step_px; - public boolean vibrateEnabled; + public VibratorCompat.VibrationBehavior vibration_behavior; public long longPressTimeout; public long longPressInterval; public float margin_bottom; @@ -123,7 +123,8 @@ final class Config float swipe_dist_value = Float.valueOf(_prefs.getString("swipe_dist", "15")); swipe_dist_px = swipe_dist_value / 25.f * swipe_scaling; slide_step_px = swipe_dist_px / 4.f; - vibrateEnabled = _prefs.getBoolean("vibrate_enabled", true); + vibration_behavior = + VibratorCompat.VibrationBehavior.of_string(_prefs.getString("vibration_behavior", "system")); longPressTimeout = _prefs.getInt("longpress_timeout", 600); longPressInterval = _prefs.getInt("longpress_interval", 65); margin_bottom = get_dip_pref(dm, oriented_pref("margin_bottom"), diff --git a/srcs/juloo.keyboard2/Keyboard2View.java b/srcs/juloo.keyboard2/Keyboard2View.java index 65098d0..f4053f2 100644 --- a/srcs/juloo.keyboard2/Keyboard2View.java +++ b/srcs/juloo.keyboard2/Keyboard2View.java @@ -10,7 +10,6 @@ import android.inputmethodservice.InputMethodService; import android.os.Build.VERSION; import android.util.AttributeSet; import android.util.DisplayMetrics; -import android.view.HapticFeedbackConstants; import android.view.MotionEvent; import android.view.View; import android.view.Window; @@ -231,13 +230,7 @@ public class Keyboard2View extends View private void vibrate() { - if (!_config.vibrateEnabled) - return ; - if (VERSION.SDK_INT >= 5) - { - performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY, - HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING); - } + VibratorCompat.vibrate(this, _config.vibration_behavior); } @Override diff --git a/srcs/juloo.keyboard2/VibratorCompat.java b/srcs/juloo.keyboard2/VibratorCompat.java new file mode 100644 index 0000000..99ff5f5 --- /dev/null +++ b/srcs/juloo.keyboard2/VibratorCompat.java @@ -0,0 +1,81 @@ +package juloo.keyboard2; + +import android.content.Context; +import android.os.Build.VERSION; +import android.os.Vibrator; +import android.view.HapticFeedbackConstants; +import android.view.View; + +public final class VibratorCompat +{ + public static void vibrate(View v, VibrationBehavior b) + { + switch (b) + { + case DISABLED: + break; + case SYSTEM: + if (VERSION.SDK_INT >= 8) + v.performHapticFeedback(HapticFeedbackConstants.KEYBOARD_TAP, + HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING); + break; + case STRONG: + vibrator_vibrate(v, 50); + break; + case MEDIUM: + vibrator_vibrate(v, 20); + break; + case LIGHT: + vibrator_vibrate(v, 10); + break; + } + } + + /** Use the older [Vibrator] when the newer API is not available or the user + wants more control. */ + static void vibrator_vibrate(View v, int duration) + { + try + { + get_vibrator(v).vibrate(duration); + } + catch (Exception e) {} + } + + static Vibrator vibrator_service = null; + + static Vibrator get_vibrator(View v) + { + if (vibrator_service == null) + { + vibrator_service = + (Vibrator)v.getContext().getSystemService(Context.VIBRATOR_SERVICE); + } + return vibrator_service; + } + + public static enum VibrationBehavior + { + DISABLED, + SYSTEM, + STRONG, + MEDIUM, + LIGHT; + + VibrationBehavior() {} + + /** Defaults [SYSTEM] for unrecognized strings. */ + public static VibrationBehavior of_string(String s) + { + switch (s) + { + case "disabled": return DISABLED; + case "system": return SYSTEM; + case "strong": return STRONG; + case "medium": return MEDIUM; + case "light": return LIGHT; + default: return SYSTEM; + } + } + } +} |
