summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHans Boehm <hboehm@google.com>2015-06-28 15:36:10 +0000
committerAndroid Git Automerger <android-git-automerger@android.com>2015-06-28 15:36:10 +0000
commit4b82029a1295251b8b520343ecc4bac211a3da71 (patch)
tree414286c470c046a19defc523ccb37519ad19310b
parentac8af098782e3bc5b63010847d8d18641d7037ea (diff)
parente8553769f4b06650da0ca22a1bf741d0aad96e74 (diff)
downloadandroid_packages_apps_ExactCalculator-4b82029a1295251b8b520343ecc4bac211a3da71.tar.gz
android_packages_apps_ExactCalculator-4b82029a1295251b8b520343ecc4bac211a3da71.tar.bz2
android_packages_apps_ExactCalculator-4b82029a1295251b8b520343ecc4bac211a3da71.zip
am e8553769: Make trailing operator handling consistent with L
* commit 'e8553769f4b06650da0ca22a1bf741d0aad96e74': Make trailing operator handling consistent with L
-rw-r--r--src/com/android/calculator2/CalculatorExpr.java39
-rw-r--r--src/com/android/calculator2/Evaluator.java10
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);
}
}