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").
This commit is contained in:
Jules Aguillon 2023-06-03 11:08:22 +02:00
parent 6f418727cf
commit 69e0b4c2a2
20 changed files with 175 additions and 11 deletions

View File

@ -67,6 +67,11 @@
<!-- <string name="pref_category_advanced">Advanced</string> --> <!-- <string name="pref_category_advanced">Advanced</string> -->
<!-- <string name="pref_custom_layout_title">Custom layout</string> --> <!-- <string name="pref_custom_layout_title">Custom layout</string> -->
<!-- <string name="pref_custom_layout_summary">See the source code. This option is not meant to be used.</string> --> <!-- <string name="pref_custom_layout_summary">See the source code. This option is not meant to be used.</string> -->
<!-- <string name="pref_vibration_e_disabled">Disabled</string> -->
<!-- <string name="pref_vibration_e_system">System</string> -->
<!-- <string name="pref_vibration_e_strong">Strong</string> -->
<!-- <string name="pref_vibration_e_medium">Medium</string> -->
<!-- <string name="pref_vibration_e_light">Light</string> -->
<string name="key_action_next">Další</string> <string name="key_action_next">Další</string>
<string name="key_action_done">Dokončit</string> <string name="key_action_done">Dokončit</string>
<string name="key_action_go">Spustit</string> <string name="key_action_go">Spustit</string>

View File

@ -67,6 +67,11 @@
<string name="pref_category_advanced">Erweitert</string> <string name="pref_category_advanced">Erweitert</string>
<string name="pref_custom_layout_title">Eigenes Layout</string> <string name="pref_custom_layout_title">Eigenes Layout</string>
<string name="pref_custom_layout_summary">Bitte Sourcecode ansehen. Diese Option ist nicht zur Verwendung vorgesehen.</string> <string name="pref_custom_layout_summary">Bitte Sourcecode ansehen. Diese Option ist nicht zur Verwendung vorgesehen.</string>
<!-- <string name="pref_vibration_e_disabled">Disabled</string> -->
<!-- <string name="pref_vibration_e_system">System</string> -->
<!-- <string name="pref_vibration_e_strong">Strong</string> -->
<!-- <string name="pref_vibration_e_medium">Medium</string> -->
<!-- <string name="pref_vibration_e_light">Light</string> -->
<string name="key_action_next">Nächstes</string> <string name="key_action_next">Nächstes</string>
<string name="key_action_done">Fertig</string> <string name="key_action_done">Fertig</string>
<string name="key_action_go">Los</string> <string name="key_action_go">Los</string>

View File

@ -67,6 +67,11 @@
<!-- <string name="pref_category_advanced">Advanced</string> --> <!-- <string name="pref_category_advanced">Advanced</string> -->
<!-- <string name="pref_custom_layout_title">Custom layout</string> --> <!-- <string name="pref_custom_layout_title">Custom layout</string> -->
<!-- <string name="pref_custom_layout_summary">See the source code. This option is not meant to be used.</string> --> <!-- <string name="pref_custom_layout_summary">See the source code. This option is not meant to be used.</string> -->
<!-- <string name="pref_vibration_e_disabled">Disabled</string> -->
<!-- <string name="pref_vibration_e_system">System</string> -->
<!-- <string name="pref_vibration_e_strong">Strong</string> -->
<!-- <string name="pref_vibration_e_medium">Medium</string> -->
<!-- <string name="pref_vibration_e_light">Light</string> -->
<string name="key_action_next">Siguiente</string> <string name="key_action_next">Siguiente</string>
<string name="key_action_done">Hecho</string> <string name="key_action_done">Hecho</string>
<string name="key_action_go">Ir</string> <string name="key_action_go">Ir</string>

View File

@ -67,6 +67,11 @@
<string name="pref_category_advanced">پیشرفته</string> <string name="pref_category_advanced">پیشرفته</string>
<string name="pref_custom_layout_title">طرح شخصی</string> <string name="pref_custom_layout_title">طرح شخصی</string>
<string name="pref_custom_layout_summary">کد منبع را ببینید. این گزینه قرار نیست استفاده شود.</string> <string name="pref_custom_layout_summary">کد منبع را ببینید. این گزینه قرار نیست استفاده شود.</string>
<!-- <string name="pref_vibration_e_disabled">Disabled</string> -->
<!-- <string name="pref_vibration_e_system">System</string> -->
<!-- <string name="pref_vibration_e_strong">Strong</string> -->
<!-- <string name="pref_vibration_e_medium">Medium</string> -->
<!-- <string name="pref_vibration_e_light">Light</string> -->
<string name="key_action_next">بعدی</string> <string name="key_action_next">بعدی</string>
<string name="key_action_done">اتمام</string> <string name="key_action_done">اتمام</string>
<string name="key_action_go">برو</string> <string name="key_action_go">برو</string>

View File

@ -67,6 +67,11 @@
<string name="pref_category_advanced">Avancé</string> <string name="pref_category_advanced">Avancé</string>
<string name="pref_custom_layout_title">Disposition personnalisée</string> <string name="pref_custom_layout_title">Disposition personnalisée</string>
<string name="pref_custom_layout_summary">Cette option n\'est pas faite pour être utilisée.</string> <string name="pref_custom_layout_summary">Cette option n\'est pas faite pour être utilisée.</string>
<!-- <string name="pref_vibration_e_disabled">Disabled</string> -->
<!-- <string name="pref_vibration_e_system">System</string> -->
<!-- <string name="pref_vibration_e_strong">Strong</string> -->
<!-- <string name="pref_vibration_e_medium">Medium</string> -->
<!-- <string name="pref_vibration_e_light">Light</string> -->
<string name="key_action_next">Suiv.</string> <string name="key_action_next">Suiv.</string>
<string name="key_action_done">Fin</string> <string name="key_action_done">Fin</string>
<string name="key_action_go">Aller</string> <string name="key_action_go">Aller</string>

View File

@ -67,6 +67,11 @@
<!-- <string name="pref_category_advanced">Advanced</string> --> <!-- <string name="pref_category_advanced">Advanced</string> -->
<!-- <string name="pref_custom_layout_title">Custom layout</string> --> <!-- <string name="pref_custom_layout_title">Custom layout</string> -->
<!-- <string name="pref_custom_layout_summary">See the source code. This option is not meant to be used.</string> --> <!-- <string name="pref_custom_layout_summary">See the source code. This option is not meant to be used.</string> -->
<!-- <string name="pref_vibration_e_disabled">Disabled</string> -->
<!-- <string name="pref_vibration_e_system">System</string> -->
<!-- <string name="pref_vibration_e_strong">Strong</string> -->
<!-- <string name="pref_vibration_e_medium">Medium</string> -->
<!-- <string name="pref_vibration_e_light">Light</string> -->
<string name="key_action_next">Prossimo</string> <string name="key_action_next">Prossimo</string>
<string name="key_action_done">Fatto</string> <string name="key_action_done">Fatto</string>
<string name="key_action_go">Vai</string> <string name="key_action_go">Vai</string>

View File

@ -67,6 +67,11 @@
<!-- <string name="pref_category_advanced">Advanced</string> --> <!-- <string name="pref_category_advanced">Advanced</string> -->
<!-- <string name="pref_custom_layout_title">Custom layout</string> --> <!-- <string name="pref_custom_layout_title">Custom layout</string> -->
<!-- <string name="pref_custom_layout_summary">See the source code. This option is not meant to be used.</string> --> <!-- <string name="pref_custom_layout_summary">See the source code. This option is not meant to be used.</string> -->
<!-- <string name="pref_vibration_e_disabled">Disabled</string> -->
<!-- <string name="pref_vibration_e_system">System</string> -->
<!-- <string name="pref_vibration_e_strong">Strong</string> -->
<!-- <string name="pref_vibration_e_medium">Medium</string> -->
<!-- <string name="pref_vibration_e_light">Light</string> -->
<string name="key_action_next">다음</string> <string name="key_action_next">다음</string>
<string name="key_action_done">확인</string> <string name="key_action_done">확인</string>
<string name="key_action_go">Go</string> <string name="key_action_go">Go</string>

View File

@ -67,6 +67,11 @@
<string name="pref_category_advanced">Papildu</string> <string name="pref_category_advanced">Papildu</string>
<string name="pref_custom_layout_title">Pielāgots izkārtojums</string> <string name="pref_custom_layout_title">Pielāgots izkārtojums</string>
<string name="pref_custom_layout_summary">Skatīt pirmkodu. Šī iespēja nav paredzēta izmantošanai.</string> <string name="pref_custom_layout_summary">Skatīt pirmkodu. Šī iespēja nav paredzēta izmantošanai.</string>
<!-- <string name="pref_vibration_e_disabled">Disabled</string> -->
<!-- <string name="pref_vibration_e_system">System</string> -->
<!-- <string name="pref_vibration_e_strong">Strong</string> -->
<!-- <string name="pref_vibration_e_medium">Medium</string> -->
<!-- <string name="pref_vibration_e_light">Light</string> -->
<string name="key_action_next">Nākamais</string> <string name="key_action_next">Nākamais</string>
<string name="key_action_done">Darīts</string> <string name="key_action_done">Darīts</string>
<string name="key_action_go">Aiziet</string> <string name="key_action_go">Aiziet</string>

View File

@ -67,6 +67,11 @@
<string name="pref_category_advanced">Zaawansowane</string> <string name="pref_category_advanced">Zaawansowane</string>
<string name="pref_custom_layout_title">Własny układ</string> <string name="pref_custom_layout_title">Własny układ</string>
<string name="pref_custom_layout_summary">Zobacz kod źródłowy. Ta opcja nie jest przeznaczona do użycia.</string> <string name="pref_custom_layout_summary">Zobacz kod źródłowy. Ta opcja nie jest przeznaczona do użycia.</string>
<!-- <string name="pref_vibration_e_disabled">Disabled</string> -->
<!-- <string name="pref_vibration_e_system">System</string> -->
<!-- <string name="pref_vibration_e_strong">Strong</string> -->
<!-- <string name="pref_vibration_e_medium">Medium</string> -->
<!-- <string name="pref_vibration_e_light">Light</string> -->
<string name="key_action_next">Dalej</string> <string name="key_action_next">Dalej</string>
<string name="key_action_done">OK</string> <string name="key_action_done">OK</string>
<string name="key_action_go">Przejdź</string> <string name="key_action_go">Przejdź</string>

View File

@ -67,6 +67,11 @@
<string name="pref_category_advanced">Avançado</string> <string name="pref_category_advanced">Avançado</string>
<string name="pref_custom_layout_title">Layout personalizado</string> <string name="pref_custom_layout_title">Layout personalizado</string>
<string name="pref_custom_layout_summary">Veja o código-fonte. Esta opção não deve ser usada.</string> <string name="pref_custom_layout_summary">Veja o código-fonte. Esta opção não deve ser usada.</string>
<!-- <string name="pref_vibration_e_disabled">Disabled</string> -->
<!-- <string name="pref_vibration_e_system">System</string> -->
<!-- <string name="pref_vibration_e_strong">Strong</string> -->
<!-- <string name="pref_vibration_e_medium">Medium</string> -->
<!-- <string name="pref_vibration_e_light">Light</string> -->
<string name="key_action_next">Próximo</string> <string name="key_action_next">Próximo</string>
<string name="key_action_done">Pronto</string> <string name="key_action_done">Pronto</string>
<string name="key_action_go">Ir</string> <string name="key_action_go">Ir</string>

View File

@ -67,6 +67,11 @@
<string name="pref_category_advanced">Расширенные настройки</string> <string name="pref_category_advanced">Расширенные настройки</string>
<string name="pref_custom_layout_title">Пользовательская раскладка</string> <string name="pref_custom_layout_title">Пользовательская раскладка</string>
<string name="pref_custom_layout_summary">Откройте исходный код. Эта параметр не предназначен для использования.</string> <string name="pref_custom_layout_summary">Откройте исходный код. Эта параметр не предназначен для использования.</string>
<!-- <string name="pref_vibration_e_disabled">Disabled</string> -->
<!-- <string name="pref_vibration_e_system">System</string> -->
<!-- <string name="pref_vibration_e_strong">Strong</string> -->
<!-- <string name="pref_vibration_e_medium">Medium</string> -->
<!-- <string name="pref_vibration_e_light">Light</string> -->
<string name="key_action_next">Далее</string> <string name="key_action_next">Далее</string>
<string name="key_action_done">Ввод</string> <string name="key_action_done">Ввод</string>
<string name="key_action_go">Go</string> <string name="key_action_go">Go</string>

View File

@ -67,6 +67,11 @@
<!-- <string name="pref_category_advanced">Advanced</string> --> <!-- <string name="pref_category_advanced">Advanced</string> -->
<!-- <string name="pref_custom_layout_title">Custom layout</string> --> <!-- <string name="pref_custom_layout_title">Custom layout</string> -->
<!-- <string name="pref_custom_layout_summary">See the source code. This option is not meant to be used.</string> --> <!-- <string name="pref_custom_layout_summary">See the source code. This option is not meant to be used.</string> -->
<!-- <string name="pref_vibration_e_disabled">Disabled</string> -->
<!-- <string name="pref_vibration_e_system">System</string> -->
<!-- <string name="pref_vibration_e_strong">Strong</string> -->
<!-- <string name="pref_vibration_e_medium">Medium</string> -->
<!-- <string name="pref_vibration_e_light">Light</string> -->
<string name="key_action_next">İleri</string> <string name="key_action_next">İleri</string>
<string name="key_action_done">Bitti</string> <string name="key_action_done">Bitti</string>
<string name="key_action_go">İlerle</string> <string name="key_action_go">İlerle</string>

View File

@ -69,6 +69,11 @@
<string name="pref_category_advanced">Nâng cao</string> <string name="pref_category_advanced">Nâng cao</string>
<string name="pref_custom_layout_title">Tùy chỉnh bố cục</string> <string name="pref_custom_layout_title">Tùy chỉnh bố cục</string>
<string name="pref_custom_layout_summary">Hãy xem mã nguồn. Lựa chọn này không có mục đích sử dụng.</string> <string name="pref_custom_layout_summary">Hãy xem mã nguồn. Lựa chọn này không có mục đích sử dụng.</string>
<!-- <string name="pref_vibration_e_disabled">Disabled</string> -->
<!-- <string name="pref_vibration_e_system">System</string> -->
<!-- <string name="pref_vibration_e_strong">Strong</string> -->
<!-- <string name="pref_vibration_e_medium">Medium</string> -->
<!-- <string name="pref_vibration_e_light">Light</string> -->
<string name="key_action_next">Tiếp</string> <string name="key_action_next">Tiếp</string>
<string name="key_action_done">Xong</string> <string name="key_action_done">Xong</string>
<string name="key_action_go">Đi</string> <string name="key_action_go">Đi</string>

View File

@ -67,6 +67,11 @@
<!-- <string name="pref_category_advanced">Advanced</string> --> <!-- <string name="pref_category_advanced">Advanced</string> -->
<!-- <string name="pref_custom_layout_title">Custom layout</string> --> <!-- <string name="pref_custom_layout_title">Custom layout</string> -->
<!-- <string name="pref_custom_layout_summary">See the source code. This option is not meant to be used.</string> --> <!-- <string name="pref_custom_layout_summary">See the source code. This option is not meant to be used.</string> -->
<!-- <string name="pref_vibration_e_disabled">Disabled</string> -->
<!-- <string name="pref_vibration_e_system">System</string> -->
<!-- <string name="pref_vibration_e_strong">Strong</string> -->
<!-- <string name="pref_vibration_e_medium">Medium</string> -->
<!-- <string name="pref_vibration_e_light">Light</string> -->
<string name="key_action_next">下一项</string> <string name="key_action_next">下一项</string>
<string name="key_action_done">完成</string> <string name="key_action_done">完成</string>
<string name="key_action_go">前往</string> <string name="key_action_go">前往</string>

View File

@ -134,4 +134,18 @@
<item>25</item> <item>25</item>
<item>35</item> <item>35</item>
</string-array> </string-array>
<string-array name="pref_vibration_entries">
<item>@string/pref_vibration_e_disabled</item>
<item>@string/pref_vibration_e_system</item>
<item>@string/pref_vibration_e_strong</item>
<item>@string/pref_vibration_e_medium</item>
<item>@string/pref_vibration_e_light</item>
</string-array>
<string-array name="pref_vibration_values">
<item>disabled</item>
<item>system</item>
<item>strong</item>
<item>medium</item>
<item>light</item>
</string-array>
</resources> </resources>

View File

@ -67,6 +67,11 @@
<string name="pref_category_advanced">Advanced</string> <string name="pref_category_advanced">Advanced</string>
<string name="pref_custom_layout_title">Custom layout</string> <string name="pref_custom_layout_title">Custom layout</string>
<string name="pref_custom_layout_summary">See the source code. This option is not meant to be used.</string> <string name="pref_custom_layout_summary">See the source code. This option is not meant to be used.</string>
<string name="pref_vibration_e_disabled">Disabled</string>
<string name="pref_vibration_e_system">System</string>
<string name="pref_vibration_e_strong">Strong</string>
<string name="pref_vibration_e_medium">Medium</string>
<string name="pref_vibration_e_light">Light</string>
<string name="key_action_next">Next</string> <string name="key_action_next">Next</string>
<string name="key_action_done">Done</string> <string name="key_action_done">Done</string>
<string name="key_action_go">Go</string> <string name="key_action_go">Go</string>

View File

@ -55,7 +55,7 @@
<PreferenceCategory android:title="@string/pref_category_behavior"> <PreferenceCategory android:title="@string/pref_category_behavior">
<CheckBoxPreference android:key="autocapitalisation" android:title="@string/pref_autocapitalisation_title" android:summary="@string/pref_autocapitalisation_summary" android:defaultValue="true"/> <CheckBoxPreference android:key="autocapitalisation" android:title="@string/pref_autocapitalisation_title" android:summary="@string/pref_autocapitalisation_summary" android:defaultValue="true"/>
<CheckBoxPreference android:key="switch_input_immediate" android:title="@string/pref_switch_input_immediate_title" android:summary="@string/pref_switch_input_immediate_summary" android:defaultValue="false"/> <CheckBoxPreference android:key="switch_input_immediate" android:title="@string/pref_switch_input_immediate_title" android:summary="@string/pref_switch_input_immediate_summary" android:defaultValue="false"/>
<CheckBoxPreference android:key="vibrate_enabled" android:title="@string/pref_vibrate_title" android:summary="@string/pref_vibrate_summary" android:defaultValue="true"/> <ListPreference android:key="vibration_behavior" android:title="@string/pref_vibrate_title" android:summary="@string/pref_vibrate_summary" android:defaultValue="system" android:entries="@array/pref_vibration_entries" android:entryValues="@array/pref_vibration_values"/>
</PreferenceCategory> </PreferenceCategory>
<PreferenceCategory android:title="@string/pref_category_style"> <PreferenceCategory android:title="@string/pref_category_style">
<ListPreference android:key="theme" android:title="@string/pref_theme" android:summary="%s" android:defaultValue="system" android:entries="@array/pref_theme_entries" android:entryValues="@array/pref_theme_values"/> <ListPreference android:key="theme" android:title="@string/pref_theme" android:summary="%s" android:defaultValue="system" android:entries="@array/pref_theme_entries" android:entryValues="@array/pref_theme_values"/>

View File

@ -31,7 +31,7 @@ final class Config
public boolean number_row; public boolean number_row;
public float swipe_dist_px; public float swipe_dist_px;
public float slide_step_px; public float slide_step_px;
public boolean vibrateEnabled; public VibratorCompat.VibrationBehavior vibration_behavior;
public long longPressTimeout; public long longPressTimeout;
public long longPressInterval; public long longPressInterval;
public float margin_bottom; public float margin_bottom;
@ -123,7 +123,8 @@ final class Config
float swipe_dist_value = Float.valueOf(_prefs.getString("swipe_dist", "15")); float swipe_dist_value = Float.valueOf(_prefs.getString("swipe_dist", "15"));
swipe_dist_px = swipe_dist_value / 25.f * swipe_scaling; swipe_dist_px = swipe_dist_value / 25.f * swipe_scaling;
slide_step_px = swipe_dist_px / 4.f; 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); longPressTimeout = _prefs.getInt("longpress_timeout", 600);
longPressInterval = _prefs.getInt("longpress_interval", 65); longPressInterval = _prefs.getInt("longpress_interval", 65);
margin_bottom = get_dip_pref(dm, oriented_pref("margin_bottom"), margin_bottom = get_dip_pref(dm, oriented_pref("margin_bottom"),

View File

@ -10,7 +10,6 @@ import android.inputmethodservice.InputMethodService;
import android.os.Build.VERSION; import android.os.Build.VERSION;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.util.DisplayMetrics; import android.util.DisplayMetrics;
import android.view.HapticFeedbackConstants;
import android.view.MotionEvent; import android.view.MotionEvent;
import android.view.View; import android.view.View;
import android.view.Window; import android.view.Window;
@ -231,13 +230,7 @@ public class Keyboard2View extends View
private void vibrate() private void vibrate()
{ {
if (!_config.vibrateEnabled) VibratorCompat.vibrate(this, _config.vibration_behavior);
return ;
if (VERSION.SDK_INT >= 5)
{
performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY,
HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
}
} }
@Override @Override

View File

@ -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;
}
}
}
}