From 1120864668f5417188b70bab96bf0674a2b5af6c Mon Sep 17 00:00:00 2001 From: serso Date: Sun, 24 Jan 2016 13:30:31 +0100 Subject: [PATCH] Functions loading fails sometimes --- .../android/calculator/AcraErrorReporter.java | 3 ++ .../calculator/BaseEntitiesRegistry.java | 20 ++++++++++ .../android/calculator/FunctionsRegistry.java | 38 +++++++++++++------ .../android/calculator/KeyboardUi.java | 6 ++- .../calculator/function/CppFunction.java | 7 ++++ 5 files changed, 61 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/org/solovyev/android/calculator/AcraErrorReporter.java b/app/src/main/java/org/solovyev/android/calculator/AcraErrorReporter.java index 3242fde8..68b9e103 100644 --- a/app/src/main/java/org/solovyev/android/calculator/AcraErrorReporter.java +++ b/app/src/main/java/org/solovyev/android/calculator/AcraErrorReporter.java @@ -22,6 +22,7 @@ package org.solovyev.android.calculator; +import android.util.Log; import org.acra.ACRA; import javax.annotation.Nonnull; @@ -33,6 +34,7 @@ public class AcraErrorReporter implements ErrorReporter { @Override public void onException(@Nonnull Throwable e) { if (!ENABLED) { + Log.e("Acra", e.getMessage(), e); return; } ACRA.getErrorReporter().reportBuilder().forceSilent().exception(e).send(); @@ -41,6 +43,7 @@ public class AcraErrorReporter implements ErrorReporter { @Override public void onError(@Nonnull String message) { if (!ENABLED) { + Log.e("Acra", message); return; } ACRA.getErrorReporter().reportBuilder().forceSilent().exception(new Throwable(message)).send(); diff --git a/app/src/main/java/org/solovyev/android/calculator/BaseEntitiesRegistry.java b/app/src/main/java/org/solovyev/android/calculator/BaseEntitiesRegistry.java index fc221cc9..12ead3dd 100644 --- a/app/src/main/java/org/solovyev/android/calculator/BaseEntitiesRegistry.java +++ b/app/src/main/java/org/solovyev/android/calculator/BaseEntitiesRegistry.java @@ -47,6 +47,12 @@ public abstract class BaseEntitiesRegistry entityDao; + @Nonnull + protected final Object lock = this; + + // synchronized on lock + private boolean initialized; + protected BaseEntitiesRegistry(@Nonnull MathRegistry mathRegistry, @Nonnull String prefix, @Nullable EntityDao

entityDao) { @@ -125,6 +131,20 @@ public abstract class BaseEntitiesRegistry result) { - final Function function = super.add(result); - // todo serso: don't save while we're initializing - save(); + public Function add(@Nonnull JBuilder builder) { + final Function function = super.add(builder); + if (isInitialized()) { + save(); + } return function; } + @Nullable + public Function addSafely(@Nonnull JBuilder 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 private List loadFunctions() { try { @@ -151,6 +166,7 @@ public class FunctionsRegistry extends BaseEntitiesRegistry functions = OldFunctions.toCppFunctions(oldFunctions); + // todo serso: fix multiplication sign issue FileSaver.save(getFunctionsFile(), Json.toJson(functions).toString()); } preferences.edit().remove(OldFunctions.PREFS_KEY).apply(); diff --git a/app/src/main/java/org/solovyev/android/calculator/KeyboardUi.java b/app/src/main/java/org/solovyev/android/calculator/KeyboardUi.java index 2268ccda..5ce9612e 100644 --- a/app/src/main/java/org/solovyev/android/calculator/KeyboardUi.java +++ b/app/src/main/java/org/solovyev/android/calculator/KeyboardUi.java @@ -49,6 +49,8 @@ public class KeyboardUi { private final int sidePadding; @DrawableRes private final int buttonBackground; + @NonNull + private final String multiplicationSign = Locator.getInstance().getEngine().getMultiplicationSign(); @SuppressWarnings("deprecation") public KeyboardUi(@NonNull User user, @NonNull List parameterNames) { @@ -110,7 +112,7 @@ public class KeyboardUi { addButton(row, 0, "7"); addButton(row, 0, "8"); 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"); row = makeRow(); @@ -264,7 +266,7 @@ public class KeyboardUi { user.insertOperator('-'); break; case R.id.cpp_kb_button_multiply: - user.insertOperator('*'); + user.insertOperator(multiplicationSign); break; case R.id.cpp_kb_button_functions_constants: user.showFunctionsConstants(v); diff --git a/app/src/main/java/org/solovyev/android/calculator/function/CppFunction.java b/app/src/main/java/org/solovyev/android/calculator/function/CppFunction.java index 976548b5..b3675ac5 100644 --- a/app/src/main/java/org/solovyev/android/calculator/function/CppFunction.java +++ b/app/src/main/java/org/solovyev/android/calculator/function/CppFunction.java @@ -235,6 +235,13 @@ public class CppFunction implements Jsonable, Parcelable { return this; } + @Nonnull + public Builder withBody(@NonNull String body) { + Check.isTrue(!built); + function.body = body; + return this; + } + @Nonnull public CppFunction build() { built = true;