preferences object

This commit is contained in:
Sergey Solovyev 2011-12-25 15:00:09 +04:00
parent 422fd8f693
commit 1dbec40d12
28 changed files with 410 additions and 106 deletions

View File

@ -12,7 +12,7 @@
a:text="×" a:text="×"
c:textUp="^" c:textUp="^"
c:textDown="^2" c:textDown="^2"
c:textLeft="PI" c:textLeft="Π"
style="?digitButtonStyle" style="?digitButtonStyle"
c:directionTextScale="0.5" c:directionTextScale="0.5"
a:onClick="digitButtonClickHandler"/> a:onClick="digitButtonClickHandler"/>

View File

@ -23,8 +23,8 @@
<a href="http://www.achartengine.org/">AChartEngine</a> <a href="http://www.achartengine.org/">AChartEngine</a>
</string> </string>
<string name="c_release_notes">Замечания к выпуску</string> <string name="c_release_notes">Примечания к выпуску</string>
<string name="c_release_notes_for_title">"Замечания к выпуску версии "</string> <string name="c_release_notes_for_title">"Примечания к выпуску версии "</string>
<string name="c_release_notes_for_4"> <string name="c_release_notes_for_4">
1. Отклик на нажатие\n 1. Отклик на нажатие\n

View File

@ -156,8 +156,8 @@
<string name="c_first_start_text">Спасибо за выбор Калькулятора++!\n\nКалькулятор++ - это мощный инструмент для проведения ежедневных расчётов.\n\nПеред началом работы рекомендуется прочитать FAQ и подсказки для использования всех возможностей программы (нажмите Меню, затем Помощь)</string> <string name="c_first_start_text">Спасибо за выбор Калькулятора++!\n\nКалькулятор++ - это мощный инструмент для проведения ежедневных расчётов.\n\nПеред началом работы рекомендуется прочитать FAQ и подсказки для использования всех возможностей программы (нажмите Меню, затем Помощь)</string>
<string name="c_first_start_text_title">Приветствие</string> <string name="c_first_start_text_title">Приветствие</string>
<string name="c_calc_show_release_notes_title">Показывать замечания к выпуску</string> <string name="c_calc_show_release_notes_title">Показывать примечания к выпуску</string>
<string name="c_calc_show_release_notes_summary">Определяет показывать или нет всплывающее окно с замечаниями к выпуску после обновления новой версии</string> <string name="c_calc_show_release_notes_summary">Определяет показывать или нет всплывающее окно с примечаниями к выпуску после обновления новой версии</string>
<string name="c_calc_use_back_button_as_prev_summary">Устанавливает поведение по нажатию на кнопку Назад</string> <string name="c_calc_use_back_button_as_prev_summary">Устанавливает поведение по нажатию на кнопку Назад</string>
<string name="c_calc_use_back_button_as_prev_title">Использовать кнопку назад как назад по истории</string> <string name="c_calc_use_back_button_as_prev_title">Использовать кнопку назад как назад по истории</string>

View File

@ -4,7 +4,7 @@
* or visit http://se.solovyev.org * or visit http://se.solovyev.org
*/ */
package org.solovyev.android.view.prefs; package org.solovyev.android;
import android.app.Activity; import android.app.Activity;
import android.content.Context; import android.content.Context;

View File

@ -4,7 +4,7 @@
* or visit http://se.solovyev.org * or visit http://se.solovyev.org
*/ */
package org.solovyev.android.view.prefs; package org.solovyev.android;
import android.app.Activity; import android.app.Activity;
import android.content.Context; import android.content.Context;

View File

@ -27,14 +27,14 @@ import jscl.AngleUnit;
import jscl.NumeralBase; import jscl.NumeralBase;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.solovyev.android.AndroidUtils;
import org.solovyev.android.ResourceCache;
import org.solovyev.android.calculator.about.CalculatorReleaseNotesActivity; import org.solovyev.android.calculator.about.CalculatorReleaseNotesActivity;
import org.solovyev.android.calculator.history.CalculatorHistory; import org.solovyev.android.calculator.history.CalculatorHistory;
import org.solovyev.android.calculator.history.CalculatorHistoryState; import org.solovyev.android.calculator.history.CalculatorHistoryState;
import org.solovyev.android.calculator.math.MathType; import org.solovyev.android.calculator.math.MathType;
import org.solovyev.android.calculator.model.CalculatorEngine; import org.solovyev.android.calculator.model.CalculatorEngine;
import org.solovyev.android.view.FontSizeAdjuster; import org.solovyev.android.view.FontSizeAdjuster;
import org.solovyev.android.view.prefs.AndroidUtils;
import org.solovyev.android.view.prefs.ResourceCache;
import org.solovyev.android.view.widgets.*; import org.solovyev.android.view.widgets.*;
import org.solovyev.common.utils.Announcer; import org.solovyev.common.utils.Announcer;
import org.solovyev.common.utils.Point2d; import org.solovyev.common.utils.Point2d;
@ -194,9 +194,8 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
final AngleUnit angleUnits = AngleUnit.valueOf(directionText); final AngleUnit angleUnits = AngleUnit.valueOf(directionText);
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(CalculatorActivity.this); final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(CalculatorActivity.this);
final SharedPreferences.Editor editor = preferences.edit();
editor.putString(CalculatorEngine.ANGLE_UNITS_P_KEY, angleUnits.name()); CalculatorEngine.Preferences.angleUnit.putPreference(preferences, angleUnits);
editor.commit();
result = true; result = true;
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
@ -229,9 +228,7 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
final NumeralBase numeralBase = NumeralBase.valueOf(directionText); final NumeralBase numeralBase = NumeralBase.valueOf(directionText);
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(CalculatorActivity.this); final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(CalculatorActivity.this);
final SharedPreferences.Editor editor = preferences.edit(); CalculatorEngine.Preferences.numeralBase.putPreference(preferences, numeralBase);
editor.putString(CalculatorEngine.NUMERAL_BASES_P_KEY, numeralBase.name());
editor.commit();
result = true; result = true;
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
@ -264,7 +261,7 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
} }
private void setDefaultValues(@NotNull SharedPreferences preferences) { private void setDefaultValues(@NotNull SharedPreferences preferences) {
if (!preferences.contains(CalculatorEngine.GROUPING_SEPARATOR_P_KEY)) { if (!preferences.contains(CalculatorEngine.Preferences.groupingSeparator.getKey())) {
final Locale locale = Locale.getDefault(); final Locale locale = Locale.getDefault();
if (locale != null) { if (locale != null) {
final DecimalFormatSymbols decimalFormatSymbols = new DecimalFormatSymbols(locale); final DecimalFormatSymbols decimalFormatSymbols = new DecimalFormatSymbols(locale);
@ -275,17 +272,19 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
} else { } else {
groupingSeparator = " "; groupingSeparator = " ";
} }
final SharedPreferences.Editor editor = preferences.edit();
editor.putString(CalculatorEngine.GROUPING_SEPARATOR_P_KEY, groupingSeparator); CalculatorEngine.Preferences.groupingSeparator.putPreference(preferences, groupingSeparator);
editor.commit();
} }
} }
if (!preferences.contains(CalculatorEngine.ANGLE_UNITS_P_KEY)) { if (!preferences.contains(CalculatorEngine.Preferences.angleUnit.getKey())) {
final SharedPreferences.Editor editor = preferences.edit(); CalculatorEngine.Preferences.angleUnit.putDefault(preferences);
editor.putString(CalculatorEngine.ANGLE_UNITS_P_KEY, CalculatorEngine.ANGLE_UNITS_DEFAULT);
editor.commit();
} }
if (!preferences.contains(CalculatorEngine.Preferences.numeralBase.getKey())) {
CalculatorEngine.Preferences.numeralBase.putDefault(preferences);
}
} }
private synchronized void setOnDragListeners(@NotNull SimpleOnDragListener.Preferences dragPreferences, @NotNull SharedPreferences preferences) { private synchronized void setOnDragListeners(@NotNull SimpleOnDragListener.Preferences dragPreferences, @NotNull SharedPreferences preferences) {
@ -623,12 +622,7 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
dpclRegister.announce().onDragPreferencesChange(SimpleOnDragListener.getPreferences(preferences, this)); dpclRegister.announce().onDragPreferencesChange(SimpleOnDragListener.getPreferences(preferences, this));
} }
if (CalculatorEngine.GROUPING_SEPARATOR_P_KEY.equals(key) || if (CalculatorEngine.Preferences.getPreferenceKeys().contains(key)) {
CalculatorEngine.MULTIPLICATION_SIGN_P_KEY.equals(key) ||
CalculatorEngine.ROUND_RESULT_P_KEY.equals(key) ||
CalculatorEngine.RESULT_PRECISION_P_KEY.equals(key) ||
CalculatorEngine.ANGLE_UNITS_P_KEY.equals(key) ||
CalculatorEngine.NUMERAL_BASES_P_KEY.equals(key)) {
CalculatorEngine.instance.reset(this, preferences); CalculatorEngine.instance.reset(this, preferences);
this.calculatorModel.evaluate(); this.calculatorModel.evaluate();
} }
@ -637,7 +631,7 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
useBackAsPrev = preferences.getBoolean(USE_BACK_AS_PREV_P_KEY, USE_BACK_AS_PREV_DEFAULT); useBackAsPrev = preferences.getBoolean(USE_BACK_AS_PREV_P_KEY, USE_BACK_AS_PREV_DEFAULT);
} }
if ( CalculatorEngine.MULTIPLICATION_SIGN_P_KEY.equals(key) ) { if ( CalculatorEngine.Preferences.multiplicationSign.getKey().equals(key) ) {
initMultiplicationButton(); initMultiplicationButton();
} }
} }

View File

@ -26,14 +26,14 @@ public class CalculatorPreferencesActivity extends PreferenceActivity implements
final SharedPreferences preferences = getPreferenceManager().getSharedPreferences(); final SharedPreferences preferences = getPreferenceManager().getSharedPreferences();
preferences.registerOnSharedPreferenceChangeListener(this); preferences.registerOnSharedPreferenceChangeListener(this);
onSharedPreferenceChanged(preferences, CalculatorEngine.ROUND_RESULT_P_KEY); onSharedPreferenceChanged(preferences, CalculatorEngine.Preferences.roundResult.getKey());
onSharedPreferenceChanged(preferences, VibratorContainer.HAPTIC_FEEDBACK_P_KEY); onSharedPreferenceChanged(preferences, VibratorContainer.HAPTIC_FEEDBACK_P_KEY);
} }
@Override @Override
public void onSharedPreferenceChanged(SharedPreferences preferences, String key) { public void onSharedPreferenceChanged(SharedPreferences preferences, String key) {
if (CalculatorEngine.ROUND_RESULT_P_KEY.equals(key)) { if (CalculatorEngine.Preferences.roundResult.getKey().equals(key)) {
findPreference(CalculatorEngine.RESULT_PRECISION_P_KEY).setEnabled(preferences.getBoolean(key, CalculatorEngine.ROUND_RESULT_DEFAULT)); findPreference(CalculatorEngine.Preferences.roundResult.getKey()).setEnabled(preferences.getBoolean(key, CalculatorEngine.Preferences.roundResult.getDefaultValue()));
} else if (VibratorContainer.HAPTIC_FEEDBACK_P_KEY.equals(key)) { } else if (VibratorContainer.HAPTIC_FEEDBACK_P_KEY.equals(key)) {
findPreference(VibratorContainer.HAPTIC_FEEDBACK_DURATION_P_KEY).setEnabled(preferences.getBoolean(key, VibratorContainer.HAPTIC_FEEDBACK_DEFAULT)); findPreference(VibratorContainer.HAPTIC_FEEDBACK_DURATION_P_KEY).setEnabled(preferences.getBoolean(key, VibratorContainer.HAPTIC_FEEDBACK_DEFAULT));
} }

View File

@ -9,8 +9,8 @@ import android.app.TabActivity;
import android.os.Bundle; import android.os.Bundle;
import android.widget.TabHost; import android.widget.TabHost;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.solovyev.android.AndroidUtils;
import org.solovyev.android.calculator.R; import org.solovyev.android.calculator.R;
import org.solovyev.android.view.prefs.AndroidUtils;
/** /**
* User: serso * User: serso

View File

@ -13,10 +13,11 @@ import android.text.Html;
import android.text.method.LinkMovementMethod; import android.text.method.LinkMovementMethod;
import android.widget.TextView; import android.widget.TextView;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.solovyev.android.AndroidUtils;
import org.solovyev.android.ResourceCache;
import org.solovyev.android.calculator.CalculatorActivity; import org.solovyev.android.calculator.CalculatorActivity;
import org.solovyev.android.calculator.R; import org.solovyev.android.calculator.R;
import org.solovyev.android.view.prefs.AndroidUtils; import org.solovyev.android.AndroidUtils;
import org.solovyev.android.view.prefs.ResourceCache;
import org.solovyev.common.utils.StringUtils; import org.solovyev.common.utils.StringUtils;
/** /**

View File

@ -13,8 +13,9 @@ import android.os.Bundle;
import android.widget.TabHost; import android.widget.TabHost;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.solovyev.android.AndroidUtils;
import org.solovyev.android.calculator.R; import org.solovyev.android.calculator.R;
import org.solovyev.android.view.prefs.AndroidUtils; import org.solovyev.android.AndroidUtils;
/** /**
* User: serso * User: serso

View File

@ -10,8 +10,9 @@ import android.app.TabActivity;
import android.os.Bundle; import android.os.Bundle;
import android.widget.TabHost; import android.widget.TabHost;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.solovyev.android.AndroidUtils;
import org.solovyev.android.calculator.R; import org.solovyev.android.calculator.R;
import org.solovyev.android.view.prefs.AndroidUtils; import org.solovyev.android.AndroidUtils;
/** /**
* User: serso * User: serso

View File

@ -7,14 +7,12 @@
package org.solovyev.android.calculator.history; package org.solovyev.android.calculator.history;
import android.content.Context; import android.content.Context;
import android.content.res.Resources;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
import android.widget.TextView; import android.widget.TextView;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.solovyev.android.calculator.R; import org.solovyev.android.calculator.R;
import org.solovyev.android.view.prefs.ResourceCache;
import org.solovyev.common.utils.StringUtils; import org.solovyev.common.utils.StringUtils;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;

View File

@ -10,9 +10,10 @@ import android.app.TabActivity;
import android.os.Bundle; import android.os.Bundle;
import android.widget.TabHost; import android.widget.TabHost;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.solovyev.android.AndroidUtils;
import org.solovyev.android.calculator.R; import org.solovyev.android.calculator.R;
import org.solovyev.android.calculator.model.AndroidFunctionsMathRegistry; import org.solovyev.android.calculator.model.AndroidFunctionsMathRegistry;
import org.solovyev.android.view.prefs.AndroidUtils; import org.solovyev.android.AndroidUtils;
/** /**
* User: serso * User: serso

View File

@ -10,9 +10,10 @@ import android.app.TabActivity;
import android.os.Bundle; import android.os.Bundle;
import android.widget.TabHost; import android.widget.TabHost;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.solovyev.android.AndroidUtils;
import org.solovyev.android.calculator.R; import org.solovyev.android.calculator.R;
import org.solovyev.android.calculator.model.VarCategory; import org.solovyev.android.calculator.model.VarCategory;
import org.solovyev.android.view.prefs.AndroidUtils; import org.solovyev.android.AndroidUtils;
/** /**
* User: serso * User: serso

View File

@ -13,7 +13,7 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.simpleframework.xml.Serializer; import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister; import org.simpleframework.xml.core.Persister;
import org.solovyev.android.view.prefs.ResourceCache; import org.solovyev.android.ResourceCache;
import org.solovyev.common.definitions.IBuilder; import org.solovyev.common.definitions.IBuilder;
import org.solovyev.common.math.MathEntity; import org.solovyev.common.math.MathEntity;
import org.solovyev.common.math.MathRegistry; import org.solovyev.common.math.MathRegistry;

View File

@ -8,17 +8,13 @@ package org.solovyev.android.calculator.model;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import jscl.math.function.IConstant; import jscl.math.function.IConstant;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;
import org.solovyev.android.calculator.R; import org.solovyev.android.calculator.R;
import org.solovyev.common.definitions.IBuilder; import org.solovyev.common.definitions.IBuilder;
import org.solovyev.common.math.MathRegistry; import org.solovyev.common.math.MathRegistry;
import java.io.StringWriter;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -33,6 +29,7 @@ class AndroidVarsRegistryImpl extends AbstractAndroidMathRegistry<IConstant, Var
private static final Map<String, String> substitutes = new HashMap<String, String>(); private static final Map<String, String> substitutes = new HashMap<String, String>();
static { static {
substitutes.put("π", "pi"); substitutes.put("π", "pi");
substitutes.put("Π", "PI");
substitutes.put("", "inf"); substitutes.put("", "inf");
substitutes.put("h", "h_reduced"); substitutes.put("h", "h_reduced");
substitutes.put("NaN", "nan"); substitutes.put("NaN", "nan");

View File

@ -16,12 +16,19 @@ import jscl.text.ParseInterruptedException;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.solovyev.android.calculator.jscl.JsclOperation; import org.solovyev.android.calculator.jscl.JsclOperation;
import org.solovyev.android.view.prefs.BooleanPreference;
import org.solovyev.android.view.prefs.EnumMapper;
import org.solovyev.android.view.prefs.Preference;
import org.solovyev.android.view.prefs.StringPreference;
import org.solovyev.common.NumberMapper; import org.solovyev.common.NumberMapper;
import org.solovyev.common.msg.MessageRegistry; import org.solovyev.common.msg.MessageRegistry;
import org.solovyev.common.utils.MutableObject; import org.solovyev.common.utils.MutableObject;
import org.solovyev.common.utils.StringUtils; import org.solovyev.common.utils.StringUtils;
import java.text.DecimalFormatSymbols; import java.text.DecimalFormatSymbols;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -35,22 +42,46 @@ public enum CalculatorEngine {
instance; instance;
public static final String GROUPING_SEPARATOR_P_KEY = "org.solovyev.android.calculator.CalculatorActivity_calc_grouping_separator"; private static final String GROUPING_SEPARATOR_P_KEY = "org.solovyev.android.calculator.CalculatorActivity_calc_grouping_separator";
public static final String MULTIPLICATION_SIGN_P_KEY = "org.solovyev.android.calculator.CalculatorActivity_calc_multiplication_sign"; private static final String MULTIPLICATION_SIGN_P_KEY = "org.solovyev.android.calculator.CalculatorActivity_calc_multiplication_sign";
public static final String MULTIPLICATION_SIGN_DEFAULT = "×"; private static final String MULTIPLICATION_SIGN_DEFAULT = "×";
public static final String ROUND_RESULT_P_KEY = "org.solovyev.android.calculator.CalculatorModel_round_result"; private static final String ROUND_RESULT_P_KEY = "org.solovyev.android.calculator.CalculatorModel_round_result";
public static final boolean ROUND_RESULT_DEFAULT = true; private static final boolean ROUND_RESULT_DEFAULT = true;
public static final String RESULT_PRECISION_P_KEY = "org.solovyev.android.calculator.CalculatorModel_result_precision"; private static final String RESULT_PRECISION_P_KEY = "org.solovyev.android.calculator.CalculatorModel_result_precision";
public static final String RESULT_PRECISION_DEFAULT = "5"; private static final String RESULT_PRECISION_DEFAULT = "5";
public static final String NUMERAL_BASES_P_KEY = "org.solovyev.android.calculator.CalculatorActivity_numeral_bases"; private static final String NUMERAL_BASES_P_KEY = "org.solovyev.android.calculator.CalculatorActivity_numeral_bases";
public static final String NUMERAL_BASES_DEFAULT = "dec"; private static final String NUMERAL_BASES_DEFAULT = "dec";
public static final String ANGLE_UNITS_P_KEY = "org.solovyev.android.calculator.CalculatorActivity_angle_units"; private static final String ANGLE_UNITS_P_KEY = "org.solovyev.android.calculator.CalculatorActivity_angle_units";
public static final String ANGLE_UNITS_DEFAULT = "deg"; private static final String ANGLE_UNITS_DEFAULT = "deg";
public static class Preferences {
public static final Preference<String> groupingSeparator = StringPreference.newInstance(GROUPING_SEPARATOR_P_KEY, JsclMathEngine.GROUPING_SEPARATOR_DEFAULT);
public static final Preference<String> multiplicationSign = StringPreference.newInstance(MULTIPLICATION_SIGN_P_KEY, MULTIPLICATION_SIGN_DEFAULT);
public static final Preference<Integer> precision = StringPreference.newInstance(RESULT_PRECISION_P_KEY, RESULT_PRECISION_DEFAULT, new NumberMapper<Integer>(Integer.class));
public static final Preference<Boolean> roundResult = new BooleanPreference(ROUND_RESULT_P_KEY, ROUND_RESULT_DEFAULT);
public static final Preference<NumeralBase> numeralBase = StringPreference.newInstance(NUMERAL_BASES_P_KEY, NUMERAL_BASES_DEFAULT, EnumMapper.newInstance(NumeralBase.class));
public static final Preference<AngleUnit> angleUnit = StringPreference.newInstance(ANGLE_UNITS_P_KEY, ANGLE_UNITS_DEFAULT, EnumMapper.newInstance(AngleUnit.class));
private static final List<String> preferenceKeys = new ArrayList<String>();
static {
preferenceKeys.add(groupingSeparator.getKey());
preferenceKeys.add(multiplicationSign.getKey());
preferenceKeys.add(precision.getKey());
preferenceKeys.add(roundResult.getKey());
preferenceKeys.add(numeralBase.getKey());
preferenceKeys.add(angleUnit.getKey());
}
@NotNull
public static List<String> getPreferenceKeys() {
return Collections.unmodifiableList(preferenceKeys);
}
}
public static final int DEFAULT_TIMEOUT = 3000; public static final int DEFAULT_TIMEOUT = 3000;
@ -266,15 +297,13 @@ public enum CalculatorEngine {
public void reset(@Nullable Context context, @Nullable SharedPreferences preferences) { public void reset(@Nullable Context context, @Nullable SharedPreferences preferences) {
synchronized (lock) { synchronized (lock) {
if (preferences != null) { if (preferences != null) {
final NumberMapper<Integer> integerNumberMapper = new NumberMapper<Integer>(Integer.class); this.setPrecision(Preferences.precision.getPreference(preferences));
//noinspection ConstantConditions this.setRoundResult(Preferences.roundResult.getPreference(preferences));
this.setPrecision(integerNumberMapper.parseValue(preferences.getString(RESULT_PRECISION_P_KEY, RESULT_PRECISION_DEFAULT)));
this.setRoundResult(preferences.getBoolean(ROUND_RESULT_P_KEY, ROUND_RESULT_DEFAULT));
this.setAngleUnits(getAngleUnitsFromPrefs(preferences)); this.setAngleUnits(getAngleUnitsFromPrefs(preferences));
this.setNumeralBase(getNumeralBaseFromPrefs(preferences)); this.setNumeralBase(getNumeralBaseFromPrefs(preferences));
this.setMultiplicationSign(preferences.getString(MULTIPLICATION_SIGN_P_KEY, MULTIPLICATION_SIGN_DEFAULT)); this.setMultiplicationSign(Preferences.multiplicationSign.getPreference(preferences));
final String groupingSeparator = preferences.getString(GROUPING_SEPARATOR_P_KEY, JsclMathEngine.GROUPING_SEPARATOR_DEFAULT); final String groupingSeparator = Preferences.groupingSeparator.getPreference(preferences);
if (StringUtils.isEmpty(groupingSeparator)) { if (StringUtils.isEmpty(groupingSeparator)) {
this.getEngine().setUseGroupingSeparator(false); this.getEngine().setUseGroupingSeparator(false);
} else { } else {
@ -292,12 +321,12 @@ public enum CalculatorEngine {
@NotNull @NotNull
public NumeralBase getNumeralBaseFromPrefs(@NotNull SharedPreferences preferences) { public NumeralBase getNumeralBaseFromPrefs(@NotNull SharedPreferences preferences) {
return NumeralBase.valueOf(preferences.getString(NUMERAL_BASES_P_KEY, NUMERAL_BASES_DEFAULT)); return Preferences.numeralBase.getPreference(preferences);
} }
@NotNull @NotNull
public AngleUnit getAngleUnitsFromPrefs(@NotNull SharedPreferences preferences) { public AngleUnit getAngleUnitsFromPrefs(@NotNull SharedPreferences preferences) {
return AngleUnit.valueOf(preferences.getString(ANGLE_UNITS_P_KEY, ANGLE_UNITS_DEFAULT)); return Preferences.angleUnit.getPreference(preferences);
} }
//for tests only //for tests only

View File

@ -7,7 +7,7 @@
package org.solovyev.android.msg; package org.solovyev.android.msg;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.solovyev.android.view.prefs.ResourceCache; import org.solovyev.android.ResourceCache;
import org.solovyev.common.msg.AbstractMessage; import org.solovyev.common.msg.AbstractMessage;
import org.solovyev.common.msg.MessageType; import org.solovyev.common.msg.MessageType;

View File

@ -0,0 +1,66 @@
/*
* 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.view.prefs;
import android.content.SharedPreferences;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* User: serso
* Date: 12/25/11
* Time: 12:23 PM
*/
public abstract class AbstractPreference<T> implements Preference<T> {
@NotNull
private final String key;
private final T defaultValue;
protected AbstractPreference(@NotNull String key, @Nullable T defaultValue) {
this.key = key;
this.defaultValue = defaultValue;
}
@NotNull
public String getKey() {
return key;
}
public T getDefaultValue() {
return defaultValue;
}
@Override
public final T getPreference(@NotNull SharedPreferences preferences) {
if ( preferences.contains(this.key) ) {
return getPersistedValue(preferences);
} else {
return this.defaultValue;
}
}
@Nullable
protected abstract T getPersistedValue(@NotNull SharedPreferences preferences);
@Override
public void putDefault(@NotNull SharedPreferences preferences) {
putPreference(preferences, this.defaultValue);
}
@Override
public void putPreference(@NotNull SharedPreferences preferences, @Nullable T value) {
if (value != null) {
final SharedPreferences.Editor editor = preferences.edit();
putPersistedValue(editor, value);
editor.commit();
}
}
protected abstract void putPersistedValue(@NotNull SharedPreferences.Editor editor, @NotNull T value);
}

View File

@ -0,0 +1,33 @@
/*
* 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.view.prefs;
import android.content.SharedPreferences;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* User: serso
* Date: 12/25/11
* Time: 1:06 PM
*/
public class BooleanPreference extends AbstractPreference<Boolean>{
public BooleanPreference(@NotNull String key, @Nullable Boolean defaultValue) {
super(key, defaultValue);
}
@Override
protected Boolean getPersistedValue(@NotNull SharedPreferences preferences) {
return preferences.getBoolean(getKey(), false);
}
@Override
protected void putPersistedValue(@NotNull SharedPreferences.Editor editor, @NotNull Boolean value) {
editor.putBoolean(getKey(), value);
}
}

View File

@ -0,0 +1,40 @@
/*
* 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.view.prefs;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.solovyev.common.utils.Mapper;
/**
* User: serso
* Date: 12/25/11
* Time: 1:17 PM
*/
public class EnumMapper<T extends Enum> implements Mapper<T>{
@NotNull
private final Class<T> enumClass;
public EnumMapper(@NotNull Class<T> enumClass) {
this.enumClass = enumClass;
}
public static <T extends Enum> Mapper<T> newInstance(@NotNull Class<T> enumClass) {
return new EnumMapper<T>(enumClass);
}
@Override
public String formatValue(@Nullable T value) throws IllegalArgumentException {
return value == null ? null : value.name();
}
@Override
public T parseValue(@Nullable String value) throws IllegalArgumentException {
return value == null ? null : (T)Enum.valueOf(enumClass, value);
}
}

View File

@ -0,0 +1,33 @@
/*
* 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.view.prefs;
import android.content.SharedPreferences;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* User: serso
* Date: 12/25/11
* Time: 1:08 PM
*/
public class FloatPreference extends AbstractPreference<Float> {
protected FloatPreference(@NotNull String key, @Nullable Float defaultValue) {
super(key, defaultValue);
}
@Override
protected Float getPersistedValue(@NotNull SharedPreferences preferences) {
return preferences.getFloat(getKey(), -1f);
}
@Override
protected void putPersistedValue(@NotNull SharedPreferences.Editor editor, @NotNull Float value) {
editor.putFloat(getKey(), value);
}
}

View File

@ -0,0 +1,34 @@
/*
* 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.view.prefs;
import android.content.SharedPreferences;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* User: serso
* Date: 12/25/11
* Time: 12:47 PM
*/
public class IntegerPreference extends AbstractPreference<Integer> {
public IntegerPreference(@NotNull String key, @Nullable Integer defaultValue) {
super(key, defaultValue);
}
@Override
protected Integer getPersistedValue(@NotNull SharedPreferences preferences) {
return preferences.getInt(getKey(), -1);
}
@Override
protected void putPersistedValue(@NotNull SharedPreferences.Editor editor, @NotNull Integer value) {
editor.putInt(getKey(), value);
}
}

View File

@ -0,0 +1,33 @@
/*
* 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.view.prefs;
import android.content.SharedPreferences;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* User: serso
* Date: 12/25/11
* Time: 1:07 PM
*/
public class LongPreference extends AbstractPreference<Long> {
protected LongPreference(@NotNull String key, @Nullable Long defaultValue) {
super(key, defaultValue);
}
@Override
protected Long getPersistedValue(@NotNull SharedPreferences preferences) {
return preferences.getLong(getKey(), -1);
}
@Override
protected void putPersistedValue(@NotNull SharedPreferences.Editor editor, @NotNull Long value) {
editor.putLong(getKey(), value);
}
}

View File

@ -1,20 +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.view.prefs;
import org.jetbrains.annotations.Nullable;
/**
* User: serso
* Date: 9/20/11
* Time: 10:15 PM
*/
public interface PersistenceValueGetter<T> {
@Nullable
T getPersistedValue(@Nullable T defaultValue);
}

View File

@ -1,19 +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.view.prefs;
import org.jetbrains.annotations.Nullable;
/**
* User: serso
* Date: 9/20/11
* Time: 10:14 PM
*/
public interface PersistenceValueSetter<T> {
void persist(@Nullable T value);
}

View File

@ -0,0 +1,32 @@
/*
* 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.view.prefs;
import android.content.SharedPreferences;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* User: serso
* Date: 12/25/11
* Time: 12:21 PM
*/
public interface Preference<T> {
@NotNull
String getKey();
T getDefaultValue();
T getPreference(@NotNull SharedPreferences preferences);
void putPreference(@NotNull SharedPreferences preferences, @Nullable T value);
void putDefault(@NotNull SharedPreferences preferences);
}

View File

@ -0,0 +1,49 @@
/*
* 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.view.prefs;
import android.content.SharedPreferences;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.solovyev.common.utils.Mapper;
import org.solovyev.common.utils.StringMapper;
/**
* User: serso
* Date: 12/25/11
* Time: 12:37 PM
*/
public class StringPreference<T> extends AbstractPreference<T> {
@NotNull
private final Mapper<T> mapper;
public StringPreference(@NotNull String id, @Nullable T defaultValue, @NotNull Mapper<T> mapper) {
super(id, defaultValue);
this.mapper = mapper;
}
@NotNull
public static StringPreference<String> newInstance(@NotNull String id, @Nullable String defaultValue) {
return new StringPreference<String>(id, defaultValue, new StringMapper());
}
@NotNull
public static <T> StringPreference<T> newInstance(@NotNull String id, @Nullable String defaultValue, @NotNull Mapper<T> parser) {
return new StringPreference<T>(id, parser.parseValue(defaultValue), parser);
}
@Override
protected T getPersistedValue(@NotNull SharedPreferences preferences) {
return mapper.parseValue(preferences.getString(getKey(), null));
}
@Override
protected void putPersistedValue(@NotNull SharedPreferences.Editor editor, @NotNull T value) {
editor.putString(getKey(), mapper.formatValue(value));
}
}