This commit is contained in:
serso 2011-11-18 00:47:33 +04:00
parent 94282706fe
commit c1a1e23e3c
13 changed files with 204 additions and 87 deletions

View File

@ -8,8 +8,10 @@
<org.solovyev.android.view.widgets.DirectionDragButton xmlns:a="http://schemas.android.com/apk/res/android"
xmlns:calc="http://schemas.android.com/apk/res/org.solovyev.android.calculator"
a:id="@+id/divisionButton" a:text="/"
calc:textUp="√"
a:id="@+id/divisionButton"
calc:textUp="%"
a:text="/"
calc:textDown="√"
calc:directionTextScale="0.5"
style="?digitButtonStyle"
a:onClick="digitButtonClickHandler"/>

View File

@ -7,6 +7,7 @@
-->
<org.solovyev.android.view.widgets.DirectionDragButton xmlns:a="http://schemas.android.com/apk/res/android"
xmlns:calc="http://schemas.android.com/apk/res/org.solovyev.android.calculator"
a:id="@+id/functionsButton"
a:text="ƒ(x)"
a:textStyle="italic"

View File

@ -8,6 +8,6 @@
<org.solovyev.android.view.widgets.DirectionDragButton xmlns:a="http://schemas.android.com/apk/res/android"
a:id="@+id/functionsButton"
a:text="∂,"
a:text="∂,"
a:onClick="operatorsButtonClickHandler"
style="?controlButtonStyle"/>

View File

@ -8,7 +8,7 @@
<org.solovyev.android.view.widgets.DirectionDragButton xmlns:a="http://schemas.android.com/apk/res/android"
xmlns:calc="http://schemas.android.com/apk/res/org.solovyev.android.calculator"
a:id="@+id/subtractionButton"
calc:textUp="%"
calc:textDown="∂,…"
a:text="-"
calc:directionTextScale="0.5"
style="?digitButtonStyle"

View File

@ -147,4 +147,11 @@
<string name="c_not_valid_result">Результат не допустим!</string>
<string name="c_operators">Операторы</string>
<string name="c_op_description_mod">Возвращает остаток от деления \'x\' на \'y\'.</string>
<string name="c_op_description_sum">Суммирует функции \'f(i)\', пробегая по переменной \'i\' от \'from\' до \'to\'.</string>
<string name="c_op_description_product">Возвращает произведение функций \'f(i)\', пробегая по переменной \'i\' от \'from\' до \'to\'.</string>
<string name="c_op_description_derivative">Возвращает производную порядка \'order\' функции \'f(x)\' по переменной \'x\' и вычисляет её в точке \'x_point\'.</string>
<string name="c_op_description_integral">Возвращает интеграл функции \'f(x)\' по переменно \'x\'.</string>
<string name="c_op_description_integral_ab">Интегрирует функцию \'f(x)\' по переменной \'x\' от \'a\' до \'b\'.</string>
</resources>

View File

@ -140,12 +140,20 @@
<string name="c_fun_description_ne">Not-equals function - gives 1 if two arguments are not equals, 0 otherwise.</string>
<string name="c_fun_description_lt">Lesser function - gives 1 if first argument is less than second, 0 otherwise.</string>
<string name="c_fun_description_gt">Greater function - gives 1 if first argument is greater than second, 0 otherwise.</string>
<string name="c_fun_description_rad">Converts degrees into radians: d - degrees, m - minutes (default = 0), s - seconds (default = 0) </string>
<string name="c_fun_description_dms">Converts degrees from DMS notation to decimal notation: d - degrees, m - minutes (default = 0), s - seconds (default = 0) </string>
<string name="c_fun_description_rad">Converts degrees into radians: d - degrees, m - minutes (default = 0), s - seconds (default = 0).</string>
<string name="c_fun_description_dms">Converts degrees from DMS notation to decimal notation: d - degrees, m - minutes (default = 0), s - seconds (default = 0).</string>
<string name="c_fun_description_deg">Converts radians into degrees.</string>
<string name="c_empty_var_error">Unable to create empty constant!</string>
<string name="c_not_valid_result">Current result is not valid!</string>
<string name="c_operators">Operators</string>
<string name="c_op_description_mod">Modulo operation finds the remainder of division of \'x\' by \'y\'.</string>
<string name="c_op_description_sum">Sums functions \'f(i)\', iterating through \'i\' from \'from\' to \'to\'.</string>
<string name="c_op_description_product">Gives product of functions \'f(i)\', iterating through \'i\' from \'from\' to \'to\'.</string>
<string name="c_op_description_derivative">Gives derivative of order \'order\' of functions \'f(x)\' by \'x\' variable and calculates at point \'x_point\'.</string>
<string name="c_op_description_integral">Gives integral of function \'f(x)\' by \'x\' variable.</string>
<string name="c_op_description_integral_ab">Integrates function \'f(x)\' by \'x\' variable from \'a\' to \'b\'.</string>
</resources>

View File

@ -96,6 +96,18 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
final OnDragListener historyOnDragListener = new OnDragListenerVibrator(newOnDragListener(new HistoryDragProcessor<CalculatorHistoryState>(this.calculatorModel), dragPreferences), vibrator, preferences);
((DragButton) findViewById(R.id.historyButton)).setOnDragListener(historyOnDragListener);
((DragButton) findViewById(R.id.subtractionButton)).setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new SimpleOnDragListener.DragProcessor() {
@Override
public boolean processDragEvent(@NotNull DragDirection dragDirection, @NotNull DragButton dragButton, @NotNull Point2d startPoint2d, @NotNull MotionEvent motionEvent) {
if (dragDirection == DragDirection.down) {
operatorsButtonClickHandler(dragButton);
return true;
}
return false;
}
}, dragPreferences), vibrator, preferences));
final OnDragListener toPositionOnDragListener = new OnDragListenerVibrator(new SimpleOnDragListener(new CursorDragProcessor(calculatorModel), dragPreferences), vibrator, preferences);
((DragButton) findViewById(R.id.rightButton)).setOnDragListener(toPositionOnDragListener);
((DragButton) findViewById(R.id.leftButton)).setOnDragListener(toPositionOnDragListener);

View File

@ -269,6 +269,9 @@ public enum CalculatorModel implements CursorControl, HistoryControl<CalculatorH
textToBeInserted.append("()");
cursorPositionOffset = -1;
break;
case comma:
textToBeInserted.append(" ");
break;
}
if (cursorPositionOffset == 0) {

View File

@ -118,6 +118,8 @@ public enum MathType {
}
},
comma(1150, false, false, ","),
text(1200, false, false) {
@Override
public int processToJscl(@NotNull StringBuilder result, int i, @NotNull String match) {

View File

@ -0,0 +1,41 @@
/*
* 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.model;
import org.jetbrains.annotations.NotNull;
import org.solovyev.common.math.MathEntity;
import org.solovyev.common.math.MathRegistry;
import java.util.HashMap;
import java.util.Map;
/**
* User: serso
* Date: 11/17/11
* Time: 11:28 PM
*/
public class AndroidFunctionsMathRegistry<T extends MathEntity> extends AndroidMathRegistryImpl<T> {
@NotNull
private static final Map<String, String> substitutes = new HashMap<String, String>();
static {
substitutes.put("", "sqrt");
}
@NotNull
private static final String FUNCTION_DESCRIPTION_PREFIX = "c_fun_description_";
public AndroidFunctionsMathRegistry(@NotNull MathRegistry<T> functionsRegistry) {
super(functionsRegistry, FUNCTION_DESCRIPTION_PREFIX);
}
@NotNull
@Override
protected Map<String, String> getSubstitutes() {
return substitutes;
}
}

View File

@ -7,7 +7,6 @@
package org.solovyev.android.calculator.model;
import android.content.Context;
import jscl.math.function.Function;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.solovyev.android.calculator.R;
@ -24,32 +23,40 @@ import java.util.Map;
* Date: 10/30/11
* Time: 1:03 AM
*/
public class AndroidMathRegistryImpl<T extends MathEntity> implements AndroidMathRegistry<T> {
@NotNull
private static final String FUNCTION_DESCRIPTION_PREFIX = "c_fun_description_";
public abstract class AndroidMathRegistryImpl<T extends MathEntity> implements AndroidMathRegistry<T> {
@NotNull
private final MathRegistry<T> functionsRegistry;
public AndroidMathRegistryImpl(@NotNull MathRegistry<T> functionsRegistry) {
@NotNull
private final String prefix;
protected AndroidMathRegistryImpl(@NotNull MathRegistry<T> functionsRegistry, @NotNull String prefix) {
this.functionsRegistry = functionsRegistry;
this.prefix = prefix;
}
@NotNull
protected abstract Map<String, String> getSubstitutes();
@Nullable
@Override
public String getDescription(@NotNull Context context, @NotNull String functionName) {
public String getDescription(@NotNull Context context, @NotNull String name) {
final String result;
final Map<String, Integer> stringsCache = RClassUtils.getCache(R.string.class);
final Integer stringId;
if (!functionName.equals("")) {
stringId = stringsCache.get(FUNCTION_DESCRIPTION_PREFIX + functionName);
final Map<String, String> substitutes = getSubstitutes();
final String substitute = substitutes.get(name);
if (substitute == null) {
stringId = stringsCache.get(prefix + name);
} else {
// todo serso: think
stringId = stringsCache.get(FUNCTION_DESCRIPTION_PREFIX + "sqrt");
stringId = stringsCache.get(prefix + substitute);
}
if (stringId != null) {
result = context.getString(stringId);
} else {

View File

@ -0,0 +1,46 @@
/*
* 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.model;
import org.jetbrains.annotations.NotNull;
import org.solovyev.common.math.MathEntity;
import org.solovyev.common.math.MathRegistry;
import java.util.HashMap;
import java.util.Map;
/**
* User: serso
* Date: 11/17/11
* Time: 11:29 PM
*/
public class AndroidOperatorsMathRegistry<T extends MathEntity> extends AndroidMathRegistryImpl<T> {
@NotNull
private static final Map<String, String> substitutes = new HashMap<String, String>();
static {
substitutes.put("Σ", "sum");
substitutes.put("", "product");
substitutes.put("", "derivative");
substitutes.put("∫ab", "integral_ab");
substitutes.put("", "integral");
substitutes.put("Σ", "sum");
}
@NotNull
private static final String OPERATOR_DESCRIPTION_PREFIX = "c_op_description_";
protected AndroidOperatorsMathRegistry(@NotNull MathRegistry<T> functionsRegistry) {
super(functionsRegistry, OPERATOR_DESCRIPTION_PREFIX);
}
@NotNull
@Override
protected Map<String, String> getSubstitutes() {
return substitutes;
}
}

View File

@ -13,24 +13,18 @@ import jscl.math.operator.Operator;
import jscl.text.ParseInterruptedException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.solovyev.android.calculator.R;
import org.solovyev.android.calculator.jscl.JsclOperation;
import org.solovyev.android.msg.AndroidMessage;
import org.solovyev.common.NumberMapper;
import org.solovyev.common.math.MathRegistry;
import org.solovyev.common.msg.MessageRegistry;
import org.solovyev.common.msg.MessageType;
import org.solovyev.common.utils.CollectionsUtils;
import org.solovyev.common.utils.Formatter;
import org.solovyev.common.utils.MutableObject;
import org.solovyev.common.utils.StringUtils;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
@ -70,18 +64,16 @@ public enum CalculatorEngine {
private final AndroidVarsRegistry varsRegister = new AndroidVarsRegistryImpl(engine.getConstantsRegistry());
@NotNull
private final AndroidMathRegistry functionsRegistry = new AndroidMathRegistryImpl(engine.getFunctionsRegistry());
private final AndroidMathRegistry functionsRegistry = new AndroidFunctionsMathRegistry(engine.getFunctionsRegistry());
@NotNull
private final AndroidMathRegistry operatorsRegistry = new AndroidMathRegistryImpl(engine.getOperatorsRegistry());
private final AndroidMathRegistry operatorsRegistry = new AndroidOperatorsMathRegistry(engine.getOperatorsRegistry());
private final MathRegistry<Operator> postfixFunctionsRegistry = engine.getPostfixFunctionsRegistry();
@NotNull
private final static Set<String> tooLongExecutionCache = new HashSet<String>();
@NotNull
private DecimalFormatSymbols decimalGroupSymbols = new DecimalFormatSymbols(Locale.getDefault());
{
decimalGroupSymbols.setDecimalSeparator('.');
decimalGroupSymbols.setGroupingSeparator(GROUPING_SEPARATOR_DEFAULT.charAt(0));
@ -178,7 +170,6 @@ public enum CalculatorEngine {
final JsclOperation finalOperation = operation;
final String result;
if (!tooLongExecutionCache.contains(jsclExpression)) {
final MutableObject<String> calculationResult = new MutableObject<String>(null);
final MutableObject<ParseException> exception = new MutableObject<ParseException>(null);
final MutableObject<Thread> calculationThread = new MutableObject<Thread>(null);
@ -234,7 +225,6 @@ public enum CalculatorEngine {
}
if (calculationResultLocal == null) {
tooLongExecutionCache.add(jsclExpression);
throw new ParseException("Too long calculation for: " + jsclExpression);
}
@ -243,9 +233,6 @@ public enum CalculatorEngine {
}
result = String.valueOf(calculationResult.getObject()).trim();
} else {
throw new ParseException("Too long calculation for: " + jsclExpression);
}
return new Result(operation.getFromProcessor().process(result), operation);
}
@ -330,6 +317,7 @@ public enum CalculatorEngine {
void setTimeout(int timeout) {
this.timeout = timeout;
}
// for tests only
void setThreadKiller(@NotNull ThreadKiller threadKiller) {
this.threadKiller = threadKiller;