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>
<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_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_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="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>
<resources>
<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_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_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="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="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>

View File

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

View File

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

View File

@ -1,189 +1,196 @@
package org.solovyev.android.calculator;
import android.content.SharedPreferences;
import org.jetbrains.annotations.NotNull;
import org.solovyev.android.AndroidUtils;
import org.solovyev.android.calculator.math.MathType;
import org.solovyev.android.calculator.model.AndroidCalculatorEngine;
import org.solovyev.android.calculator.plot.GraphLineColor;
import org.solovyev.android.prefs.BooleanPreference;
import org.solovyev.android.prefs.IntegerPreference;
import org.solovyev.android.prefs.Preference;
import org.solovyev.android.prefs.StringPreference;
import org.solovyev.android.view.VibratorContainer;
import java.text.DecimalFormatSymbols;
import java.util.Locale;
/**
* User: serso
* Date: 4/20/12
* Time: 12:42 PM
*/
public final class CalculatorPreferences {
private CalculatorPreferences() {
throw new AssertionError();
}
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 class Gui {
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<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 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<Boolean> autoOrientation = new BooleanPreference("autoOrientation", true);
public static final Preference<Boolean> hideNumeralBaseDigits = new BooleanPreference("hideNumeralBaseDigits", true);
@NotNull
public static Theme getTheme(@NotNull SharedPreferences preferences) {
return theme.getPreferenceNoError(preferences);
}
@NotNull
public static Layout getLayout(@NotNull SharedPreferences preferences) {
return layout.getPreferenceNoError(preferences);
}
public static enum Theme {
default_theme(ThemeType.other, R.style.default_theme),
violet_theme(ThemeType.other, R.style.violet_theme),
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),
metro_green_theme(ThemeType.metro, R.style.metro_green_theme);
@NotNull
private final ThemeType themeType;
@NotNull
private final Integer themeId;
Theme(@NotNull ThemeType themeType, Integer themeId) {
this.themeType = themeType;
this.themeId = themeId;
}
@NotNull
public ThemeType getThemeType() {
return themeType;
}
@NotNull
public Integer getThemeId() {
return themeId;
}
}
public static enum ThemeType {
metro,
other
}
public static enum Layout {
main_calculator(R.layout.main_calculator),
// not used anymore
@Deprecated
main_cellphone(R.layout.main_calculator),
simple(R.layout.main_calculator);
private final int layoutId;
Layout(int layoutId) {
this.layoutId = layoutId;
}
public int getLayoutId() {
return layoutId;
}
}
}
public static class Graph {
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);
}
public static class History {
public static final Preference<Boolean> showIntermediateCalculations = new BooleanPreference("history_show_intermediate_calculations", false);
}
static void setDefaultValues(@NotNull SharedPreferences preferences) {
if (!AndroidCalculatorEngine.Preferences.groupingSeparator.isSet(preferences)) {
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;
if (index >= 0) {
groupingSeparator = MathType.grouping_separator.getTokens().get(index);
} else {
groupingSeparator = " ";
}
AndroidCalculatorEngine.Preferences.groupingSeparator.putPreference(preferences, groupingSeparator);
}
}
if (!AndroidCalculatorEngine.Preferences.angleUnit.isSet(preferences)) {
AndroidCalculatorEngine.Preferences.angleUnit.putDefault(preferences);
}
if (!AndroidCalculatorEngine.Preferences.numeralBase.isSet(preferences)) {
AndroidCalculatorEngine.Preferences.numeralBase.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
AndroidCalculatorEngine.Preferences.multiplicationSign.putPreference(preferences, "*");
}
}
applyDefaultPreference(preferences, Gui.theme);
applyDefaultPreference(preferences, Gui.layout);
if ( Gui.layout.getPreference(preferences) == Gui.Layout.main_cellphone ) {
Gui.layout.putDefault(preferences);
}
applyDefaultPreference(preferences, Gui.feedbackWindowShown);
applyDefaultPreference(preferences, Gui.notesppAnnounceShown);
applyDefaultPreference(preferences, Gui.showReleaseNotes);
applyDefaultPreference(preferences, Gui.usePrevAsBack);
applyDefaultPreference(preferences, Gui.showEqualsButton);
applyDefaultPreference(preferences, Gui.autoOrientation);
applyDefaultPreference(preferences, Gui.hideNumeralBaseDigits);
applyDefaultPreference(preferences, Graph.interpolate);
applyDefaultPreference(preferences, Graph.lineColorImag);
applyDefaultPreference(preferences, Graph.lineColorReal);
applyDefaultPreference(preferences, History.showIntermediateCalculations);
if ( !VibratorContainer.Preferences.hapticFeedbackEnabled.isSet(preferences) ) {
VibratorContainer.Preferences.hapticFeedbackEnabled.putPreference(preferences, true);
}
if ( !VibratorContainer.Preferences.hapticFeedbackDuration.isSet(preferences) ) {
VibratorContainer.Preferences.hapticFeedbackDuration.putPreference(preferences, 60L);
}
}
private static void applyDefaultPreference(@NotNull SharedPreferences preferences, @NotNull Preference<?> preference) {
if (!preference.isSet(preferences)) {
preference.putDefault(preferences);
}
}
}
package org.solovyev.android.calculator;
import android.content.SharedPreferences;
import org.jetbrains.annotations.NotNull;
import org.solovyev.android.AndroidUtils;
import org.solovyev.android.calculator.math.MathType;
import org.solovyev.android.calculator.model.AndroidCalculatorEngine;
import org.solovyev.android.calculator.plot.GraphLineColor;
import org.solovyev.android.prefs.BooleanPreference;
import org.solovyev.android.prefs.IntegerPreference;
import org.solovyev.android.prefs.Preference;
import org.solovyev.android.prefs.StringPreference;
import org.solovyev.android.view.VibratorContainer;
import java.text.DecimalFormatSymbols;
import java.util.Locale;
/**
* User: serso
* Date: 4/20/12
* Time: 12:42 PM
*/
public final class CalculatorPreferences {
private CalculatorPreferences() {
throw new AssertionError();
}
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 class Calculations {
public static final Preference<Boolean> calculateOnFly = new BooleanPreference("calculations_calculate_on_fly", true);
}
public static class Gui {
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<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 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<Boolean> autoOrientation = new BooleanPreference("autoOrientation", true);
public static final Preference<Boolean> hideNumeralBaseDigits = new BooleanPreference("hideNumeralBaseDigits", true);
@NotNull
public static Theme getTheme(@NotNull SharedPreferences preferences) {
return theme.getPreferenceNoError(preferences);
}
@NotNull
public static Layout getLayout(@NotNull SharedPreferences preferences) {
return layout.getPreferenceNoError(preferences);
}
public static enum Theme {
default_theme(ThemeType.other, R.style.default_theme),
violet_theme(ThemeType.other, R.style.violet_theme),
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),
metro_green_theme(ThemeType.metro, R.style.metro_green_theme);
@NotNull
private final ThemeType themeType;
@NotNull
private final Integer themeId;
Theme(@NotNull ThemeType themeType, Integer themeId) {
this.themeType = themeType;
this.themeId = themeId;
}
@NotNull
public ThemeType getThemeType() {
return themeType;
}
@NotNull
public Integer getThemeId() {
return themeId;
}
}
public static enum ThemeType {
metro,
other
}
public static enum Layout {
main_calculator(R.layout.main_calculator),
// not used anymore
@Deprecated
main_cellphone(R.layout.main_calculator),
simple(R.layout.main_calculator);
private final int layoutId;
Layout(int layoutId) {
this.layoutId = layoutId;
}
public int getLayoutId() {
return layoutId;
}
}
}
public static class Graph {
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);
}
public static class History {
public static final Preference<Boolean> showIntermediateCalculations = new BooleanPreference("history_show_intermediate_calculations", false);
}
static void setDefaultValues(@NotNull SharedPreferences preferences) {
if (!AndroidCalculatorEngine.Preferences.groupingSeparator.isSet(preferences)) {
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;
if (index >= 0) {
groupingSeparator = MathType.grouping_separator.getTokens().get(index);
} else {
groupingSeparator = " ";
}
AndroidCalculatorEngine.Preferences.groupingSeparator.putPreference(preferences, groupingSeparator);
}
}
if (!AndroidCalculatorEngine.Preferences.angleUnit.isSet(preferences)) {
AndroidCalculatorEngine.Preferences.angleUnit.putDefault(preferences);
}
if (!AndroidCalculatorEngine.Preferences.numeralBase.isSet(preferences)) {
AndroidCalculatorEngine.Preferences.numeralBase.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
AndroidCalculatorEngine.Preferences.multiplicationSign.putPreference(preferences, "*");
}
}
applyDefaultPreference(preferences, Gui.theme);
applyDefaultPreference(preferences, Gui.layout);
if ( Gui.layout.getPreference(preferences) == Gui.Layout.main_cellphone ) {
Gui.layout.putDefault(preferences);
}
applyDefaultPreference(preferences, Gui.feedbackWindowShown);
applyDefaultPreference(preferences, Gui.notesppAnnounceShown);
applyDefaultPreference(preferences, Gui.showReleaseNotes);
applyDefaultPreference(preferences, Gui.usePrevAsBack);
applyDefaultPreference(preferences, Gui.showEqualsButton);
applyDefaultPreference(preferences, Gui.autoOrientation);
applyDefaultPreference(preferences, Gui.hideNumeralBaseDigits);
applyDefaultPreference(preferences, Graph.interpolate);
applyDefaultPreference(preferences, Graph.lineColorImag);
applyDefaultPreference(preferences, Graph.lineColorReal);
applyDefaultPreference(preferences, History.showIntermediateCalculations);
applyDefaultPreference(preferences, Calculations.calculateOnFly);
if ( !VibratorContainer.Preferences.hapticFeedbackEnabled.isSet(preferences) ) {
VibratorContainer.Preferences.hapticFeedbackEnabled.putPreference(preferences, true);
}
if ( !VibratorContainer.Preferences.hapticFeedbackDuration.isSet(preferences) ) {
VibratorContainer.Preferences.hapticFeedbackDuration.putPreference(preferences, 60L);
}
}
private static void applyDefaultPreference(@NotNull SharedPreferences preferences, @NotNull Preference<?> preference) {
if (!preference.isSet(preferences)) {
preference.putDefault(preferences);
}
}
}