From 1dbec40d120b2527d9057e4b60f30fb5ba935f55 Mon Sep 17 00:00:00 2001 From: Sergey Solovyev Date: Sun, 25 Dec 2011 15:00:09 +0400 Subject: [PATCH] preferences object --- res/layout/calc_multiplication_button.xml | 2 +- res/values-ru/text_about.xml | 4 +- res/values-ru/text_strings.xml | 4 +- .../{view/prefs => }/AndroidUtils.java | 2 +- .../{view/prefs => }/ResourceCache.java | 2 +- .../calculator/CalculatorActivity.java | 40 +++++------ .../CalculatorPreferencesActivity.java | 6 +- .../about/CalculatorAboutTabActivity.java | 2 +- .../about/CalculatorReleaseNotesActivity.java | 5 +- .../help/CalculatorHelpTabActivity.java | 3 +- .../history/CalculatorHistoryActivity.java | 3 +- .../history/HistoryArrayAdapter.java | 2 - .../edit/CalculatorFunctionsActivity.java | 3 +- .../math/edit/CalculatorVarsActivity.java | 3 +- .../model/AbstractAndroidMathRegistry.java | 2 +- .../model/AndroidVarsRegistryImpl.java | 5 +- .../calculator/model/CalculatorEngine.java | 67 +++++++++++++------ .../solovyev/android/msg/AndroidMessage.java | 2 +- .../view/prefs/AbstractPreference.java | 66 ++++++++++++++++++ .../android/view/prefs/BooleanPreference.java | 33 +++++++++ .../android/view/prefs/EnumMapper.java | 40 +++++++++++ .../android/view/prefs/FloatPreference.java | 33 +++++++++ .../android/view/prefs/IntegerPreference.java | 34 ++++++++++ .../android/view/prefs/LongPreference.java | 33 +++++++++ .../view/prefs/PersistenceValueGetter.java | 20 ------ .../view/prefs/PersistenceValueSetter.java | 19 ------ .../android/view/prefs/Preference.java | 32 +++++++++ .../android/view/prefs/StringPreference.java | 49 ++++++++++++++ 28 files changed, 410 insertions(+), 106 deletions(-) rename src/main/java/org/solovyev/android/{view/prefs => }/AndroidUtils.java (98%) rename src/main/java/org/solovyev/android/{view/prefs => }/ResourceCache.java (99%) create mode 100644 src/main/java/org/solovyev/android/view/prefs/AbstractPreference.java create mode 100644 src/main/java/org/solovyev/android/view/prefs/BooleanPreference.java create mode 100644 src/main/java/org/solovyev/android/view/prefs/EnumMapper.java create mode 100644 src/main/java/org/solovyev/android/view/prefs/FloatPreference.java create mode 100644 src/main/java/org/solovyev/android/view/prefs/IntegerPreference.java create mode 100644 src/main/java/org/solovyev/android/view/prefs/LongPreference.java delete mode 100644 src/main/java/org/solovyev/android/view/prefs/PersistenceValueGetter.java delete mode 100644 src/main/java/org/solovyev/android/view/prefs/PersistenceValueSetter.java create mode 100644 src/main/java/org/solovyev/android/view/prefs/Preference.java create mode 100644 src/main/java/org/solovyev/android/view/prefs/StringPreference.java diff --git a/res/layout/calc_multiplication_button.xml b/res/layout/calc_multiplication_button.xml index 603204b6..a733692c 100644 --- a/res/layout/calc_multiplication_button.xml +++ b/res/layout/calc_multiplication_button.xml @@ -12,7 +12,7 @@ a:text="×" c:textUp="^" c:textDown="^2" - c:textLeft="PI" + c:textLeft="Π" style="?digitButtonStyle" c:directionTextScale="0.5" a:onClick="digitButtonClickHandler"/> \ No newline at end of file diff --git a/res/values-ru/text_about.xml b/res/values-ru/text_about.xml index 6f818919..3b9c7605 100644 --- a/res/values-ru/text_about.xml +++ b/res/values-ru/text_about.xml @@ -23,8 +23,8 @@ AChartEngine - Замечания к выпуску - "Замечания к выпуску версии " + Примечания к выпуску + "Примечания к выпуску версии " 1. Отклик на нажатие\n diff --git a/res/values-ru/text_strings.xml b/res/values-ru/text_strings.xml index 13440b6d..89db23c9 100644 --- a/res/values-ru/text_strings.xml +++ b/res/values-ru/text_strings.xml @@ -156,8 +156,8 @@ Спасибо за выбор Калькулятора++!\n\nКалькулятор++ - это мощный инструмент для проведения ежедневных расчётов.\n\nПеред началом работы рекомендуется прочитать FAQ и подсказки для использования всех возможностей программы (нажмите Меню, затем Помощь) Приветствие - Показывать замечания к выпуску - Определяет показывать или нет всплывающее окно с замечаниями к выпуску после обновления новой версии + Показывать примечания к выпуску + Определяет показывать или нет всплывающее окно с примечаниями к выпуску после обновления новой версии Устанавливает поведение по нажатию на кнопку Назад Использовать кнопку назад как назад по истории diff --git a/src/main/java/org/solovyev/android/view/prefs/AndroidUtils.java b/src/main/java/org/solovyev/android/AndroidUtils.java similarity index 98% rename from src/main/java/org/solovyev/android/view/prefs/AndroidUtils.java rename to src/main/java/org/solovyev/android/AndroidUtils.java index 4827aa8c..7275a27b 100644 --- a/src/main/java/org/solovyev/android/view/prefs/AndroidUtils.java +++ b/src/main/java/org/solovyev/android/AndroidUtils.java @@ -4,7 +4,7 @@ * or visit http://se.solovyev.org */ -package org.solovyev.android.view.prefs; +package org.solovyev.android; import android.app.Activity; import android.content.Context; diff --git a/src/main/java/org/solovyev/android/view/prefs/ResourceCache.java b/src/main/java/org/solovyev/android/ResourceCache.java similarity index 99% rename from src/main/java/org/solovyev/android/view/prefs/ResourceCache.java rename to src/main/java/org/solovyev/android/ResourceCache.java index 1a3276fb..dd3b82c6 100644 --- a/src/main/java/org/solovyev/android/view/prefs/ResourceCache.java +++ b/src/main/java/org/solovyev/android/ResourceCache.java @@ -4,7 +4,7 @@ * or visit http://se.solovyev.org */ -package org.solovyev.android.view.prefs; +package org.solovyev.android; import android.app.Activity; import android.content.Context; diff --git a/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java b/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java index 8ddf54fa..038ab4f1 100644 --- a/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java +++ b/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java @@ -27,14 +27,14 @@ import jscl.AngleUnit; import jscl.NumeralBase; import org.jetbrains.annotations.NotNull; 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.history.CalculatorHistory; import org.solovyev.android.calculator.history.CalculatorHistoryState; import org.solovyev.android.calculator.math.MathType; import org.solovyev.android.calculator.model.CalculatorEngine; 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.common.utils.Announcer; 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 SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(CalculatorActivity.this); - final SharedPreferences.Editor editor = preferences.edit(); - editor.putString(CalculatorEngine.ANGLE_UNITS_P_KEY, angleUnits.name()); - editor.commit(); + + CalculatorEngine.Preferences.angleUnit.putPreference(preferences, angleUnits); result = true; } catch (IllegalArgumentException e) { @@ -229,9 +228,7 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh final NumeralBase numeralBase = NumeralBase.valueOf(directionText); final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(CalculatorActivity.this); - final SharedPreferences.Editor editor = preferences.edit(); - editor.putString(CalculatorEngine.NUMERAL_BASES_P_KEY, numeralBase.name()); - editor.commit(); + CalculatorEngine.Preferences.numeralBase.putPreference(preferences, numeralBase); result = true; } catch (IllegalArgumentException e) { @@ -264,7 +261,7 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh } 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(); if (locale != null) { final DecimalFormatSymbols decimalFormatSymbols = new DecimalFormatSymbols(locale); @@ -275,17 +272,19 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh } else { groupingSeparator = " "; } - final SharedPreferences.Editor editor = preferences.edit(); - editor.putString(CalculatorEngine.GROUPING_SEPARATOR_P_KEY, groupingSeparator); - editor.commit(); + + CalculatorEngine.Preferences.groupingSeparator.putPreference(preferences, groupingSeparator); } } - if (!preferences.contains(CalculatorEngine.ANGLE_UNITS_P_KEY)) { - final SharedPreferences.Editor editor = preferences.edit(); - editor.putString(CalculatorEngine.ANGLE_UNITS_P_KEY, CalculatorEngine.ANGLE_UNITS_DEFAULT); - editor.commit(); + if (!preferences.contains(CalculatorEngine.Preferences.angleUnit.getKey())) { + CalculatorEngine.Preferences.angleUnit.putDefault(preferences); } + + if (!preferences.contains(CalculatorEngine.Preferences.numeralBase.getKey())) { + CalculatorEngine.Preferences.numeralBase.putDefault(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)); } - if (CalculatorEngine.GROUPING_SEPARATOR_P_KEY.equals(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)) { + if (CalculatorEngine.Preferences.getPreferenceKeys().contains(key)) { CalculatorEngine.instance.reset(this, preferences); 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); } - if ( CalculatorEngine.MULTIPLICATION_SIGN_P_KEY.equals(key) ) { + if ( CalculatorEngine.Preferences.multiplicationSign.getKey().equals(key) ) { initMultiplicationButton(); } } diff --git a/src/main/java/org/solovyev/android/calculator/CalculatorPreferencesActivity.java b/src/main/java/org/solovyev/android/calculator/CalculatorPreferencesActivity.java index d1565abc..41a022c2 100644 --- a/src/main/java/org/solovyev/android/calculator/CalculatorPreferencesActivity.java +++ b/src/main/java/org/solovyev/android/calculator/CalculatorPreferencesActivity.java @@ -26,14 +26,14 @@ public class CalculatorPreferencesActivity extends PreferenceActivity implements final SharedPreferences preferences = getPreferenceManager().getSharedPreferences(); preferences.registerOnSharedPreferenceChangeListener(this); - onSharedPreferenceChanged(preferences, CalculatorEngine.ROUND_RESULT_P_KEY); + onSharedPreferenceChanged(preferences, CalculatorEngine.Preferences.roundResult.getKey()); onSharedPreferenceChanged(preferences, VibratorContainer.HAPTIC_FEEDBACK_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, CalculatorEngine.ROUND_RESULT_DEFAULT)); + if (CalculatorEngine.Preferences.roundResult.getKey().equals(key)) { + findPreference(CalculatorEngine.Preferences.roundResult.getKey()).setEnabled(preferences.getBoolean(key, CalculatorEngine.Preferences.roundResult.getDefaultValue())); } else if (VibratorContainer.HAPTIC_FEEDBACK_P_KEY.equals(key)) { findPreference(VibratorContainer.HAPTIC_FEEDBACK_DURATION_P_KEY).setEnabled(preferences.getBoolean(key, VibratorContainer.HAPTIC_FEEDBACK_DEFAULT)); } diff --git a/src/main/java/org/solovyev/android/calculator/about/CalculatorAboutTabActivity.java b/src/main/java/org/solovyev/android/calculator/about/CalculatorAboutTabActivity.java index 17ee9195..e91a18fa 100644 --- a/src/main/java/org/solovyev/android/calculator/about/CalculatorAboutTabActivity.java +++ b/src/main/java/org/solovyev/android/calculator/about/CalculatorAboutTabActivity.java @@ -9,8 +9,8 @@ import android.app.TabActivity; import android.os.Bundle; import android.widget.TabHost; import org.jetbrains.annotations.Nullable; +import org.solovyev.android.AndroidUtils; import org.solovyev.android.calculator.R; -import org.solovyev.android.view.prefs.AndroidUtils; /** * User: serso diff --git a/src/main/java/org/solovyev/android/calculator/about/CalculatorReleaseNotesActivity.java b/src/main/java/org/solovyev/android/calculator/about/CalculatorReleaseNotesActivity.java index f1b42eb6..7aa4cd44 100644 --- a/src/main/java/org/solovyev/android/calculator/about/CalculatorReleaseNotesActivity.java +++ b/src/main/java/org/solovyev/android/calculator/about/CalculatorReleaseNotesActivity.java @@ -13,10 +13,11 @@ import android.text.Html; import android.text.method.LinkMovementMethod; import android.widget.TextView; 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.R; -import org.solovyev.android.view.prefs.AndroidUtils; -import org.solovyev.android.view.prefs.ResourceCache; +import org.solovyev.android.AndroidUtils; import org.solovyev.common.utils.StringUtils; /** diff --git a/src/main/java/org/solovyev/android/calculator/help/CalculatorHelpTabActivity.java b/src/main/java/org/solovyev/android/calculator/help/CalculatorHelpTabActivity.java index 25dc9e56..566aa6d1 100644 --- a/src/main/java/org/solovyev/android/calculator/help/CalculatorHelpTabActivity.java +++ b/src/main/java/org/solovyev/android/calculator/help/CalculatorHelpTabActivity.java @@ -13,8 +13,9 @@ import android.os.Bundle; import android.widget.TabHost; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import org.solovyev.android.AndroidUtils; import org.solovyev.android.calculator.R; -import org.solovyev.android.view.prefs.AndroidUtils; +import org.solovyev.android.AndroidUtils; /** * User: serso diff --git a/src/main/java/org/solovyev/android/calculator/history/CalculatorHistoryActivity.java b/src/main/java/org/solovyev/android/calculator/history/CalculatorHistoryActivity.java index 9c4410cc..ca86b9a2 100644 --- a/src/main/java/org/solovyev/android/calculator/history/CalculatorHistoryActivity.java +++ b/src/main/java/org/solovyev/android/calculator/history/CalculatorHistoryActivity.java @@ -10,8 +10,9 @@ import android.app.TabActivity; import android.os.Bundle; import android.widget.TabHost; import org.jetbrains.annotations.Nullable; +import org.solovyev.android.AndroidUtils; import org.solovyev.android.calculator.R; -import org.solovyev.android.view.prefs.AndroidUtils; +import org.solovyev.android.AndroidUtils; /** * User: serso diff --git a/src/main/java/org/solovyev/android/calculator/history/HistoryArrayAdapter.java b/src/main/java/org/solovyev/android/calculator/history/HistoryArrayAdapter.java index 4a0ac87c..46f57c31 100644 --- a/src/main/java/org/solovyev/android/calculator/history/HistoryArrayAdapter.java +++ b/src/main/java/org/solovyev/android/calculator/history/HistoryArrayAdapter.java @@ -7,14 +7,12 @@ package org.solovyev.android.calculator.history; import android.content.Context; -import android.content.res.Resources; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.TextView; import org.jetbrains.annotations.NotNull; import org.solovyev.android.calculator.R; -import org.solovyev.android.view.prefs.ResourceCache; import org.solovyev.common.utils.StringUtils; import java.text.SimpleDateFormat; diff --git a/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorFunctionsActivity.java b/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorFunctionsActivity.java index 4f616cea..926cfcd7 100644 --- a/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorFunctionsActivity.java +++ b/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorFunctionsActivity.java @@ -10,9 +10,10 @@ import android.app.TabActivity; import android.os.Bundle; import android.widget.TabHost; import org.jetbrains.annotations.Nullable; +import org.solovyev.android.AndroidUtils; import org.solovyev.android.calculator.R; import org.solovyev.android.calculator.model.AndroidFunctionsMathRegistry; -import org.solovyev.android.view.prefs.AndroidUtils; +import org.solovyev.android.AndroidUtils; /** * User: serso diff --git a/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorVarsActivity.java b/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorVarsActivity.java index 4ad8e5bb..f6834862 100644 --- a/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorVarsActivity.java +++ b/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorVarsActivity.java @@ -10,9 +10,10 @@ import android.app.TabActivity; import android.os.Bundle; import android.widget.TabHost; import org.jetbrains.annotations.Nullable; +import org.solovyev.android.AndroidUtils; import org.solovyev.android.calculator.R; import org.solovyev.android.calculator.model.VarCategory; -import org.solovyev.android.view.prefs.AndroidUtils; +import org.solovyev.android.AndroidUtils; /** * User: serso diff --git a/src/main/java/org/solovyev/android/calculator/model/AbstractAndroidMathRegistry.java b/src/main/java/org/solovyev/android/calculator/model/AbstractAndroidMathRegistry.java index 84637142..8fa62fb3 100644 --- a/src/main/java/org/solovyev/android/calculator/model/AbstractAndroidMathRegistry.java +++ b/src/main/java/org/solovyev/android/calculator/model/AbstractAndroidMathRegistry.java @@ -13,7 +13,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.simpleframework.xml.Serializer; 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.math.MathEntity; import org.solovyev.common.math.MathRegistry; diff --git a/src/main/java/org/solovyev/android/calculator/model/AndroidVarsRegistryImpl.java b/src/main/java/org/solovyev/android/calculator/model/AndroidVarsRegistryImpl.java index 96dfb8bb..43b80b7f 100644 --- a/src/main/java/org/solovyev/android/calculator/model/AndroidVarsRegistryImpl.java +++ b/src/main/java/org/solovyev/android/calculator/model/AndroidVarsRegistryImpl.java @@ -8,17 +8,13 @@ package org.solovyev.android.calculator.model; import android.content.Context; import android.content.SharedPreferences; -import android.preference.PreferenceManager; import jscl.math.function.IConstant; import org.jetbrains.annotations.NotNull; 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.common.definitions.IBuilder; import org.solovyev.common.math.MathRegistry; -import java.io.StringWriter; import java.util.HashMap; import java.util.Map; @@ -33,6 +29,7 @@ class AndroidVarsRegistryImpl extends AbstractAndroidMathRegistry substitutes = new HashMap(); static { substitutes.put("π", "pi"); + substitutes.put("Π", "PI"); substitutes.put("∞", "inf"); substitutes.put("h", "h_reduced"); substitutes.put("NaN", "nan"); 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 a6d64424..33555938 100644 --- a/src/main/java/org/solovyev/android/calculator/model/CalculatorEngine.java +++ b/src/main/java/org/solovyev/android/calculator/model/CalculatorEngine.java @@ -16,12 +16,19 @@ import jscl.text.ParseInterruptedException; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; 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.msg.MessageRegistry; import org.solovyev.common.utils.MutableObject; import org.solovyev.common.utils.StringUtils; 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.TimeUnit; @@ -35,22 +42,46 @@ public enum CalculatorEngine { 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"; - public static final String MULTIPLICATION_SIGN_DEFAULT = "×"; + private static final String MULTIPLICATION_SIGN_P_KEY = "org.solovyev.android.calculator.CalculatorActivity_calc_multiplication_sign"; + private static final String MULTIPLICATION_SIGN_DEFAULT = "×"; - public 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 String ROUND_RESULT_P_KEY = "org.solovyev.android.calculator.CalculatorModel_round_result"; + private 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"; + private static final String RESULT_PRECISION_P_KEY = "org.solovyev.android.calculator.CalculatorModel_result_precision"; + private static final String RESULT_PRECISION_DEFAULT = "5"; - public 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_P_KEY = "org.solovyev.android.calculator.CalculatorActivity_numeral_bases"; + private static final String NUMERAL_BASES_DEFAULT = "dec"; - public 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_P_KEY = "org.solovyev.android.calculator.CalculatorActivity_angle_units"; + private static final String ANGLE_UNITS_DEFAULT = "deg"; + + public static class Preferences { + public static final Preference groupingSeparator = StringPreference.newInstance(GROUPING_SEPARATOR_P_KEY, JsclMathEngine.GROUPING_SEPARATOR_DEFAULT); + public static final Preference multiplicationSign = StringPreference.newInstance(MULTIPLICATION_SIGN_P_KEY, MULTIPLICATION_SIGN_DEFAULT); + public static final Preference precision = StringPreference.newInstance(RESULT_PRECISION_P_KEY, RESULT_PRECISION_DEFAULT, new NumberMapper(Integer.class)); + public static final Preference roundResult = new BooleanPreference(ROUND_RESULT_P_KEY, ROUND_RESULT_DEFAULT); + public static final Preference numeralBase = StringPreference.newInstance(NUMERAL_BASES_P_KEY, NUMERAL_BASES_DEFAULT, EnumMapper.newInstance(NumeralBase.class)); + public static final Preference angleUnit = StringPreference.newInstance(ANGLE_UNITS_P_KEY, ANGLE_UNITS_DEFAULT, EnumMapper.newInstance(AngleUnit.class)); + + private static final List preferenceKeys = new ArrayList(); + 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 getPreferenceKeys() { + return Collections.unmodifiableList(preferenceKeys); + } + } public static final int DEFAULT_TIMEOUT = 3000; @@ -266,15 +297,13 @@ public enum CalculatorEngine { public void reset(@Nullable Context context, @Nullable SharedPreferences preferences) { synchronized (lock) { if (preferences != null) { - final NumberMapper integerNumberMapper = new NumberMapper(Integer.class); - //noinspection ConstantConditions - 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.setPrecision(Preferences.precision.getPreference(preferences)); + this.setRoundResult(Preferences.roundResult.getPreference(preferences)); this.setAngleUnits(getAngleUnitsFromPrefs(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)) { this.getEngine().setUseGroupingSeparator(false); } else { @@ -292,12 +321,12 @@ public enum CalculatorEngine { @NotNull public NumeralBase getNumeralBaseFromPrefs(@NotNull SharedPreferences preferences) { - return NumeralBase.valueOf(preferences.getString(NUMERAL_BASES_P_KEY, NUMERAL_BASES_DEFAULT)); + return Preferences.numeralBase.getPreference(preferences); } @NotNull 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 diff --git a/src/main/java/org/solovyev/android/msg/AndroidMessage.java b/src/main/java/org/solovyev/android/msg/AndroidMessage.java index ca8e1757..a5a232ef 100644 --- a/src/main/java/org/solovyev/android/msg/AndroidMessage.java +++ b/src/main/java/org/solovyev/android/msg/AndroidMessage.java @@ -7,7 +7,7 @@ package org.solovyev.android.msg; 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.MessageType; diff --git a/src/main/java/org/solovyev/android/view/prefs/AbstractPreference.java b/src/main/java/org/solovyev/android/view/prefs/AbstractPreference.java new file mode 100644 index 00000000..bf2f16a9 --- /dev/null +++ b/src/main/java/org/solovyev/android/view/prefs/AbstractPreference.java @@ -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 implements Preference { + + @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); +} diff --git a/src/main/java/org/solovyev/android/view/prefs/BooleanPreference.java b/src/main/java/org/solovyev/android/view/prefs/BooleanPreference.java new file mode 100644 index 00000000..fde4674d --- /dev/null +++ b/src/main/java/org/solovyev/android/view/prefs/BooleanPreference.java @@ -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{ + + 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); + } +} diff --git a/src/main/java/org/solovyev/android/view/prefs/EnumMapper.java b/src/main/java/org/solovyev/android/view/prefs/EnumMapper.java new file mode 100644 index 00000000..ece18e45 --- /dev/null +++ b/src/main/java/org/solovyev/android/view/prefs/EnumMapper.java @@ -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 implements Mapper{ + + @NotNull + private final Class enumClass; + + public EnumMapper(@NotNull Class enumClass) { + this.enumClass = enumClass; + } + + public static Mapper newInstance(@NotNull Class enumClass) { + return new EnumMapper(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); + } +} diff --git a/src/main/java/org/solovyev/android/view/prefs/FloatPreference.java b/src/main/java/org/solovyev/android/view/prefs/FloatPreference.java new file mode 100644 index 00000000..9caf6ee6 --- /dev/null +++ b/src/main/java/org/solovyev/android/view/prefs/FloatPreference.java @@ -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 { + + 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); + } +} diff --git a/src/main/java/org/solovyev/android/view/prefs/IntegerPreference.java b/src/main/java/org/solovyev/android/view/prefs/IntegerPreference.java new file mode 100644 index 00000000..658776fe --- /dev/null +++ b/src/main/java/org/solovyev/android/view/prefs/IntegerPreference.java @@ -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 { + + 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); + } + +} diff --git a/src/main/java/org/solovyev/android/view/prefs/LongPreference.java b/src/main/java/org/solovyev/android/view/prefs/LongPreference.java new file mode 100644 index 00000000..29989775 --- /dev/null +++ b/src/main/java/org/solovyev/android/view/prefs/LongPreference.java @@ -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 { + + 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); + } +} diff --git a/src/main/java/org/solovyev/android/view/prefs/PersistenceValueGetter.java b/src/main/java/org/solovyev/android/view/prefs/PersistenceValueGetter.java deleted file mode 100644 index 69749f07..00000000 --- a/src/main/java/org/solovyev/android/view/prefs/PersistenceValueGetter.java +++ /dev/null @@ -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 { - - @Nullable - T getPersistedValue(@Nullable T defaultValue); -} diff --git a/src/main/java/org/solovyev/android/view/prefs/PersistenceValueSetter.java b/src/main/java/org/solovyev/android/view/prefs/PersistenceValueSetter.java deleted file mode 100644 index 63acddbe..00000000 --- a/src/main/java/org/solovyev/android/view/prefs/PersistenceValueSetter.java +++ /dev/null @@ -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 { - - void persist(@Nullable T value); -} diff --git a/src/main/java/org/solovyev/android/view/prefs/Preference.java b/src/main/java/org/solovyev/android/view/prefs/Preference.java new file mode 100644 index 00000000..b250b0a8 --- /dev/null +++ b/src/main/java/org/solovyev/android/view/prefs/Preference.java @@ -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 { + + @NotNull + String getKey(); + + T getDefaultValue(); + + T getPreference(@NotNull SharedPreferences preferences); + + void putPreference(@NotNull SharedPreferences preferences, @Nullable T value); + + void putDefault(@NotNull SharedPreferences preferences); + +} diff --git a/src/main/java/org/solovyev/android/view/prefs/StringPreference.java b/src/main/java/org/solovyev/android/view/prefs/StringPreference.java new file mode 100644 index 00000000..de37604b --- /dev/null +++ b/src/main/java/org/solovyev/android/view/prefs/StringPreference.java @@ -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 extends AbstractPreference { + + @NotNull + private final Mapper mapper; + + public StringPreference(@NotNull String id, @Nullable T defaultValue, @NotNull Mapper mapper) { + super(id, defaultValue); + this.mapper = mapper; + } + + @NotNull + public static StringPreference newInstance(@NotNull String id, @Nullable String defaultValue) { + return new StringPreference(id, defaultValue, new StringMapper()); + } + + @NotNull + public static StringPreference newInstance(@NotNull String id, @Nullable String defaultValue, @NotNull Mapper parser) { + return new StringPreference(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)); + } +}