From a556a3a56a7f207f29fdf559d16b7f18558bd9a6 Mon Sep 17 00:00:00 2001 From: serso Date: Thu, 22 Sep 2011 00:23:41 +0400 Subject: [PATCH] fixes --- res/values/default_values.xml | 15 +++++++ res/xml/preferences.xml | 14 +++---- .../DragButtonCalibrationActivity.java | 42 +++++++------------ .../prefs/FloatRangeSeekBarPreference.java | 32 ++++++++++++++ .../view/prefs/RangeSeekBarPreference.java | 21 +++++++--- .../view/widgets/AbstractRangeSeekBar.java | 8 ++-- .../view/widgets/SimpleOnDragListener.java | 11 ++++- 7 files changed, 99 insertions(+), 44 deletions(-) create mode 100644 res/values/default_values.xml create mode 100644 src/main/java/org/solovyev/android/view/prefs/FloatRangeSeekBarPreference.java diff --git a/res/values/default_values.xml b/res/values/default_values.xml new file mode 100644 index 00000000..fdac1def --- /dev/null +++ b/res/values/default_values.xml @@ -0,0 +1,15 @@ + + + + + org.solovyev.android.calculator.DragButtonCalibrationActivity_distance + 15;350 + org.solovyev.android.calculator.DragButtonCalibrationActivity_angle + 130;180 + org.solovyev.android.calculator.DragButtonCalibrationActivity_duration + 40;2500 + \ No newline at end of file diff --git a/res/xml/preferences.xml b/res/xml/preferences.xml index 72310bb3..bd3f65d8 100644 --- a/res/xml/preferences.xml +++ b/res/xml/preferences.xml @@ -14,24 +14,24 @@ + a:defaultValue="@string/p_drag_distance" + range:boundaries="10;500"/> diff --git a/src/main/java/org/solovyev/android/calculator/DragButtonCalibrationActivity.java b/src/main/java/org/solovyev/android/calculator/DragButtonCalibrationActivity.java index e87e9ae0..07b58e1e 100644 --- a/src/main/java/org/solovyev/android/calculator/DragButtonCalibrationActivity.java +++ b/src/main/java/org/solovyev/android/calculator/DragButtonCalibrationActivity.java @@ -202,44 +202,35 @@ public class DragButtonCalibrationActivity extends Activity { for (PreferenceType preferenceType : PreferenceType.values()) { for (DragDirection dragDirection : DragDirection.values()) { - Log.d(DragButtonCalibrationActivity.class.getName(), "Determining drag preference for " + preferenceType + ", " + dragDirection); + final String preferenceId = getPreferenceId(preferenceType, dragDirection); - final Interval defaultValue; + final String defaultValue; switch (preferenceType) { case angle: - switch (dragDirection) { - case up: - defaultValue = new IntervalImpl(130f, 180f); - break; - case down: - defaultValue = new IntervalImpl(130f, 180f); - break; - default: - defaultValue = new IntervalImpl(0f, 0f); - } + defaultValue = context.getResources().getString(R.string.p_drag_angle); break; case distance: - defaultValue = new IntervalImpl(10f, 150f); + defaultValue = context.getResources().getString(R.string.p_drag_distance); break; case duration: - defaultValue = new IntervalImpl(40f, 1000f); + defaultValue = context.getResources().getString(R.string.p_drag_duration); break; default: - defaultValue = new IntervalImpl(DEFAULT_VALUE, DEFAULT_VALUE); + defaultValue = null; + Log.e(DragButtonCalibrationActivity.class.getName(), "New preference type added: default preferences should be defined. Preference id: " + preferenceId); } - final String preferenceId = getPreferenceId(preferenceType, dragDirection); - final String value = preferences.getString(preferenceId, mapper.formatValue(defaultValue)); - Log.d(DragButtonCalibrationActivity.class.getName(), "For " + preferenceId + " next value found in persistence: " + value); + final String value = preferences.getString(preferenceId, defaultValue); - final Interval interval = mapper.parseValue(value); - assert interval != null; + if (defaultValue != null) { + final Interval intervalPref = mapper.parseValue(value); + assert intervalPref != null; - transformInterval(preferenceType, dragDirection, interval); - if (!new IntervalImpl(DEFAULT_VALUE, DEFAULT_VALUE).equals(interval)) { - Log.d(DragButtonCalibrationActivity.class.getName(), "Preference retrieved from persistence. Preference id: " + preferenceId + ", value: " + interval.toString()); + transformInterval(preferenceType, dragDirection, intervalPref); - final DragPreference directionPreference = new DragPreference(dragDirection, interval); + Log.d(DragButtonCalibrationActivity.class.getName(), "Preference loaded. Id: " + preferenceId + ", value: " + intervalPref.toString()); + + final DragPreference directionPreference = new DragPreference(dragDirection, intervalPref); Preference preference = result.getPreferencesMap().get(preferenceType); if (preference == null) { @@ -248,9 +239,6 @@ public class DragButtonCalibrationActivity extends Activity { } preference.getDirectionPreferences().put(dragDirection, directionPreference); - - } else { - Log.e(DragButtonCalibrationActivity.class.getName(), "New preference type added: default preferences should be defined. Preference id: " + preferenceId); } } } diff --git a/src/main/java/org/solovyev/android/view/prefs/FloatRangeSeekBarPreference.java b/src/main/java/org/solovyev/android/view/prefs/FloatRangeSeekBarPreference.java new file mode 100644 index 00000000..90e804eb --- /dev/null +++ b/src/main/java/org/solovyev/android/view/prefs/FloatRangeSeekBarPreference.java @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2009-2011. Created by serso aka se.solovyev. + * For more information, please, contact se.solovyev@gmail.com + * or visit http://se.solovyev.org + */ + +package org.solovyev.android.view.prefs; + +import android.content.Context; +import android.util.AttributeSet; +import org.jetbrains.annotations.NotNull; +import org.solovyev.common.FloatIntervalMapper; +import org.solovyev.common.utils.Interval; +import org.solovyev.common.utils.Mapper; + +/** + * User: serso + * Date: 9/21/11 + * Time: 11:41 PM + */ +public class FloatRangeSeekBarPreference extends RangeSeekBarPreference { + + public FloatRangeSeekBarPreference(@NotNull Context context, AttributeSet attrs) { + super(context, attrs); + } + + @NotNull + @Override + protected Mapper> getMapper() { + return new FloatIntervalMapper() ; + } +} diff --git a/src/main/java/org/solovyev/android/view/prefs/RangeSeekBarPreference.java b/src/main/java/org/solovyev/android/view/prefs/RangeSeekBarPreference.java index 982f0c87..b7c2c3fb 100644 --- a/src/main/java/org/solovyev/android/view/prefs/RangeSeekBarPreference.java +++ b/src/main/java/org/solovyev/android/view/prefs/RangeSeekBarPreference.java @@ -30,8 +30,16 @@ public abstract class RangeSeekBarPreference extends AbstractD boundaries = getMapper().parseValue(attrs.getAttributeValue(localNameSpace, "boundaries")); assert boundaries != null; + + createPreferenceView(); + } + + private void createPreferenceView() { this.rangeSeekBar = new NumberRangeSeekBar(boundaries, null, context); - rangeSeekBar.setOnRangeSeekBarChangeListener(this); + this.rangeSeekBar.setNotifyWhileDragging(true); + this.rangeSeekBar.setOnRangeSeekBarChangeListener(this); + + initPreferenceView(); } @NotNull @@ -39,9 +47,7 @@ public abstract class RangeSeekBarPreference extends AbstractD protected LinearLayout onCreateDialogView() { final LinearLayout result = super.onCreateDialogView(); - this.rangeSeekBar = new NumberRangeSeekBar(boundaries, null, context); - rangeSeekBar.setOnRangeSeekBarChangeListener(this); - initPreferenceView(); + createPreferenceView(); result.addView(rangeSeekBar); @@ -58,9 +64,12 @@ public abstract class RangeSeekBarPreference extends AbstractD } @Override - public void rangeSeekBarValuesChanged(T minValue, T maxValue) { + public void rangeSeekBarValuesChanged(T minValue, T maxValue, boolean changeComplete) { final Interval interval = new NumberInterval(minValue, maxValue); - persistValue(interval); + + if (changeComplete) { + persistValue(interval); + } setValueText(interval); } diff --git a/src/main/java/org/solovyev/android/view/widgets/AbstractRangeSeekBar.java b/src/main/java/org/solovyev/android/view/widgets/AbstractRangeSeekBar.java index 843b4f65..3091ed18 100644 --- a/src/main/java/org/solovyev/android/view/widgets/AbstractRangeSeekBar.java +++ b/src/main/java/org/solovyev/android/view/widgets/AbstractRangeSeekBar.java @@ -186,7 +186,7 @@ public abstract class AbstractRangeSeekBar extends ImageView { setNormalizedMaxValue(convertToNormalizedValue(event.getX())); } if (notifyWhileDragging && listener != null) { - listener.rangeSeekBarValuesChanged(getSelectedMinValue(), getSelectedMaxValue()); + listener.rangeSeekBarValuesChanged(getSelectedMinValue(), getSelectedMaxValue(), false); } } break; @@ -195,7 +195,7 @@ public abstract class AbstractRangeSeekBar extends ImageView { pressedThumb = null; invalidate(); if (listener != null) { - listener.rangeSeekBarValuesChanged(getSelectedMinValue(), getSelectedMaxValue()); + listener.rangeSeekBarValuesChanged(getSelectedMinValue(), getSelectedMaxValue(), true); } break; } @@ -364,7 +364,9 @@ public abstract class AbstractRangeSeekBar extends ImageView { * @author Stephan Tittel (stephan.tittel@kom.tu-darmstadt.de) */ public interface OnRangeSeekBarChangeListener { - void rangeSeekBarValuesChanged(T minValue, T maxValue); + + void rangeSeekBarValuesChanged(T minValue, T maxValue, boolean changeComplete); + } /** diff --git a/src/main/java/org/solovyev/android/view/widgets/SimpleOnDragListener.java b/src/main/java/org/solovyev/android/view/widgets/SimpleOnDragListener.java index 13a7b109..e86eac4c 100644 --- a/src/main/java/org/solovyev/android/view/widgets/SimpleOnDragListener.java +++ b/src/main/java/org/solovyev/android/view/widgets/SimpleOnDragListener.java @@ -57,9 +57,17 @@ public class SimpleOnDragListener implements OnDragListener, DragPreferencesChan DragDirection direction = null; for (Map.Entry directionEntry : distancePreferences.getDirectionPreferences().entrySet()) { + Log.d(String.valueOf(dragButton.getId()), "Trying direction interval: " + directionEntry.getValue().getInterval()); + if (isInInterval(directionEntry.getValue().getInterval(), distance)) { for (Map.Entry angleEntry : anglePreferences.getDirectionPreferences().entrySet()) { + + Log.d(String.valueOf(dragButton.getId()), "Trying angle interval: " + angleEntry.getValue().getInterval()); + if (isInInterval(angleEntry.getValue().getInterval(), (float)angle)) { + + Log.d(String.valueOf(dragButton.getId()), "MATCH! Direction: " + angleEntry.getKey()); + direction = angleEntry.getKey(); break; } @@ -76,9 +84,10 @@ public class SimpleOnDragListener implements OnDragListener, DragPreferencesChan final DragButtonCalibrationActivity.DragPreference durationDragPreferences = durationPreferences.getDirectionPreferences().get(direction); + Log.d(String.valueOf(dragButton.getId()), "Trying time interval: " + durationDragPreferences.getInterval()); if (isInInterval(durationDragPreferences.getInterval(), (float)duration)) { + Log.d(String.valueOf(dragButton.getId()), "MATCH!"); result = dragProcessor.processDragEvent(direction, dragButton, startPoint, motionEvent); - } }