new version + language added + refactor
This commit is contained in:
@@ -30,7 +30,6 @@ import org.solovyev.android.calculator.model.CalculatorEngine;
|
||||
import org.solovyev.android.view.FontSizeAdjuster;
|
||||
import org.solovyev.android.view.prefs.ResourceCache;
|
||||
import org.solovyev.android.view.widgets.*;
|
||||
import org.solovyev.common.BooleanMapper;
|
||||
import org.solovyev.common.utils.Announcer;
|
||||
import org.solovyev.common.utils.Point2d;
|
||||
import org.solovyev.common.utils.history.HistoryAction;
|
||||
@@ -288,7 +287,7 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
|
||||
|
||||
|
||||
private synchronized void setLayout(@NotNull SharedPreferences preferences) {
|
||||
final Map<String, Integer> layouts = RClassUtils.getCache(R.layout.class);
|
||||
final Map<String, Integer> layouts = ResourceCache.instance.getNameToIdCache(R.layout.class);
|
||||
|
||||
layoutName = preferences.getString(getString(R.string.p_calc_layout_key), getString(R.string.p_calc_layout));
|
||||
|
||||
@@ -304,7 +303,7 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
|
||||
}
|
||||
|
||||
private synchronized void setTheme(@NotNull SharedPreferences preferences) {
|
||||
final Map<String, Integer> styles = RClassUtils.getCache(R.style.class);
|
||||
final Map<String, Integer> styles = ResourceCache.instance.getNameToIdCache(R.style.class);
|
||||
|
||||
themeName = preferences.getString(getString(R.string.p_calc_theme_key), getString(R.string.p_calc_theme));
|
||||
|
||||
@@ -553,9 +552,5 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
|
||||
CalculatorEngine.instance.reset(this, preferences);
|
||||
this.calculatorModel.evaluate();
|
||||
}
|
||||
|
||||
final Boolean colorExpressionsInBracketsDefault = new BooleanMapper().parseValue(this.getString(R.string.p_calc_color_display));
|
||||
assert colorExpressionsInBracketsDefault != null;
|
||||
this.calculatorModel.getEditor().setHighlightText(preferences.getBoolean(this.getString(R.string.p_calc_color_display_key), colorExpressionsInBracketsDefault));
|
||||
}
|
||||
}
|
@@ -6,6 +6,7 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.graphics.Color;
|
||||
import android.text.Html;
|
||||
import android.util.AttributeSet;
|
||||
@@ -22,7 +23,10 @@ import org.solovyev.android.calculator.model.TextProcessor;
|
||||
* Date: 9/17/11
|
||||
* Time: 12:25 AM
|
||||
*/
|
||||
public class CalculatorEditor extends EditText {
|
||||
public class CalculatorEditor extends EditText implements SharedPreferences.OnSharedPreferenceChangeListener {
|
||||
|
||||
private static final String CALC_COLOR_DISPLAY_KEY = "org.solovyev.android.calculator.CalculatorModel_color_display";
|
||||
private static final boolean CALC_COLOR_DISPLAY_DEFAULT = true;
|
||||
|
||||
private boolean highlightText = true;
|
||||
|
||||
@@ -104,4 +108,15 @@ public class CalculatorEditor extends EditText {
|
||||
this.highlightText = highlightText;
|
||||
redraw();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSharedPreferenceChanged(SharedPreferences preferences, String key) {
|
||||
if (CALC_COLOR_DISPLAY_KEY.equals(key)) {
|
||||
this.setHighlightText(preferences.getBoolean(CALC_COLOR_DISPLAY_KEY, CALC_COLOR_DISPLAY_DEFAULT));
|
||||
}
|
||||
}
|
||||
|
||||
public void init(@NotNull SharedPreferences preferences) {
|
||||
onSharedPreferenceChanged(preferences, CALC_COLOR_DISPLAY_KEY);
|
||||
}
|
||||
}
|
||||
|
@@ -30,7 +30,6 @@ import org.solovyev.android.calculator.model.CalculatorParseException;
|
||||
import org.solovyev.android.calculator.model.Var;
|
||||
import org.solovyev.android.view.CursorControl;
|
||||
import org.solovyev.android.view.HistoryControl;
|
||||
import org.solovyev.common.BooleanMapper;
|
||||
import org.solovyev.common.msg.Message;
|
||||
import org.solovyev.common.utils.CollectionsUtils;
|
||||
import org.solovyev.common.utils.MutableObject;
|
||||
@@ -66,10 +65,8 @@ public enum CalculatorModel implements CursorControl, HistoryControl<CalculatorH
|
||||
this.calculatorEngine = calculator;
|
||||
|
||||
this.editor = (CalculatorEditor) activity.findViewById(R.id.calculatorEditor);
|
||||
|
||||
final Boolean colorExpressionsInBracketsDefault = new BooleanMapper().parseValue(activity.getString(R.string.p_calc_color_display));
|
||||
assert colorExpressionsInBracketsDefault != null;
|
||||
this.editor.setHighlightText(preferences.getBoolean(activity.getString(R.string.p_calc_color_display_key), colorExpressionsInBracketsDefault));
|
||||
this.editor.init(preferences);
|
||||
preferences.registerOnSharedPreferenceChangeListener(editor);
|
||||
|
||||
this.display = (CalculatorDisplay) activity.findViewById(R.id.calculatorDisplay);
|
||||
this.display.setOnClickListener(new CalculatorDisplayOnClickListener(activity));
|
||||
@@ -384,11 +381,6 @@ public enum CalculatorModel implements CursorControl, HistoryControl<CalculatorH
|
||||
return CalculatorDisplayHistoryState.newInstance(display);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public CalculatorEditor getEditor() {
|
||||
return editor;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public CalculatorDisplay getDisplay() {
|
||||
return display;
|
||||
|
@@ -1,55 +0,0 @@
|
||||
/*
|
||||
* 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;
|
||||
}
|
||||
}
|
@@ -9,8 +9,7 @@ package org.solovyev.android.calculator.model;
|
||||
import android.content.Context;
|
||||
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.android.view.prefs.ResourceCache;
|
||||
import org.solovyev.common.definitions.IBuilder;
|
||||
import org.solovyev.common.math.MathEntity;
|
||||
import org.solovyev.common.math.MathRegistry;
|
||||
@@ -42,27 +41,17 @@ public abstract class AndroidMathRegistryImpl<T extends MathEntity> implements A
|
||||
@Nullable
|
||||
@Override
|
||||
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;
|
||||
final String stringName;
|
||||
|
||||
final Map<String, String> substitutes = getSubstitutes();
|
||||
final String substitute = substitutes.get(name);
|
||||
if (substitute == null) {
|
||||
stringId = stringsCache.get(prefix + name);
|
||||
stringName = prefix + name;
|
||||
} else {
|
||||
stringId = stringsCache.get(prefix + substitute);
|
||||
stringName = prefix + substitute;
|
||||
}
|
||||
|
||||
if (stringId != null) {
|
||||
result = context.getString(stringId);
|
||||
} else {
|
||||
result = null;
|
||||
}
|
||||
|
||||
return result;
|
||||
return ResourceCache.instance.getCaption(stringName);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
|
@@ -30,6 +30,9 @@ public enum ResourceCache {
|
||||
|
||||
instance;
|
||||
|
||||
@NotNull
|
||||
private static final Locale DEFAULT_LOCALE = Locale.ENGLISH;
|
||||
|
||||
// ids of drag buttons in R.class
|
||||
private List<Integer> dragButtonIds = null;
|
||||
|
||||
@@ -44,6 +47,9 @@ public enum ResourceCache {
|
||||
|
||||
private Context context;
|
||||
|
||||
@NotNull
|
||||
private final NameToIdCache nameToIdCache = new NameToIdCache();
|
||||
|
||||
public List<Integer> getDragButtonIds() {
|
||||
return dragButtonIds;
|
||||
}
|
||||
@@ -54,7 +60,8 @@ public enum ResourceCache {
|
||||
|
||||
/**
|
||||
* Method load captions for default locale using android R class
|
||||
* @param context STATIC CONTEXT
|
||||
*
|
||||
* @param context STATIC CONTEXT
|
||||
* @param resourceClass class of captions in android (SUBCLASS of R class)
|
||||
*/
|
||||
public void initCaptions(@NotNull Context context, @NotNull Class<?> resourceClass) {
|
||||
@@ -63,9 +70,10 @@ public enum ResourceCache {
|
||||
|
||||
/**
|
||||
* Method load captions for specified locale using android R class
|
||||
* @param context STATIC CONTEXT
|
||||
*
|
||||
* @param context STATIC CONTEXT
|
||||
* @param resourceClass class of captions in android (SUBCLASS of R class)
|
||||
* @param locale language to be used for translation
|
||||
* @param locale language to be used for translation
|
||||
*/
|
||||
public void initCaptions(@NotNull Context context, @NotNull Class<?> resourceClass, @NotNull Locale locale) {
|
||||
assert this.resourceClass == null || this.resourceClass.equals(resourceClass);
|
||||
@@ -84,6 +92,8 @@ public enum ResourceCache {
|
||||
captionsByLanguage.put(field.getName(), context.getString(captionId));
|
||||
} catch (IllegalAccessException e) {
|
||||
Log.e(ResourceCache.class.getName(), e.getMessage());
|
||||
} catch (Resources.NotFoundException e) {
|
||||
Log.e(ResourceCache.class.getName(), "Caption with name " + field.getName() + " was not found for " + locale.getLanguage() + " language: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -107,15 +117,15 @@ public enum ResourceCache {
|
||||
|
||||
|
||||
/**
|
||||
* @param captionId id of caption to be translated
|
||||
* @param locale language to be used for translation
|
||||
* @param captionId id of caption to be translated
|
||||
* @param locale language to be used for translation
|
||||
* @return translation by caption id in specified language, null if no translation in specified language present
|
||||
*/
|
||||
@Nullable
|
||||
public String getCaption(@NotNull String captionId, @NotNull final Locale locale) {
|
||||
Map<String, String> captionsByLanguage = captions.get(locale.getLanguage());
|
||||
if (captionsByLanguage != null) {
|
||||
return captionsByLanguage.get(captionId);
|
||||
return getCaption(captionsByLanguage, captionId, locale);
|
||||
} else {
|
||||
assert resourceClass != null && context != null;
|
||||
|
||||
@@ -123,13 +133,22 @@ public enum ResourceCache {
|
||||
|
||||
captionsByLanguage = captions.get(locale.getLanguage());
|
||||
if (captionsByLanguage != null) {
|
||||
return captionsByLanguage.get(captionId);
|
||||
return getCaption(captionsByLanguage, captionId, locale);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private String getCaption(@NotNull Map<String, String> captionsByLanguage, @NotNull String captionId, @NotNull Locale locale) {
|
||||
String result = captionsByLanguage.get(captionId);
|
||||
if (result == null && !locale.getLanguage().equals(DEFAULT_LOCALE.getLanguage())) {
|
||||
result = getCaption(captionId, DEFAULT_LOCALE);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public void init(@NotNull Class<?> resourceClass, @NotNull Activity activity) {
|
||||
dragButtonIds = new ArrayList<Integer>();
|
||||
buttonIds = new ArrayList<Integer>();
|
||||
@@ -152,4 +171,44 @@ public enum ResourceCache {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Map<String, Integer> getNameToIdCache(@NotNull Class<?> clazz) {
|
||||
return this.nameToIdCache.getCache(clazz);
|
||||
}
|
||||
|
||||
private static class NameToIdCache {
|
||||
|
||||
@NotNull
|
||||
private final Map<Class<?>, Map<String, Integer>> caches = new HashMap<Class<?>, Map<String, Integer>>(3);
|
||||
|
||||
// not intended for instantiation
|
||||
private NameToIdCache() {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public 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(clazz));
|
||||
} catch (IllegalAccessException e) {
|
||||
Log.e(CalculatorActivity.class.getName(), e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
caches.put(clazz, result);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -89,9 +89,10 @@ public class ColorButton extends Button {
|
||||
feedbackPaint.setStyle(Style.STROKE);
|
||||
feedbackPaint.setStrokeWidth(2);
|
||||
|
||||
|
||||
if (CollectionsUtils.contains(getText().toString(), Arrays.asList("+", "-", "/", "×"))) {
|
||||
getPaint().setColor(resources.getColor(R.color.button_operator_text_color));
|
||||
} else if (getText().toString().equals("CE")) {
|
||||
} else if (getText().toString().equals("C")) {
|
||||
getPaint().setColor(resources.getColor(R.color.button_ce_text_color));
|
||||
} else {
|
||||
getPaint().setColor(resources.getColor(R.color.button_text_color));
|
||||
|
Reference in New Issue
Block a user