haptic feedback duration preference

This commit is contained in:
serso 2011-10-29 12:12:06 +04:00
parent fcea2f9e2e
commit 040a21087e
10 changed files with 63 additions and 12 deletions

View File

@ -107,7 +107,14 @@
<string name="p_layout_calculator">Калькулятор</string> <string name="p_layout_calculator">Калькулятор</string>
<string name="c_calc_layout_summary">Устанавливает раскладку кнопок</string> <string name="c_calc_layout_summary">Устанавливает раскладку кнопок</string>
<string name="c_calc_haptic_feedback_title">Тактильный ввод</string> <string name="c_calc_haptic_feedback_title">Отклик на нажатие</string>
<string name="c_calc_haptic_feedback_summary">Включает/выключает вибрацию по нажатию клавиши</string> <string name="c_calc_haptic_feedback_summary">Включает/выключает вибрацию по нажатию клавиши</string>
<string name="p_calc_haptic_feedback_strength_short">Маленькая</string>
<string name="p_calc_haptic_feedback_strength_middle">Средняя</string>
<string name="p_calc_haptic_feedback_strength_long">Большая</string>
<string name="p_calc_haptic_feedback_duration_title">Длительность отклика</string>
<string name="p_calc_haptic_feedback_duration_summary">Длительность вибрации по нажатию клавиши</string>
</resources> </resources>

View File

@ -36,4 +36,15 @@
<item>main_calculator</item> <item>main_calculator</item>
</string-array> </string-array>
<string-array name="p_calc_haptic_feedback_duration_names">
<item>@string/p_calc_haptic_feedback_strength_short</item>
<item>@string/p_calc_haptic_feedback_strength_middle</item>
<item>@string/p_calc_haptic_feedback_strength_long</item>
</string-array>
<string-array name="p_calc_haptic_feedback_duration_values">
<item>30</item>
<item>60</item>
<item>100</item>
</string-array>
</resources> </resources>

View File

@ -34,6 +34,9 @@
<string name="p_calc_layout">main_calculator</string> <string name="p_calc_layout">main_calculator</string>
<string name="p_calc_grouping_separator_key">org.solovyev.android.calculator.CalculatorActivity_calc_grouping_separator</string> <string name="p_calc_grouping_separator_key">org.solovyev.android.calculator.CalculatorActivity_calc_grouping_separator</string>
<string name="p_calc_grouping_separator"> </string> <string name="p_calc_grouping_separator">" "</string>
<string name="p_calc_haptic_feedback_duration_key">org.solovyev.android.calculator.CalculatorActivity_calc_haptic_feedback_duration_key</string>
<string name="p_calc_haptic_feedback_duration">" "</string>
</resources> </resources>

View File

@ -111,4 +111,11 @@
<string name="c_calc_haptic_feedback_title">Haptic feedback</string> <string name="c_calc_haptic_feedback_title">Haptic feedback</string>
<string name="c_calc_haptic_feedback_summary">Toggles vibration on button click</string> <string name="c_calc_haptic_feedback_summary">Toggles vibration on button click</string>
<string name="p_calc_haptic_feedback_strength_short">Short</string>
<string name="p_calc_haptic_feedback_strength_middle">Middle</string>
<string name="p_calc_haptic_feedback_strength_long">Long</string>
<string name="p_calc_haptic_feedback_duration_title">Haptic feedback duration</string>
<string name="p_calc_haptic_feedback_duration_summary">Duration vibration on button click</string>
</resources> </resources>

View File

@ -40,6 +40,12 @@
a:title="@string/c_calc_haptic_feedback_title" a:title="@string/c_calc_haptic_feedback_title"
a:defaultValue="false"/> a:defaultValue="false"/>
<ListPreference a:key="@string/p_calc_haptic_feedback_duration_key"
a:title="@string/p_calc_haptic_feedback_duration_title"
a:entries="@array/p_calc_haptic_feedback_duration_names"
a:summary="@string/p_calc_haptic_feedback_duration_summary"
a:entryValues="@array/p_calc_haptic_feedback_duration_values"/>
<ListPreference a:key="@string/p_calc_theme_key" <ListPreference a:key="@string/p_calc_theme_key"
a:title="@string/c_calc_theme" a:title="@string/c_calc_theme"
a:entries="@array/p_theme_names" a:entries="@array/p_theme_names"

View File

@ -152,7 +152,7 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
private class OnDragListenerVibrator extends OnDragListenerWrapper { private class OnDragListenerVibrator extends OnDragListenerWrapper {
private static final long VIBRATION_TIME = 50; private static final float VIBRATION_TIME_SCALE = 0.5f;
@NotNull @NotNull
private final VibratorContainer vibrator; private final VibratorContainer vibrator;
@ -161,7 +161,7 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
@Nullable Vibrator vibrator, @Nullable Vibrator vibrator,
@NotNull SharedPreferences preferences) { @NotNull SharedPreferences preferences) {
super(onDragListener); super(onDragListener);
this.vibrator = new VibratorContainer(vibrator, preferences, VIBRATION_TIME); this.vibrator = new VibratorContainer(vibrator, preferences, VIBRATION_TIME_SCALE);
} }
@Override @Override

View File

@ -9,6 +9,7 @@ import android.content.SharedPreferences;
import android.os.Bundle; import android.os.Bundle;
import android.preference.PreferenceActivity; import android.preference.PreferenceActivity;
import org.solovyev.android.calculator.model.CalculatorEngine; import org.solovyev.android.calculator.model.CalculatorEngine;
import org.solovyev.android.view.widgets.VibratorContainer;
/** /**
* User: serso * User: serso
@ -26,12 +27,15 @@ public class CalculatorPreferencesActivity extends PreferenceActivity implements
final SharedPreferences preferences = getPreferenceManager().getSharedPreferences(); final SharedPreferences preferences = getPreferenceManager().getSharedPreferences();
preferences.registerOnSharedPreferenceChangeListener(this); preferences.registerOnSharedPreferenceChangeListener(this);
onSharedPreferenceChanged(preferences, CalculatorEngine.ROUND_RESULT_P_KEY); onSharedPreferenceChanged(preferences, CalculatorEngine.ROUND_RESULT_P_KEY);
onSharedPreferenceChanged(preferences, VibratorContainer.HAPTIC_FEEDBACK_P_KEY);
} }
@Override @Override
public void onSharedPreferenceChanged(SharedPreferences preferences, String key) { public void onSharedPreferenceChanged(SharedPreferences preferences, String key) {
if (CalculatorEngine.ROUND_RESULT_P_KEY.equals(key)) { if (CalculatorEngine.ROUND_RESULT_P_KEY.equals(key)) {
findPreference(CalculatorEngine.RESULT_PRECISION_P_KEY).setEnabled(preferences.getBoolean(key, true)); findPreference(CalculatorEngine.RESULT_PRECISION_P_KEY).setEnabled(preferences.getBoolean(key, true));
} else if (VibratorContainer.HAPTIC_FEEDBACK_P_KEY.equals(key)) {
findPreference(VibratorContainer.HAPTIC_FEEDBACK_DURATION_P_KEY).setEnabled(preferences.getBoolean(key, true));
} }
} }
} }

View File

@ -19,14 +19,14 @@ import org.jetbrains.annotations.Nullable;
*/ */
public class OnClickListenerVibrator implements View.OnClickListener { public class OnClickListenerVibrator implements View.OnClickListener {
private static final long VIBRATION_TIME = 100; private static final float VIBRATION_TIME_SCALE = 1.0f;
@NotNull @NotNull
private VibratorContainer vibrator; private VibratorContainer vibrator;
public OnClickListenerVibrator(@Nullable Vibrator vibrator, public OnClickListenerVibrator(@Nullable Vibrator vibrator,
@NotNull SharedPreferences preferences) { @NotNull SharedPreferences preferences) {
this.vibrator = new VibratorContainer(vibrator, preferences, VIBRATION_TIME); this.vibrator = new VibratorContainer(vibrator, preferences, VIBRATION_TIME_SCALE);
} }
@Override @Override

View File

@ -10,6 +10,7 @@ import android.content.SharedPreferences;
import android.os.Vibrator; import android.os.Vibrator;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.solovyev.common.NumberMapper;
/** /**
* User: serso * User: serso
@ -18,18 +19,23 @@ import org.jetbrains.annotations.Nullable;
*/ */
public class VibratorContainer implements SharedPreferences.OnSharedPreferenceChangeListener { public class VibratorContainer implements SharedPreferences.OnSharedPreferenceChangeListener {
private static final String HAPTIC_FEEDBACK_PREFERENCE = "org.solovyev.android.calculator.CalculatorModel_haptic_feedback"; public static final String HAPTIC_FEEDBACK_P_KEY = "org.solovyev.android.calculator.CalculatorModel_haptic_feedback";
public static final String HAPTIC_FEEDBACK_DURATION_P_KEY = "org.solovyev.android.calculator.CalculatorActivity_calc_haptic_feedback_duration_key";
private final long defaultVibrationTime; private final static NumberMapper<Long> mapper = new NumberMapper<Long>(Long.class);
private static final long defaultVibrationTime = 100;
private final float vibrationTimeScale;
@Nullable @Nullable
private final Vibrator vibrator; private final Vibrator vibrator;
private long time = 0; 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.vibrator = vibrator;
this.defaultVibrationTime = defaultVibrationTime; this.vibrationTimeScale = vibrationTimeScale;
preferences.registerOnSharedPreferenceChangeListener(this); preferences.registerOnSharedPreferenceChangeListener(this);
onSharedPreferenceChanged(preferences, null); onSharedPreferenceChanged(preferences, null);
@ -44,10 +50,15 @@ public class VibratorContainer implements SharedPreferences.OnSharedPreferenceCh
@Override @Override
public void onSharedPreferenceChanged(SharedPreferences preferences, @Nullable String key) { public void onSharedPreferenceChanged(SharedPreferences preferences, @Nullable String key) {
if (preferences.getBoolean(HAPTIC_FEEDBACK_PREFERENCE, false)) { if (preferences.getBoolean(HAPTIC_FEEDBACK_P_KEY, false)) {
this.time = defaultVibrationTime; //noinspection ConstantConditions
this.time = getScaledValue(mapper.parseValue(preferences.getString(HAPTIC_FEEDBACK_DURATION_P_KEY, mapper.formatValue(getScaledValue(defaultVibrationTime)))));
} else { } else {
this.time = 0; this.time = 0;
} }
} }
private long getScaledValue(long vibrationTime) {
return (long) (vibrationTime * vibrationTimeScale);
}
} }

View File

@ -33,6 +33,8 @@ public class NumberParser<T extends Number> implements Parser<T> {
result = (T) Integer.valueOf(value); result = (T) Integer.valueOf(value);
} else if (this.clazz.equals(Float.class)) { } else if (this.clazz.equals(Float.class)) {
result = (T) Float.valueOf(value); result = (T) Float.valueOf(value);
} else if (this.clazz.equals(Long.class)) {
result = (T) Long.valueOf(value);
} else { } else {
throw new UnsupportedOperationException(this.clazz + " is not supported!"); throw new UnsupportedOperationException(this.clazz + " is not supported!");
} }