Engine refactor
This commit is contained in:
@@ -40,11 +40,6 @@ import javax.annotation.Nullable;
|
||||
import jscl.NumeralBase;
|
||||
import jscl.math.Generic;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 9/22/12
|
||||
* Time: 5:42 PM
|
||||
*/
|
||||
public class AndroidCalculator implements Calculator, CalculatorEventListener, SharedPreferences.OnSharedPreferenceChangeListener {
|
||||
|
||||
@Nonnull
|
||||
@@ -61,14 +56,6 @@ public class AndroidCalculator implements Calculator, CalculatorEventListener, S
|
||||
PreferenceManager.getDefaultSharedPreferences(application).registerOnSharedPreferenceChangeListener(this);
|
||||
}
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* DELEGATED TO CALCULATOR
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
@Override
|
||||
@Nonnull
|
||||
public CalculatorEventData evaluate(@Nonnull JsclOperation operation, @Nonnull String expression) {
|
||||
@@ -127,8 +114,8 @@ public class AndroidCalculator implements Calculator, CalculatorEventListener, S
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init() {
|
||||
this.calculator.init();
|
||||
public void init(@Nonnull Executor initThread) {
|
||||
this.calculator.init(initThread);
|
||||
|
||||
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
this.calculator.setCalculateOnFly(Preferences.Calculations.calculateOnFly.getPreference(prefs));
|
||||
|
@@ -50,7 +50,7 @@ public abstract class BaseNumberBuilder {
|
||||
|
||||
protected BaseNumberBuilder(@Nonnull Engine engine) {
|
||||
this.engine = engine;
|
||||
this.nb = engine.getNumeralBase();
|
||||
this.nb = engine.getMathEngine().getNumeralBase();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -94,12 +94,12 @@ public abstract class BaseNumberBuilder {
|
||||
}
|
||||
|
||||
public boolean isHexMode() {
|
||||
return nb == NumeralBase.hex || (nb == null && engine.getNumeralBase() == NumeralBase.hex);
|
||||
return nb == NumeralBase.hex || (nb == null && engine.getMathEngine().getNumeralBase() == NumeralBase.hex);
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
protected NumeralBase getNumeralBase() {
|
||||
return nb == null ? engine.getNumeralBase() : nb;
|
||||
return nb == null ? engine.getMathEngine().getNumeralBase() : nb;
|
||||
}
|
||||
|
||||
public abstract int process(@Nonnull SpannableStringBuilder sb, @Nonnull MathType.Result result);
|
||||
|
@@ -24,6 +24,8 @@ package org.solovyev.android.calculator;
|
||||
|
||||
import org.solovyev.android.calculator.jscl.JsclOperation;
|
||||
|
||||
import java.util.concurrent.Executor;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
@@ -37,7 +39,7 @@ import jscl.math.Generic;
|
||||
*/
|
||||
public interface Calculator extends CalculatorEventContainer {
|
||||
|
||||
void init();
|
||||
void init(@Nonnull Executor initThread);
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
|
@@ -26,9 +26,10 @@ import android.content.SharedPreferences;
|
||||
import android.os.Handler;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.util.Log;
|
||||
|
||||
import com.squareup.leakcanary.LeakCanary;
|
||||
import com.squareup.otto.Bus;
|
||||
import jscl.MathEngine;
|
||||
|
||||
import org.acra.ACRA;
|
||||
import org.acra.ACRAConfiguration;
|
||||
import org.acra.sender.HttpSender;
|
||||
@@ -41,14 +42,17 @@ import org.solovyev.android.calculator.plot.AndroidCalculatorPlotter;
|
||||
import org.solovyev.android.calculator.plot.CalculatorPlotterImpl;
|
||||
import org.solovyev.common.msg.MessageType;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Named;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.concurrent.Executor;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Named;
|
||||
|
||||
import jscl.MathEngine;
|
||||
|
||||
public class CalculatorApplication extends android.app.Application implements SharedPreferences.OnSharedPreferenceChangeListener {
|
||||
|
||||
@Inject
|
||||
@@ -79,6 +83,9 @@ public class CalculatorApplication extends android.app.Application implements Sh
|
||||
@Inject
|
||||
Calculator calculator;
|
||||
|
||||
@Inject
|
||||
Engine engine;
|
||||
|
||||
@Inject
|
||||
Keyboard keyboard;
|
||||
|
||||
@@ -103,6 +110,7 @@ public class CalculatorApplication extends android.app.Application implements Sh
|
||||
.appModule(new AppModule(this))
|
||||
.build();
|
||||
component.inject(this);
|
||||
editor.init();
|
||||
history.init(initThread);
|
||||
|
||||
onPostCreate(preferences, languages);
|
||||
@@ -116,7 +124,7 @@ public class CalculatorApplication extends android.app.Application implements Sh
|
||||
App.getGa().reportInitially(preferences);
|
||||
|
||||
Locator.getInstance().init(calculator,
|
||||
new Engine(this),
|
||||
engine,
|
||||
new AndroidCalculatorClipboard(this),
|
||||
new AndroidCalculatorNotifier(this),
|
||||
errorReporter,
|
||||
@@ -130,7 +138,7 @@ public class CalculatorApplication extends android.app.Application implements Sh
|
||||
calculator.addCalculatorEventListener(listener);
|
||||
}
|
||||
|
||||
Locator.getInstance().getCalculator().init();
|
||||
calculator.init(initThread);
|
||||
|
||||
initThread.execute(new Runnable() {
|
||||
@Override
|
||||
@@ -143,7 +151,7 @@ public class CalculatorApplication extends android.app.Application implements Sh
|
||||
private void warmUpEngine() {
|
||||
try {
|
||||
// warm-up engine
|
||||
MathEngine mathEngine = Locator.getInstance().getEngine().getMathEngine();
|
||||
final MathEngine mathEngine = engine.getMathEngine();
|
||||
mathEngine.evaluate("1+1");
|
||||
mathEngine.evaluate("1*1");
|
||||
} catch (Throwable e) {
|
||||
|
@@ -70,16 +70,6 @@ public enum CalculatorEventType {
|
||||
|
||||
conversion_finished,
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* ENGINE
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
engine_preferences_changed,
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
|
@@ -24,17 +24,10 @@ package org.solovyev.android.calculator;
|
||||
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
|
||||
import com.squareup.otto.Bus;
|
||||
import com.squareup.otto.Subscribe;
|
||||
import jscl.AbstractJsclArithmeticException;
|
||||
import jscl.MathEngine;
|
||||
import jscl.NumeralBase;
|
||||
import jscl.NumeralBaseException;
|
||||
import jscl.math.Generic;
|
||||
import jscl.math.function.Function;
|
||||
import jscl.math.function.IConstant;
|
||||
import jscl.math.operator.Operator;
|
||||
import jscl.text.ParseInterruptedException;
|
||||
|
||||
import org.solovyev.android.calculator.jscl.JsclOperation;
|
||||
import org.solovyev.android.calculator.model.Var;
|
||||
import org.solovyev.android.calculator.text.TextProcessor;
|
||||
@@ -47,40 +40,30 @@ import org.solovyev.common.text.Strings;
|
||||
import org.solovyev.common.units.ConversionException;
|
||||
import org.solovyev.common.units.Conversions;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
/**
|
||||
* User: Solovyev_S
|
||||
* Date: 20.09.12
|
||||
* Time: 16:42
|
||||
*/
|
||||
public class CalculatorImpl implements Calculator, CalculatorEventListener {
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* CONSTANTS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
import jscl.AbstractJsclArithmeticException;
|
||||
import jscl.MathEngine;
|
||||
import jscl.NumeralBase;
|
||||
import jscl.NumeralBaseException;
|
||||
import jscl.math.Generic;
|
||||
import jscl.math.function.Function;
|
||||
import jscl.math.function.IConstant;
|
||||
import jscl.math.operator.Operator;
|
||||
import jscl.text.ParseInterruptedException;
|
||||
|
||||
public class CalculatorImpl implements Calculator, CalculatorEventListener {
|
||||
|
||||
// one minute
|
||||
private static final long PREFERENCE_CHECK_INTERVAL = 1000L * 60L;
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* FIELDS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
@Nonnull
|
||||
private final CalculatorEventContainer calculatorEventContainer = new ListCalculatorEventContainer();
|
||||
|
||||
@@ -205,8 +188,8 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init() {
|
||||
Locator.getInstance().getEngine().init();
|
||||
public void init(@Nonnull Executor initThread) {
|
||||
Locator.getInstance().getEngine().init(initThread);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -373,7 +356,7 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
|
||||
final CalculatorEventData eventDataId = nextEventData();
|
||||
|
||||
final DisplayState displayViewState = App.getDisplay().getState();
|
||||
final NumeralBase from = Locator.getInstance().getEngine().getNumeralBase();
|
||||
final NumeralBase from = Locator.getInstance().getEngine().getMathEngine().getNumeralBase();
|
||||
|
||||
calculationsExecutor.execute(new Runnable() {
|
||||
@Override
|
||||
@@ -524,11 +507,6 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
|
||||
case function_removed:
|
||||
evaluate();
|
||||
break;
|
||||
|
||||
case engine_preferences_changed:
|
||||
evaluate(calculatorEventData.getSequenceId());
|
||||
break;
|
||||
|
||||
case use_constant:
|
||||
final IConstant constant = (IConstant) data;
|
||||
Locator.getInstance().getKeyboard().buttonPressed(constant.getName());
|
||||
|
@@ -56,7 +56,7 @@ enum ConversionMenuItem implements AMenuItem<DisplayState> {
|
||||
if (operation == JsclOperation.numeric) {
|
||||
if (generic.getConstants().isEmpty()) {
|
||||
// conversion possible => return true
|
||||
final NumeralBase fromNumeralBase = Locator.getInstance().getEngine().getNumeralBase();
|
||||
final NumeralBase fromNumeralBase = Locator.getInstance().getEngine().getMathEngine().getNumeralBase();
|
||||
if (fromNumeralBase != toNumeralBase) {
|
||||
result = Locator.getInstance().getCalculator().isConversionPossible(generic, fromNumeralBase, this.toNumeralBase);
|
||||
}
|
||||
|
@@ -25,6 +25,7 @@ package org.solovyev.android.calculator;
|
||||
import android.content.SharedPreferences;
|
||||
|
||||
import com.squareup.otto.Bus;
|
||||
import com.squareup.otto.Subscribe;
|
||||
|
||||
import org.solovyev.android.Check;
|
||||
import org.solovyev.android.calculator.text.TextProcessorEditorResult;
|
||||
@@ -40,7 +41,6 @@ import static java.lang.Math.min;
|
||||
@Singleton
|
||||
public class Editor {
|
||||
|
||||
private static final String TAG = App.subTag("Editor");
|
||||
@Nullable
|
||||
private final EditorTextProcessor textProcessor;
|
||||
@Nullable
|
||||
@@ -55,6 +55,10 @@ public class Editor {
|
||||
textProcessor = new EditorTextProcessor(preferences);
|
||||
}
|
||||
|
||||
public void init() {
|
||||
bus.register(this);
|
||||
}
|
||||
|
||||
public static int clamp(int selection, @Nonnull CharSequence text) {
|
||||
return clamp(selection, text.length());
|
||||
}
|
||||
@@ -214,6 +218,13 @@ public class Editor {
|
||||
return onSelectionChanged(EditorState.forNewSelection(state, clamp(selection, state.text)));
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
public void onEngineChanged(@Nonnull Engine.ChangedEvent e) {
|
||||
// this will effectively apply new formatting (if f.e. grouping separator has changed) and
|
||||
// will start new evaluation
|
||||
onTextChanged(getState());
|
||||
}
|
||||
|
||||
public static class ChangedEvent {
|
||||
@Nonnull
|
||||
public final EditorState oldState;
|
||||
|
@@ -23,9 +23,30 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import android.app.Application;
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.preference.PreferenceManager;
|
||||
|
||||
import com.squareup.otto.Bus;
|
||||
|
||||
import org.solovyev.android.calculator.model.AndroidMathEntityDao;
|
||||
import org.solovyev.android.calculator.model.Functions;
|
||||
import org.solovyev.android.calculator.model.Vars;
|
||||
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.common.text.EnumMapper;
|
||||
import org.solovyev.common.text.NumberMapper;
|
||||
import org.solovyev.common.text.Strings;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Executor;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Singleton;
|
||||
|
||||
import jscl.AngleUnit;
|
||||
import jscl.JsclMathEngine;
|
||||
import jscl.MathEngine;
|
||||
@@ -33,48 +54,10 @@ import jscl.NumeralBase;
|
||||
import jscl.math.function.Function;
|
||||
import jscl.math.function.IConstant;
|
||||
import jscl.math.operator.Operator;
|
||||
import org.solovyev.android.calculator.model.AndroidMathEntityDao;
|
||||
import org.solovyev.android.calculator.model.Functions;
|
||||
import org.solovyev.android.calculator.model.Vars;
|
||||
import org.solovyev.android.prefs.BooleanPreference;
|
||||
import org.solovyev.android.prefs.Preference;
|
||||
import org.solovyev.android.prefs.StringPreference;
|
||||
import org.solovyev.common.text.EnumMapper;
|
||||
import org.solovyev.common.text.NumberMapper;
|
||||
import org.solovyev.common.text.Strings;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.text.DecimalFormatSymbols;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
@Singleton
|
||||
public class Engine implements SharedPreferences.OnSharedPreferenceChangeListener {
|
||||
|
||||
private static final String MULTIPLICATION_SIGN_DEFAULT = "×";
|
||||
|
||||
private static final String GROUPING_SEPARATOR_P_KEY = "org.solovyev.android.calculator.CalculatorActivity_calc_grouping_separator";
|
||||
|
||||
private static final String MULTIPLICATION_SIGN_P_KEY = "org.solovyev.android.calculator.CalculatorActivity_calc_multiplication_sign";
|
||||
|
||||
private static final String SCIENCE_NOTATION_P_KEY = "calculation.output.science_notation";
|
||||
private static final boolean SCIENCE_NOTATION_DEFAULT = false;
|
||||
|
||||
private static final String ROUND_RESULT_P_KEY = "org.solovyev.android.calculator.CalculatorModel_round_result";
|
||||
private static final boolean ROUND_RESULT_DEFAULT = true;
|
||||
|
||||
private static final String RESULT_PRECISION_P_KEY = "org.solovyev.android.calculator.CalculatorModel_result_precision";
|
||||
private static final String RESULT_PRECISION_DEFAULT = "5";
|
||||
|
||||
private static final String NUMERAL_BASES_P_KEY = "org.solovyev.android.calculator.CalculatorActivity_numeral_bases";
|
||||
private static final String NUMERAL_BASES_DEFAULT = "dec";
|
||||
|
||||
private static final String ANGLE_UNITS_P_KEY = "org.solovyev.android.calculator.CalculatorActivity_angle_units";
|
||||
private static final String ANGLE_UNITS_DEFAULT = "deg";
|
||||
@Nonnull
|
||||
private final Context context;
|
||||
@Nonnull
|
||||
private final Object lock;
|
||||
@Nonnull
|
||||
private final MathEngine mathEngine;
|
||||
@Nonnull
|
||||
@@ -85,12 +68,16 @@ public class Engine implements SharedPreferences.OnSharedPreferenceChangeListene
|
||||
private final EntitiesRegistry<Operator> operatorsRegistry;
|
||||
@Nonnull
|
||||
private final EntitiesRegistry<Operator> postfixFunctionsRegistry;
|
||||
@Inject
|
||||
SharedPreferences preferences;
|
||||
@Inject
|
||||
Bus bus;
|
||||
@Inject
|
||||
ErrorReporter errorReporter;
|
||||
@Nonnull
|
||||
private String multiplicationSign = MULTIPLICATION_SIGN_DEFAULT;
|
||||
private String multiplicationSign = Preferences.multiplicationSign.getDefaultValue();
|
||||
|
||||
public Engine(@Nonnull Context context, @Nonnull MathEngine mathEngine, @Nonnull EntitiesRegistry<IConstant> varsRegistry, @Nonnull EntitiesRegistry<Function> functionsRegistry, @Nonnull EntitiesRegistry<Operator> operatorsRegistry, @Nonnull EntitiesRegistry<Operator> postfixFunctionsRegistry) {
|
||||
this.context = context;
|
||||
this.lock = new Object();
|
||||
public Engine(@Nonnull MathEngine mathEngine, @Nonnull EntitiesRegistry<IConstant> varsRegistry, @Nonnull EntitiesRegistry<Function> functionsRegistry, @Nonnull EntitiesRegistry<Operator> operatorsRegistry, @Nonnull EntitiesRegistry<Operator> postfixFunctionsRegistry) {
|
||||
this.mathEngine = mathEngine;
|
||||
this.varsRegistry = varsRegistry;
|
||||
this.functionsRegistry = functionsRegistry;
|
||||
@@ -98,13 +85,10 @@ public class Engine implements SharedPreferences.OnSharedPreferenceChangeListene
|
||||
this.postfixFunctionsRegistry = postfixFunctionsRegistry;
|
||||
}
|
||||
|
||||
@Inject
|
||||
public Engine(@Nonnull Application application) {
|
||||
this.mathEngine = JsclMathEngine.getInstance();
|
||||
this.context = application;
|
||||
|
||||
PreferenceManager.getDefaultSharedPreferences(application).registerOnSharedPreferenceChangeListener(this);
|
||||
|
||||
this.lock = new Object();
|
||||
this.mathEngine.setRoundResult(true);
|
||||
this.mathEngine.setUseGroupingSeparator(true);
|
||||
|
||||
@@ -114,34 +98,38 @@ public class Engine implements SharedPreferences.OnSharedPreferenceChangeListene
|
||||
this.postfixFunctionsRegistry = new PostfixFunctionsRegistry(mathEngine.getPostfixFunctionsRegistry(), new AndroidMathEntityDao<>(null, application, null));
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public static NumeralBase getNumeralBaseFromPrefs(@Nonnull SharedPreferences preferences) {
|
||||
return Preferences.numeralBase.getPreference(preferences);
|
||||
private static void migratePreference(@Nonnull SharedPreferences preferences, @Nonnull BooleanPreference preference, @Nonnull String oldKey, @Nonnull SharedPreferences.Editor editor) {
|
||||
if (!preferences.contains(oldKey)) {
|
||||
return;
|
||||
}
|
||||
editor.putBoolean(preference.getKey(), preferences.getBoolean(oldKey, false));
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public static AngleUnit getAngleUnitsFromPrefs(@Nonnull SharedPreferences preferences) {
|
||||
return Preferences.angleUnit.getPreference(preferences);
|
||||
private static void migratePreference(@Nonnull SharedPreferences preferences, @Nonnull StringPreference<?> preference, @Nonnull String oldKey, @Nonnull SharedPreferences.Editor editor) {
|
||||
if (!preferences.contains(oldKey)) {
|
||||
return;
|
||||
}
|
||||
editor.putString(preference.getKey(), preferences.getString(oldKey, null));
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public EntitiesRegistry<IConstant> getVarsRegistry() {
|
||||
return this.varsRegistry;
|
||||
return varsRegistry;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public EntitiesRegistry<Function> getFunctionsRegistry() {
|
||||
return this.functionsRegistry;
|
||||
return functionsRegistry;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public EntitiesRegistry<Operator> getOperatorsRegistry() {
|
||||
return this.operatorsRegistry;
|
||||
return operatorsRegistry;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public EntitiesRegistry<Operator> getPostfixFunctionsRegistry() {
|
||||
return this.postfixFunctionsRegistry;
|
||||
return postfixFunctionsRegistry;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@@ -149,45 +137,75 @@ public class Engine implements SharedPreferences.OnSharedPreferenceChangeListene
|
||||
return mathEngine;
|
||||
}
|
||||
|
||||
public void init() {
|
||||
synchronized (lock) {
|
||||
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
resetPreferences(preferences);
|
||||
safeLoadRegistry(varsRegistry);
|
||||
safeLoadRegistry(functionsRegistry);
|
||||
safeLoadRegistry(operatorsRegistry);
|
||||
safeLoadRegistry(postfixFunctionsRegistry);
|
||||
}
|
||||
public void init(@Nonnull Executor initThread) {
|
||||
checkPreferences();
|
||||
preferences.registerOnSharedPreferenceChangeListener(this);
|
||||
applyPreferences();
|
||||
initThread.execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
initAsync();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void resetPreferences() {
|
||||
resetPreferences(App.getPreferences());
|
||||
private void checkPreferences() {
|
||||
final int oldVersion;
|
||||
if (Preferences.version.isSet(preferences)) {
|
||||
oldVersion = Preferences.version.getPreference(preferences);
|
||||
} else {
|
||||
oldVersion = 0;
|
||||
}
|
||||
final int newVersion = Preferences.version.getPreference(preferences);
|
||||
if (oldVersion == newVersion) {
|
||||
return;
|
||||
}
|
||||
final SharedPreferences.Editor editor = preferences.edit();
|
||||
if (oldVersion == 0) {
|
||||
migratePreference(preferences, Preferences.groupingSeparator, "org.solovyev.android.calculator.CalculatorActivity_calc_grouping_separator", editor);
|
||||
migratePreference(preferences, Preferences.multiplicationSign, "org.solovyev.android.calculator.CalculatorActivity_calc_multiplication_sign", editor);
|
||||
migratePreference(preferences, Preferences.numeralBase, "org.solovyev.android.calculator.CalculatorActivity_numeral_bases", editor);
|
||||
migratePreference(preferences, Preferences.angleUnit, "org.solovyev.android.calculator.CalculatorActivity_angle_units", editor);
|
||||
migratePreference(preferences, Preferences.Output.precision, "org.solovyev.android.calculator.CalculatorModel_result_precision", editor);
|
||||
migratePreference(preferences, Preferences.Output.scientificNotation, "calculation.output.science_notation", editor);
|
||||
migratePreference(preferences, Preferences.Output.round, "org.solovyev.android.calculator.CalculatorModel_round_result", editor);
|
||||
}
|
||||
Preferences.version.putDefault(preferences);
|
||||
editor.apply();
|
||||
}
|
||||
|
||||
private void initAsync() {
|
||||
safeLoadRegistry(varsRegistry);
|
||||
safeLoadRegistry(functionsRegistry);
|
||||
safeLoadRegistry(operatorsRegistry);
|
||||
safeLoadRegistry(postfixFunctionsRegistry);
|
||||
}
|
||||
|
||||
private void safeLoadRegistry(@Nonnull EntitiesRegistry<?> registry) {
|
||||
try {
|
||||
registry.load();
|
||||
} catch (Exception e) {
|
||||
Locator.getInstance().getErrorReporter().onException(e);
|
||||
errorReporter.onException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private void resetPreferences(@Nonnull SharedPreferences preferences) {
|
||||
setPrecision(Preferences.precision.getPreference(preferences));
|
||||
setRoundResult(Preferences.roundResult.getPreference(preferences));
|
||||
setAngleUnits(getAngleUnitsFromPrefs(preferences));
|
||||
setNumeralBase(getNumeralBaseFromPrefs(preferences));
|
||||
private void applyPreferences() {
|
||||
mathEngine.setAngleUnits(Preferences.angleUnit.getPreference(preferences));
|
||||
mathEngine.setNumeralBase(Preferences.numeralBase.getPreference(preferences));
|
||||
setMultiplicationSign(Preferences.multiplicationSign.getPreference(preferences));
|
||||
setScienceNotation(Preferences.scienceNotation.getPreference(preferences));
|
||||
|
||||
mathEngine.setPrecision(Preferences.Output.precision.getPreference(preferences));
|
||||
mathEngine.setScienceNotation(Preferences.Output.scientificNotation.getPreference(preferences));
|
||||
mathEngine.setRoundResult(Preferences.Output.round.getPreference(preferences));
|
||||
|
||||
final String groupingSeparator = Preferences.groupingSeparator.getPreference(preferences);
|
||||
if (Strings.isEmpty(groupingSeparator)) {
|
||||
setUseGroupingSeparator(false);
|
||||
mathEngine.setUseGroupingSeparator(false);
|
||||
} else {
|
||||
setUseGroupingSeparator(true);
|
||||
setGroupingSeparator(groupingSeparator.charAt(0));
|
||||
mathEngine.setUseGroupingSeparator(true);
|
||||
mathEngine.setGroupingSeparator(groupingSeparator.charAt(0));
|
||||
}
|
||||
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.engine_preferences_changed, null);
|
||||
bus.post(ChangedEvent.INSTANCE);
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@@ -199,99 +217,46 @@ public class Engine implements SharedPreferences.OnSharedPreferenceChangeListene
|
||||
this.multiplicationSign = multiplicationSign;
|
||||
}
|
||||
|
||||
public void setUseGroupingSeparator(boolean useGroupingSeparator) {
|
||||
synchronized (lock) {
|
||||
this.mathEngine.setUseGroupingSeparator(useGroupingSeparator);
|
||||
}
|
||||
}
|
||||
|
||||
public void setGroupingSeparator(char groupingSeparator) {
|
||||
synchronized (lock) {
|
||||
this.mathEngine.setGroupingSeparator(groupingSeparator);
|
||||
}
|
||||
}
|
||||
|
||||
public void setPrecision(@Nonnull Integer precision) {
|
||||
synchronized (lock) {
|
||||
this.mathEngine.setPrecision(precision);
|
||||
}
|
||||
}
|
||||
|
||||
public void setRoundResult(@Nonnull Boolean round) {
|
||||
synchronized (lock) {
|
||||
this.mathEngine.setRoundResult(round);
|
||||
}
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public AngleUnit getAngleUnits() {
|
||||
synchronized (lock) {
|
||||
return this.mathEngine.getAngleUnits();
|
||||
}
|
||||
}
|
||||
|
||||
public void setAngleUnits(@Nonnull AngleUnit angleUnits) {
|
||||
synchronized (lock) {
|
||||
this.mathEngine.setAngleUnits(angleUnits);
|
||||
}
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public NumeralBase getNumeralBase() {
|
||||
synchronized (lock) {
|
||||
return this.mathEngine.getNumeralBase();
|
||||
}
|
||||
}
|
||||
|
||||
public void setNumeralBase(@Nonnull NumeralBase numeralBase) {
|
||||
synchronized (lock) {
|
||||
this.mathEngine.setNumeralBase(numeralBase);
|
||||
}
|
||||
}
|
||||
|
||||
public void setScienceNotation(@Nonnull Boolean scienceNotation) {
|
||||
synchronized (lock) {
|
||||
this.mathEngine.setScienceNotation(scienceNotation);
|
||||
}
|
||||
}
|
||||
|
||||
public void setDecimalGroupSymbols(@Nonnull DecimalFormatSymbols decimalGroupSymbols) {
|
||||
synchronized (lock) {
|
||||
this.mathEngine.setDecimalGroupSymbols(decimalGroupSymbols);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
|
||||
if (Preferences.getPreferenceKeys().contains(key)) {
|
||||
this.resetPreferences();
|
||||
applyPreferences();
|
||||
}
|
||||
}
|
||||
|
||||
public static class ChangedEvent {
|
||||
static final ChangedEvent INSTANCE = new ChangedEvent();
|
||||
private ChangedEvent() {
|
||||
}
|
||||
}
|
||||
|
||||
public static class Preferences {
|
||||
public static final Preference<String> groupingSeparator = StringPreference.of(GROUPING_SEPARATOR_P_KEY, JsclMathEngine.GROUPING_SEPARATOR_DEFAULT);
|
||||
public static final Preference<String> multiplicationSign = StringPreference.of(MULTIPLICATION_SIGN_P_KEY, MULTIPLICATION_SIGN_DEFAULT);
|
||||
public static final Preference<Integer> precision = StringPreference.ofTypedValue(RESULT_PRECISION_P_KEY, RESULT_PRECISION_DEFAULT, NumberMapper.of(Integer.class));
|
||||
public static final Preference<Boolean> roundResult = BooleanPreference.of(ROUND_RESULT_P_KEY, ROUND_RESULT_DEFAULT);
|
||||
public static final Preference<NumeralBase> numeralBase = StringPreference.ofTypedValue(NUMERAL_BASES_P_KEY, NUMERAL_BASES_DEFAULT, EnumMapper.of(NumeralBase.class));
|
||||
public static final Preference<AngleUnit> angleUnit = StringPreference.ofTypedValue(ANGLE_UNITS_P_KEY, ANGLE_UNITS_DEFAULT, EnumMapper.of(AngleUnit.class));
|
||||
public static final Preference<Boolean> scienceNotation = BooleanPreference.of(SCIENCE_NOTATION_P_KEY, SCIENCE_NOTATION_DEFAULT);
|
||||
|
||||
public static final StringPreference<String> groupingSeparator = StringPreference.of("engine.groupingSeparator", JsclMathEngine.GROUPING_SEPARATOR_DEFAULT);
|
||||
public static final StringPreference<String> multiplicationSign = StringPreference.of("engine.multiplicationSign", "×");
|
||||
public static final StringPreference<NumeralBase> numeralBase = StringPreference.ofTypedValue("engine.numeralBase", "dec", EnumMapper.of(NumeralBase.class));
|
||||
public static final StringPreference<AngleUnit> angleUnit = StringPreference.ofTypedValue("engine.angleUnit", "deg", EnumMapper.of(AngleUnit.class));
|
||||
public static final Preference<Integer> version = IntegerPreference.of("engine.version", 1);
|
||||
private static final List<String> 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());
|
||||
preferenceKeys.add(scienceNotation.getKey());
|
||||
preferenceKeys.add(Output.precision.getKey());
|
||||
preferenceKeys.add(Output.scientificNotation.getKey());
|
||||
preferenceKeys.add(Output.round.getKey());
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public static List<String> getPreferenceKeys() {
|
||||
return Collections.unmodifiableList(preferenceKeys);
|
||||
}
|
||||
|
||||
public static class Output {
|
||||
public static final StringPreference<Integer> precision = StringPreference.ofTypedValue("engine.output.precision", "5", NumberMapper.of(Integer.class));
|
||||
public static final BooleanPreference scientificNotation = BooleanPreference.of("engine.output.scientificNotation", false);
|
||||
public static final BooleanPreference round = BooleanPreference.of("engine.output.round", true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -40,7 +40,7 @@ public class LiteNumberBuilder extends BaseNumberBuilder {
|
||||
|
||||
public LiteNumberBuilder(@Nonnull Engine engine) {
|
||||
super(engine);
|
||||
this.nb = engine.getNumeralBase();
|
||||
this.nb = engine.getMathEngine().getNumeralBase();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -71,7 +71,7 @@ public class LiteNumberBuilder extends BaseNumberBuilder {
|
||||
numberBuilder = null;
|
||||
|
||||
// must set default numeral base (exit numeral base mode)
|
||||
nb = engine.getNumeralBase();
|
||||
nb = engine.getMathEngine().getNumeralBase();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -23,17 +23,24 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import android.text.SpannableStringBuilder;
|
||||
|
||||
import org.solovyev.android.calculator.math.MathType;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import jscl.MathContext;
|
||||
import jscl.MathEngine;
|
||||
import jscl.NumeralBase;
|
||||
import jscl.math.numeric.Real;
|
||||
import jscl.text.*;
|
||||
import org.solovyev.android.calculator.math.MathType;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import jscl.text.DoubleParser;
|
||||
import jscl.text.JsclIntegerParser;
|
||||
import jscl.text.MutableInt;
|
||||
import jscl.text.ParseException;
|
||||
import jscl.text.Parser;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
@@ -193,7 +200,7 @@ public class NumberBuilder extends BaseNumberBuilder {
|
||||
numberBuilder = null;
|
||||
|
||||
// must set default numeral base (exit numeral base mode)
|
||||
nb = engine.getNumeralBase();
|
||||
nb = engine.getMathEngine().getNumeralBase();
|
||||
}
|
||||
|
||||
return replaceNumberInText(sb, number, trimmedChars, localNb, engine.getMathEngine());
|
||||
|
@@ -10,7 +10,12 @@ import android.util.SparseArray;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.widget.ListView;
|
||||
import org.solovyev.android.calculator.*;
|
||||
|
||||
import org.solovyev.android.calculator.AdView;
|
||||
import org.solovyev.android.calculator.App;
|
||||
import org.solovyev.android.calculator.Engine;
|
||||
import org.solovyev.android.calculator.Preferences;
|
||||
import org.solovyev.android.calculator.R;
|
||||
import org.solovyev.android.calculator.language.Language;
|
||||
import org.solovyev.android.calculator.language.Languages;
|
||||
import org.solovyev.android.checkout.BillingRequests;
|
||||
@@ -18,12 +23,11 @@ import org.solovyev.android.checkout.Checkout;
|
||||
import org.solovyev.android.checkout.ProductTypes;
|
||||
import org.solovyev.android.checkout.RequestListener;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.List;
|
||||
|
||||
import static org.solovyev.android.calculator.Engine.Preferences.precision;
|
||||
import static org.solovyev.android.calculator.Engine.Preferences.roundResult;
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import static org.solovyev.android.calculator.wizard.CalculatorWizards.DEFAULT_WIZARD_FLOW;
|
||||
import static org.solovyev.android.wizard.WizardUi.startWizard;
|
||||
|
||||
@@ -118,7 +122,7 @@ public class PreferencesFragment extends org.solovyev.android.material.preferenc
|
||||
});
|
||||
|
||||
final SharedPreferences preferences = App.getPreferences();
|
||||
onSharedPreferenceChanged(preferences, roundResult.getKey());
|
||||
onSharedPreferenceChanged(preferences, Engine.Preferences.Output.round.getKey());
|
||||
}
|
||||
|
||||
private void prepareLanguagePreference(int preference) {
|
||||
@@ -156,10 +160,10 @@ public class PreferencesFragment extends org.solovyev.android.material.preferenc
|
||||
|
||||
@Override
|
||||
public void onSharedPreferenceChanged(SharedPreferences preferences, String key) {
|
||||
if (roundResult.getKey().equals(key)) {
|
||||
final Preference preference = findPreference(precision.getKey());
|
||||
if (Engine.Preferences.Output.round.getKey().equals(key)) {
|
||||
final Preference preference = findPreference(Engine.Preferences.Output.precision.getKey());
|
||||
if (preference != null) {
|
||||
preference.setEnabled(preferences.getBoolean(key, roundResult.getDefaultValue()));
|
||||
preference.setEnabled(preferences.getBoolean(key, Engine.Preferences.Output.round.getDefaultValue()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -48,7 +48,7 @@ public class AngleUnitsButton extends DirectionDragButton {
|
||||
|
||||
public AngleUnitsButton(Context context, @Nonnull AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
this.angleUnit = Locator.getInstance().getEngine().getAngleUnits();
|
||||
this.angleUnit = Locator.getInstance().getEngine().getMathEngine().getAngleUnits();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -1,65 +0,0 @@
|
||||
/*
|
||||
* Copyright 2013 serso aka se.solovyev
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
* Contact details
|
||||
*
|
||||
* Email: se.solovyev@gmail.com
|
||||
* Site: http://se.solovyev.org
|
||||
*/
|
||||
|
||||
package org.solovyev.android.calculator.view;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.util.AttributeSet;
|
||||
import android.widget.TextView;
|
||||
|
||||
import org.solovyev.android.calculator.Engine;
|
||||
import org.solovyev.android.calculator.Locator;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 12/10/11
|
||||
* Time: 10:34 PM
|
||||
*/
|
||||
public class CalculatorAdditionalTitle extends TextView implements SharedPreferences.OnSharedPreferenceChangeListener {
|
||||
|
||||
public CalculatorAdditionalTitle(Context context) {
|
||||
super(context);
|
||||
}
|
||||
|
||||
public CalculatorAdditionalTitle(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
}
|
||||
|
||||
public CalculatorAdditionalTitle(Context context, AttributeSet attrs, int defStyle) {
|
||||
super(context, attrs, defStyle);
|
||||
}
|
||||
|
||||
public void init(@Nonnull SharedPreferences preferences) {
|
||||
onSharedPreferenceChanged(preferences, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSharedPreferenceChanged(SharedPreferences preferences, @Nullable String key) {
|
||||
setText(((Engine) Locator.getInstance().getEngine()).getNumeralBaseFromPrefs(preferences)
|
||||
+ " / " +
|
||||
((Engine) Locator.getInstance().getEngine()).getAngleUnitsFromPrefs(preferences));
|
||||
}
|
||||
}
|
@@ -65,12 +65,12 @@ public class NumeralBaseConverterDialog {
|
||||
String value = initialFromValue;
|
||||
try {
|
||||
value = ToJsclTextProcessor.getInstance().process(value).getExpression();
|
||||
b.setFromValue(UnitImpl.newInstance(value, CalculatorNumeralBase.valueOf(Locator.getInstance().getEngine().getNumeralBase())));
|
||||
b.setFromValue(UnitImpl.newInstance(value, CalculatorNumeralBase.valueOf(Locator.getInstance().getEngine().getMathEngine().getNumeralBase())));
|
||||
} catch (CalculatorParseException e) {
|
||||
b.setFromValue(UnitImpl.newInstance(value, CalculatorNumeralBase.valueOf(Locator.getInstance().getEngine().getNumeralBase())));
|
||||
b.setFromValue(UnitImpl.newInstance(value, CalculatorNumeralBase.valueOf(Locator.getInstance().getEngine().getMathEngine().getNumeralBase())));
|
||||
}
|
||||
} else {
|
||||
b.setFromValue(UnitImpl.newInstance("", CalculatorNumeralBase.valueOf(Locator.getInstance().getEngine().getNumeralBase())));
|
||||
b.setFromValue(UnitImpl.newInstance("", CalculatorNumeralBase.valueOf(Locator.getInstance().getEngine().getMathEngine().getNumeralBase())));
|
||||
}
|
||||
|
||||
b.setConverter(CalculatorNumeralBase.getConverter());
|
||||
@@ -91,7 +91,7 @@ public class NumeralBaseConverterDialog {
|
||||
public void onClick(@Nonnull Unit<String> fromUnits, @Nonnull Unit<String> toUnits) {
|
||||
String toUnitsValue = toUnits.getValue();
|
||||
|
||||
if (!toUnits.getUnitType().equals(CalculatorNumeralBase.valueOf(Locator.getInstance().getEngine().getNumeralBase()))) {
|
||||
if (!toUnits.getUnitType().equals(CalculatorNumeralBase.valueOf(Locator.getInstance().getEngine().getMathEngine().getNumeralBase()))) {
|
||||
toUnitsValue = ((CalculatorNumeralBase) toUnits.getUnitType()).getNumeralBase().getJsclPrefix() + toUnitsValue;
|
||||
}
|
||||
|
||||
|
@@ -47,7 +47,7 @@ public class NumeralBasesButton extends DirectionDragButton {
|
||||
|
||||
public NumeralBasesButton(Context context, @Nonnull AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
this.numeralBase = Locator.getInstance().getEngine().getNumeralBase();
|
||||
this.numeralBase = Locator.getInstance().getEngine().getMathEngine().getNumeralBase();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -23,13 +23,15 @@
|
||||
package org.solovyev.android.calculator.wizard;
|
||||
|
||||
import android.content.SharedPreferences;
|
||||
import jscl.AngleUnit;
|
||||
|
||||
import org.solovyev.android.calculator.Engine;
|
||||
import org.solovyev.android.calculator.Preferences;
|
||||
import org.solovyev.android.calculator.R;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
import jscl.AngleUnit;
|
||||
|
||||
import static org.solovyev.android.calculator.Preferences.Gui.Layout.main_calculator;
|
||||
import static org.solovyev.android.calculator.Preferences.Gui.Layout.main_calculator_mobile;
|
||||
|
||||
@@ -51,8 +53,8 @@ enum CalculatorMode {
|
||||
}
|
||||
Preferences.Calculations.preferredAngleUnits.putPreference(preferences, AngleUnit.deg);
|
||||
Engine.Preferences.angleUnit.putPreference(preferences, AngleUnit.deg);
|
||||
Engine.Preferences.scienceNotation.putPreference(preferences, false);
|
||||
Engine.Preferences.roundResult.putPreference(preferences, true);
|
||||
Engine.Preferences.Output.scientificNotation.putPreference(preferences, false);
|
||||
Engine.Preferences.Output.round.putPreference(preferences, true);
|
||||
}
|
||||
},
|
||||
|
||||
@@ -67,8 +69,8 @@ enum CalculatorMode {
|
||||
}
|
||||
Preferences.Calculations.preferredAngleUnits.putPreference(preferences, AngleUnit.rad);
|
||||
Engine.Preferences.angleUnit.putPreference(preferences, AngleUnit.rad);
|
||||
Engine.Preferences.scienceNotation.putPreference(preferences, true);
|
||||
Engine.Preferences.roundResult.putPreference(preferences, false);
|
||||
Engine.Preferences.Output.scientificNotation.putPreference(preferences, true);
|
||||
Engine.Preferences.Output.round.putPreference(preferences, false);
|
||||
}
|
||||
};
|
||||
|
||||
|
Reference in New Issue
Block a user