Engine refactor
This commit is contained in:
parent
1d366123b3
commit
7faef42dea
@ -40,11 +40,6 @@ import javax.annotation.Nullable;
|
|||||||
import jscl.NumeralBase;
|
import jscl.NumeralBase;
|
||||||
import jscl.math.Generic;
|
import jscl.math.Generic;
|
||||||
|
|
||||||
/**
|
|
||||||
* User: serso
|
|
||||||
* Date: 9/22/12
|
|
||||||
* Time: 5:42 PM
|
|
||||||
*/
|
|
||||||
public class AndroidCalculator implements Calculator, CalculatorEventListener, SharedPreferences.OnSharedPreferenceChangeListener {
|
public class AndroidCalculator implements Calculator, CalculatorEventListener, SharedPreferences.OnSharedPreferenceChangeListener {
|
||||||
|
|
||||||
@Nonnull
|
@Nonnull
|
||||||
@ -61,14 +56,6 @@ public class AndroidCalculator implements Calculator, CalculatorEventListener, S
|
|||||||
PreferenceManager.getDefaultSharedPreferences(application).registerOnSharedPreferenceChangeListener(this);
|
PreferenceManager.getDefaultSharedPreferences(application).registerOnSharedPreferenceChangeListener(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
**********************************************************************
|
|
||||||
*
|
|
||||||
* DELEGATED TO CALCULATOR
|
|
||||||
*
|
|
||||||
**********************************************************************
|
|
||||||
*/
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Nonnull
|
@Nonnull
|
||||||
public CalculatorEventData evaluate(@Nonnull JsclOperation operation, @Nonnull String expression) {
|
public CalculatorEventData evaluate(@Nonnull JsclOperation operation, @Nonnull String expression) {
|
||||||
@ -127,8 +114,8 @@ public class AndroidCalculator implements Calculator, CalculatorEventListener, S
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void init() {
|
public void init(@Nonnull Executor initThread) {
|
||||||
this.calculator.init();
|
this.calculator.init(initThread);
|
||||||
|
|
||||||
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
||||||
this.calculator.setCalculateOnFly(Preferences.Calculations.calculateOnFly.getPreference(prefs));
|
this.calculator.setCalculateOnFly(Preferences.Calculations.calculateOnFly.getPreference(prefs));
|
||||||
|
@ -50,7 +50,7 @@ public abstract class BaseNumberBuilder {
|
|||||||
|
|
||||||
protected BaseNumberBuilder(@Nonnull Engine engine) {
|
protected BaseNumberBuilder(@Nonnull Engine engine) {
|
||||||
this.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() {
|
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
|
@Nonnull
|
||||||
protected NumeralBase getNumeralBase() {
|
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);
|
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 org.solovyev.android.calculator.jscl.JsclOperation;
|
||||||
|
|
||||||
|
import java.util.concurrent.Executor;
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
@ -37,7 +39,7 @@ import jscl.math.Generic;
|
|||||||
*/
|
*/
|
||||||
public interface Calculator extends CalculatorEventContainer {
|
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.os.Handler;
|
||||||
import android.preference.PreferenceManager;
|
import android.preference.PreferenceManager;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
import com.squareup.leakcanary.LeakCanary;
|
import com.squareup.leakcanary.LeakCanary;
|
||||||
import com.squareup.otto.Bus;
|
import com.squareup.otto.Bus;
|
||||||
import jscl.MathEngine;
|
|
||||||
import org.acra.ACRA;
|
import org.acra.ACRA;
|
||||||
import org.acra.ACRAConfiguration;
|
import org.acra.ACRAConfiguration;
|
||||||
import org.acra.sender.HttpSender;
|
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.android.calculator.plot.CalculatorPlotterImpl;
|
||||||
import org.solovyev.common.msg.MessageType;
|
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.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.concurrent.Executor;
|
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 {
|
public class CalculatorApplication extends android.app.Application implements SharedPreferences.OnSharedPreferenceChangeListener {
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
@ -79,6 +83,9 @@ public class CalculatorApplication extends android.app.Application implements Sh
|
|||||||
@Inject
|
@Inject
|
||||||
Calculator calculator;
|
Calculator calculator;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
Engine engine;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
Keyboard keyboard;
|
Keyboard keyboard;
|
||||||
|
|
||||||
@ -103,6 +110,7 @@ public class CalculatorApplication extends android.app.Application implements Sh
|
|||||||
.appModule(new AppModule(this))
|
.appModule(new AppModule(this))
|
||||||
.build();
|
.build();
|
||||||
component.inject(this);
|
component.inject(this);
|
||||||
|
editor.init();
|
||||||
history.init(initThread);
|
history.init(initThread);
|
||||||
|
|
||||||
onPostCreate(preferences, languages);
|
onPostCreate(preferences, languages);
|
||||||
@ -116,7 +124,7 @@ public class CalculatorApplication extends android.app.Application implements Sh
|
|||||||
App.getGa().reportInitially(preferences);
|
App.getGa().reportInitially(preferences);
|
||||||
|
|
||||||
Locator.getInstance().init(calculator,
|
Locator.getInstance().init(calculator,
|
||||||
new Engine(this),
|
engine,
|
||||||
new AndroidCalculatorClipboard(this),
|
new AndroidCalculatorClipboard(this),
|
||||||
new AndroidCalculatorNotifier(this),
|
new AndroidCalculatorNotifier(this),
|
||||||
errorReporter,
|
errorReporter,
|
||||||
@ -130,7 +138,7 @@ public class CalculatorApplication extends android.app.Application implements Sh
|
|||||||
calculator.addCalculatorEventListener(listener);
|
calculator.addCalculatorEventListener(listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
Locator.getInstance().getCalculator().init();
|
calculator.init(initThread);
|
||||||
|
|
||||||
initThread.execute(new Runnable() {
|
initThread.execute(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
@ -143,7 +151,7 @@ public class CalculatorApplication extends android.app.Application implements Sh
|
|||||||
private void warmUpEngine() {
|
private void warmUpEngine() {
|
||||||
try {
|
try {
|
||||||
// warm-up engine
|
// warm-up engine
|
||||||
MathEngine mathEngine = Locator.getInstance().getEngine().getMathEngine();
|
final MathEngine mathEngine = engine.getMathEngine();
|
||||||
mathEngine.evaluate("1+1");
|
mathEngine.evaluate("1+1");
|
||||||
mathEngine.evaluate("1*1");
|
mathEngine.evaluate("1*1");
|
||||||
} catch (Throwable e) {
|
} catch (Throwable e) {
|
||||||
|
@ -70,16 +70,6 @@ public enum CalculatorEventType {
|
|||||||
|
|
||||||
conversion_finished,
|
conversion_finished,
|
||||||
|
|
||||||
/*
|
|
||||||
**********************************************************************
|
|
||||||
*
|
|
||||||
* ENGINE
|
|
||||||
*
|
|
||||||
**********************************************************************
|
|
||||||
*/
|
|
||||||
|
|
||||||
engine_preferences_changed,
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
**********************************************************************
|
**********************************************************************
|
||||||
*
|
*
|
||||||
|
@ -24,17 +24,10 @@ package org.solovyev.android.calculator;
|
|||||||
|
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
import com.squareup.otto.Bus;
|
import com.squareup.otto.Bus;
|
||||||
import com.squareup.otto.Subscribe;
|
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.jscl.JsclOperation;
|
||||||
import org.solovyev.android.calculator.model.Var;
|
import org.solovyev.android.calculator.model.Var;
|
||||||
import org.solovyev.android.calculator.text.TextProcessor;
|
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.ConversionException;
|
||||||
import org.solovyev.common.units.Conversions;
|
import org.solovyev.common.units.Conversions;
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
|
||||||
import javax.annotation.Nullable;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.Executor;
|
import java.util.concurrent.Executor;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
import java.util.concurrent.atomic.AtomicLong;
|
import java.util.concurrent.atomic.AtomicLong;
|
||||||
|
|
||||||
/**
|
import javax.annotation.Nonnull;
|
||||||
* User: Solovyev_S
|
import javax.annotation.Nullable;
|
||||||
* Date: 20.09.12
|
|
||||||
* Time: 16:42
|
|
||||||
*/
|
|
||||||
public class CalculatorImpl implements Calculator, CalculatorEventListener {
|
|
||||||
|
|
||||||
/*
|
import jscl.AbstractJsclArithmeticException;
|
||||||
**********************************************************************
|
import jscl.MathEngine;
|
||||||
*
|
import jscl.NumeralBase;
|
||||||
* CONSTANTS
|
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
|
// one minute
|
||||||
private static final long PREFERENCE_CHECK_INTERVAL = 1000L * 60L;
|
private static final long PREFERENCE_CHECK_INTERVAL = 1000L * 60L;
|
||||||
|
|
||||||
/*
|
|
||||||
**********************************************************************
|
|
||||||
*
|
|
||||||
* FIELDS
|
|
||||||
*
|
|
||||||
**********************************************************************
|
|
||||||
*/
|
|
||||||
|
|
||||||
@Nonnull
|
@Nonnull
|
||||||
private final CalculatorEventContainer calculatorEventContainer = new ListCalculatorEventContainer();
|
private final CalculatorEventContainer calculatorEventContainer = new ListCalculatorEventContainer();
|
||||||
|
|
||||||
@ -205,8 +188,8 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void init() {
|
public void init(@Nonnull Executor initThread) {
|
||||||
Locator.getInstance().getEngine().init();
|
Locator.getInstance().getEngine().init(initThread);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -373,7 +356,7 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
|
|||||||
final CalculatorEventData eventDataId = nextEventData();
|
final CalculatorEventData eventDataId = nextEventData();
|
||||||
|
|
||||||
final DisplayState displayViewState = App.getDisplay().getState();
|
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() {
|
calculationsExecutor.execute(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
@ -524,11 +507,6 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
|
|||||||
case function_removed:
|
case function_removed:
|
||||||
evaluate();
|
evaluate();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case engine_preferences_changed:
|
|
||||||
evaluate(calculatorEventData.getSequenceId());
|
|
||||||
break;
|
|
||||||
|
|
||||||
case use_constant:
|
case use_constant:
|
||||||
final IConstant constant = (IConstant) data;
|
final IConstant constant = (IConstant) data;
|
||||||
Locator.getInstance().getKeyboard().buttonPressed(constant.getName());
|
Locator.getInstance().getKeyboard().buttonPressed(constant.getName());
|
||||||
|
@ -56,7 +56,7 @@ enum ConversionMenuItem implements AMenuItem<DisplayState> {
|
|||||||
if (operation == JsclOperation.numeric) {
|
if (operation == JsclOperation.numeric) {
|
||||||
if (generic.getConstants().isEmpty()) {
|
if (generic.getConstants().isEmpty()) {
|
||||||
// conversion possible => return true
|
// conversion possible => return true
|
||||||
final NumeralBase fromNumeralBase = Locator.getInstance().getEngine().getNumeralBase();
|
final NumeralBase fromNumeralBase = Locator.getInstance().getEngine().getMathEngine().getNumeralBase();
|
||||||
if (fromNumeralBase != toNumeralBase) {
|
if (fromNumeralBase != toNumeralBase) {
|
||||||
result = Locator.getInstance().getCalculator().isConversionPossible(generic, fromNumeralBase, this.toNumeralBase);
|
result = Locator.getInstance().getCalculator().isConversionPossible(generic, fromNumeralBase, this.toNumeralBase);
|
||||||
}
|
}
|
||||||
|
@ -25,6 +25,7 @@ package org.solovyev.android.calculator;
|
|||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
|
|
||||||
import com.squareup.otto.Bus;
|
import com.squareup.otto.Bus;
|
||||||
|
import com.squareup.otto.Subscribe;
|
||||||
|
|
||||||
import org.solovyev.android.Check;
|
import org.solovyev.android.Check;
|
||||||
import org.solovyev.android.calculator.text.TextProcessorEditorResult;
|
import org.solovyev.android.calculator.text.TextProcessorEditorResult;
|
||||||
@ -40,7 +41,6 @@ import static java.lang.Math.min;
|
|||||||
@Singleton
|
@Singleton
|
||||||
public class Editor {
|
public class Editor {
|
||||||
|
|
||||||
private static final String TAG = App.subTag("Editor");
|
|
||||||
@Nullable
|
@Nullable
|
||||||
private final EditorTextProcessor textProcessor;
|
private final EditorTextProcessor textProcessor;
|
||||||
@Nullable
|
@Nullable
|
||||||
@ -55,6 +55,10 @@ public class Editor {
|
|||||||
textProcessor = new EditorTextProcessor(preferences);
|
textProcessor = new EditorTextProcessor(preferences);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void init() {
|
||||||
|
bus.register(this);
|
||||||
|
}
|
||||||
|
|
||||||
public static int clamp(int selection, @Nonnull CharSequence text) {
|
public static int clamp(int selection, @Nonnull CharSequence text) {
|
||||||
return clamp(selection, text.length());
|
return clamp(selection, text.length());
|
||||||
}
|
}
|
||||||
@ -214,6 +218,13 @@ public class Editor {
|
|||||||
return onSelectionChanged(EditorState.forNewSelection(state, clamp(selection, state.text)));
|
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 {
|
public static class ChangedEvent {
|
||||||
@Nonnull
|
@Nonnull
|
||||||
public final EditorState oldState;
|
public final EditorState oldState;
|
||||||
|
@ -23,9 +23,30 @@
|
|||||||
package org.solovyev.android.calculator;
|
package org.solovyev.android.calculator;
|
||||||
|
|
||||||
import android.app.Application;
|
import android.app.Application;
|
||||||
import android.content.Context;
|
|
||||||
import android.content.SharedPreferences;
|
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.AngleUnit;
|
||||||
import jscl.JsclMathEngine;
|
import jscl.JsclMathEngine;
|
||||||
import jscl.MathEngine;
|
import jscl.MathEngine;
|
||||||
@ -33,48 +54,10 @@ import jscl.NumeralBase;
|
|||||||
import jscl.math.function.Function;
|
import jscl.math.function.Function;
|
||||||
import jscl.math.function.IConstant;
|
import jscl.math.function.IConstant;
|
||||||
import jscl.math.operator.Operator;
|
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 {
|
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
|
@Nonnull
|
||||||
private final MathEngine mathEngine;
|
private final MathEngine mathEngine;
|
||||||
@Nonnull
|
@Nonnull
|
||||||
@ -85,12 +68,16 @@ public class Engine implements SharedPreferences.OnSharedPreferenceChangeListene
|
|||||||
private final EntitiesRegistry<Operator> operatorsRegistry;
|
private final EntitiesRegistry<Operator> operatorsRegistry;
|
||||||
@Nonnull
|
@Nonnull
|
||||||
private final EntitiesRegistry<Operator> postfixFunctionsRegistry;
|
private final EntitiesRegistry<Operator> postfixFunctionsRegistry;
|
||||||
|
@Inject
|
||||||
|
SharedPreferences preferences;
|
||||||
|
@Inject
|
||||||
|
Bus bus;
|
||||||
|
@Inject
|
||||||
|
ErrorReporter errorReporter;
|
||||||
@Nonnull
|
@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) {
|
public Engine(@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();
|
|
||||||
this.mathEngine = mathEngine;
|
this.mathEngine = mathEngine;
|
||||||
this.varsRegistry = varsRegistry;
|
this.varsRegistry = varsRegistry;
|
||||||
this.functionsRegistry = functionsRegistry;
|
this.functionsRegistry = functionsRegistry;
|
||||||
@ -98,13 +85,10 @@ public class Engine implements SharedPreferences.OnSharedPreferenceChangeListene
|
|||||||
this.postfixFunctionsRegistry = postfixFunctionsRegistry;
|
this.postfixFunctionsRegistry = postfixFunctionsRegistry;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Inject
|
||||||
public Engine(@Nonnull Application application) {
|
public Engine(@Nonnull Application application) {
|
||||||
this.mathEngine = JsclMathEngine.getInstance();
|
this.mathEngine = JsclMathEngine.getInstance();
|
||||||
this.context = application;
|
|
||||||
|
|
||||||
PreferenceManager.getDefaultSharedPreferences(application).registerOnSharedPreferenceChangeListener(this);
|
|
||||||
|
|
||||||
this.lock = new Object();
|
|
||||||
this.mathEngine.setRoundResult(true);
|
this.mathEngine.setRoundResult(true);
|
||||||
this.mathEngine.setUseGroupingSeparator(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));
|
this.postfixFunctionsRegistry = new PostfixFunctionsRegistry(mathEngine.getPostfixFunctionsRegistry(), new AndroidMathEntityDao<>(null, application, null));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nonnull
|
private static void migratePreference(@Nonnull SharedPreferences preferences, @Nonnull BooleanPreference preference, @Nonnull String oldKey, @Nonnull SharedPreferences.Editor editor) {
|
||||||
public static NumeralBase getNumeralBaseFromPrefs(@Nonnull SharedPreferences preferences) {
|
if (!preferences.contains(oldKey)) {
|
||||||
return Preferences.numeralBase.getPreference(preferences);
|
return;
|
||||||
|
}
|
||||||
|
editor.putBoolean(preference.getKey(), preferences.getBoolean(oldKey, false));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nonnull
|
private static void migratePreference(@Nonnull SharedPreferences preferences, @Nonnull StringPreference<?> preference, @Nonnull String oldKey, @Nonnull SharedPreferences.Editor editor) {
|
||||||
public static AngleUnit getAngleUnitsFromPrefs(@Nonnull SharedPreferences preferences) {
|
if (!preferences.contains(oldKey)) {
|
||||||
return Preferences.angleUnit.getPreference(preferences);
|
return;
|
||||||
|
}
|
||||||
|
editor.putString(preference.getKey(), preferences.getString(oldKey, null));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nonnull
|
@Nonnull
|
||||||
public EntitiesRegistry<IConstant> getVarsRegistry() {
|
public EntitiesRegistry<IConstant> getVarsRegistry() {
|
||||||
return this.varsRegistry;
|
return varsRegistry;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nonnull
|
@Nonnull
|
||||||
public EntitiesRegistry<Function> getFunctionsRegistry() {
|
public EntitiesRegistry<Function> getFunctionsRegistry() {
|
||||||
return this.functionsRegistry;
|
return functionsRegistry;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nonnull
|
@Nonnull
|
||||||
public EntitiesRegistry<Operator> getOperatorsRegistry() {
|
public EntitiesRegistry<Operator> getOperatorsRegistry() {
|
||||||
return this.operatorsRegistry;
|
return operatorsRegistry;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nonnull
|
@Nonnull
|
||||||
public EntitiesRegistry<Operator> getPostfixFunctionsRegistry() {
|
public EntitiesRegistry<Operator> getPostfixFunctionsRegistry() {
|
||||||
return this.postfixFunctionsRegistry;
|
return postfixFunctionsRegistry;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nonnull
|
@Nonnull
|
||||||
@ -149,45 +137,75 @@ public class Engine implements SharedPreferences.OnSharedPreferenceChangeListene
|
|||||||
return mathEngine;
|
return mathEngine;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void init() {
|
public void init(@Nonnull Executor initThread) {
|
||||||
synchronized (lock) {
|
checkPreferences();
|
||||||
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
|
preferences.registerOnSharedPreferenceChangeListener(this);
|
||||||
resetPreferences(preferences);
|
applyPreferences();
|
||||||
safeLoadRegistry(varsRegistry);
|
initThread.execute(new Runnable() {
|
||||||
safeLoadRegistry(functionsRegistry);
|
@Override
|
||||||
safeLoadRegistry(operatorsRegistry);
|
public void run() {
|
||||||
safeLoadRegistry(postfixFunctionsRegistry);
|
initAsync();
|
||||||
}
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public void resetPreferences() {
|
private void checkPreferences() {
|
||||||
resetPreferences(App.getPreferences());
|
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) {
|
private void safeLoadRegistry(@Nonnull EntitiesRegistry<?> registry) {
|
||||||
try {
|
try {
|
||||||
registry.load();
|
registry.load();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
Locator.getInstance().getErrorReporter().onException(e);
|
errorReporter.onException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void resetPreferences(@Nonnull SharedPreferences preferences) {
|
private void applyPreferences() {
|
||||||
setPrecision(Preferences.precision.getPreference(preferences));
|
mathEngine.setAngleUnits(Preferences.angleUnit.getPreference(preferences));
|
||||||
setRoundResult(Preferences.roundResult.getPreference(preferences));
|
mathEngine.setNumeralBase(Preferences.numeralBase.getPreference(preferences));
|
||||||
setAngleUnits(getAngleUnitsFromPrefs(preferences));
|
|
||||||
setNumeralBase(getNumeralBaseFromPrefs(preferences));
|
|
||||||
setMultiplicationSign(Preferences.multiplicationSign.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);
|
final String groupingSeparator = Preferences.groupingSeparator.getPreference(preferences);
|
||||||
if (Strings.isEmpty(groupingSeparator)) {
|
if (Strings.isEmpty(groupingSeparator)) {
|
||||||
setUseGroupingSeparator(false);
|
mathEngine.setUseGroupingSeparator(false);
|
||||||
} else {
|
} else {
|
||||||
setUseGroupingSeparator(true);
|
mathEngine.setUseGroupingSeparator(true);
|
||||||
setGroupingSeparator(groupingSeparator.charAt(0));
|
mathEngine.setGroupingSeparator(groupingSeparator.charAt(0));
|
||||||
}
|
}
|
||||||
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.engine_preferences_changed, null);
|
bus.post(ChangedEvent.INSTANCE);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nonnull
|
@Nonnull
|
||||||
@ -199,99 +217,46 @@ public class Engine implements SharedPreferences.OnSharedPreferenceChangeListene
|
|||||||
this.multiplicationSign = multiplicationSign;
|
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
|
@Override
|
||||||
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
|
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
|
||||||
if (Preferences.getPreferenceKeys().contains(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 class Preferences {
|
||||||
public static final Preference<String> groupingSeparator = StringPreference.of(GROUPING_SEPARATOR_P_KEY, JsclMathEngine.GROUPING_SEPARATOR_DEFAULT);
|
public static final StringPreference<String> groupingSeparator = StringPreference.of("engine.groupingSeparator", JsclMathEngine.GROUPING_SEPARATOR_DEFAULT);
|
||||||
public static final Preference<String> multiplicationSign = StringPreference.of(MULTIPLICATION_SIGN_P_KEY, MULTIPLICATION_SIGN_DEFAULT);
|
public static final StringPreference<String> multiplicationSign = StringPreference.of("engine.multiplicationSign", "×");
|
||||||
public static final Preference<Integer> precision = StringPreference.ofTypedValue(RESULT_PRECISION_P_KEY, RESULT_PRECISION_DEFAULT, NumberMapper.of(Integer.class));
|
public static final StringPreference<NumeralBase> numeralBase = StringPreference.ofTypedValue("engine.numeralBase", "dec", EnumMapper.of(NumeralBase.class));
|
||||||
public static final Preference<Boolean> roundResult = BooleanPreference.of(ROUND_RESULT_P_KEY, ROUND_RESULT_DEFAULT);
|
public static final StringPreference<AngleUnit> angleUnit = StringPreference.ofTypedValue("engine.angleUnit", "deg", EnumMapper.of(AngleUnit.class));
|
||||||
public static final Preference<NumeralBase> numeralBase = StringPreference.ofTypedValue(NUMERAL_BASES_P_KEY, NUMERAL_BASES_DEFAULT, EnumMapper.of(NumeralBase.class));
|
public static final Preference<Integer> version = IntegerPreference.of("engine.version", 1);
|
||||||
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);
|
|
||||||
|
|
||||||
private static final List<String> preferenceKeys = new ArrayList<>();
|
private static final List<String> preferenceKeys = new ArrayList<>();
|
||||||
|
|
||||||
static {
|
static {
|
||||||
preferenceKeys.add(groupingSeparator.getKey());
|
preferenceKeys.add(groupingSeparator.getKey());
|
||||||
preferenceKeys.add(multiplicationSign.getKey());
|
preferenceKeys.add(multiplicationSign.getKey());
|
||||||
preferenceKeys.add(precision.getKey());
|
|
||||||
preferenceKeys.add(roundResult.getKey());
|
|
||||||
preferenceKeys.add(numeralBase.getKey());
|
preferenceKeys.add(numeralBase.getKey());
|
||||||
preferenceKeys.add(angleUnit.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
|
@Nonnull
|
||||||
public static List<String> getPreferenceKeys() {
|
public static List<String> getPreferenceKeys() {
|
||||||
return Collections.unmodifiableList(preferenceKeys);
|
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) {
|
public LiteNumberBuilder(@Nonnull Engine engine) {
|
||||||
super(engine);
|
super(engine);
|
||||||
this.nb = engine.getNumeralBase();
|
this.nb = engine.getMathEngine().getNumeralBase();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -71,7 +71,7 @@ public class LiteNumberBuilder extends BaseNumberBuilder {
|
|||||||
numberBuilder = null;
|
numberBuilder = null;
|
||||||
|
|
||||||
// must set default numeral base (exit numeral base mode)
|
// 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;
|
package org.solovyev.android.calculator;
|
||||||
|
|
||||||
import android.text.SpannableStringBuilder;
|
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.MathContext;
|
||||||
import jscl.MathEngine;
|
import jscl.MathEngine;
|
||||||
import jscl.NumeralBase;
|
import jscl.NumeralBase;
|
||||||
import jscl.math.numeric.Real;
|
import jscl.math.numeric.Real;
|
||||||
import jscl.text.*;
|
import jscl.text.DoubleParser;
|
||||||
import org.solovyev.android.calculator.math.MathType;
|
import jscl.text.JsclIntegerParser;
|
||||||
|
import jscl.text.MutableInt;
|
||||||
import javax.annotation.Nonnull;
|
import jscl.text.ParseException;
|
||||||
import javax.annotation.Nullable;
|
import jscl.text.Parser;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* User: serso
|
* User: serso
|
||||||
@ -193,7 +200,7 @@ public class NumberBuilder extends BaseNumberBuilder {
|
|||||||
numberBuilder = null;
|
numberBuilder = null;
|
||||||
|
|
||||||
// must set default numeral base (exit numeral base mode)
|
// must set default numeral base (exit numeral base mode)
|
||||||
nb = engine.getNumeralBase();
|
nb = engine.getMathEngine().getNumeralBase();
|
||||||
}
|
}
|
||||||
|
|
||||||
return replaceNumberInText(sb, number, trimmedChars, localNb, engine.getMathEngine());
|
return replaceNumberInText(sb, number, trimmedChars, localNb, engine.getMathEngine());
|
||||||
|
@ -10,7 +10,12 @@ import android.util.SparseArray;
|
|||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.widget.ListView;
|
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.Language;
|
||||||
import org.solovyev.android.calculator.language.Languages;
|
import org.solovyev.android.calculator.language.Languages;
|
||||||
import org.solovyev.android.checkout.BillingRequests;
|
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.ProductTypes;
|
||||||
import org.solovyev.android.checkout.RequestListener;
|
import org.solovyev.android.checkout.RequestListener;
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
|
||||||
import javax.annotation.Nullable;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static org.solovyev.android.calculator.Engine.Preferences.precision;
|
import javax.annotation.Nonnull;
|
||||||
import static org.solovyev.android.calculator.Engine.Preferences.roundResult;
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
import static org.solovyev.android.calculator.wizard.CalculatorWizards.DEFAULT_WIZARD_FLOW;
|
import static org.solovyev.android.calculator.wizard.CalculatorWizards.DEFAULT_WIZARD_FLOW;
|
||||||
import static org.solovyev.android.wizard.WizardUi.startWizard;
|
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();
|
final SharedPreferences preferences = App.getPreferences();
|
||||||
onSharedPreferenceChanged(preferences, roundResult.getKey());
|
onSharedPreferenceChanged(preferences, Engine.Preferences.Output.round.getKey());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void prepareLanguagePreference(int preference) {
|
private void prepareLanguagePreference(int preference) {
|
||||||
@ -156,10 +160,10 @@ public class PreferencesFragment extends org.solovyev.android.material.preferenc
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onSharedPreferenceChanged(SharedPreferences preferences, String key) {
|
public void onSharedPreferenceChanged(SharedPreferences preferences, String key) {
|
||||||
if (roundResult.getKey().equals(key)) {
|
if (Engine.Preferences.Output.round.getKey().equals(key)) {
|
||||||
final Preference preference = findPreference(precision.getKey());
|
final Preference preference = findPreference(Engine.Preferences.Output.precision.getKey());
|
||||||
if (preference != null) {
|
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) {
|
public AngleUnitsButton(Context context, @Nonnull AttributeSet attrs) {
|
||||||
super(context, attrs);
|
super(context, attrs);
|
||||||
this.angleUnit = Locator.getInstance().getEngine().getAngleUnits();
|
this.angleUnit = Locator.getInstance().getEngine().getMathEngine().getAngleUnits();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@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;
|
String value = initialFromValue;
|
||||||
try {
|
try {
|
||||||
value = ToJsclTextProcessor.getInstance().process(value).getExpression();
|
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) {
|
} 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 {
|
} 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());
|
b.setConverter(CalculatorNumeralBase.getConverter());
|
||||||
@ -91,7 +91,7 @@ public class NumeralBaseConverterDialog {
|
|||||||
public void onClick(@Nonnull Unit<String> fromUnits, @Nonnull Unit<String> toUnits) {
|
public void onClick(@Nonnull Unit<String> fromUnits, @Nonnull Unit<String> toUnits) {
|
||||||
String toUnitsValue = toUnits.getValue();
|
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;
|
toUnitsValue = ((CalculatorNumeralBase) toUnits.getUnitType()).getNumeralBase().getJsclPrefix() + toUnitsValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -47,7 +47,7 @@ public class NumeralBasesButton extends DirectionDragButton {
|
|||||||
|
|
||||||
public NumeralBasesButton(Context context, @Nonnull AttributeSet attrs) {
|
public NumeralBasesButton(Context context, @Nonnull AttributeSet attrs) {
|
||||||
super(context, attrs);
|
super(context, attrs);
|
||||||
this.numeralBase = Locator.getInstance().getEngine().getNumeralBase();
|
this.numeralBase = Locator.getInstance().getEngine().getMathEngine().getNumeralBase();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -23,13 +23,15 @@
|
|||||||
package org.solovyev.android.calculator.wizard;
|
package org.solovyev.android.calculator.wizard;
|
||||||
|
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
import jscl.AngleUnit;
|
|
||||||
import org.solovyev.android.calculator.Engine;
|
import org.solovyev.android.calculator.Engine;
|
||||||
import org.solovyev.android.calculator.Preferences;
|
import org.solovyev.android.calculator.Preferences;
|
||||||
import org.solovyev.android.calculator.R;
|
import org.solovyev.android.calculator.R;
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
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;
|
||||||
import static org.solovyev.android.calculator.Preferences.Gui.Layout.main_calculator_mobile;
|
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);
|
Preferences.Calculations.preferredAngleUnits.putPreference(preferences, AngleUnit.deg);
|
||||||
Engine.Preferences.angleUnit.putPreference(preferences, AngleUnit.deg);
|
Engine.Preferences.angleUnit.putPreference(preferences, AngleUnit.deg);
|
||||||
Engine.Preferences.scienceNotation.putPreference(preferences, false);
|
Engine.Preferences.Output.scientificNotation.putPreference(preferences, false);
|
||||||
Engine.Preferences.roundResult.putPreference(preferences, true);
|
Engine.Preferences.Output.round.putPreference(preferences, true);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -67,8 +69,8 @@ enum CalculatorMode {
|
|||||||
}
|
}
|
||||||
Preferences.Calculations.preferredAngleUnits.putPreference(preferences, AngleUnit.rad);
|
Preferences.Calculations.preferredAngleUnits.putPreference(preferences, AngleUnit.rad);
|
||||||
Engine.Preferences.angleUnit.putPreference(preferences, AngleUnit.rad);
|
Engine.Preferences.angleUnit.putPreference(preferences, AngleUnit.rad);
|
||||||
Engine.Preferences.scienceNotation.putPreference(preferences, true);
|
Engine.Preferences.Output.scientificNotation.putPreference(preferences, true);
|
||||||
Engine.Preferences.roundResult.putPreference(preferences, false);
|
Engine.Preferences.Output.round.putPreference(preferences, false);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -21,9 +21,6 @@
|
|||||||
-->
|
-->
|
||||||
|
|
||||||
<resources>
|
<resources>
|
||||||
<string name="p_calc_result_precision_key" translatable="false">org.solovyev.android.calculator.CalculatorModel_result_precision</string>
|
|
||||||
<string name="p_calc_result_precision" translatable="false">5</string>
|
|
||||||
|
|
||||||
<string name="p_calc_color_display_key" translatable="false">org.solovyev.android.calculator.CalculatorModel_color_display</string>
|
<string name="p_calc_color_display_key" translatable="false">org.solovyev.android.calculator.CalculatorModel_color_display</string>
|
||||||
<string name="p_calc_color_display" translatable="false">true</string>
|
<string name="p_calc_color_display" translatable="false">true</string>
|
||||||
|
|
||||||
@ -39,36 +36,14 @@
|
|||||||
</string>
|
</string>
|
||||||
<string name="p_calc_show_release_notes" translatable="false">true</string>
|
<string name="p_calc_show_release_notes" translatable="false">true</string>
|
||||||
|
|
||||||
<string name="p_calc_round_result_key" translatable="false">org.solovyev.android.calculator.CalculatorModel_round_result</string>
|
|
||||||
<string name="p_calc_round_result" translatable="false">true</string>
|
|
||||||
|
|
||||||
<string name="p_calc_science_notation_key" translatable="false">calculation.output.science_notation</string>
|
|
||||||
<string name="p_calc_science_notation" translatable="false">false</string>
|
|
||||||
|
|
||||||
<string name="p_calc_history" translatable="false">org.solovyev.android.calculator.CalculatorModel_history</string>
|
<string name="p_calc_history" translatable="false">org.solovyev.android.calculator.CalculatorModel_history</string>
|
||||||
|
|
||||||
<string name="p_calc_angle_units_key" translatable="false">org.solovyev.android.calculator.CalculatorActivity_angle_units</string>
|
|
||||||
<string name="p_calc_angle_units" translatable="false">deg</string>
|
|
||||||
|
|
||||||
<string name="p_calc_numeral_bases_key" translatable="false">org.solovyev.android.calculator.CalculatorActivity_numeral_bases</string>
|
|
||||||
<string name="p_calc_numeral_bases" translatable="false">dec</string>
|
|
||||||
|
|
||||||
<string name="p_calc_theme_key" translatable="false">org.solovyev.android.calculator.CalculatorActivity_calc_theme</string>
|
<string name="p_calc_theme_key" translatable="false">org.solovyev.android.calculator.CalculatorActivity_calc_theme</string>
|
||||||
<string name="p_calc_theme" translatable="false">default_theme</string>
|
<string name="p_calc_theme" translatable="false">default_theme</string>
|
||||||
|
|
||||||
<string name="p_calc_layout_key" translatable="false">org.solovyev.android.calculator.CalculatorActivity_calc_layout</string>
|
<string name="p_calc_layout_key" translatable="false">org.solovyev.android.calculator.CalculatorActivity_calc_layout</string>
|
||||||
<string name="p_calc_layout" translatable="false">main_calculator</string>
|
<string name="p_calc_layout" translatable="false">main_calculator</string>
|
||||||
|
|
||||||
<string name="p_calc_grouping_separator_key" translatable="false">
|
|
||||||
org.solovyev.android.calculator.CalculatorActivity_calc_grouping_separator
|
|
||||||
</string>
|
|
||||||
<string name="p_calc_grouping_separator" translatable="false">" "</string>
|
|
||||||
|
|
||||||
<string name="p_calc_multiplication_sign_key" translatable="false">
|
|
||||||
org.solovyev.android.calculator.CalculatorActivity_calc_multiplication_sign
|
|
||||||
</string>
|
|
||||||
<string name="p_calc_multiplication_sign" translatable="false">"×"</string>
|
|
||||||
|
|
||||||
<string name="p_calc_ad_free" translatable="false">false</string>
|
<string name="p_calc_ad_free" translatable="false">false</string>
|
||||||
|
|
||||||
</resources>
|
</resources>
|
@ -39,7 +39,7 @@
|
|||||||
<ListPreference
|
<ListPreference
|
||||||
a:entries="@array/p_multiplication_sign_values"
|
a:entries="@array/p_multiplication_sign_values"
|
||||||
a:entryValues="@array/p_multiplication_sign_values"
|
a:entryValues="@array/p_multiplication_sign_values"
|
||||||
a:key="@string/p_calc_multiplication_sign_key"
|
a:key="engine.multiplicationSign"
|
||||||
a:summary="@string/c_calc_multiplication_sign_summary"
|
a:summary="@string/c_calc_multiplication_sign_summary"
|
||||||
a:title="@string/c_calc_multiplication_sign" />
|
a:title="@string/c_calc_multiplication_sign" />
|
||||||
|
|
||||||
|
@ -27,27 +27,27 @@
|
|||||||
|
|
||||||
<android.preference.CheckBoxPreference
|
<android.preference.CheckBoxPreference
|
||||||
a:defaultValue="true"
|
a:defaultValue="true"
|
||||||
a:key="@string/p_calc_round_result_key"
|
a:key="engine.output.round"
|
||||||
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" />
|
||||||
|
|
||||||
<org.solovyev.android.prefs.IntegerPickerDialogPreference
|
<org.solovyev.android.prefs.IntegerPickerDialogPreference
|
||||||
a:defaultValue="5"
|
a:defaultValue="5"
|
||||||
a:key="@string/p_calc_result_precision_key"
|
a:key="engine.output.precision"
|
||||||
a:summary="@string/c_calc_result_precision_summary"
|
a:summary="@string/c_calc_result_precision_summary"
|
||||||
a:title="@string/p_calc_result_precision_title"
|
a:title="@string/p_calc_result_precision_title"
|
||||||
range:boundaries="0;16" />
|
range:boundaries="0;16" />
|
||||||
|
|
||||||
<android.preference.CheckBoxPreference
|
<android.preference.CheckBoxPreference
|
||||||
a:defaultValue="false"
|
a:defaultValue="false"
|
||||||
a:key="@string/p_calc_science_notation_key"
|
a:key="engine.output.scientificNotation"
|
||||||
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" />
|
||||||
|
|
||||||
<ListPreference
|
<ListPreference
|
||||||
a:entries="@array/p_grouping_separator_names"
|
a:entries="@array/p_grouping_separator_names"
|
||||||
a:entryValues="@array/p_grouping_separator_values"
|
a:entryValues="@array/p_grouping_separator_values"
|
||||||
a:key="@string/p_calc_grouping_separator_key"
|
a:key="engine.groupingSeparator"
|
||||||
a:summary="@string/c_calc_grouping_separator_summary"
|
a:summary="@string/c_calc_grouping_separator_summary"
|
||||||
a:title="@string/c_calc_grouping_separator" />
|
a:title="@string/c_calc_grouping_separator" />
|
||||||
|
|
||||||
@ -61,7 +61,7 @@
|
|||||||
<ListPreference
|
<ListPreference
|
||||||
a:entries="@array/p_angle_units_names"
|
a:entries="@array/p_angle_units_names"
|
||||||
a:entryValues="@array/p_angle_units"
|
a:entryValues="@array/p_angle_units"
|
||||||
a:key="@string/p_calc_angle_units_key"
|
a:key="engine.angleUnit"
|
||||||
a:summary="@string/c_angle_units_summary"
|
a:summary="@string/c_angle_units_summary"
|
||||||
a:title="@string/c_calc_angle_units" />
|
a:title="@string/c_calc_angle_units" />
|
||||||
|
|
||||||
@ -75,7 +75,7 @@
|
|||||||
<ListPreference
|
<ListPreference
|
||||||
a:entries="@array/p_numeral_bases_names"
|
a:entries="@array/p_numeral_bases_names"
|
||||||
a:entryValues="@array/p_numeral_bases"
|
a:entryValues="@array/p_numeral_bases"
|
||||||
a:key="@string/p_calc_numeral_bases_key"
|
a:key="engine.numeralBase"
|
||||||
a:summary="@string/c_numeral_bases_summary"
|
a:summary="@string/c_numeral_bases_summary"
|
||||||
a:title="@string/c_calc_numeral_bases" />
|
a:title="@string/c_calc_numeral_bases" />
|
||||||
|
|
||||||
|
@ -37,7 +37,12 @@ public class AbstractCalculatorTest {
|
|||||||
|
|
||||||
protected void setUp() throws Exception {
|
protected void setUp() throws Exception {
|
||||||
Locator.getInstance().init(new CalculatorImpl(Mockito.mock(Bus.class), Mockito.mock(Executor.class)), CalculatorTestUtils.newCalculatorEngine(), Mockito.mock(CalculatorClipboard.class), Mockito.mock(CalculatorNotifier.class), new SystemErrorReporter(), Mockito.mock(CalculatorPreferenceService.class), Mockito.mock(Keyboard.class), Mockito.mock(CalculatorPlotter.class));
|
Locator.getInstance().init(new CalculatorImpl(Mockito.mock(Bus.class), Mockito.mock(Executor.class)), CalculatorTestUtils.newCalculatorEngine(), Mockito.mock(CalculatorClipboard.class), Mockito.mock(CalculatorNotifier.class), new SystemErrorReporter(), Mockito.mock(CalculatorPreferenceService.class), Mockito.mock(Keyboard.class), Mockito.mock(CalculatorPlotter.class));
|
||||||
Locator.getInstance().getEngine().init();
|
Locator.getInstance().getEngine().init(new Executor() {
|
||||||
|
@Override
|
||||||
|
public void execute(Runnable command) {
|
||||||
|
command.run();
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -23,19 +23,22 @@
|
|||||||
package org.solovyev.android.calculator;
|
package org.solovyev.android.calculator;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
|
||||||
import com.squareup.otto.Bus;
|
import com.squareup.otto.Bus;
|
||||||
import jscl.JsclMathEngine;
|
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.mockito.Mockito;
|
import org.mockito.Mockito;
|
||||||
import org.robolectric.RuntimeEnvironment;
|
|
||||||
import org.robolectric.fakes.RoboSharedPreferences;
|
import org.robolectric.fakes.RoboSharedPreferences;
|
||||||
import org.solovyev.android.calculator.jscl.JsclOperation;
|
import org.solovyev.android.calculator.jscl.JsclOperation;
|
||||||
import org.solovyev.android.calculator.language.Languages;
|
import org.solovyev.android.calculator.language.Languages;
|
||||||
import org.solovyev.android.calculator.plot.CalculatorPlotter;
|
import org.solovyev.android.calculator.plot.CalculatorPlotter;
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
import java.io.ByteArrayInputStream;
|
||||||
import javax.annotation.Nullable;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.*;
|
import java.io.IOException;
|
||||||
|
import java.io.ObjectInputStream;
|
||||||
|
import java.io.ObjectOutputStream;
|
||||||
|
import java.io.Serializable;
|
||||||
import java.text.DecimalFormatSymbols;
|
import java.text.DecimalFormatSymbols;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@ -43,6 +46,11 @@ import java.util.concurrent.CountDownLatch;
|
|||||||
import java.util.concurrent.Executor;
|
import java.util.concurrent.Executor;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
import javax.annotation.Nonnull;
|
||||||
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
|
import jscl.JsclMathEngine;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* User: serso
|
* User: serso
|
||||||
* Date: 10/7/12
|
* Date: 10/7/12
|
||||||
@ -56,17 +64,27 @@ public class CalculatorTestUtils {
|
|||||||
public static void staticSetUp() throws Exception {
|
public static void staticSetUp() throws Exception {
|
||||||
App.init(new CalculatorApplication(), new Languages(new RoboSharedPreferences(new HashMap<String, Map<String, Object>>(), "test", 0)));
|
App.init(new CalculatorApplication(), new Languages(new RoboSharedPreferences(new HashMap<String, Map<String, Object>>(), "test", 0)));
|
||||||
Locator.getInstance().init(new CalculatorImpl(Mockito.mock(Bus.class), Mockito.mock(Executor.class)), newCalculatorEngine(), Mockito.mock(CalculatorClipboard.class), Mockito.mock(CalculatorNotifier.class), new SystemErrorReporter(), Mockito.mock(CalculatorPreferenceService.class), Mockito.mock(Keyboard.class), Mockito.mock(CalculatorPlotter.class));
|
Locator.getInstance().init(new CalculatorImpl(Mockito.mock(Bus.class), Mockito.mock(Executor.class)), newCalculatorEngine(), Mockito.mock(CalculatorClipboard.class), Mockito.mock(CalculatorNotifier.class), new SystemErrorReporter(), Mockito.mock(CalculatorPreferenceService.class), Mockito.mock(Keyboard.class), Mockito.mock(CalculatorPlotter.class));
|
||||||
Locator.getInstance().getEngine().init();
|
Locator.getInstance().getEngine().init(new Executor() {
|
||||||
|
@Override
|
||||||
|
public void execute(Runnable command) {
|
||||||
|
command.run();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
final DecimalFormatSymbols decimalGroupSymbols = new DecimalFormatSymbols();
|
final DecimalFormatSymbols decimalGroupSymbols = new DecimalFormatSymbols();
|
||||||
decimalGroupSymbols.setDecimalSeparator('.');
|
decimalGroupSymbols.setDecimalSeparator('.');
|
||||||
decimalGroupSymbols.setGroupingSeparator(' ');
|
decimalGroupSymbols.setGroupingSeparator(' ');
|
||||||
Locator.getInstance().getEngine().setDecimalGroupSymbols(decimalGroupSymbols);
|
Locator.getInstance().getEngine().getMathEngine().setDecimalGroupSymbols(decimalGroupSymbols);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void staticSetUp(@Nullable Context context) throws Exception {
|
public static void staticSetUp(@Nullable Context context) throws Exception {
|
||||||
Locator.getInstance().init(new CalculatorImpl(Mockito.mock(Bus.class), Mockito.mock(Executor.class)), newCalculatorEngine(), Mockito.mock(CalculatorClipboard.class), Mockito.mock(CalculatorNotifier.class), new SystemErrorReporter(), Mockito.mock(CalculatorPreferenceService.class), Mockito.mock(Keyboard.class), Mockito.mock(CalculatorPlotter.class));
|
Locator.getInstance().init(new CalculatorImpl(Mockito.mock(Bus.class), Mockito.mock(Executor.class)), newCalculatorEngine(), Mockito.mock(CalculatorClipboard.class), Mockito.mock(CalculatorNotifier.class), new SystemErrorReporter(), Mockito.mock(CalculatorPreferenceService.class), Mockito.mock(Keyboard.class), Mockito.mock(CalculatorPlotter.class));
|
||||||
Locator.getInstance().getEngine().init();
|
Locator.getInstance().getEngine().init(new Executor() {
|
||||||
|
@Override
|
||||||
|
public void execute(Runnable command) {
|
||||||
|
command.run();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
if (context != null) {
|
if (context != null) {
|
||||||
initViews(context);
|
initViews(context);
|
||||||
@ -89,7 +107,7 @@ public class CalculatorTestUtils {
|
|||||||
final OperatorsRegistry operatorsRegistry = new OperatorsRegistry(jsclEngine.getOperatorsRegistry(), mathEntityDao);
|
final OperatorsRegistry operatorsRegistry = new OperatorsRegistry(jsclEngine.getOperatorsRegistry(), mathEntityDao);
|
||||||
final PostfixFunctionsRegistry postfixFunctionsRegistry = new PostfixFunctionsRegistry(jsclEngine.getPostfixFunctionsRegistry(), mathEntityDao);
|
final PostfixFunctionsRegistry postfixFunctionsRegistry = new PostfixFunctionsRegistry(jsclEngine.getPostfixFunctionsRegistry(), mathEntityDao);
|
||||||
|
|
||||||
return new Engine(RuntimeEnvironment.application, jsclEngine, varsRegistry, functionsRegistry, operatorsRegistry, postfixFunctionsRegistry);
|
return new Engine(jsclEngine, varsRegistry, functionsRegistry, operatorsRegistry, postfixFunctionsRegistry);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void assertEval(@Nonnull String expected, @Nonnull String expression) {
|
public static void assertEval(@Nonnull String expected, @Nonnull String expression) {
|
||||||
|
@ -50,7 +50,7 @@ public class FromJsclSimplifyTextProcessorTest extends AbstractCalculatorTest {
|
|||||||
//Assert.assertEquals("((e)(e))", tp.process("((2.718281828459045)*(2.718281828459045))"));
|
//Assert.assertEquals("((e)(e))", tp.process("((2.718281828459045)*(2.718281828459045))"));
|
||||||
DecimalFormatSymbols decimalGroupSymbols = new DecimalFormatSymbols();
|
DecimalFormatSymbols decimalGroupSymbols = new DecimalFormatSymbols();
|
||||||
decimalGroupSymbols.setGroupingSeparator(' ');
|
decimalGroupSymbols.setGroupingSeparator(' ');
|
||||||
Locator.getInstance().getEngine().setDecimalGroupSymbols(decimalGroupSymbols);
|
Locator.getInstance().getEngine().getMathEngine().setDecimalGroupSymbols(decimalGroupSymbols);
|
||||||
//Assert.assertEquals("123 456 789e", tp.process("123456789*2.718281828459045"));
|
//Assert.assertEquals("123 456 789e", tp.process("123456789*2.718281828459045"));
|
||||||
//Assert.assertEquals("123 456 789e", tp.process("123 456 789 * 2.718281828459045"));
|
//Assert.assertEquals("123 456 789e", tp.process("123 456 789 * 2.718281828459045"));
|
||||||
//Assert.assertEquals("t11e", tp.process("t11*2.718281828459045"));
|
//Assert.assertEquals("t11e", tp.process("t11*2.718281828459045"));
|
||||||
|
@ -58,7 +58,7 @@ public class AndroidEngineTest extends AbstractCalculatorTest {
|
|||||||
@BeforeClass
|
@BeforeClass
|
||||||
public static void staticSetUp() throws Exception {
|
public static void staticSetUp() throws Exception {
|
||||||
CalculatorTestUtils.staticSetUp();
|
CalculatorTestUtils.staticSetUp();
|
||||||
Locator.getInstance().getEngine().setPrecision(3);
|
Locator.getInstance().getEngine().getMathEngine().setPrecision(3);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -41,7 +41,7 @@ public class ComparisonTest extends AbstractCalculatorTest {
|
|||||||
@BeforeClass
|
@BeforeClass
|
||||||
public static void staticSetUp() throws Exception {
|
public static void staticSetUp() throws Exception {
|
||||||
CalculatorTestUtils.staticSetUp();
|
CalculatorTestUtils.staticSetUp();
|
||||||
Locator.getInstance().getEngine().setPrecision(3);
|
Locator.getInstance().getEngine().getMathEngine().setPrecision(3);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -49,7 +49,7 @@ public class EvaluateTest extends AbstractCalculatorTest {
|
|||||||
@BeforeClass
|
@BeforeClass
|
||||||
public static void staticSetUp() throws Exception {
|
public static void staticSetUp() throws Exception {
|
||||||
CalculatorTestUtils.staticSetUp();
|
CalculatorTestUtils.staticSetUp();
|
||||||
Locator.getInstance().getEngine().setPrecision(3);
|
Locator.getInstance().getEngine().getMathEngine().setPrecision(3);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -22,22 +22,28 @@
|
|||||||
|
|
||||||
package org.solovyev.android.calculator.model;
|
package org.solovyev.android.calculator.model;
|
||||||
|
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.BeforeClass;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.solovyev.android.calculator.AbstractCalculatorTest;
|
||||||
|
import org.solovyev.android.calculator.CalculatorEvalException;
|
||||||
|
import org.solovyev.android.calculator.CalculatorParseException;
|
||||||
|
import org.solovyev.android.calculator.CalculatorTestUtils;
|
||||||
|
import org.solovyev.android.calculator.Locator;
|
||||||
|
import org.solovyev.common.Converter;
|
||||||
|
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import javax.annotation.Nonnull;
|
||||||
|
|
||||||
import au.com.bytecode.opencsv.CSVReader;
|
import au.com.bytecode.opencsv.CSVReader;
|
||||||
import jscl.JsclMathEngine;
|
import jscl.JsclMathEngine;
|
||||||
import jscl.MathEngine;
|
import jscl.MathEngine;
|
||||||
import jscl.math.Expression;
|
import jscl.math.Expression;
|
||||||
import jscl.text.ParseException;
|
import jscl.text.ParseException;
|
||||||
import jscl.util.ExpressionGeneratorWithInput;
|
import jscl.util.ExpressionGeneratorWithInput;
|
||||||
import org.junit.Assert;
|
|
||||||
import org.junit.BeforeClass;
|
|
||||||
import org.junit.Test;
|
|
||||||
import org.solovyev.android.calculator.*;
|
|
||||||
import org.solovyev.common.Converter;
|
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
|
||||||
import java.io.InputStreamReader;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* User: serso
|
* User: serso
|
||||||
@ -49,7 +55,7 @@ public class NumeralBaseTest extends AbstractCalculatorTest {
|
|||||||
@BeforeClass
|
@BeforeClass
|
||||||
public static void staticSetUp() throws Exception {
|
public static void staticSetUp() throws Exception {
|
||||||
CalculatorTestUtils.staticSetUp();
|
CalculatorTestUtils.staticSetUp();
|
||||||
Locator.getInstance().getEngine().setPrecision(3);
|
Locator.getInstance().getEngine().getMathEngine().setPrecision(3);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void testExpression(@Nonnull String[] line, @Nonnull Converter<String, String> converter) throws ParseException, CalculatorEvalException, CalculatorParseException {
|
public static void testExpression(@Nonnull String[] line, @Nonnull Converter<String, String> converter) throws ParseException, CalculatorEvalException, CalculatorParseException {
|
||||||
|
@ -87,10 +87,10 @@ public class ToJsclTextProcessorTest extends AbstractCalculatorTest {
|
|||||||
Assert.assertEquals("EE", preprocessor.process("EE").toString());
|
Assert.assertEquals("EE", preprocessor.process("EE").toString());
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Locator.getInstance().getEngine().setNumeralBase(NumeralBase.hex);
|
Locator.getInstance().getEngine().getMathEngine().setNumeralBase(NumeralBase.hex);
|
||||||
Assert.assertEquals("22F*exp(F)", preprocessor.process("22Fexp(F)").toString());
|
Assert.assertEquals("22F*exp(F)", preprocessor.process("22Fexp(F)").toString());
|
||||||
} finally {
|
} finally {
|
||||||
Locator.getInstance().getEngine().setNumeralBase(NumeralBase.dec);
|
Locator.getInstance().getEngine().getMathEngine().setNumeralBase(NumeralBase.dec);
|
||||||
}
|
}
|
||||||
Assert.assertEquals("0x:ABCDEF", preprocessor.process("0x:ABCDEF").toString());
|
Assert.assertEquals("0x:ABCDEF", preprocessor.process("0x:ABCDEF").toString());
|
||||||
Assert.assertEquals("0x:ABCDEF", preprocessor.process("0x:A BC DEF").toString());
|
Assert.assertEquals("0x:ABCDEF", preprocessor.process("0x:A BC DEF").toString());
|
||||||
|
Loading…
Reference in New Issue
Block a user