function description

This commit is contained in:
serso 2011-10-30 02:02:43 +04:00
parent 93b8ce8f6e
commit 47616d125c
9 changed files with 222 additions and 39 deletions

View File

@ -118,4 +118,26 @@
<string name="p_calc_haptic_feedback_duration_summary">Длительность вибрации по нажатию клавиши</string> <string name="p_calc_haptic_feedback_duration_summary">Длительность вибрации по нажатию клавиши</string>
<string name="c_functions">Функции</string> <string name="c_functions">Функции</string>
<string name="c_fun_description_sin">Тригонометрическая функция синус.</string>
<string name="c_fun_description_cos">Тригонометрическая функция косинус.</string>
<string name="c_fun_description_tan">Тригонометрическая функция тангенс.</string>
<string name="c_fun_description_cot">Тригонометрическая функция котангенс.</string>
<string name="c_fun_description_asin">Арксинус - обратная функция к синусу.</string>
<string name="c_fun_description_acos">Арккосинус - обратная функция к косинусу.</string>
<string name="c_fun_description_atan">Арктангенс - обратная функция к тангенсу.</string>
<string name="c_fun_description_acot">Арккотангенс - обратная функция к котангенсу.</string>
<string name="c_fun_description_ln">Натуральный логарифм - логарифм по основанию e.</string>
<string name="c_fun_description_lg">Десятичный логарифм - логарифм по основанию 10.</string>
<string name="c_fun_description_exp">Экспонента.</string>
<string name="c_fun_description_sqrt">Функция квадратного корня.</string>
<string name="c_fun_description_cubic">Функция кубического корня.</string>
<string name="c_fun_description_abs">Модуль.</string>
<string name="c_fun_description_sgn">Знак - возвращает знак аргумента: -1, если аргумент меньше0, 0, если равен 0, 1, если больше 0.</string>
<string name="c_fun_description_eq">Равно - возвращает 1, если два аргумента равны, иначе 0.</string>
<string name="c_fun_description_le">Меньше-либо-равно - возвращает 1, если два аргумента равны или первое меньше второго, иначе 0.</string>
<string name="c_fun_description_ge">Больше-либо-равно - возвращает 1 если два аргумента равны или первое больше второго, иначе 0.</string>
<string name="c_fun_description_ne">Не-равно - возвращает 1, если два аргумента не равны, иначе 0.</string>
<string name="c_fun_description_lt">Меньше - возвращает 1, если первый аргумент меньше второго, иначе 0.</string>
<string name="c_fun_description_gt">Больше - возвращает 1, если первый аргумент больше второго, иначе 0.</string>
</resources> </resources>

View File

@ -119,4 +119,26 @@
<string name="p_calc_haptic_feedback_duration_summary">Duration vibration on button click</string> <string name="p_calc_haptic_feedback_duration_summary">Duration vibration on button click</string>
<string name="c_functions">Functions</string> <string name="c_functions">Functions</string>
<string name="c_fun_description_sin">Trigonometric sine function.</string>
<string name="c_fun_description_cos">Trigonometric cosine function.</string>
<string name="c_fun_description_tan">Trigonometric tangent function.</string>
<string name="c_fun_description_cot">Trigonometric cotangent function.</string>
<string name="c_fun_description_asin">Arcsine - the inverse of sine function.</string>
<string name="c_fun_description_acos">Arccosine - the inverse of cosine function.</string>
<string name="c_fun_description_atan">Arctangent - the inverse of tangent function.</string>
<string name="c_fun_description_acot">Arccotangent - the inverse of cotangent function.</string>
<string name="c_fun_description_ln">Natural logarithm - logarithm the base e.</string>
<string name="c_fun_description_lg">Decadic logarithm - logarithm the base 10.</string>
<string name="c_fun_description_exp">Exponential function.</string>
<string name="c_fun_description_sqrt">Square root function.</string>
<string name="c_fun_description_cubic">Cubic root function.</string>
<string name="c_fun_description_abs">Function that gives absolute value of an argument.</string>
<string name="c_fun_description_sgn">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.</string>
<string name="c_fun_description_eq">Equals function - gives 1 if two arguments are equals, 0 otherwise.</string>
<string name="c_fun_description_le">Lesser-or-equals function - gives 1 if two arguments are equals or first is less than second, 0 otherwise.</string>
<string name="c_fun_description_ge">Greater-or-equals function - gives 1 if two arguments are equals or first is greater than second, 0 otherwise.</string>
<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>
</resources> </resources>

View File

@ -47,9 +47,6 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
@NotNull @NotNull
private final Announcer<DragPreferencesChangeListener> dpclRegister = new Announcer<DragPreferencesChangeListener>(DragPreferencesChangeListener.class); private final Announcer<DragPreferencesChangeListener> dpclRegister = new Announcer<DragPreferencesChangeListener>(DragPreferencesChangeListener.class);
@NotNull
private final static Map<Class<?>, Map<String, Integer>> caches = new HashMap<Class<?>, Map<String, Integer>>(3);
@NotNull @NotNull
private CalculatorModel calculatorModel; private CalculatorModel calculatorModel;
@ -178,7 +175,7 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
private synchronized void setLayout(@NotNull SharedPreferences preferences) { private synchronized void setLayout(@NotNull SharedPreferences preferences) {
final Map<String, Integer> layouts = getCache(R.layout.class); final Map<String, Integer> layouts = RClassUtils.getCache(R.layout.class);
layoutName = preferences.getString(getString(R.string.p_calc_layout_key), getString(R.string.p_calc_layout)); 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) { private synchronized void setTheme(@NotNull SharedPreferences preferences) {
final Map<String, Integer> styles = getCache(R.style.class); final Map<String, Integer> styles = RClassUtils.getCache(R.style.class);
themeName = preferences.getString(getString(R.string.p_calc_theme_key), getString(R.string.p_calc_theme)); 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); setTheme(styleId);
} }
@NotNull
private static Map<String, Integer> getCache(@NotNull Class<?> clazz) {
Map<String, Integer> result = caches.get(clazz);
if (result == null) {
result = new HashMap<String, Integer>();
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) { private synchronized void firstTimeInit(@NotNull SharedPreferences preferences) {
if (!initialized) { if (!initialized) {
dragButtonIds = new ArrayList<Integer>(); dragButtonIds = new ArrayList<Integer>();

View File

@ -86,7 +86,7 @@ public class CalculatorEditor extends EditText {
Log.d(this.getClass().getName(), getText().toString()); Log.d(this.getClass().getName(), getText().toString());
int length = getText().length(); 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() { public boolean isHighlightText() {

View File

@ -43,8 +43,6 @@ public class CalculatorFunctionsActivity extends ListActivity{
final ListView lv = getListView(); final ListView lv = getListView();
lv.setTextFilterEnabled(true); lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(final AdapterView<?> parent, public void onItemClick(final AdapterView<?> parent,
final View view, final View view,
@ -71,7 +69,8 @@ public class CalculatorFunctionsActivity extends ListActivity{
final Function function = getItem(position); 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); TextView description = (TextView) result.findViewById(R.id.var_description);
if (description == null) { if (description == null) {
final LayoutInflater layoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); final LayoutInflater layoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
@ -80,7 +79,7 @@ public class CalculatorFunctionsActivity extends ListActivity{
itemView.removeView(description); itemView.removeView(description);
result.addView(description); result.addView(description);
} }
description.setText(function.getDescription()); description.setText(functionDescription);
} else { } else {
TextView description = (TextView) result.findViewById(R.id.var_description); TextView description = (TextView) result.findViewById(R.id.var_description);
if (description != null) { if (description != null) {

View File

@ -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<Class<?>, Map<String, Integer>> caches = new HashMap<Class<?>, Map<String, Integer>>(3);
// not intended for instantiation
private RClassUtils() {
throw new AssertionError();
}
@NotNull
public static Map<String, Integer> getCache(@NotNull Class<?> clazz) {
Map<String, Integer> result = caches.get(clazz);
if (result == null) {
result = new HashMap<String, Integer>();
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;
}
}

View File

@ -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<Function> {
@Nullable
String getDescription(@NotNull Context context, @NotNull String functionName);
}

View File

@ -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<String, Integer> 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<Function> getEntities() {
return FunctionsRegistry.getInstance().getEntities();
}
@NotNull
@Override
public List<Function> getSystemEntities() {
return FunctionsRegistry.getInstance().getSystemEntities();
}
@Override
public Function add(@Nullable String name, @NotNull IBuilder<Function> IBuilder) {
return FunctionsRegistry.getInstance().add(name, IBuilder);
}
@Override
public void remove(@NotNull Function var) {
FunctionsRegistry.getInstance().remove(var);
}
@NotNull
@Override
public List<String> 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);
}
}

View File

@ -8,8 +8,6 @@ package org.solovyev.android.calculator.model;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import jscl.math.Expression; import jscl.math.Expression;
import jscl.math.function.Function;
import jscl.math.function.FunctionsRegistry;
import jscl.text.ParseInterruptedException; import jscl.text.ParseInterruptedException;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; 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.calculator.jscl.JsclOperation;
import org.solovyev.android.msg.AndroidMessage; import org.solovyev.android.msg.AndroidMessage;
import org.solovyev.common.NumberMapper; import org.solovyev.common.NumberMapper;
import org.solovyev.common.math.MathRegistry;
import org.solovyev.common.msg.MessageRegistry; import org.solovyev.common.msg.MessageRegistry;
import org.solovyev.common.msg.MessageType; import org.solovyev.common.msg.MessageType;
import org.solovyev.common.utils.CollectionsUtils; import org.solovyev.common.utils.CollectionsUtils;
@ -67,7 +64,7 @@ public enum CalculatorEngine {
private final AndroidVarsRegistry varsRegister = new AndroidVarsRegistryImpl(); private final AndroidVarsRegistry varsRegister = new AndroidVarsRegistryImpl();
@NotNull @NotNull
private final MathRegistry<Function> functionsRegistry = FunctionsRegistry.getInstance(); private final AndroidFunctionsRegistry functionsRegistry = new AndroidFunctionsRegistryImpl();
@NotNull @NotNull
private final static Set<String> tooLongExecutionCache = new HashSet<String>(); private final static Set<String> tooLongExecutionCache = new HashSet<String>();
@ -296,7 +293,7 @@ public enum CalculatorEngine {
} }
@NotNull @NotNull
public MathRegistry<Function> getFunctionsRegistry() { public AndroidFunctionsRegistry getFunctionsRegistry() {
return functionsRegistry; return functionsRegistry;
} }