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/CalculatorResult.java b/src/com/android/calculator2/CalculatorResult.java
index 0b399c4..5e61dc4 100644
--- a/src/com/android/calculator2/CalculatorResult.java
+++ b/src/com/android/calculator2/CalculatorResult.java
@@ -23,6 +23,7 @@
 import android.content.Context;
 import android.graphics.Rect;
 import android.os.Build;
+import android.support.annotation.IntDef;
 import android.support.v4.content.ContextCompat;
 import android.support.v4.os.BuildCompat;
 import android.text.Layout;
@@ -45,6 +46,9 @@
 import android.widget.OverScroller;
 import android.widget.Toast;
 
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+
 // A text widget that is "infinitely" scrollable to the right,
 // and obtains the text to display via a callback to Logic.
 public class CalculatorResult extends AlignedTextView implements MenuItem.OnMenuItemClickListener,
@@ -114,7 +118,14 @@
     private float mNoEllipsisCredit;
                             // Fraction of digit width saved by both replacing ellipsis with digit
                             // and avoiding scientific notation.
-    private boolean mShouldRequireResult = true;
+    @Retention(RetentionPolicy.SOURCE)
+    @IntDef({SHOULD_REQUIRE, SHOULD_EVALUATE, SHOULD_NOT_EVALUATE})
+    public @interface EvaluationRequest {}
+    public static final int SHOULD_REQUIRE = 2;
+    public static final int SHOULD_EVALUATE = 1;
+    public static final int SHOULD_NOT_EVALUATE = 0;
+    @EvaluationRequest private int mEvaluationRequest = SHOULD_REQUIRE;
+                            // Should we evaluate when layout completes, and how?
     private Evaluator.EvaluationListener mEvaluationListener = this;
                             // Listener to use if/when evaluation is requested.
     public static final int MAX_LEADING_ZEROES = 6;
@@ -313,17 +324,26 @@
     protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
         super.onLayout(changed, left, top, right, bottom);
 
-        if (mEvaluator != null && mShouldRequireResult) {
+        if (mEvaluator != null && mEvaluationRequest != SHOULD_NOT_EVALUATE) {
             final CalculatorExpr expr = mEvaluator.getExpr(mIndex);
             if (expr != null && expr.hasInterestingOps()) {
-                mEvaluator.requireResult(mIndex, mEvaluationListener, this);
+                if (mEvaluationRequest == SHOULD_REQUIRE) {
+                    mEvaluator.requireResult(mIndex, mEvaluationListener, this);
+                } else {
+                    mEvaluator.evaluateAndNotify(mIndex, mEvaluationListener, this);
+                }
             }
         }
     }
 
-    public void setShouldRequireResult(boolean should, Evaluator.EvaluationListener listener) {
+    /**
+     * Specify whether we should evaluate result on layout.
+     * @param should one of SHOULD_REQUIRE, SHOULD_EVALUATE, SHOULD_NOT_EVALUATE
+     */
+    public void setShouldEvaluateResult(@EvaluationRequest int request,
+            Evaluator.EvaluationListener listener) {
         mEvaluationListener = listener;
-        mShouldRequireResult = should;
+        mEvaluationRequest = request;
     }
 
     // From Evaluator.CharMetricsInfo.
@@ -1113,4 +1133,4 @@
                 return false;
         }
     }
-}
\ No newline at end of file
+}