diff --git a/res/values-ru/strings.xml b/res/values-ru/strings.xml index ef4b03b6..538fc79f 100644 --- a/res/values-ru/strings.xml +++ b/res/values-ru/strings.xml @@ -118,4 +118,26 @@ Длительность вибрации по нажатию клавиши Функции + Тригонометрическая функция синус. + Тригонометрическая функция косинус. + Тригонометрическая функция тангенс. + Тригонометрическая функция котангенс. + Арксинус - обратная функция к синусу. + Арккосинус - обратная функция к косинусу. + Арктангенс - обратная функция к тангенсу. + Арккотангенс - обратная функция к котангенсу. + Натуральный логарифм - логарифм по основанию e. + Десятичный логарифм - логарифм по основанию 10. + Экспонента. + Функция квадратного корня. + Функция кубического корня. + Модуль. + Знак - возвращает знак аргумента: -1, если аргумент меньше0, 0, если равен 0, 1, если больше 0. + Равно - возвращает 1, если два аргумента равны, иначе 0. + Меньше-либо-равно - возвращает 1, если два аргумента равны или первое меньше второго, иначе 0. + Больше-либо-равно - возвращает 1 если два аргумента равны или первое больше второго, иначе 0. + Не-равно - возвращает 1, если два аргумента не равны, иначе 0. + Меньше - возвращает 1, если первый аргумент меньше второго, иначе 0. + Больше - возвращает 1, если первый аргумент больше второго, иначе 0. + diff --git a/res/values/strings.xml b/res/values/strings.xml index c81c905b..a814e7d9 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -119,4 +119,26 @@ Duration vibration on button click Functions + Trigonometric sine function. + Trigonometric cosine function. + Trigonometric tangent function. + Trigonometric cotangent function. + Arcsine - the inverse of sine function. + Arccosine - the inverse of cosine function. + Arctangent - the inverse of tangent function. + Arccotangent - the inverse of cotangent function. + Natural logarithm - logarithm the base e. + Decadic logarithm - logarithm the base 10. + Exponential function. + Square root function. + Cubic root function. + Function that gives absolute value of an argument. + Function that gives the sign of an argument: -1 if argument is less than 0, 0 if equals to 0, 1 if more than 0. + Equals function - gives 1 if two arguments are equals, 0 otherwise. + Lesser-or-equals function - gives 1 if two arguments are equals or first is less than second, 0 otherwise. + Greater-or-equals function - gives 1 if two arguments are equals or first is greater than second, 0 otherwise. + Not-equals function - gives 1 if two arguments are not equals, 0 otherwise. + Lesser function - gives 1 if first argument is less than second, 0 otherwise. + Greater function - gives 1 if first argument is greater than second, 0 otherwise. + diff --git a/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java b/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java index 664289ea..c1a4669a 100644 --- a/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java +++ b/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java @@ -47,9 +47,6 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh @NotNull private final Announcer dpclRegister = new Announcer(DragPreferencesChangeListener.class); - @NotNull - private final static Map, Map> caches = new HashMap, Map>(3); - @NotNull private CalculatorModel calculatorModel; @@ -178,7 +175,7 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh private synchronized void setLayout(@NotNull SharedPreferences preferences) { - final Map layouts = getCache(R.layout.class); + final Map layouts = RClassUtils.getCache(R.layout.class); layoutName = preferences.getString(getString(R.string.p_calc_layout_key), getString(R.string.p_calc_layout)); @@ -194,7 +191,7 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh } private synchronized void setTheme(@NotNull SharedPreferences preferences) { - final Map styles = getCache(R.style.class); + final Map styles = RClassUtils.getCache(R.style.class); themeName = preferences.getString(getString(R.string.p_calc_theme_key), getString(R.string.p_calc_theme)); @@ -209,30 +206,6 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh setTheme(styleId); } - @NotNull - private static Map getCache(@NotNull Class clazz) { - Map result = caches.get(clazz); - - if (result == null) { - result = new HashMap(); - - for (Field field : clazz.getDeclaredFields()) { - int modifiers = field.getModifiers(); - if (Modifier.isFinal(modifiers) && Modifier.isStatic(modifiers)) { - try { - result.put(field.getName(), field.getInt(R.style.class)); - } catch (IllegalAccessException e) { - Log.e(CalculatorActivity.class.getName(), e.getMessage()); - } - } - } - - caches.put(clazz, result); - } - - return result; - } - private synchronized void firstTimeInit(@NotNull SharedPreferences preferences) { if (!initialized) { dragButtonIds = new ArrayList(); diff --git a/src/main/java/org/solovyev/android/calculator/CalculatorEditor.java b/src/main/java/org/solovyev/android/calculator/CalculatorEditor.java index 4f0194bf..e1d252c6 100644 --- a/src/main/java/org/solovyev/android/calculator/CalculatorEditor.java +++ b/src/main/java/org/solovyev/android/calculator/CalculatorEditor.java @@ -86,7 +86,7 @@ public class CalculatorEditor extends EditText { Log.d(this.getClass().getName(), getText().toString()); int length = getText().length(); - setSelection(Math.max(Math.min(length - 1, selectionStart), 0), Math.max(Math.min(length - 1, selectionEnd), 0)); + setSelection(Math.max(Math.min(length, selectionStart), 0), Math.max(Math.min(length, selectionEnd), 0)); } public boolean isHighlightText() { diff --git a/src/main/java/org/solovyev/android/calculator/CalculatorFunctionsActivity.java b/src/main/java/org/solovyev/android/calculator/CalculatorFunctionsActivity.java index 837d973a..7b79290b 100644 --- a/src/main/java/org/solovyev/android/calculator/CalculatorFunctionsActivity.java +++ b/src/main/java/org/solovyev/android/calculator/CalculatorFunctionsActivity.java @@ -43,8 +43,6 @@ public class CalculatorFunctionsActivity extends ListActivity{ final ListView lv = getListView(); lv.setTextFilterEnabled(true); - - lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { public void onItemClick(final AdapterView parent, final View view, @@ -71,7 +69,8 @@ public class CalculatorFunctionsActivity extends ListActivity{ final Function function = getItem(position); - if (!StringUtils.isEmpty(function.getDescription())) { + final String functionDescription = CalculatorEngine.instance.getFunctionsRegistry().getDescription(getContext(), function.getName()); + if (!StringUtils.isEmpty(functionDescription)) { TextView description = (TextView) result.findViewById(R.id.var_description); if (description == null) { final LayoutInflater layoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); @@ -80,7 +79,7 @@ public class CalculatorFunctionsActivity extends ListActivity{ itemView.removeView(description); result.addView(description); } - description.setText(function.getDescription()); + description.setText(functionDescription); } else { TextView description = (TextView) result.findViewById(R.id.var_description); if (description != null) { diff --git a/src/main/java/org/solovyev/android/calculator/RClassUtils.java b/src/main/java/org/solovyev/android/calculator/RClassUtils.java new file mode 100644 index 00000000..51a31c84 --- /dev/null +++ b/src/main/java/org/solovyev/android/calculator/RClassUtils.java @@ -0,0 +1,55 @@ +/* + * 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.util.Log; +import org.jetbrains.annotations.NotNull; + +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; +import java.util.HashMap; +import java.util.Map; + +/** + * User: serso + * Date: 10/30/11 + * Time: 1:11 AM + */ +public class RClassUtils { + + @NotNull + private final static Map, Map> caches = new HashMap, Map>(3); + + // not intended for instantiation + private RClassUtils() { + throw new AssertionError(); + } + + @NotNull + public static Map getCache(@NotNull Class clazz) { + Map result = caches.get(clazz); + + if (result == null) { + result = new HashMap(); + + for (Field field : clazz.getDeclaredFields()) { + int modifiers = field.getModifiers(); + if (Modifier.isFinal(modifiers) && Modifier.isStatic(modifiers)) { + try { + result.put(field.getName(), field.getInt(R.style.class)); + } catch (IllegalAccessException e) { + Log.e(CalculatorActivity.class.getName(), e.getMessage()); + } + } + } + + caches.put(clazz, result); + } + + return result; + } +} diff --git a/src/main/java/org/solovyev/android/calculator/model/AndroidFunctionsRegistry.java b/src/main/java/org/solovyev/android/calculator/model/AndroidFunctionsRegistry.java new file mode 100644 index 00000000..d5f687db --- /dev/null +++ b/src/main/java/org/solovyev/android/calculator/model/AndroidFunctionsRegistry.java @@ -0,0 +1,24 @@ +/* + * 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 android.content.Context; +import jscl.math.function.Function; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.solovyev.common.math.MathRegistry; + +/** + * User: serso + * Date: 10/30/11 + * Time: 1:02 AM + */ +public interface AndroidFunctionsRegistry extends MathRegistry { + + @Nullable + String getDescription(@NotNull Context context, @NotNull String functionName); +} diff --git a/src/main/java/org/solovyev/android/calculator/model/AndroidFunctionsRegistryImpl.java b/src/main/java/org/solovyev/android/calculator/model/AndroidFunctionsRegistryImpl.java new file mode 100644 index 00000000..590292e5 --- /dev/null +++ b/src/main/java/org/solovyev/android/calculator/model/AndroidFunctionsRegistryImpl.java @@ -0,0 +1,91 @@ +/* + * 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 android.content.Context; +import jscl.math.function.Function; +import jscl.math.function.FunctionsRegistry; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.solovyev.android.calculator.R; +import org.solovyev.android.calculator.RClassUtils; +import org.solovyev.common.definitions.IBuilder; + +import java.util.List; +import java.util.Map; + +/** + * User: serso + * Date: 10/30/11 + * Time: 1:03 AM + */ +public class AndroidFunctionsRegistryImpl implements AndroidFunctionsRegistry { + + @NotNull + private static final String FUNCTION_DESCRIPTION_PREFIX = "c_fun_description_"; + + @Nullable + @Override + public String getDescription(@NotNull Context context, @NotNull String functionName) { + final String result; + + final Map stringsCache = RClassUtils.getCache(R.string.class); + + final Integer stringId; + if (!functionName.equals("√")) { + stringId = stringsCache.get(FUNCTION_DESCRIPTION_PREFIX + functionName); + } else { + // todo serso: think + stringId = stringsCache.get(FUNCTION_DESCRIPTION_PREFIX + "sqrt"); + } + if (stringId != null) { + result = context.getString(stringId); + } else { + result = null; + } + + return result; + } + + @NotNull + @Override + public List getEntities() { + return FunctionsRegistry.getInstance().getEntities(); + } + + @NotNull + @Override + public List getSystemEntities() { + return FunctionsRegistry.getInstance().getSystemEntities(); + } + + @Override + public Function add(@Nullable String name, @NotNull IBuilder IBuilder) { + return FunctionsRegistry.getInstance().add(name, IBuilder); + } + + @Override + public void remove(@NotNull Function var) { + FunctionsRegistry.getInstance().remove(var); + } + + @NotNull + @Override + public List getNames() { + return FunctionsRegistry.getInstance().getNames(); + } + + @Override + public boolean contains(@NotNull String name) { + return FunctionsRegistry.getInstance().contains(name); + } + + @Override + public Function get(@NotNull String name) { + return FunctionsRegistry.getInstance().get(name); + } +} diff --git a/src/main/java/org/solovyev/android/calculator/model/CalculatorEngine.java b/src/main/java/org/solovyev/android/calculator/model/CalculatorEngine.java index e1353677..3324c35d 100644 --- a/src/main/java/org/solovyev/android/calculator/model/CalculatorEngine.java +++ b/src/main/java/org/solovyev/android/calculator/model/CalculatorEngine.java @@ -8,8 +8,6 @@ package org.solovyev.android.calculator.model; import android.content.Context; import android.content.SharedPreferences; import jscl.math.Expression; -import jscl.math.function.Function; -import jscl.math.function.FunctionsRegistry; import jscl.text.ParseInterruptedException; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -17,7 +15,6 @@ 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; @@ -67,7 +64,7 @@ public enum CalculatorEngine { private final AndroidVarsRegistry varsRegister = new AndroidVarsRegistryImpl(); @NotNull - private final MathRegistry functionsRegistry = FunctionsRegistry.getInstance(); + private final AndroidFunctionsRegistry functionsRegistry = new AndroidFunctionsRegistryImpl(); @NotNull private final static Set tooLongExecutionCache = new HashSet(); @@ -296,7 +293,7 @@ public enum CalculatorEngine { } @NotNull - public MathRegistry getFunctionsRegistry() { + public AndroidFunctionsRegistry getFunctionsRegistry() { return functionsRegistry; }