Functions loading fails sometimes

This commit is contained in:
serso 2016-01-24 13:30:31 +01:00
parent 26ac11ce89
commit 1120864668
5 changed files with 61 additions and 13 deletions

View File

@ -22,6 +22,7 @@
package org.solovyev.android.calculator; package org.solovyev.android.calculator;
import android.util.Log;
import org.acra.ACRA; import org.acra.ACRA;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
@ -33,6 +34,7 @@ public class AcraErrorReporter implements ErrorReporter {
@Override @Override
public void onException(@Nonnull Throwable e) { public void onException(@Nonnull Throwable e) {
if (!ENABLED) { if (!ENABLED) {
Log.e("Acra", e.getMessage(), e);
return; return;
} }
ACRA.getErrorReporter().reportBuilder().forceSilent().exception(e).send(); ACRA.getErrorReporter().reportBuilder().forceSilent().exception(e).send();
@ -41,6 +43,7 @@ public class AcraErrorReporter implements ErrorReporter {
@Override @Override
public void onError(@Nonnull String message) { public void onError(@Nonnull String message) {
if (!ENABLED) { if (!ENABLED) {
Log.e("Acra", message);
return; return;
} }
ACRA.getErrorReporter().reportBuilder().forceSilent().exception(new Throwable(message)).send(); ACRA.getErrorReporter().reportBuilder().forceSilent().exception(new Throwable(message)).send();

View File

@ -47,6 +47,12 @@ public abstract class BaseEntitiesRegistry<T extends MathEntity, P extends Persi
@Nullable @Nullable
protected final EntityDao<P> entityDao; protected final EntityDao<P> entityDao;
@Nonnull
protected final Object lock = this;
// synchronized on lock
private boolean initialized;
protected BaseEntitiesRegistry(@Nonnull MathRegistry<T> mathRegistry, protected BaseEntitiesRegistry(@Nonnull MathRegistry<T> mathRegistry,
@Nonnull String prefix, @Nonnull String prefix,
@Nullable EntityDao<P> entityDao) { @Nullable EntityDao<P> entityDao) {
@ -125,6 +131,20 @@ public abstract class BaseEntitiesRegistry<T extends MathEntity, P extends Persi
// just in case // just in case
Locator.getInstance().getErrorReporter().onException(e); Locator.getInstance().getErrorReporter().onException(e);
} }
setInitialized();
}
protected final void setInitialized() {
synchronized (lock) {
Check.isTrue(!initialized);
initialized = true;
}
}
public boolean isInitialized() {
synchronized (lock) {
return initialized;
}
} }
@Nonnull @Nonnull

View File

@ -104,16 +104,18 @@ public class FunctionsRegistry extends BaseEntitiesRegistry<Function, OldFunctio
Check.isNotMainThread(); Check.isNotMainThread();
migrateOldFunctions(); migrateOldFunctions();
add(new CustomFunction.Builder(true, "log", Arrays.asList("base", "x"), "ln(x)/ln(base)")); addSafely(new CustomFunction.Builder(true, "log", Arrays.asList("base", "x"), "ln(x)/ln(base)"));
add(new CustomFunction.Builder(true, "√3", Collections.singletonList("x"), "x^(1/3)")); addSafely(new CustomFunction.Builder(true, "√3", Collections.singletonList("x"), "x^(1/3)"));
add(new CustomFunction.Builder(true, "√4", Collections.singletonList("x"), "x^(1/4)")); addSafely(new CustomFunction.Builder(true, "√4", Collections.singletonList("x"), "x^(1/4)"));
add(new CustomFunction.Builder(true, "√n", Arrays.asList("x", "n"), "x^(1/n)")); addSafely(new CustomFunction.Builder(true, "√n", Arrays.asList("x", "n"), "x^(1/n)"));
add(new CustomFunction.Builder(true, "re", Collections.singletonList("x"), "(x+conjugate(x))/2")); addSafely(new CustomFunction.Builder(true, "re", Collections.singletonList("x"), "(x+conjugate(x))/2"));
add(new CustomFunction.Builder(true, "im", Collections.singletonList("x"), "(x-conjugate(x))/(2*i)")); addSafely(new CustomFunction.Builder(true, "im", Collections.singletonList("x"), "(x-conjugate(x))/(2*i)"));
for (CppFunction function : loadFunctions()) { for (CppFunction function : loadFunctions()) {
add(function.toCustomFunctionBuilder()); addSafely(function.toCustomFunctionBuilder());
} }
setInitialized();
} }
@Override @Override
@ -124,13 +126,26 @@ public class FunctionsRegistry extends BaseEntitiesRegistry<Function, OldFunctio
} }
@Override @Override
public Function add(@Nonnull JBuilder<? extends Function> result) { public Function add(@Nonnull JBuilder<? extends Function> builder) {
final Function function = super.add(result); final Function function = super.add(builder);
// todo serso: don't save while we're initializing if (isInitialized()) {
save(); save();
}
return function; return function;
} }
@Nullable
public Function addSafely(@Nonnull JBuilder<? extends Function> builder) {
try {
// todo serso: currently JSCL might produce function's body which can't be loaded afterwards. F.e. f(x) = 3 * 6
// might be simpliffied to f(x) = 18 × 10 ^ 0 and × is not supported by JSCL
return add(builder);
} catch (Exception e) {
errorReporter.onException(e);
}
return null;
}
@NonNull @NonNull
private List<CppFunction> loadFunctions() { private List<CppFunction> loadFunctions() {
try { try {
@ -151,6 +166,7 @@ public class FunctionsRegistry extends BaseEntitiesRegistry<Function, OldFunctio
final OldFunctions oldFunctions = serializer.read(OldFunctions.class, xml); final OldFunctions oldFunctions = serializer.read(OldFunctions.class, xml);
if (oldFunctions != null) { if (oldFunctions != null) {
List<CppFunction> functions = OldFunctions.toCppFunctions(oldFunctions); List<CppFunction> functions = OldFunctions.toCppFunctions(oldFunctions);
// todo serso: fix multiplication sign issue
FileSaver.save(getFunctionsFile(), Json.toJson(functions).toString()); FileSaver.save(getFunctionsFile(), Json.toJson(functions).toString());
} }
preferences.edit().remove(OldFunctions.PREFS_KEY).apply(); preferences.edit().remove(OldFunctions.PREFS_KEY).apply();

View File

@ -49,6 +49,8 @@ public class KeyboardUi {
private final int sidePadding; private final int sidePadding;
@DrawableRes @DrawableRes
private final int buttonBackground; private final int buttonBackground;
@NonNull
private final String multiplicationSign = Locator.getInstance().getEngine().getMultiplicationSign();
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public KeyboardUi(@NonNull User user, @NonNull List<String> parameterNames) { public KeyboardUi(@NonNull User user, @NonNull List<String> parameterNames) {
@ -110,7 +112,7 @@ public class KeyboardUi {
addButton(row, 0, "7"); addButton(row, 0, "7");
addButton(row, 0, "8"); addButton(row, 0, "8");
addButton(row, 0, "9").setText("π", up).setText("e", down); addButton(row, 0, "9").setText("π", up).setText("e", down);
addOperationButton(row, R.id.cpp_kb_button_multiply, Locator.getInstance().getEngine().getMultiplicationSign()).setText("^n", up).setText("^2", down); addOperationButton(row, R.id.cpp_kb_button_multiply, multiplicationSign).setText("^n", up).setText("^2", down);
addButton(row, R.id.cpp_kb_button_clear, "C"); addButton(row, R.id.cpp_kb_button_clear, "C");
row = makeRow(); row = makeRow();
@ -264,7 +266,7 @@ public class KeyboardUi {
user.insertOperator('-'); user.insertOperator('-');
break; break;
case R.id.cpp_kb_button_multiply: case R.id.cpp_kb_button_multiply:
user.insertOperator('*'); user.insertOperator(multiplicationSign);
break; break;
case R.id.cpp_kb_button_functions_constants: case R.id.cpp_kb_button_functions_constants:
user.showFunctionsConstants(v); user.showFunctionsConstants(v);

View File

@ -235,6 +235,13 @@ public class CppFunction implements Jsonable, Parcelable {
return this; return this;
} }
@Nonnull
public Builder withBody(@NonNull String body) {
Check.isTrue(!built);
function.body = body;
return this;
}
@Nonnull @Nonnull
public CppFunction build() { public CppFunction build() {
built = true; built = true;