Arrange for evaluateAndNotify calls at the right time.

Bug: 33011774
Bug: 33087975

We were sometimes calling it before layout after restart. Defer to
onLayout, as with requireResult.

Arrange for an explicit call when returning from HistoryFragment
to compensate for unintentional cancellation. This part is a hack
that needs to be revisited.

Adjust some constants to make it less likely that we will not see
am instant result, but equals will produce a valid result. This is
not a fundamental change, but 10000! now works as expected.
As does e^(1+10^-1000)-e, since we now force more evaluation to
try to distinguish a result from zero .  This may slow things down
on underpowered devices, but it shouldn't be a serious issue.

Change-Id: I5fcddef84dd907bc83d9bf575d0d378ca99d6359
diff --git a/src/com/android/calculator2/Calculator.java b/src/com/android/calculator2/Calculator.java
index f13a9ed..ce11d7a 100644
--- a/src/com/android/calculator2/Calculator.java
+++ b/src/com/android/calculator2/Calculator.java
@@ -402,16 +402,15 @@
             showAndMaybeHideToolbar();
         }
 
+        redisplayFormula();
         if (mCurrentState != CalculatorState.INPUT) {
             // Just reevaluate.
-            redisplayFormula();
             setState(CalculatorState.INIT);
             // Request evaluation when we know display width.
-            mResultText.setShouldRequireResult(true, this);
+            mResultText.setShouldEvaluateResult(CalculatorResult.SHOULD_REQUIRE, this);
         } else {
             // This resultText will explicitly call evaluateAndNotify when ready.
-            mResultText.setShouldRequireResult(false, null);
-            redisplayAfterFormulaChange();
+            mResultText.setShouldEvaluateResult(CalculatorResult.SHOULD_EVALUATE, this);
         }
         // TODO: We're currently not saving and restoring scroll position.
         //       We probably should.  Details may require care to deal with:
@@ -457,7 +456,7 @@
         if (mCurrentState != state) {
             if (state == CalculatorState.INPUT) {
                 // We'll explicitly request evaluation from now on.
-                mResultText.setShouldRequireResult(false, null);
+                mResultText.setShouldEvaluateResult(CalculatorResult.SHOULD_NOT_EVALUATE, null);
                 restoreDisplayPositions();
             }
             mCurrentState = state;
@@ -722,6 +721,13 @@
         addKeyToExpr(id);
     }
 
+    public void evaluateInstantIfNecessary() {
+        if (mCurrentState == CalculatorState.INPUT
+                && mEvaluator.getExpr(Evaluator.MAIN_INDEX).hasInterestingOps()) {
+            mEvaluator.evaluateAndNotify(Evaluator.MAIN_INDEX, this, mResultText);
+        }
+    }
+
     private void redisplayAfterFormulaChange() {
         // TODO: Could do this more incrementally.
         redisplayFormula();
@@ -731,9 +737,7 @@
             // Force reevaluation when text is deleted, even if expression is unchanged.
             mEvaluator.touch();
         } else {
-            if (mEvaluator.getExpr(Evaluator.MAIN_INDEX).hasInterestingOps()) {
-                mEvaluator.evaluateAndNotify(Evaluator.MAIN_INDEX, this, mResultText);
-            }
+            evaluateInstantIfNecessary();
         }
     }
 
@@ -806,9 +810,8 @@
                 showAndMaybeHideToolbar();
                 setState(CalculatorState.INPUT);
                 mResultText.clear();
-                if (!haveUnprocessed()
-                        && mEvaluator.getExpr(Evaluator.MAIN_INDEX).hasInterestingOps()) {
-                    mEvaluator.evaluateAndNotify(mEvaluator.MAIN_INDEX, this, mResultText);
+                if (!haveUnprocessed()) {
+                    evaluateInstantIfNecessary();
                 }
                 return;
             default: