Calculations on fly preference

This commit is contained in:
Sergey Solovyev 2012-10-26 22:13:17 +04:00
parent 401ff87e91
commit ef141ed054
6 changed files with 801 additions and 759 deletions

View File

@ -1,13 +1,16 @@
<resources> <resources>
<string name="c_auto_orientation_title">Toggle screen orientation change</string> <string name="c_auto_orientation_title">Toggle screen orientation change</string>
<string name="c_auto_orientation_summary">If turned on calculator will change screen orientation automatically</string> <string name="c_auto_orientation_summary">If turned on calculator will change screen orientation automatically</string>
<string name="c_show_equals_button_title">Show equals button</string> <string name="c_show_equals_button_title">Show equals button</string>
<string name="c_show_equals_button_summary">If turned on equals button is shown</string> <string name="c_show_equals_button_summary">If turned on equals button is shown</string>
<string name="c_hide_numeral_base_digits_title">Hide numeral base digits</string> <string name="c_hide_numeral_base_digits_title">Hide numeral base digits</string>
<string name="c_hide_numeral_base_digits_summary">If turned on numeral base digits of other numeral bases will be hidden</string> <string name="c_hide_numeral_base_digits_summary">If turned on numeral base digits of other numeral bases will be hidden</string>
<string name="prefs_history_show_intermediate_calculations_title">Show intermediate calculations in history</string> <string name="prefs_history_show_intermediate_calculations_title">Show intermediate calculations in history</string>
<string name="prefs_history_show_intermediate_calculations_summary">If turned on all calculations will be shown on history screen</string> <string name="prefs_history_show_intermediate_calculations_summary">If turned on all calculations will be shown on history screen</string>
<string name="p_calculations_calculate_on_fly_title">Result is calculated while typing expression</string>
<string name="p_calculations_calculate_on_fly_summary">If turned on calculations are done automatically while typing expression</string>
</resources> </resources>

View File

@ -1,56 +1,61 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:a="http://schemas.android.com/apk/res/android" <PreferenceScreen xmlns:a="http://schemas.android.com/apk/res/android"
xmlns:range="http://schemas.android.com/apk/res-auto"> xmlns:range="http://schemas.android.com/apk/res-auto">
<PreferenceScreen a:title="@string/c_prefs_calculations_category"> <PreferenceScreen a:title="@string/c_prefs_calculations_category">
<org.solovyev.android.ads.AdViewPreference a:layout="@layout/admob_pref"/> <org.solovyev.android.ads.AdViewPreference a:layout="@layout/admob_pref"/>
<android.preference.CheckBoxPreference <android.preference.CheckBoxPreference
a:key="@string/p_calc_round_result_key" a:key="@string/p_calc_round_result_key"
a:summary="@string/c_calc_round_result_summary" a:summary="@string/c_calc_round_result_summary"
a:title="@string/c_calc_round_result_title" a:title="@string/c_calc_round_result_title"
a:defaultValue="true"/> a:defaultValue="true"/>
<org.solovyev.android.prefs.IntegerPickerDialogPreference <org.solovyev.android.prefs.IntegerPickerDialogPreference
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;16"/> range:boundaries="0;16"/>
<android.preference.CheckBoxPreference <android.preference.CheckBoxPreference
a:key="@string/p_calc_science_notation_key" a:key="@string/p_calc_science_notation_key"
a:summary="@string/c_calc_science_notation_summary" a:summary="@string/c_calc_science_notation_summary"
a:title="@string/c_calc_science_notation_title" a:title="@string/c_calc_science_notation_title"
a:defaultValue="false"/> a:defaultValue="false"/>
<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"
a:entries="@array/p_grouping_separator_names" a:entries="@array/p_grouping_separator_names"
a:summary="@string/c_calc_grouping_separator_summary" a:summary="@string/c_calc_grouping_separator_summary"
a:entryValues="@array/p_grouping_separator_values"/> a:entryValues="@array/p_grouping_separator_values"/>
<ListPreference a:key="@string/p_calc_angle_units_key" <ListPreference a:key="@string/p_calc_angle_units_key"
a:title="@string/c_calc_angle_units" a:title="@string/c_calc_angle_units"
a:entries="@array/p_angle_units_names" a:entries="@array/p_angle_units_names"
a:summary="@string/c_angle_units_summary" a:summary="@string/c_angle_units_summary"
a:entryValues="@array/p_angle_units"/> a:entryValues="@array/p_angle_units"/>
<ListPreference a:key="@string/p_calc_numeral_bases_key" <ListPreference a:key="@string/p_calc_numeral_bases_key"
a:title="@string/c_calc_numeral_bases" a:title="@string/c_calc_numeral_bases"
a:entries="@array/p_numeral_bases_names" a:entries="@array/p_numeral_bases_names"
a:summary="@string/c_numeral_bases_summary" a:summary="@string/c_numeral_bases_summary"
a:entryValues="@array/p_numeral_bases"/> a:entryValues="@array/p_numeral_bases"/>
<org.solovyev.android.prefs.IntegerPickerDialogPreference <org.solovyev.android.prefs.IntegerPickerDialogPreference
a:key="@string/p_calc_max_calculation_time_key" a:key="@string/p_calc_max_calculation_time_key"
a:title="@string/p_calc_max_calculation_time_title" a:title="@string/p_calc_max_calculation_time_title"
a:summary="@string/p_calc_max_calculation_time_summary" a:summary="@string/p_calc_max_calculation_time_summary"
a:defaultValue="5" a:defaultValue="5"
range:boundaries="3;1000"/> range:boundaries="3;1000"/>
</PreferenceScreen> <android.preference.CheckBoxPreference
a:key="calculations_calculate_on_fly"
a:title="@string/p_calculations_calculate_on_fly_title"
a:summary="@string/p_calculations_calculate_on_fly_summary"/>
</PreferenceScreen>
</PreferenceScreen> </PreferenceScreen>

View File

@ -2,7 +2,10 @@ package org.solovyev.android.calculator;
import android.app.Activity; import android.app.Activity;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.app.Application;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.widget.TextView; import android.widget.TextView;
@ -21,13 +24,19 @@ import java.util.List;
* Date: 9/22/12 * Date: 9/22/12
* Time: 5:42 PM * Time: 5:42 PM
*/ */
public class AndroidCalculator implements Calculator, CalculatorEventListener { public class AndroidCalculator implements Calculator, CalculatorEventListener, SharedPreferences.OnSharedPreferenceChangeListener {
@NotNull @NotNull
private final Calculator calculator = new CalculatorImpl(); private final CalculatorImpl calculator = new CalculatorImpl();
public AndroidCalculator() { @NotNull
private final Application context;
public AndroidCalculator(@NotNull Application application) {
this.context = application;
this.calculator.addCalculatorEventListener(this); this.calculator.addCalculatorEventListener(this);
PreferenceManager.getDefaultSharedPreferences(application).registerOnSharedPreferenceChangeListener(this);
} }
public static void showEvaluationError(@NotNull Context context, @NotNull final String errorMessage) { public static void showEvaluationError(@NotNull Context context, @NotNull final String errorMessage) {
@ -121,6 +130,9 @@ public class AndroidCalculator implements Calculator, CalculatorEventListener {
@Override @Override
public void init() { public void init() {
this.calculator.init(); this.calculator.init();
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
this.calculator.setCalculateOnFly(CalculatorPreferences.Calculations.calculateOnFly.getPreference(prefs));
} }
@Override @Override
@ -215,4 +227,11 @@ public class AndroidCalculator implements Calculator, CalculatorEventListener {
break; break;
} }
} }
@Override
public void onSharedPreferenceChanged(@NotNull SharedPreferences prefs, @NotNull String key) {
if ( CalculatorPreferences.Calculations.calculateOnFly.getKey().equals(key) ) {
this.calculator.setCalculateOnFly(CalculatorPreferences.Calculations.calculateOnFly.getPreference(prefs));
}
}
} }

View File

@ -83,7 +83,7 @@ public class CalculatorApplication extends android.app.Application {
super.onCreate(); super.onCreate();
final AndroidCalculator calculator = new AndroidCalculator(); final AndroidCalculator calculator = new AndroidCalculator(this);
CalculatorLocatorImpl.getInstance().init(calculator, CalculatorLocatorImpl.getInstance().init(calculator,
new AndroidCalculatorEngine(this), new AndroidCalculatorEngine(this),

View File

@ -1,189 +1,196 @@
package org.solovyev.android.calculator; package org.solovyev.android.calculator;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.solovyev.android.AndroidUtils; import org.solovyev.android.AndroidUtils;
import org.solovyev.android.calculator.math.MathType; import org.solovyev.android.calculator.math.MathType;
import org.solovyev.android.calculator.model.AndroidCalculatorEngine; import org.solovyev.android.calculator.model.AndroidCalculatorEngine;
import org.solovyev.android.calculator.plot.GraphLineColor; import org.solovyev.android.calculator.plot.GraphLineColor;
import org.solovyev.android.prefs.BooleanPreference; import org.solovyev.android.prefs.BooleanPreference;
import org.solovyev.android.prefs.IntegerPreference; import org.solovyev.android.prefs.IntegerPreference;
import org.solovyev.android.prefs.Preference; import org.solovyev.android.prefs.Preference;
import org.solovyev.android.prefs.StringPreference; import org.solovyev.android.prefs.StringPreference;
import org.solovyev.android.view.VibratorContainer; import org.solovyev.android.view.VibratorContainer;
import java.text.DecimalFormatSymbols; import java.text.DecimalFormatSymbols;
import java.util.Locale; import java.util.Locale;
/** /**
* User: serso * User: serso
* Date: 4/20/12 * Date: 4/20/12
* Time: 12:42 PM * Time: 12:42 PM
*/ */
public final class CalculatorPreferences { public final class CalculatorPreferences {
private CalculatorPreferences() { private CalculatorPreferences() {
throw new AssertionError(); throw new AssertionError();
} }
public static final Preference<Integer> appVersion = new IntegerPreference("application.version", -1); public static final Preference<Integer> appVersion = new IntegerPreference("application.version", -1);
public static final Preference<Integer> appOpenedCounter = new IntegerPreference("app_opened_counter", 0); public static final Preference<Integer> appOpenedCounter = new IntegerPreference("app_opened_counter", 0);
public static class Gui { public static class Calculations {
public static final Preference<Theme> theme = StringPreference.newInstance("org.solovyev.android.calculator.CalculatorActivity_calc_theme", Theme.metro_blue_theme, Theme.class); public static final Preference<Boolean> calculateOnFly = new BooleanPreference("calculations_calculate_on_fly", true);
public static final Preference<Layout> layout = StringPreference.newInstance("org.solovyev.android.calculator.CalculatorActivity_calc_layout", Layout.main_calculator, Layout.class);
public static final Preference<Boolean> feedbackWindowShown = new BooleanPreference("feedback_window_shown", false); }
public static final Preference<Boolean> notesppAnnounceShown = new BooleanPreference("notespp_announce_shown", false);
public static final Preference<Boolean> showReleaseNotes = new BooleanPreference("org.solovyev.android.calculator.CalculatorActivity_show_release_notes", true); public static class Gui {
public static final Preference<Boolean> usePrevAsBack = new BooleanPreference("org.solovyev.android.calculator.CalculatorActivity_use_back_button_as_prev", false);
public static final Preference<Boolean> showEqualsButton = new BooleanPreference("showEqualsButton", true); public static final Preference<Theme> theme = StringPreference.newInstance("org.solovyev.android.calculator.CalculatorActivity_calc_theme", Theme.metro_blue_theme, Theme.class);
public static final Preference<Boolean> autoOrientation = new BooleanPreference("autoOrientation", true); public static final Preference<Layout> layout = StringPreference.newInstance("org.solovyev.android.calculator.CalculatorActivity_calc_layout", Layout.main_calculator, Layout.class);
public static final Preference<Boolean> hideNumeralBaseDigits = new BooleanPreference("hideNumeralBaseDigits", true); public static final Preference<Boolean> feedbackWindowShown = new BooleanPreference("feedback_window_shown", false);
public static final Preference<Boolean> notesppAnnounceShown = new BooleanPreference("notespp_announce_shown", false);
@NotNull public static final Preference<Boolean> showReleaseNotes = new BooleanPreference("org.solovyev.android.calculator.CalculatorActivity_show_release_notes", true);
public static Theme getTheme(@NotNull SharedPreferences preferences) { public static final Preference<Boolean> usePrevAsBack = new BooleanPreference("org.solovyev.android.calculator.CalculatorActivity_use_back_button_as_prev", false);
return theme.getPreferenceNoError(preferences); public static final Preference<Boolean> showEqualsButton = new BooleanPreference("showEqualsButton", true);
} public static final Preference<Boolean> autoOrientation = new BooleanPreference("autoOrientation", true);
public static final Preference<Boolean> hideNumeralBaseDigits = new BooleanPreference("hideNumeralBaseDigits", true);
@NotNull
public static Layout getLayout(@NotNull SharedPreferences preferences) { @NotNull
return layout.getPreferenceNoError(preferences); public static Theme getTheme(@NotNull SharedPreferences preferences) {
} return theme.getPreferenceNoError(preferences);
}
public static enum Theme {
@NotNull
default_theme(ThemeType.other, R.style.default_theme), public static Layout getLayout(@NotNull SharedPreferences preferences) {
violet_theme(ThemeType.other, R.style.violet_theme), return layout.getPreferenceNoError(preferences);
light_blue_theme(ThemeType.other, R.style.light_blue_theme), }
metro_blue_theme(ThemeType.metro, R.style.metro_blue_theme),
metro_purple_theme(ThemeType.metro, R.style.metro_purple_theme), public static enum Theme {
metro_green_theme(ThemeType.metro, R.style.metro_green_theme);
default_theme(ThemeType.other, R.style.default_theme),
@NotNull violet_theme(ThemeType.other, R.style.violet_theme),
private final ThemeType themeType; light_blue_theme(ThemeType.other, R.style.light_blue_theme),
metro_blue_theme(ThemeType.metro, R.style.metro_blue_theme),
@NotNull metro_purple_theme(ThemeType.metro, R.style.metro_purple_theme),
private final Integer themeId; metro_green_theme(ThemeType.metro, R.style.metro_green_theme);
Theme(@NotNull ThemeType themeType, Integer themeId) { @NotNull
this.themeType = themeType; private final ThemeType themeType;
this.themeId = themeId;
} @NotNull
private final Integer themeId;
@NotNull
public ThemeType getThemeType() { Theme(@NotNull ThemeType themeType, Integer themeId) {
return themeType; this.themeType = themeType;
} this.themeId = themeId;
}
@NotNull
public Integer getThemeId() { @NotNull
return themeId; public ThemeType getThemeType() {
} return themeType;
} }
public static enum ThemeType { @NotNull
metro, public Integer getThemeId() {
other return themeId;
} }
}
public static enum Layout {
main_calculator(R.layout.main_calculator), public static enum ThemeType {
metro,
// not used anymore other
@Deprecated }
main_cellphone(R.layout.main_calculator),
public static enum Layout {
simple(R.layout.main_calculator); main_calculator(R.layout.main_calculator),
private final int layoutId; // not used anymore
@Deprecated
Layout(int layoutId) { main_cellphone(R.layout.main_calculator),
this.layoutId = layoutId;
} simple(R.layout.main_calculator);
public int getLayoutId() { private final int layoutId;
return layoutId;
} Layout(int layoutId) {
} this.layoutId = layoutId;
} }
public static class Graph { public int getLayoutId() {
public static final Preference<Boolean> interpolate = new BooleanPreference("graph_interpolate", true); return layoutId;
public static final Preference<GraphLineColor> lineColorReal = StringPreference.newInstance("graph_line_color_real", GraphLineColor.white, GraphLineColor.class); }
public static final Preference<GraphLineColor> lineColorImag = StringPreference.newInstance("graph_line_color_imag", GraphLineColor.blue, GraphLineColor.class); }
} }
public static class History { public static class Graph {
public static final Preference<Boolean> showIntermediateCalculations = new BooleanPreference("history_show_intermediate_calculations", false); public static final Preference<Boolean> interpolate = new BooleanPreference("graph_interpolate", true);
} public static final Preference<GraphLineColor> lineColorReal = StringPreference.newInstance("graph_line_color_real", GraphLineColor.white, GraphLineColor.class);
public static final Preference<GraphLineColor> lineColorImag = StringPreference.newInstance("graph_line_color_imag", GraphLineColor.blue, GraphLineColor.class);
}
static void setDefaultValues(@NotNull SharedPreferences preferences) { public static class History {
if (!AndroidCalculatorEngine.Preferences.groupingSeparator.isSet(preferences)) { public static final Preference<Boolean> showIntermediateCalculations = new BooleanPreference("history_show_intermediate_calculations", false);
final Locale locale = Locale.getDefault(); }
if (locale != null) {
final DecimalFormatSymbols decimalFormatSymbols = new DecimalFormatSymbols(locale);
int index = MathType.grouping_separator.getTokens().indexOf(String.valueOf(decimalFormatSymbols.getGroupingSeparator()));
final String groupingSeparator; static void setDefaultValues(@NotNull SharedPreferences preferences) {
if (index >= 0) { if (!AndroidCalculatorEngine.Preferences.groupingSeparator.isSet(preferences)) {
groupingSeparator = MathType.grouping_separator.getTokens().get(index); final Locale locale = Locale.getDefault();
} else { if (locale != null) {
groupingSeparator = " "; final DecimalFormatSymbols decimalFormatSymbols = new DecimalFormatSymbols(locale);
} int index = MathType.grouping_separator.getTokens().indexOf(String.valueOf(decimalFormatSymbols.getGroupingSeparator()));
final String groupingSeparator;
AndroidCalculatorEngine.Preferences.groupingSeparator.putPreference(preferences, groupingSeparator); if (index >= 0) {
} groupingSeparator = MathType.grouping_separator.getTokens().get(index);
} } else {
groupingSeparator = " ";
if (!AndroidCalculatorEngine.Preferences.angleUnit.isSet(preferences)) { }
AndroidCalculatorEngine.Preferences.angleUnit.putDefault(preferences);
} AndroidCalculatorEngine.Preferences.groupingSeparator.putPreference(preferences, groupingSeparator);
}
if (!AndroidCalculatorEngine.Preferences.numeralBase.isSet(preferences)) { }
AndroidCalculatorEngine.Preferences.numeralBase.putDefault(preferences);
} if (!AndroidCalculatorEngine.Preferences.angleUnit.isSet(preferences)) {
AndroidCalculatorEngine.Preferences.angleUnit.putDefault(preferences);
if (!AndroidCalculatorEngine.Preferences.multiplicationSign.isSet(preferences)) { }
if ( AndroidUtils.isPhoneModel(AndroidUtils.PhoneModel.samsung_galaxy_s) || AndroidUtils.isPhoneModel(AndroidUtils.PhoneModel.samsung_galaxy_s_2) ) {
// workaround ofr samsung galaxy s phones if (!AndroidCalculatorEngine.Preferences.numeralBase.isSet(preferences)) {
AndroidCalculatorEngine.Preferences.multiplicationSign.putPreference(preferences, "*"); AndroidCalculatorEngine.Preferences.numeralBase.putDefault(preferences);
} }
}
if (!AndroidCalculatorEngine.Preferences.multiplicationSign.isSet(preferences)) {
applyDefaultPreference(preferences, Gui.theme); if ( AndroidUtils.isPhoneModel(AndroidUtils.PhoneModel.samsung_galaxy_s) || AndroidUtils.isPhoneModel(AndroidUtils.PhoneModel.samsung_galaxy_s_2) ) {
applyDefaultPreference(preferences, Gui.layout); // workaround ofr samsung galaxy s phones
if ( Gui.layout.getPreference(preferences) == Gui.Layout.main_cellphone ) { AndroidCalculatorEngine.Preferences.multiplicationSign.putPreference(preferences, "*");
Gui.layout.putDefault(preferences); }
} }
applyDefaultPreference(preferences, Gui.feedbackWindowShown);
applyDefaultPreference(preferences, Gui.notesppAnnounceShown); applyDefaultPreference(preferences, Gui.theme);
applyDefaultPreference(preferences, Gui.showReleaseNotes); applyDefaultPreference(preferences, Gui.layout);
applyDefaultPreference(preferences, Gui.usePrevAsBack); if ( Gui.layout.getPreference(preferences) == Gui.Layout.main_cellphone ) {
applyDefaultPreference(preferences, Gui.showEqualsButton); Gui.layout.putDefault(preferences);
applyDefaultPreference(preferences, Gui.autoOrientation); }
applyDefaultPreference(preferences, Gui.hideNumeralBaseDigits); applyDefaultPreference(preferences, Gui.feedbackWindowShown);
applyDefaultPreference(preferences, Gui.notesppAnnounceShown);
applyDefaultPreference(preferences, Graph.interpolate); applyDefaultPreference(preferences, Gui.showReleaseNotes);
applyDefaultPreference(preferences, Graph.lineColorImag); applyDefaultPreference(preferences, Gui.usePrevAsBack);
applyDefaultPreference(preferences, Graph.lineColorReal); applyDefaultPreference(preferences, Gui.showEqualsButton);
applyDefaultPreference(preferences, History.showIntermediateCalculations); applyDefaultPreference(preferences, Gui.autoOrientation);
applyDefaultPreference(preferences, Gui.hideNumeralBaseDigits);
if ( !VibratorContainer.Preferences.hapticFeedbackEnabled.isSet(preferences) ) {
VibratorContainer.Preferences.hapticFeedbackEnabled.putPreference(preferences, true); applyDefaultPreference(preferences, Graph.interpolate);
} applyDefaultPreference(preferences, Graph.lineColorImag);
applyDefaultPreference(preferences, Graph.lineColorReal);
if ( !VibratorContainer.Preferences.hapticFeedbackDuration.isSet(preferences) ) { applyDefaultPreference(preferences, History.showIntermediateCalculations);
VibratorContainer.Preferences.hapticFeedbackDuration.putPreference(preferences, 60L); applyDefaultPreference(preferences, Calculations.calculateOnFly);
}
if ( !VibratorContainer.Preferences.hapticFeedbackEnabled.isSet(preferences) ) {
} VibratorContainer.Preferences.hapticFeedbackEnabled.putPreference(preferences, true);
}
private static void applyDefaultPreference(@NotNull SharedPreferences preferences, @NotNull Preference<?> preference) {
if (!preference.isSet(preferences)) { if ( !VibratorContainer.Preferences.hapticFeedbackDuration.isSet(preferences) ) {
preference.putDefault(preferences); VibratorContainer.Preferences.hapticFeedbackDuration.putPreference(preferences, 60L);
} }
}
}
}
private static void applyDefaultPreference(@NotNull SharedPreferences preferences, @NotNull Preference<?> preference) {
if (!preference.isSet(preferences)) {
preference.putDefault(preferences);
}
}
}