diff options
author | Hans Boehm <hboehm@google.com> | 2015-06-26 17:56:52 -0700 |
---|---|---|
committer | Hans Boehm <hboehm@google.com> | 2015-06-26 17:56:52 -0700 |
commit | e8553769f4b06650da0ca22a1bf741d0aad96e74 (patch) | |
tree | 65e2ac93d68cfec881252671ec07011651249387 /src/com | |
parent | 19e93c9806c245084d81df601d5ebc38c73dd5a7 (diff) | |
download | android_packages_apps_ExactCalculator-e8553769f4b06650da0ca22a1bf741d0aad96e74.tar.gz android_packages_apps_ExactCalculator-e8553769f4b06650da0ca22a1bf741d0aad96e74.tar.bz2 android_packages_apps_ExactCalculator-e8553769f4b06650da0ca22a1bf741d0aad96e74.zip |
Make trailing operator handling consistent with L
Bug: 246391
Only ignore trailing BINARY operators when computing instant results.
We used to be much more aggressive.
Also ignore trailing binary operators when the user hits "=".
This makes us consistent with the L design and ensures that instant
results don't turn into errors when the user hits "=".
Change-Id: I260e95d152168b70774330ac95d5bc567cf79b3d
Diffstat (limited to 'src/com')
-rw-r--r-- | src/com/android/calculator2/CalculatorExpr.java | 39 | ||||
-rw-r--r-- | src/com/android/calculator2/Evaluator.java | 10 |
2 files changed, 25 insertions, 24 deletions
diff --git a/src/com/android/calculator2/CalculatorExpr.java b/src/com/android/calculator2/CalculatorExpr.java index 73e09bf..2162bb2 100644 --- a/src/com/android/calculator2/CalculatorExpr.java +++ b/src/com/android/calculator2/CalculatorExpr.java @@ -914,30 +914,24 @@ class CalculatorExpr { final BoundedRational mRatVal; } - // Return the starting position of the sequence of trailing operators - // that cannot be meaningfully evaluated. - private int trailingOpsStart() { + /** + * Return the starting position of the sequence of trailing binary operators. + */ + private int trailingBinaryOpsStart() { int result = mExpr.size(); while (result > 0) { Token last = mExpr.get(result - 1); if (!(last instanceof Operator)) break; Operator o = (Operator)last; - if (KeyMaps.isSuffix(o.mId) || o.mId == R.id.const_pi - || o.mId == R.id.const_e) { - break; - } + if (!KeyMaps.isBinary(o.mId)) break; --result; } return result; } - public boolean hasTrailingOperators() { - return trailingOpsStart() != mExpr.size(); - } - // Is the current expression worth evaluating? public boolean hasInterestingOps() { - int last = trailingOpsStart(); + int last = trailingBinaryOpsStart(); int first = 0; if (last > first && isOperatorUnchecked(first, R.id.op_sub)) { // Leading minus is not by itself interesting. @@ -953,16 +947,25 @@ class CalculatorExpr { return false; } - // Evaluate the entire expression, returning null in the event - // of an error. - // Not called from the UI thread, but should not be called - // concurrently with modifications to the expression. - EvalResult eval(boolean degreeMode, boolean required) throws SyntaxException + /** + * Evaluate the expression excluding trailing binary operators. + * Errors result in exceptions, most of which are unchecked. + * Should not be called concurrently with modification of the expression. + * May take a very long time; avoid calling from UI thread. + * + * @param degreeMode use degrees rather than radians + */ + EvalResult eval(boolean degreeMode) throws SyntaxException // And unchecked exceptions thrown by CR // and BoundedRational. { try { - int prefixLen = required ? mExpr.size() : trailingOpsStart(); + // We currently never include trailing binary operators, but include + // other trailing operators. + // Thus we usually, but not always, display results for prefixes + // of valid expressions, and don't generate an error where we previously + // displayed an instant result. This reflects the Android L design. + int prefixLen = trailingBinaryOpsStart(); EvalContext ec = new EvalContext(degreeMode, prefixLen); EvalRet res = evalExpr(0, ec); if (res.mPos != prefixLen) { diff --git a/src/com/android/calculator2/Evaluator.java b/src/com/android/calculator2/Evaluator.java index afde752..9c4346e 100644 --- a/src/com/android/calculator2/Evaluator.java +++ b/src/com/android/calculator2/Evaluator.java @@ -382,7 +382,7 @@ class Evaluator { @Override protected InitialResult doInBackground(Void... nothing) { try { - CalculatorExpr.EvalResult res = mExpr.eval(mDm, mRequired); + CalculatorExpr.EvalResult res = mExpr.eval(mDm); int prec = INIT_PREC; String initCache = res.mVal.toString(prec); int msd = getMsdPos(initCache); @@ -810,9 +810,8 @@ class Evaluator { // We presume that any prior result was computed using the same // expression. void requireResult() { - if (mCache == null || mExpr.hasTrailingOperators()) { - // Restart evaluator in requested mode, i.e. with - // longer timeout, not ignoring trailing operators. + if (mCache == null) { + // Restart evaluator in requested mode, i.e. with longer timeout. cancelAll(true); clearCache(); mEvaluator = new AsyncDisplayResult(mDegreeMode, true); @@ -895,8 +894,7 @@ class Evaluator { add10pow(); // Handled as macro expansion. return true; } else { - mChangedValue = mChangedValue || (KeyMaps.digVal(id) != KeyMaps.NOT_DIGIT - || KeyMaps.isSuffix(id) || id == R.id.const_pi || id == R.id.const_e); + mChangedValue = mChangedValue || !KeyMaps.isBinary(id); return mExpr.add(id); } } |