From bda3b05624a85772d2af74614153cf29062cdd6a Mon Sep 17 00:00:00 2001 From: serso Date: Sat, 29 Oct 2011 12:12:06 +0400 Subject: [PATCH] haptic feedback duration preference --- res/values-ru/strings.xml | 9 +++++++- res/values/arrays.xml | 11 +++++++++ res/values/default_values.xml | 5 +++- res/values/strings.xml | 7 ++++++ res/xml/main_preferences.xml | 6 +++++ .../calculator/CalculatorActivity.java | 4 ++-- .../CalculatorPreferencesActivity.java | 4 ++++ .../view/widgets/OnClickListenerVibrator.java | 4 ++-- .../view/widgets/VibratorContainer.java | 23 ++++++++++++++----- .../org/solovyev/common/NumberParser.java | 2 ++ 10 files changed, 63 insertions(+), 12 deletions(-) diff --git a/res/values-ru/strings.xml b/res/values-ru/strings.xml index cdb22f0e..dc32f56c 100644 --- a/res/values-ru/strings.xml +++ b/res/values-ru/strings.xml @@ -107,7 +107,14 @@ Калькулятор Устанавливает раскладку кнопок - Тактильный ввод + Отклик на нажатие Включает/выключает вибрацию по нажатию клавиши + Маленькая + Средняя + Большая + + Длительность отклика + Длительность вибрации по нажатию клавиши + diff --git a/res/values/arrays.xml b/res/values/arrays.xml index 19744b9e..3e88a0d8 100644 --- a/res/values/arrays.xml +++ b/res/values/arrays.xml @@ -36,4 +36,15 @@ main_calculator + + @string/p_calc_haptic_feedback_strength_short + @string/p_calc_haptic_feedback_strength_middle + @string/p_calc_haptic_feedback_strength_long + + + 30 + 60 + 100 + + \ No newline at end of file diff --git a/res/values/default_values.xml b/res/values/default_values.xml index eea88c45..80035004 100644 --- a/res/values/default_values.xml +++ b/res/values/default_values.xml @@ -34,6 +34,9 @@ main_calculator org.solovyev.android.calculator.CalculatorActivity_calc_grouping_separator - + " " + + org.solovyev.android.calculator.CalculatorActivity_calc_haptic_feedback_duration_key + " " \ No newline at end of file diff --git a/res/values/strings.xml b/res/values/strings.xml index d7fcd38c..30136621 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -111,4 +111,11 @@ Haptic feedback Toggles vibration on button click + Short + Middle + Long + + Haptic feedback duration + Duration vibration on button click + diff --git a/res/xml/main_preferences.xml b/res/xml/main_preferences.xml index e8cce53f..14adcc94 100644 --- a/res/xml/main_preferences.xml +++ b/res/xml/main_preferences.xml @@ -40,6 +40,12 @@ a:title="@string/c_calc_haptic_feedback_title" a:defaultValue="false"/> + + mapper = new NumberMapper(Long.class); + + private static final long defaultVibrationTime = 100; + + private final float vibrationTimeScale; @Nullable private final Vibrator vibrator; private long time = 0; - public VibratorContainer(@Nullable Vibrator vibrator, @NotNull SharedPreferences preferences, long defaultVibrationTime) { + public VibratorContainer(@Nullable Vibrator vibrator, @NotNull SharedPreferences preferences, float vibrationTimeScale) { this.vibrator = vibrator; - this.defaultVibrationTime = defaultVibrationTime; + this.vibrationTimeScale = vibrationTimeScale; preferences.registerOnSharedPreferenceChangeListener(this); onSharedPreferenceChanged(preferences, null); @@ -44,10 +50,15 @@ public class VibratorContainer implements SharedPreferences.OnSharedPreferenceCh @Override public void onSharedPreferenceChanged(SharedPreferences preferences, @Nullable String key) { - if (preferences.getBoolean(HAPTIC_FEEDBACK_PREFERENCE, false)) { - this.time = defaultVibrationTime; + if (preferences.getBoolean(HAPTIC_FEEDBACK_P_KEY, false)) { + //noinspection ConstantConditions + this.time = getScaledValue(mapper.parseValue(preferences.getString(HAPTIC_FEEDBACK_DURATION_P_KEY, mapper.formatValue(getScaledValue(defaultVibrationTime))))); } else { this.time = 0; } } + + private long getScaledValue(long vibrationTime) { + return (long) (vibrationTime * vibrationTimeScale); + } } diff --git a/src/main/java/org/solovyev/common/NumberParser.java b/src/main/java/org/solovyev/common/NumberParser.java index ce4bdb33..9cb7de97 100644 --- a/src/main/java/org/solovyev/common/NumberParser.java +++ b/src/main/java/org/solovyev/common/NumberParser.java @@ -33,6 +33,8 @@ public class NumberParser implements Parser { result = (T) Integer.valueOf(value); } else if (this.clazz.equals(Float.class)) { result = (T) Float.valueOf(value); + } else if (this.clazz.equals(Long.class)) { + result = (T) Long.valueOf(value); } else { throw new UnsupportedOperationException(this.clazz + " is not supported!"); }