From b1e2afcce384fba020a18ea8ddcec39cdfba9d24 Mon Sep 17 00:00:00 2001 From: serso Date: Thu, 29 Sep 2011 00:11:12 +0400 Subject: [PATCH] vars activity --- AndroidManifest.xml | 4 +- res/layout-land/main.xml | 6 +- res/layout-land/var.xml | 26 +++ res/layout-port/main.xml | 173 ++++++++-------- res/layout-port/var.xml | 26 +++ res/values/strings.xml | 2 + .../calculator/CalculatorActivity.java | 36 +++- .../calculator/CalculatorVarsActivity.java | 53 +++++ .../DragButtonCalibrationActivity.java | 174 +--------------- .../android/calculator/VariableContainer.java | 51 +++++ .../DragPreferencesChangeListener.java | 3 +- .../view/widgets/SimpleOnDragListener.java | 191 ++++++++++++++++-- 12 files changed, 464 insertions(+), 281 deletions(-) create mode 100644 res/layout-land/var.xml create mode 100644 res/layout-port/var.xml create mode 100644 src/main/java/org/solovyev/android/calculator/CalculatorVarsActivity.java create mode 100644 src/main/java/org/solovyev/android/calculator/VariableContainer.java diff --git a/AndroidManifest.xml b/AndroidManifest.xml index afdbc3f6..ddac321a 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -13,13 +13,13 @@ - + - + \ No newline at end of file diff --git a/res/layout-land/main.xml b/res/layout-land/main.xml index 935ce95d..338c6962 100644 --- a/res/layout-land/main.xml +++ b/res/layout-land/main.xml @@ -183,7 +183,11 @@ calc:textDown=")" style="@style/digit_button_style" a:onClick="digitButtonClickHandler"/> - + diff --git a/res/layout-land/var.xml b/res/layout-land/var.xml new file mode 100644 index 00000000..c92c018f --- /dev/null +++ b/res/layout-land/var.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/res/layout-port/main.xml b/res/layout-port/main.xml index 0bc2fdd9..3b1c6196 100644 --- a/res/layout-port/main.xml +++ b/res/layout-port/main.xml @@ -27,11 +27,11 @@ + a:text="=" + a:layout_width="fill_parent" + a:layout_weight="4" + style="@style/control_button_style" + a:onClick="numericButtonClickHandler"/> - + + calc:textDown="asin" style="@style/digit_button_style" + a:onClick="digitButtonClickHandler"/> + calc:textDown="acos" style="@style/digit_button_style" + a:onClick="digitButtonClickHandler"/> + calc:textDown="atan" style="@style/digit_button_style" + a:onClick="digitButtonClickHandler"/> + calc:textUp="^" + calc:textDown="^2" style="@style/digit_button_style" + a:onClick="digitButtonClickHandler"/> + a:text="@string/c_clear" + style="@style/control_button_style" + a:onClick="clearButtonClickHandler"/> - + + a:text="4" + calc:textUp="sinh" + calc:textDown="asinh" + style="@style/digit_button_style" + a:onClick="digitButtonClickHandler"/> + a:text="5" + calc:textUp="cosh" + calc:textDown="acosh" + style="@style/digit_button_style" + a:onClick="digitButtonClickHandler"/> + calc:textUp="tanh" + calc:textDown="atanh" + style="@style/digit_button_style" + a:onClick="digitButtonClickHandler"/> + calc:textUp="√" + style="@style/digit_button_style" + a:onClick="digitButtonClickHandler"/> + a:text="↚" + style="@style/control_button_style" + a:onClick="eraseButtonClickHandler"/> + calc:textUp="i" + calc:textDown="" + style="@style/digit_button_style" + a:onClick="digitButtonClickHandler"/> + calc:textUp="ln" + calc:textDown="" + style="@style/digit_button_style" + a:onClick="digitButtonClickHandler"/> + calc:textDown="e" + calc:textUp="π" + style="@style/digit_button_style" + a:onClick="digitButtonClickHandler"/> + style="@style/digit_button_style" + a:onClick="digitButtonClickHandler"/> + a:text="@string/c_paste" + style="@style/control_button_style" + a:onClick="pasteButtonClickHandler"/> @@ -141,33 +141,33 @@ + calc:textUp="(" + calc:textDown=")" style="@style/digit_button_style" + a:onClick="digitButtonClickHandler"/> + calc:textUp="." + calc:textDown="," + style="@style/digit_button_style" + a:onClick="digitButtonClickHandler"/> + calc:textUp="[" + calc:textDown="]" style="@style/digit_button_style" + a:onClick="digitButtonClickHandler"/> + calc:textUp="{" + calc:textDown="}" + style="@style/digit_button_style" + a:onClick="digitButtonClickHandler"/> + a:text="" + calc:textUp="@string/c_undo" + calc:textDown="@string/c_redo" + style="@style/control_button_style"/> @@ -184,18 +184,23 @@ a:onClick="simplifyButtonClickHandler"/>--> + calc:textUp="↞" + a:text="←" + style="@style/control_button_style" + a:onClick="moveLeftButtonClickHandler"/> - + - + diff --git a/res/layout-port/var.xml b/res/layout-port/var.xml new file mode 100644 index 00000000..c92c018f --- /dev/null +++ b/res/layout-port/var.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/res/values/strings.xml b/res/values/strings.xml index ff316f9b..03689a1c 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -7,6 +7,7 @@ Settings Help About + Variables And Constants Main settings Drag buttons settings @@ -28,6 +29,7 @@ redo clear paste + vars Color expressions Precision of result diff --git a/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java b/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java index 5ff6204a..7d1b7006 100644 --- a/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java +++ b/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java @@ -23,6 +23,7 @@ import org.solovyev.android.view.widgets.*; import org.solovyev.common.BooleanMapper; import org.solovyev.common.NumberMapper; import org.solovyev.common.utils.Announcer; +import org.solovyev.common.utils.StringUtils; import org.solovyev.common.utils.history.HistoryAction; import java.lang.reflect.Field; @@ -30,6 +31,9 @@ import java.lang.reflect.Modifier; public class CalculatorActivity extends Activity implements FontSizeAdjuster, SharedPreferences.OnSharedPreferenceChangeListener { + public static final String INSERT_TEXT_INTENT = "org.solovyev.android.calculator.CalculatorActivity.insertText"; + public static final String INSERT_TEXT_INTENT_EXTRA_STRING = "org.solovyev.android.calculator.CalculatorActivity.insertText.extraString"; + private static final int HVGA_WIDTH_PIXELS = 320; @NotNull @@ -42,7 +46,7 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh private CalculatorModel calculatorModel; @NotNull - private BroadcastReceiver preferencesChangesReceiver; + private BroadcastReceiver insertTextReceiver; /** * Called when the activity is first created. @@ -61,7 +65,7 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh this.calculatorView = new CalculatorView(this, this.calculatorModel); - final DragButtonCalibrationActivity.Preferences dragPreferences = DragButtonCalibrationActivity.getPreferences(this); + final SimpleOnDragListener.Preferences dragPreferences = SimpleOnDragListener.getPreferences(this); final SimpleOnDragListener onDragListener = new SimpleOnDragListener(new DigitButtonDragProcessor(calculatorView), dragPreferences); dpclRegister.addListener(onDragListener); @@ -94,17 +98,24 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh dpclRegister.addListener(toPositionOnDragListener); - preferencesChangesReceiver = new BroadcastReceiver() { + insertTextReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { - - if (DragButtonCalibrationActivity.INTENT_ACTION.equals(intent.getAction())) { - dpclRegister.announce().onDragPreferencesChange(DragButtonCalibrationActivity.getPreferences(CalculatorActivity.this)); + if (INSERT_TEXT_INTENT.equals(intent.getAction())) { + final String s = intent.getStringExtra(INSERT_TEXT_INTENT_EXTRA_STRING); + if (!StringUtils.isEmpty(s)) { + calculatorView.doTextOperation(new CalculatorView.TextOperation() { + @Override + public void doOperation(@NotNull EditText editor) { + editor.getText().insert(editor.getSelectionStart(), s); + } + }); + } } } }; - registerReceiver(preferencesChangesReceiver, new IntentFilter(DragButtonCalibrationActivity.INTENT_ACTION)); + registerReceiver(insertTextReceiver, new IntentFilter(INSERT_TEXT_INTENT)); final SharedPreferences defaultSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); defaultSharedPreferences.registerOnSharedPreferenceChangeListener(this); @@ -114,7 +125,7 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh @Override protected void onDestroy() { - unregisterReceiver(preferencesChangesReceiver); + unregisterReceiver(insertTextReceiver); super.onDestroy(); } @@ -179,6 +190,11 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh calculatorView.processDigitButtonAction(((DirectionDragButton) v).getTextMiddle()); } + @SuppressWarnings({"UnusedDeclaration"}) + public void varsButtonClickHandler(@NotNull View v) { + startActivity(new Intent(this, CalculatorVarsActivity.class)); + } + @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { @@ -239,8 +255,8 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh } @Override - public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String s) { - dpclRegister.announce().onDragPreferencesChange(DragButtonCalibrationActivity.getPreferences(CalculatorActivity.this)); + public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, @Nullable String s) { + dpclRegister.announce().onDragPreferencesChange(SimpleOnDragListener.getPreferences(CalculatorActivity.this)); final NumberMapper integerNumberMapper = new NumberMapper(Integer.class); this.calculatorModel.setNumberOfFractionDigits(integerNumberMapper.parseValue(sharedPreferences.getString(this.getString(R.string.p_calc_result_precision_key), this.getString(R.string.p_calc_result_precision)))); diff --git a/src/main/java/org/solovyev/android/calculator/CalculatorVarsActivity.java b/src/main/java/org/solovyev/android/calculator/CalculatorVarsActivity.java new file mode 100644 index 00000000..8b1b2048 --- /dev/null +++ b/src/main/java/org/solovyev/android/calculator/CalculatorVarsActivity.java @@ -0,0 +1,53 @@ +/* + * 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.calculator; + +import android.app.ListActivity; +import android.content.Intent; +import android.os.Bundle; +import android.view.View; +import android.widget.AdapterView; +import android.widget.ArrayAdapter; +import android.widget.ListView; + +import java.util.ArrayList; +import java.util.List; + +/** + * User: serso + * Date: 9/28/11 + * Time: 10:55 PM + */ +public class CalculatorVarsActivity extends ListActivity { + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + setTheme(android.R.style.Theme_Dialog); + + final List vars = new ArrayList(); + vars.add(new VariableContainer("e", 2.71, true)); + vars.add(new VariableContainer("π", 3.14, true)); + setListAdapter(new ArrayAdapter(this, R.layout.var, R.id.var_text, vars)); + + final ListView lv = getListView(); + lv.setTextFilterEnabled(true); + + lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { + public void onItemClick(AdapterView parent, View view, + int position, long id) { + final Intent intent = new Intent(CalculatorActivity.INSERT_TEXT_INTENT); + intent.putExtra(CalculatorActivity.INSERT_TEXT_INTENT_EXTRA_STRING, vars.get(position).getId()); + sendOrderedBroadcast(intent, null); + + CalculatorVarsActivity.this.finish(); + } + }); + + } +} diff --git a/src/main/java/org/solovyev/android/calculator/DragButtonCalibrationActivity.java b/src/main/java/org/solovyev/android/calculator/DragButtonCalibrationActivity.java index 8732a1f1..be71bd41 100644 --- a/src/main/java/org/solovyev/android/calculator/DragButtonCalibrationActivity.java +++ b/src/main/java/org/solovyev/android/calculator/DragButtonCalibrationActivity.java @@ -6,8 +6,6 @@ package org.solovyev.android.calculator; import android.app.Activity; -import android.content.Context; -import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.os.Handler; @@ -43,14 +41,6 @@ public class DragButtonCalibrationActivity extends Activity { private static final float DEFAULT_VALUE = -999; private static final int MIN_HISTORY_FOR_CALIBRATION = 10; - public static final String INTENT_ACTION = "org.solovyev.android.calculator.DragButtonPreferencesChanged"; - - public static enum PreferenceType { - angle, - distance, - duration - } - @Override protected void onCreate(Bundle savedInstanceState) { @@ -163,13 +153,11 @@ public class DragButtonCalibrationActivity extends Activity { final SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this); final SharedPreferences.Editor editor = settings.edit(); - setPreferences(angleStatData, editor, PreferenceType.angle); - setPreferences(distanceStatData, editor, PreferenceType.distance); - setPreferences(timeStatData, editor, PreferenceType.duration); + setPreferences(angleStatData, editor, SimpleOnDragListener.PreferenceType.angle); + setPreferences(distanceStatData, editor, SimpleOnDragListener.PreferenceType.distance); + setPreferences(timeStatData, editor, SimpleOnDragListener.PreferenceType.duration); editor.commit(); - - sendOrderedBroadcast(new Intent(INTENT_ACTION), null); } } @@ -177,167 +165,15 @@ public class DragButtonCalibrationActivity extends Activity { return super.onKeyDown(keyCode, event); } - private void setPreferences(@NotNull Map statData, @NotNull SharedPreferences.Editor editor, @NotNull PreferenceType preferenceType) { + private void setPreferences(@NotNull Map statData, @NotNull SharedPreferences.Editor editor, @NotNull SimpleOnDragListener.PreferenceType preferenceType) { final Mapper> mapper = new NumberIntervalMapper(Float.class); for (Map.Entry entry : statData.entrySet()) { final float min = (float) entry.getValue().getMean() - 2 * (float) entry.getValue().getStandardDeviation(); final float max = (float) entry.getValue().getMean() + 2 * (float) entry.getValue().getStandardDeviation(); - editor.putString(getPreferenceId(preferenceType, entry.getKey()), mapper.formatValue(transformInterval(preferenceType, entry.getKey(), new IntervalImpl(Math.max(0, min), max)))); + editor.putString(SimpleOnDragListener.getPreferenceId(preferenceType, entry.getKey()), mapper.formatValue(SimpleOnDragListener.transformInterval(preferenceType, entry.getKey(), new IntervalImpl(Math.max(0, min), max)))); } } - // todo serso: currently we do not use direction - public static String getPreferenceId(@NotNull PreferenceType preferenceType, @NotNull DragDirection direction) { - return "org.solovyev.android.calculator.DragButtonCalibrationActivity" + "_" + preferenceType.name() /*+ "_" + direction.name()*/; - } - - @NotNull - public static Preferences getPreferences(@NotNull Context context) { - final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); - - final Mapper> mapper = new NumberIntervalMapper(Float.class); - - final Preferences result = new Preferences(); - - for (PreferenceType preferenceType : PreferenceType.values()) { - for (DragDirection dragDirection : DragDirection.values()) { - - final String preferenceId = getPreferenceId(preferenceType, dragDirection); - - final String defaultValue; - switch (preferenceType) { - case angle: - defaultValue = context.getResources().getString(R.string.p_drag_angle); - break; - case distance: - defaultValue = context.getResources().getString(R.string.p_drag_distance); - break; - case duration: - defaultValue = context.getResources().getString(R.string.p_drag_duration); - break; - default: - defaultValue = null; - Log.e(DragButtonCalibrationActivity.class.getName(), "New preference type added: default preferences should be defined. Preference id: " + preferenceId); - } - - final String value = preferences.getString(preferenceId, defaultValue); - - if (defaultValue != null) { - final Interval intervalPref = mapper.parseValue(value); - assert intervalPref != null; - - transformInterval(preferenceType, dragDirection, intervalPref); - - 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) { - preference = new Preference(preferenceType); - result.getPreferencesMap().put(preferenceType, preference); - } - - preference.getDirectionPreferences().put(dragDirection, directionPreference); - } - } - } - - return result; - } - - @NotNull - private static Interval transformInterval(@NotNull PreferenceType preferenceType, @NotNull DragDirection dragDirection, @NotNull Interval interval) { - if (preferenceType == PreferenceType.angle) { - final Float leftBorder = interval.getLeftBorder(); - final Float rightBorder = interval.getRightBorder(); - - if (dragDirection == DragDirection.up) { - interval.setLeftBorder(180f - rightBorder); - interval.setRightBorder(180f - leftBorder); - } else if (dragDirection == DragDirection.left || dragDirection == DragDirection.right) { - interval.setLeftBorder(90f - rightBorder / 2); - interval.setRightBorder(90f + leftBorder / 2); - } - } - - return interval; - } - - public static class DragPreference { - - @NotNull - private DragDirection direction; - - @NotNull - private Interval interval; - - - public DragPreference(@NotNull DragDirection direction, @NotNull Interval interval) { - this.direction = direction; - this.interval = interval; - } - - @NotNull - public DragDirection getDirection() { - return direction; - } - - public void setDirection(@NotNull DragDirection direction) { - this.direction = direction; - } - - @NotNull - public Interval getInterval() { - return interval; - } - - public void setInterval(@NotNull Interval interval) { - this.interval = interval; - } - } - - public static class Preference { - - @NotNull - private PreferenceType preferenceType; - - @NotNull - private Map directionPreferences = new HashMap(); - - - public Preference(@NotNull PreferenceType preferenceType) { - this.preferenceType = preferenceType; - } - - @NotNull - public PreferenceType getPreferenceType() { - return preferenceType; - } - - public void setPreferenceType(@NotNull PreferenceType preferenceType) { - this.preferenceType = preferenceType; - } - - @NotNull - public Map getDirectionPreferences() { - return directionPreferences; - } - - public void setDirectionPreferences(@NotNull Map directionPreferences) { - this.directionPreferences = directionPreferences; - } - } - - - public static class Preferences { - - private final Map preferencesMap = new HashMap(); - - public Map getPreferencesMap() { - return preferencesMap; - } - } private void logStatData(@NotNull Map statData) { for (Map.Entry entry : statData.entrySet()) { diff --git a/src/main/java/org/solovyev/android/calculator/VariableContainer.java b/src/main/java/org/solovyev/android/calculator/VariableContainer.java new file mode 100644 index 00000000..cbeabdda --- /dev/null +++ b/src/main/java/org/solovyev/android/calculator/VariableContainer.java @@ -0,0 +1,51 @@ +/* + * 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.calculator; + +import org.jetbrains.annotations.NotNull; +import org.solovyev.common.definitions.Identity; + +/** + * User: serso + * Date: 9/28/11 + * Time: 11:22 PM + */ +public class VariableContainer extends Identity{ + + @NotNull + private Double value; + + private boolean system; + + public VariableContainer(@NotNull String id, @NotNull Double value, boolean system) { + super(id); + this.value = value; + this.system = system; + } + + @NotNull + public Double getValue() { + return value; + } + + public void setValue(@NotNull Double value) { + this.value = value; + } + + public boolean isSystem() { + return system; + } + + public void setSystem(boolean system) { + this.system = system; + } + + @Override + public String toString() { + return getId() + " = " + value; + } +} diff --git a/src/main/java/org/solovyev/android/view/widgets/DragPreferencesChangeListener.java b/src/main/java/org/solovyev/android/view/widgets/DragPreferencesChangeListener.java index 1da5646f..a98a2836 100644 --- a/src/main/java/org/solovyev/android/view/widgets/DragPreferencesChangeListener.java +++ b/src/main/java/org/solovyev/android/view/widgets/DragPreferencesChangeListener.java @@ -6,7 +6,6 @@ package org.solovyev.android.view.widgets; import org.jetbrains.annotations.NotNull; -import org.solovyev.android.calculator.DragButtonCalibrationActivity; import java.util.EventListener; @@ -17,5 +16,5 @@ import java.util.EventListener; */ public interface DragPreferencesChangeListener extends EventListener{ - void onDragPreferencesChange(@NotNull DragButtonCalibrationActivity.Preferences preferences ); + void onDragPreferencesChange(@NotNull SimpleOnDragListener.Preferences preferences ); } 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 e86eac4c..6c475c94 100644 --- a/src/main/java/org/solovyev/android/view/widgets/SimpleOnDragListener.java +++ b/src/main/java/org/solovyev/android/view/widgets/SimpleOnDragListener.java @@ -5,14 +5,20 @@ package org.solovyev.android.view.widgets; +import android.content.Context; +import android.content.SharedPreferences; +import android.preference.PreferenceManager; import android.util.Log; import android.view.MotionEvent; import org.jetbrains.annotations.NotNull; -import org.solovyev.android.calculator.DragButtonCalibrationActivity; +import org.solovyev.android.calculator.R; +import org.solovyev.common.NumberIntervalMapper; import org.solovyev.common.utils.Interval; +import org.solovyev.common.utils.Mapper; import org.solovyev.common.utils.MathUtils; import org.solovyev.common.utils.Point2d; +import java.util.HashMap; import java.util.Map; public class SimpleOnDragListener implements OnDragListener, DragPreferencesChangeListener { @@ -24,13 +30,13 @@ public class SimpleOnDragListener implements OnDragListener, DragPreferencesChan private DragProcessor dragProcessor; @NotNull - private DragButtonCalibrationActivity.Preferences preferences; + private Preferences preferences; - public SimpleOnDragListener(@NotNull DragButtonCalibrationActivity.Preferences preferences) { + public SimpleOnDragListener(@NotNull Preferences preferences) { this.preferences = preferences; } - public SimpleOnDragListener(@NotNull DragProcessor dragProcessor, @NotNull DragButtonCalibrationActivity.Preferences preferences) { + public SimpleOnDragListener(@NotNull DragProcessor dragProcessor, @NotNull Preferences preferences) { this.dragProcessor = dragProcessor; this.preferences = preferences; } @@ -51,20 +57,20 @@ public class SimpleOnDragListener implements OnDragListener, DragPreferencesChan final double angle = Math.toDegrees(MathUtils.getAngle(startPoint, MathUtils.sum(startPoint, axis), endPoint)); final double duration = motionEvent.getEventTime() - motionEvent.getDownTime(); - final DragButtonCalibrationActivity.Preference distancePreferences = preferences.getPreferencesMap().get(DragButtonCalibrationActivity.PreferenceType.distance); - final DragButtonCalibrationActivity.Preference anglePreferences = preferences.getPreferencesMap().get(DragButtonCalibrationActivity.PreferenceType.angle); + final Preference distancePreferences = preferences.getPreferencesMap().get(PreferenceType.distance); + final Preference anglePreferences = preferences.getPreferencesMap().get(PreferenceType.angle); DragDirection direction = null; - for (Map.Entry directionEntry : distancePreferences.getDirectionPreferences().entrySet()) { + 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()) { + 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)) { + if (isInInterval(angleEntry.getValue().getInterval(), (float) angle)) { Log.d(String.valueOf(dragButton.getId()), "MATCH! Direction: " + angleEntry.getKey()); @@ -80,12 +86,12 @@ public class SimpleOnDragListener implements OnDragListener, DragPreferencesChan } if (direction != null) { - final DragButtonCalibrationActivity.Preference durationPreferences = preferences.getPreferencesMap().get(DragButtonCalibrationActivity.PreferenceType.duration); + final Preference durationPreferences = preferences.getPreferencesMap().get(PreferenceType.duration); - final DragButtonCalibrationActivity.DragPreference durationDragPreferences = durationPreferences.getDirectionPreferences().get(direction); + final DragPreference durationDragPreferences = durationPreferences.getDirectionPreferences().get(direction); Log.d(String.valueOf(dragButton.getId()), "Trying time interval: " + durationDragPreferences.getInterval()); - if (isInInterval(durationDragPreferences.getInterval(), (float)duration)) { + if (isInInterval(durationDragPreferences.getInterval(), (float) duration)) { Log.d(String.valueOf(dragButton.getId()), "MATCH!"); result = dragProcessor.processDragEvent(direction, dragButton, startPoint, motionEvent); } @@ -125,7 +131,7 @@ public class SimpleOnDragListener implements OnDragListener, DragPreferencesChan } @Override - public void onDragPreferencesChange(@NotNull DragButtonCalibrationActivity.Preferences preferences) { + public void onDragPreferencesChange(@NotNull Preferences preferences) { this.preferences = preferences; } @@ -133,4 +139,163 @@ public class SimpleOnDragListener implements OnDragListener, DragPreferencesChan boolean processDragEvent(@NotNull DragDirection dragDirection, @NotNull DragButton dragButton, @NotNull Point2d startPoint2d, @NotNull MotionEvent motionEvent); } + + // todo serso: currently we do not use direction + public static String getPreferenceId(@NotNull PreferenceType preferenceType, @NotNull DragDirection direction) { + return "org.solovyev.android.calculator.DragButtonCalibrationActivity" + "_" + preferenceType.name() /*+ "_" + direction.name()*/; + } + + @NotNull + public static Preferences getPreferences(@NotNull Context context) { + final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); + + final Mapper> mapper = new NumberIntervalMapper(Float.class); + + final Preferences result = new Preferences(); + + for (PreferenceType preferenceType : PreferenceType.values()) { + for (DragDirection dragDirection : DragDirection.values()) { + + final String preferenceId = getPreferenceId(preferenceType, dragDirection); + + final String defaultValue; + switch (preferenceType) { + case angle: + defaultValue = context.getResources().getString(R.string.p_drag_angle); + break; + case distance: + defaultValue = context.getResources().getString(R.string.p_drag_distance); + break; + case duration: + defaultValue = context.getResources().getString(R.string.p_drag_duration); + break; + default: + defaultValue = null; + Log.e(SimpleOnDragListener.class.getName(), "New preference type added: default preferences should be defined. Preference id: " + preferenceId); + } + + final String value = preferences.getString(preferenceId, defaultValue); + + if (defaultValue != null) { + final Interval intervalPref = mapper.parseValue(value); + assert intervalPref != null; + + transformInterval(preferenceType, dragDirection, intervalPref); + + Log.d(SimpleOnDragListener.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) { + preference = new Preference(preferenceType); + result.getPreferencesMap().put(preferenceType, preference); + } + + preference.getDirectionPreferences().put(dragDirection, directionPreference); + } + } + } + + return result; + } + + @NotNull + public static Interval transformInterval(@NotNull PreferenceType preferenceType, @NotNull DragDirection dragDirection, @NotNull Interval interval) { + if (preferenceType == PreferenceType.angle) { + final Float leftBorder = interval.getLeftBorder(); + final Float rightBorder = interval.getRightBorder(); + + if (dragDirection == DragDirection.up) { + interval.setLeftBorder(180f - rightBorder); + interval.setRightBorder(180f - leftBorder); + } else if (dragDirection == DragDirection.left || dragDirection == DragDirection.right) { + interval.setLeftBorder(90f - rightBorder / 2); + interval.setRightBorder(90f + leftBorder / 2); + } + } + + return interval; + } + + + public static enum PreferenceType { + angle, + distance, + duration + } + + public static class DragPreference { + + @NotNull + private DragDirection direction; + + @NotNull + private Interval interval; + + + public DragPreference(@NotNull DragDirection direction, @NotNull Interval interval) { + this.direction = direction; + this.interval = interval; + } + + @NotNull + public DragDirection getDirection() { + return direction; + } + + public void setDirection(@NotNull DragDirection direction) { + this.direction = direction; + } + + @NotNull + public Interval getInterval() { + return interval; + } + + public void setInterval(@NotNull Interval interval) { + this.interval = interval; + } + } + + public static class Preference { + + @NotNull + private PreferenceType preferenceType; + + @NotNull + private Map directionPreferences = new HashMap(); + + + public Preference(@NotNull PreferenceType preferenceType) { + this.preferenceType = preferenceType; + } + + @NotNull + public PreferenceType getPreferenceType() { + return preferenceType; + } + + public void setPreferenceType(@NotNull PreferenceType preferenceType) { + this.preferenceType = preferenceType; + } + + @NotNull + public Map getDirectionPreferences() { + return directionPreferences; + } + + public void setDirectionPreferences(@NotNull Map directionPreferences) { + this.directionPreferences = directionPreferences; + } + } + + public static class Preferences { + + private final Map preferencesMap = new HashMap(); + + public Map getPreferencesMap() { + return preferencesMap; + } + } } \ No newline at end of file