round preference

This commit is contained in:
serso 2011-10-27 01:13:51 +04:00
parent 000ac5bce9
commit 75a888b4fe
7 changed files with 59 additions and 10 deletions

View File

@ -35,6 +35,8 @@
<string name="c_vars">переменные</string> <string name="c_vars">переменные</string>
<string name="c_calc_color_display_title">Подсветка выражений</string> <string name="c_calc_color_display_title">Подсветка выражений</string>
<string name="c_calc_round_result_title">Округление результата</string>
<string name="c_calc_round_result_summary">Включает/выключает округление результата</string>
<string name="p_calc_result_precision_title">Точность результата</string> <string name="p_calc_result_precision_title">Точность результата</string>
<string name="c_exit">Выход</string> <string name="c_exit">Выход</string>
<string name="c_add">Добавить</string> <string name="c_add">Добавить</string>

View File

@ -22,6 +22,9 @@
<string name="p_calc_haptic_feedback_key">org.solovyev.android.calculator.CalculatorModel_haptic_feedback</string> <string name="p_calc_haptic_feedback_key">org.solovyev.android.calculator.CalculatorModel_haptic_feedback</string>
<string name="p_calc_haptic_feedback">false</string> <string name="p_calc_haptic_feedback">false</string>
<string name="p_calc_round_result_key">org.solovyev.android.calculator.CalculatorModel_round_result</string>
<string name="p_calc_round_result">true</string>
<string name="p_calc_vars">org.solovyev.android.calculator.CalculatorModel_vars</string> <string name="p_calc_vars">org.solovyev.android.calculator.CalculatorModel_vars</string>
<string name="p_calc_theme_key">org.solovyev.android.calculator.CalculatorActivity_calc_theme</string> <string name="p_calc_theme_key">org.solovyev.android.calculator.CalculatorActivity_calc_theme</string>

View File

@ -38,6 +38,8 @@
<string name="c_vars">vars</string> <string name="c_vars">vars</string>
<string name="c_calc_color_display_title">Highlight expressions</string> <string name="c_calc_color_display_title">Highlight expressions</string>
<string name="c_calc_round_result_title">Round result</string>
<string name="c_calc_round_result_summary">Toggles rounding of the result</string>
<string name="p_calc_result_precision_title">Precision of result</string> <string name="p_calc_result_precision_title">Precision of result</string>
<string name="c_exit">Exit</string> <string name="c_exit">Exit</string>
<string name="c_add">Add</string> <string name="c_add">Add</string>

View File

@ -4,12 +4,18 @@
xmlns:range="http://schemas.android.com/apk/res/org.solovyev.android.calculator"> xmlns:range="http://schemas.android.com/apk/res/org.solovyev.android.calculator">
<PreferenceCategory a:title="@string/c_prefs_calculations_category"> <PreferenceCategory a:title="@string/c_prefs_calculations_category">
<android.preference.CheckBoxPreference
a:key="@string/p_calc_round_result_key"
a:summary="@string/c_calc_round_result_summary"
a:title="@string/c_calc_round_result_title"
a:defaultValue="true"/>
<org.solovyev.android.view.prefs.NumberPickerDialogPreference <org.solovyev.android.view.prefs.NumberPickerDialogPreference
a:key="@string/p_calc_result_precision_key" a:key="@string/p_calc_result_precision_key"
a:title="@string/p_calc_result_precision_title" a:title="@string/p_calc_result_precision_title"
a:summary="@string/c_calc_result_precision_summary" a:summary="@string/c_calc_result_precision_summary"
a:defaultValue="5" a:defaultValue="5"
range:boundaries="0;10"/> range:boundaries="0;16"/>
<ListPreference a:key="@string/p_calc_grouping_separator_key" <ListPreference a:key="@string/p_calc_grouping_separator_key"
a:title="@string/c_calc_grouping_separator" a:title="@string/c_calc_grouping_separator"

View File

@ -481,10 +481,15 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
} }
@Override @Override
public void onSharedPreferenceChanged(SharedPreferences preferences, @Nullable String s) { public void onSharedPreferenceChanged(SharedPreferences preferences, @Nullable String key) {
dpclRegister.announce().onDragPreferencesChange(SimpleOnDragListener.getPreferences(preferences, this)); dpclRegister.announce().onDragPreferencesChange(SimpleOnDragListener.getPreferences(preferences, this));
if (CalculatorEngine.GROUPING_SEPARATOR_P_KEY.equals(key) ||
CalculatorEngine.ROUND_RESULT_P_KEY.equals(key) ||
CalculatorEngine.RESULT_PRECISION_P_KEY.equals(key)) {
CalculatorEngine.instance.reset(this, preferences); CalculatorEngine.instance.reset(this, preferences);
this.calculatorModel.evaluate();
}
final Boolean colorExpressionsInBracketsDefault = new BooleanMapper().parseValue(this.getString(R.string.p_calc_color_display)); final Boolean colorExpressionsInBracketsDefault = new BooleanMapper().parseValue(this.getString(R.string.p_calc_color_display));
assert colorExpressionsInBracketsDefault != null; assert colorExpressionsInBracketsDefault != null;

View File

@ -5,20 +5,33 @@
package org.solovyev.android.calculator; package org.solovyev.android.calculator;
import android.content.SharedPreferences;
import android.os.Bundle; import android.os.Bundle;
import android.preference.PreferenceActivity; import android.preference.PreferenceActivity;
import org.solovyev.android.calculator.model.CalculatorEngine;
/** /**
* User: serso * User: serso
* Date: 7/16/11 * Date: 7/16/11
* Time: 6:37 PM * Time: 6:37 PM
*/ */
public class CalculatorPreferencesActivity extends PreferenceActivity { public class CalculatorPreferencesActivity extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener {
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.main_preferences); addPreferencesFromResource(R.xml.main_preferences);
final SharedPreferences preferences = getPreferenceManager().getSharedPreferences();
preferences.registerOnSharedPreferenceChangeListener(this);
onSharedPreferenceChanged(preferences, CalculatorEngine.ROUND_RESULT_P_KEY);
}
@Override
public void onSharedPreferenceChanged(SharedPreferences preferences, String key) {
if (CalculatorEngine.ROUND_RESULT_P_KEY.equals(key)) {
findPreference(CalculatorEngine.RESULT_PRECISION_P_KEY).setEnabled(preferences.getBoolean(key, true));
}
} }
} }

View File

@ -44,8 +44,11 @@ public enum CalculatorEngine {
public static final String GROUPING_SEPARATOR_P_KEY = "org.solovyev.android.calculator.CalculatorActivity_calc_grouping_separator"; public static final String GROUPING_SEPARATOR_P_KEY = "org.solovyev.android.calculator.CalculatorActivity_calc_grouping_separator";
private static final String GROUPING_SEPARATOR_DEFAULT = " "; private static final String GROUPING_SEPARATOR_DEFAULT = " ";
private static final String RESULT_PRECISION_P_KEY = "org.solovyev.android.calculator.CalculatorModel_result_precision"; public static final String ROUND_RESULT_P_KEY = "org.solovyev.android.calculator.CalculatorModel_round_result";
private static final String RESULT_PRECISION_DEFAULT = "5"; public static final boolean ROUND_RESULT_DEFAULT = true;
public static final String RESULT_PRECISION_P_KEY = "org.solovyev.android.calculator.CalculatorModel_result_precision";
public static final String RESULT_PRECISION_DEFAULT = "5";
@NotNull @NotNull
private Interpreter interpreter; private Interpreter interpreter;
@ -53,6 +56,8 @@ public enum CalculatorEngine {
@NotNull @NotNull
private final Object lock = new Object(); private final Object lock = new Object();
private boolean roundResult = true;
private int precision = 5; private int precision = 5;
@NotNull @NotNull
@ -88,8 +93,12 @@ public enum CalculatorEngine {
df.setDecimalFormatSymbols(decimalGroupSymbols); df.setDecimalFormatSymbols(decimalGroupSymbols);
df.setGroupingUsed(useGroupingSeparator); df.setGroupingUsed(useGroupingSeparator);
if (round ) { if (round ) {
if (isRoundResult()) {
df.setMaximumFractionDigits(instance.getPrecision()); df.setMaximumFractionDigits(instance.getPrecision());
return df.format(new BigDecimal(value).setScale(instance.getPrecision(), BigDecimal.ROUND_HALF_UP).doubleValue()); return df.format(new BigDecimal(value).setScale(instance.getPrecision(), BigDecimal.ROUND_HALF_UP).doubleValue());
} else {
return String.valueOf(value);
}
} else { } else {
return df.format(value); return df.format(value);
} }
@ -217,7 +226,7 @@ public enum CalculatorEngine {
} }
} }
public int getPrecision() { private int getPrecision() {
return precision; return precision;
} }
@ -225,6 +234,14 @@ public enum CalculatorEngine {
this.precision = precision; this.precision = precision;
} }
private boolean isRoundResult() {
return roundResult;
}
public void setRoundResult(boolean roundResult) {
this.roundResult = roundResult;
}
public void init(@Nullable Context context, @Nullable SharedPreferences preferences) throws EvalError { public void init(@Nullable Context context, @Nullable SharedPreferences preferences) throws EvalError {
synchronized (lock) { synchronized (lock) {
reset(context, preferences); reset(context, preferences);
@ -238,6 +255,7 @@ public enum CalculatorEngine {
final NumberMapper<Integer> integerNumberMapper = new NumberMapper<Integer>(Integer.class); final NumberMapper<Integer> integerNumberMapper = new NumberMapper<Integer>(Integer.class);
//noinspection ConstantConditions //noinspection ConstantConditions
this.setPrecision(integerNumberMapper.parseValue(preferences.getString(RESULT_PRECISION_P_KEY, RESULT_PRECISION_DEFAULT))); this.setPrecision(integerNumberMapper.parseValue(preferences.getString(RESULT_PRECISION_P_KEY, RESULT_PRECISION_DEFAULT)));
this.setRoundResult(preferences.getBoolean(ROUND_RESULT_P_KEY, ROUND_RESULT_DEFAULT));
final String groupingSeparator = preferences.getString(GROUPING_SEPARATOR_P_KEY, GROUPING_SEPARATOR_DEFAULT); final String groupingSeparator = preferences.getString(GROUPING_SEPARATOR_P_KEY, GROUPING_SEPARATOR_DEFAULT);
if (StringUtils.isEmpty(groupingSeparator)) { if (StringUtils.isEmpty(groupingSeparator)) {