diff --git a/calculatorpp-core/src/main/java/org/solovyev/android/calculator/CalculatorFixableError.java b/calculatorpp-core/src/main/java/org/solovyev/android/calculator/CalculatorFixableError.java
index 6ba533d6..5d898311 100644
--- a/calculatorpp-core/src/main/java/org/solovyev/android/calculator/CalculatorFixableError.java
+++ b/calculatorpp-core/src/main/java/org/solovyev/android/calculator/CalculatorFixableError.java
@@ -20,6 +20,20 @@ public enum CalculatorFixableError {
public void fix() {
CalculatorLocatorImpl.getInstance().getPreferenceService().setAngleUnits(AngleUnit.rad);
}
+ },
+
+ preferred_numeral_base() {
+ @Override
+ public void fix() {
+ CalculatorLocatorImpl.getInstance().getPreferenceService().setPreferredNumeralBase();
+ }
+ },
+
+ preferred_angle_units() {
+ @Override
+ public void fix() {
+ CalculatorLocatorImpl.getInstance().getPreferenceService().setPreferredAngleUnits();
+ }
};
@NotNull
diff --git a/calculatorpp-core/src/main/java/org/solovyev/android/calculator/CalculatorImpl.java b/calculatorpp-core/src/main/java/org/solovyev/android/calculator/CalculatorImpl.java
index 3f1a6c2a..191a471a 100644
--- a/calculatorpp-core/src/main/java/org/solovyev/android/calculator/CalculatorImpl.java
+++ b/calculatorpp-core/src/main/java/org/solovyev/android/calculator/CalculatorImpl.java
@@ -163,6 +163,8 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
@NotNull String expression,
@Nullable MessageRegistry mr) {
+ CalculatorLocatorImpl.getInstance().getPreferenceService().checkPreferredPreferences(false);
+
PreparedExpression preparedExpression = null;
try {
diff --git a/calculatorpp-core/src/main/java/org/solovyev/android/calculator/CalculatorPreferenceService.java b/calculatorpp-core/src/main/java/org/solovyev/android/calculator/CalculatorPreferenceService.java
index ca6a5d70..0d012015 100644
--- a/calculatorpp-core/src/main/java/org/solovyev/android/calculator/CalculatorPreferenceService.java
+++ b/calculatorpp-core/src/main/java/org/solovyev/android/calculator/CalculatorPreferenceService.java
@@ -1,6 +1,7 @@
package org.solovyev.android.calculator;
import jscl.AngleUnit;
+import jscl.NumeralBase;
import org.jetbrains.annotations.NotNull;
/**
@@ -10,5 +11,11 @@ import org.jetbrains.annotations.NotNull;
*/
public interface CalculatorPreferenceService {
+ void setPreferredAngleUnits();
void setAngleUnits(@NotNull AngleUnit angleUnit);
+
+ void setPreferredNumeralBase();
+ void setNumeralBase(@NotNull NumeralBase numeralBase);
+
+ void checkPreferredPreferences(boolean force);
}
diff --git a/calculatorpp/res/layout/calculation_messages_dialog.xml b/calculatorpp/res/layout/calculator_messages_dialog.xml
similarity index 100%
rename from calculatorpp/res/layout/calculation_messages_dialog.xml
rename to calculatorpp/res/layout/calculator_messages_dialog.xml
diff --git a/calculatorpp/res/layout/calculation_messages_dialog_message.xml b/calculatorpp/res/layout/calculator_messages_dialog_message.xml
similarity index 83%
rename from calculatorpp/res/layout/calculation_messages_dialog_message.xml
rename to calculatorpp/res/layout/calculator_messages_dialog_message.xml
index 65dd3e3b..ce34cfcd 100644
--- a/calculatorpp/res/layout/calculation_messages_dialog_message.xml
+++ b/calculatorpp/res/layout/calculator_messages_dialog_message.xml
@@ -8,13 +8,15 @@
+ a:layout_weight="5"
+ a:layout_gravity="top|left"/>
\ No newline at end of file
diff --git a/calculatorpp/res/values/text_strings.xml b/calculatorpp/res/values/text_strings.xml
index c1c191a0..fe0a547b 100644
--- a/calculatorpp/res/values/text_strings.xml
+++ b/calculatorpp/res/values/text_strings.xml
@@ -257,4 +257,12 @@
Fix
+ Preferred numeral base
+ Defines the preferred numeral base
+ Preferred numeral base \'%1$s\' differs from current numeral base \'%2$s\'. Forgot to change back?
+
+ Preferred angle units
+ Defines the preferred angle units
+ Preferred angle units \'%1$s\' differs from current angle units \'%2$s\'. Forgot to change back?
+
\ No newline at end of file
diff --git a/calculatorpp/res/xml/calculations_preferences.xml b/calculatorpp/res/xml/calculations_preferences.xml
index 0c71b68f..bd9e0d28 100644
--- a/calculatorpp/res/xml/calculations_preferences.xml
+++ b/calculatorpp/res/xml/calculations_preferences.xml
@@ -32,12 +32,26 @@
a:summary="@string/c_calc_grouping_separator_summary"
a:entryValues="@array/p_grouping_separator_values"/>
+
+
+
+
messages = new ArrayList(2);
+ if ( numeralBase != preferredNumeralBase ) {
+ messages.add(new CalculatorFixableMessage(application.getString(R.string.preferred_numeral_base_message, preferredNumeralBase.name(), numeralBase.name()), MessageType.warning, CalculatorFixableError.preferred_numeral_base));
+ }
+
+ if ( angleUnits != preferredAngleUnits ) {
+ messages.add(new CalculatorFixableMessage(application.getString(R.string.preferred_angle_units_message, preferredAngleUnits.name(), angleUnits.name()), MessageType.warning, CalculatorFixableError.preferred_angle_units));
+ }
+
+ CalculatorMessagesDialog.showDialog(messages, application);
+
+ CalculatorPreferences.Calculations.lastPreferredPreferencesCheck.putPreference(preferences, currentTime);
+ }
+ }
+
+ private boolean isTimeForCheck(@NotNull Long currentTime, @NotNull SharedPreferences preferences) {
+ final Long lastPreferredPreferencesCheckTime = CalculatorPreferences.Calculations.lastPreferredPreferencesCheck.getPreference(preferences);
+
+ return currentTime - lastPreferredPreferencesCheckTime > PREFERRED_PREFS_INTERVAL_TIME;
+ }
+
+ @Override
+ public void setPreferredAngleUnits() {
+ final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(application);
+ setAngleUnits(CalculatorPreferences.Calculations.preferredAngleUnits.getPreference(preferences));
+ }
+
@Override
public void setAngleUnits(@NotNull AngleUnit angleUnit) {
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(application);
@@ -30,4 +77,18 @@ public class AndroidCalculatorPreferenceService implements CalculatorPreferenceS
CalculatorLocatorImpl.getInstance().getNotifier().showMessage(new AndroidMessage(R.string.c_angle_units_changed_to, MessageType.info, application, angleUnit.name()));
}
+
+ @Override
+ public void setPreferredNumeralBase() {
+ final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(application);
+ setNumeralBase(CalculatorPreferences.Calculations.preferredNumeralBase.getPreference(preferences));
+ }
+
+ @Override
+ public void setNumeralBase(@NotNull NumeralBase numeralBase) {
+ final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(application);
+ AndroidCalculatorEngine.Preferences.numeralBase.putPreference(preferences, numeralBase);
+
+ CalculatorLocatorImpl.getInstance().getNotifier().showMessage(new AndroidMessage(R.string.c_numeral_base_changed_to, MessageType.info, application, numeralBase.name()));
+ }
}
diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorActivityLauncher.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorActivityLauncher.java
index 4446e926..c9d843ad 100644
--- a/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorActivityLauncher.java
+++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorActivityLauncher.java
@@ -163,7 +163,7 @@ public class CalculatorActivityLauncher {
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
if ( CalculatorPreferences.Calculations.showCalculationMessagesDialog.getPreference(prefs) ) {
- CalculatorMessagesDialog.showDialog(messages, context);
+ CalculatorMessagesDialog.showDialogForMessages(messages, context);
}
}
}
diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorApplication.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorApplication.java
index fb0ce759..86f6b121 100644
--- a/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorApplication.java
+++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorApplication.java
@@ -126,6 +126,8 @@ public class CalculatorApplication extends android.app.Application {
CalculatorLocatorImpl.getInstance().getLogger().debug(TAG, "Application started!");
CalculatorLocatorImpl.getInstance().getNotifier().showDebugMessage(TAG, "Application started!");
+
+ CalculatorLocatorImpl.getInstance().getPreferenceService().checkPreferredPreferences(true);
}
private void setTheme(@NotNull SharedPreferences preferences) {
diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorButtons.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorButtons.java
index f499f456..b401210a 100644
--- a/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorButtons.java
+++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorButtons.java
@@ -10,7 +10,6 @@ import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.RemoteViews;
-import android.widget.Toast;
import jscl.AngleUnit;
import jscl.NumeralBase;
import org.jetbrains.annotations.NotNull;
@@ -19,7 +18,6 @@ import org.solovyev.android.AndroidUtils;
import org.solovyev.android.calculator.model.AndroidCalculatorEngine;
import org.solovyev.android.calculator.view.AngleUnitsButton;
import org.solovyev.android.calculator.view.NumeralBasesButton;
-import org.solovyev.android.view.drag.DirectionDragButton;
import org.solovyev.android.view.drag.DragButton;
import org.solovyev.android.view.drag.DragDirection;
import org.solovyev.android.view.drag.SimpleOnDragListener;
@@ -217,9 +215,7 @@ public final class CalculatorButtons {
final NumeralBase oldNumeralBase = AndroidCalculatorEngine.Preferences.numeralBase.getPreference(preferences);
if (oldNumeralBase != numeralBase) {
- AndroidCalculatorEngine.Preferences.numeralBase.putPreference(preferences, numeralBase);
-
- Toast.makeText(context, context.getString(R.string.c_numeral_base_changed_to, numeralBase.name()), Toast.LENGTH_LONG).show();
+ CalculatorLocatorImpl.getInstance().getPreferenceService().setNumeralBase(numeralBase);
}
result = true;
diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculationMessage.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorFixableMessage.java
similarity index 66%
rename from calculatorpp/src/main/java/org/solovyev/android/calculator/CalculationMessage.java
rename to calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorFixableMessage.java
index 0660a480..8d6110e7 100644
--- a/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculationMessage.java
+++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorFixableMessage.java
@@ -12,27 +12,27 @@ import org.solovyev.common.msg.MessageType;
* Date: 11/17/12
* Time: 6:54 PM
*/
-public class CalculationMessage implements Parcelable {
+public class CalculatorFixableMessage implements Parcelable {
- public static final Creator CREATOR = new Creator() {
+ public static final Creator CREATOR = new Creator() {
@Override
- public CalculationMessage createFromParcel(@NotNull Parcel in) {
- return CalculationMessage.fromParcel(in);
+ public CalculatorFixableMessage createFromParcel(@NotNull Parcel in) {
+ return CalculatorFixableMessage.fromParcel(in);
}
@Override
- public CalculationMessage[] newArray(int size) {
- return new CalculationMessage[size];
+ public CalculatorFixableMessage[] newArray(int size) {
+ return new CalculatorFixableMessage[size];
}
};
@NotNull
- private static CalculationMessage fromParcel(@NotNull Parcel in) {
+ private static CalculatorFixableMessage fromParcel(@NotNull Parcel in) {
final String message = in.readString();
final MessageType messageType = (MessageType) in.readSerializable();
final CalculatorFixableError fixableError = (CalculatorFixableError) in.readSerializable();
- return new CalculationMessage(message, messageType, fixableError);
+ return new CalculatorFixableMessage(message, messageType, fixableError);
}
@NotNull
@@ -44,15 +44,15 @@ public class CalculationMessage implements Parcelable {
@Nullable
private final CalculatorFixableError fixableError;
- public CalculationMessage(@NotNull Message message) {
+ public CalculatorFixableMessage(@NotNull Message message) {
this.message = message.getLocalizedMessage();
this.messageType = message.getMessageType();
this.fixableError = CalculatorFixableError.getErrorByMessageCode(message.getMessageCode());
}
- public CalculationMessage(@NotNull String message,
- @NotNull MessageType messageType,
- @Nullable CalculatorFixableError fixableError) {
+ public CalculatorFixableMessage(@NotNull String message,
+ @NotNull MessageType messageType,
+ @Nullable CalculatorFixableError fixableError) {
this.message = message;
this.messageType = messageType;
this.fixableError = fixableError;
diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorMessagesDialog.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorMessagesDialog.java
index 2ef1966a..88f5ea01 100644
--- a/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorMessagesDialog.java
+++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorMessagesDialog.java
@@ -31,7 +31,7 @@ public class CalculatorMessagesDialog extends SherlockActivity {
private static final String INPUT = "input";
@NotNull
- private Input input = new Input(Collections.emptyList());
+ private Input input = new Input(Collections.emptyList());
public CalculatorMessagesDialog() {
}
@@ -40,7 +40,7 @@ public class CalculatorMessagesDialog extends SherlockActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
- setContentView(R.layout.calculation_messages_dialog);
+ setContentView(R.layout.calculator_messages_dialog);
final Intent intent = getIntent();
if (intent != null) {
@@ -77,9 +77,9 @@ public class CalculatorMessagesDialog extends SherlockActivity {
final LayoutInflater layoutInflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- final List messages = input.getMessages();
- for (final CalculationMessage message : messages) {
- final View view = layoutInflater.inflate(R.layout.calculation_messages_dialog_message, null);
+ final List messages = input.getMessages();
+ for (final CalculatorFixableMessage message : messages) {
+ final View view = layoutInflater.inflate(R.layout.calculator_messages_dialog_message, null);
final TextView calculationMessagesTextView = (TextView) view.findViewById(R.id.calculation_messages_text_view);
calculationMessagesTextView.setText(message.getMessage());
@@ -91,26 +91,7 @@ public class CalculatorMessagesDialog extends SherlockActivity {
fixButton.setOnClickListener(null);
} else {
fixButton.setVisibility(View.VISIBLE);
- fixButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- final List filteredMessages = new ArrayList(messages.size() - 1);
- for (CalculationMessage calculationMessage : messages) {
- if ( calculationMessage.getFixableError() == null || calculationMessage.getFixableError() != message.getFixableError() ) {
- filteredMessages.add(message);
- }
- }
-
- fixableError.fix();
-
- if (!filteredMessages.isEmpty()) {
- CalculatorMessagesDialog.this.input = new Input(filteredMessages);
- onInputChanged();
- } else {
- CalculatorMessagesDialog.this.finish();
- }
- }
- });
+ fixButton.setOnClickListener(new FixErrorOnClickListener(messages, message));
}
viewGroup.addView(view, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
@@ -132,7 +113,7 @@ public class CalculatorMessagesDialog extends SherlockActivity {
**********************************************************************
*/
- public static void showDialog(@NotNull List messages, @NotNull Context context) {
+ public static void showDialogForMessages(@NotNull List messages, @NotNull Context context) {
if (!messages.isEmpty()) {
final Intent intent = new Intent(context, CalculatorMessagesDialog.class);
@@ -143,6 +124,17 @@ public class CalculatorMessagesDialog extends SherlockActivity {
}
}
+ public static void showDialog(@NotNull List messages, @NotNull Context context) {
+ if (!messages.isEmpty()) {
+ final Intent intent = new Intent(context, CalculatorMessagesDialog.class);
+
+ intent.putExtra(INPUT, new Input(messages));
+ intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+
+ context.startActivity(intent);
+ }
+ }
+
private static final class Input implements Parcelable {
public static final Creator CREATOR = new Creator() {
@@ -159,21 +151,21 @@ public class CalculatorMessagesDialog extends SherlockActivity {
@NotNull
private static Input fromParcel(@NotNull Parcel in) {
- final List messages = new ArrayList();
- in.readTypedList(messages, CalculationMessage.CREATOR);
+ final List messages = new ArrayList();
+ in.readTypedList(messages, CalculatorFixableMessage.CREATOR);
return new Input(messages);
}
@NotNull
- private List messages = new ArrayList();
+ private List messages = new ArrayList();
- private Input(@NotNull List messages) {
+ private Input(@NotNull List messages) {
this.messages = messages;
}
@NotNull
- public List getMessages() {
+ public List getMessages() {
return messages;
}
@@ -189,13 +181,49 @@ public class CalculatorMessagesDialog extends SherlockActivity {
@NotNull
public static Input fromMessages(@NotNull List messages) {
- final List stringMessages = new ArrayList(messages.size());
+ final List stringMessages = new ArrayList(messages.size());
for (Message message : messages) {
- stringMessages.add(new CalculationMessage(message));
+ stringMessages.add(new CalculatorFixableMessage(message));
}
return new Input(stringMessages);
}
}
+
+ private class FixErrorOnClickListener implements View.OnClickListener {
+
+ @NotNull
+ private final List messages;
+
+ @NotNull
+ private final CalculatorFixableMessage currentMessage;
+
+ public FixErrorOnClickListener(@NotNull List messages,
+ @NotNull CalculatorFixableMessage message) {
+ this.messages = messages;
+ this.currentMessage = message;
+ }
+
+ @Override
+ public void onClick(View v) {
+ final List filteredMessages = new ArrayList(messages.size() - 1);
+ for (CalculatorFixableMessage message : messages) {
+ if ( message.getFixableError() == null ) {
+ filteredMessages.add(message);
+ } else if ( message.getFixableError() != currentMessage.getFixableError() ) {
+ filteredMessages.add(message);
+ }
+ }
+
+ currentMessage.getFixableError().fix();
+
+ if (!filteredMessages.isEmpty()) {
+ CalculatorMessagesDialog.this.input = new Input(filteredMessages);
+ onInputChanged();
+ } else {
+ CalculatorMessagesDialog.this.finish();
+ }
+ }
+ }
}
diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorPreferences.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorPreferences.java
index 720a3d0c..2c75846c 100644
--- a/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorPreferences.java
+++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorPreferences.java
@@ -8,10 +8,7 @@ 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.prefs.*;
import org.solovyev.android.view.VibratorContainer;
import java.text.DecimalFormatSymbols;
@@ -36,8 +33,9 @@ public final class CalculatorPreferences {
public static final Preference calculateOnFly = new BooleanPreference("calculations_calculate_on_fly", true);
public static final Preference showCalculationMessagesDialog = new BooleanPreference("show_calculation_messages_dialog", true);
- public static final Preference preferredNumeralBase = StringPreference.newInstance("preferred_numeral_base", NumeralBase.dec, NumeralBase.class);
- public static final Preference preferredAngleUnit = StringPreference.newInstance("preferred_angle_units", AngleUnit.deg, AngleUnit.class);
+ public static final Preference preferredNumeralBase = StringPreference.newInstance("preferred_numeral_base", AndroidCalculatorEngine.Preferences.numeralBase.getDefaultValue(), NumeralBase.class);
+ public static final Preference preferredAngleUnits = StringPreference.newInstance("preferred_angle_units", AndroidCalculatorEngine.Preferences.angleUnit.getDefaultValue(), AngleUnit.class);
+ public static final Preference lastPreferredPreferencesCheck = new LongPreference("preferred_preferences_check_time", 0L);
}
@@ -183,8 +181,9 @@ public final class CalculatorPreferences {
applyDefaultPreference(preferences, Graph.lineColorReal);
applyDefaultPreference(preferences, History.showIntermediateCalculations);
applyDefaultPreference(preferences, Calculations.calculateOnFly);
- applyDefaultPreference(preferences, Calculations.preferredAngleUnit);
+ applyDefaultPreference(preferences, Calculations.preferredAngleUnits);
applyDefaultPreference(preferences, Calculations.preferredNumeralBase);
+ applyDefaultPreference(preferences, Calculations.lastPreferredPreferencesCheck);
// renew value after each application start