Consistently avoid displaying trailing zeroes

Bug: 21405924
Bug: 21529236
Bug: 21534231

This limits the display of trailing zeroes to the case in which
they avoid the need for scientic notation, and to the case of
results we did not identify as rational.  This means that you
can use scrolling as an indicator of whether there may be more
digits.

The old code exhibited some misbehavior around this, the most
serious of which was probably the second bug listed above.

This now uses scientific notation more aggressively for small
numbers (b/21534231).

This patch unfortunately needs to deal with many odd corner cases
to make sure that we stop scrolling at just the right point, before
the first trailing zero appears, even if there are exponents
involved.  I tested as many corner cases as I could think of.
And the testing exposed other preexisting bugs.
I do not know of a good way to avoid this without reverting
to the old scroll-through-trailing-zeroes behavior.

This significantly changes the behavior on e.g.

10^30 (Previously allowed scrolling to the decimal point, now is
unscrollable.)
10^-20 (Weird initial display with trailing zeroes; which could not
be reproduced after scrolling.)

It turns out that formatResult() scientific notation formatting
could accidentally extend the input result by 1 or 2 characters.
Based on my testing, the one character case was actually a feature:
Since there was a decimal point in the result the extra ellipsis
space seems to always give us plenty of room.  The two character
case whoever sometimes resulted in wrapping, and is fixed here.
The one character case is now official.  Ideally we should check
that we actually have enough space; currently I just assume a
single additional character.

Disallow scrolling left of the default position.  This seems
more consistent with TextView scrolling and eliminates some
crufty code.

Fixed an off-by-one error in getPreferredPrec, which resulted in
positive results that were one character too short.

Change-Id: I13657377d098055def99e7a173f71f40d361fe3c
diff --git a/src/com/android/calculator2/Calculator.java b/src/com/android/calculator2/Calculator.java
index b2cf477..dddbad8 100644
--- a/src/com/android/calculator2/Calculator.java
+++ b/src/com/android/calculator2/Calculator.java
@@ -535,11 +535,12 @@
     }
 
     // Initial evaluation completed successfully.  Initiate display.
-    public void onEvaluate(int initDisplayPrec, int leastDigPos, String truncatedWholeNumber) {
+    public void onEvaluate(int initDisplayPrec, int msd, int leastDigPos,
+            String truncatedWholeNumber) {
         // Invalidate any options that may depend on the current result.
         invalidateOptionsMenu();
 
-        mResultText.displayResult(initDisplayPrec, leastDigPos, truncatedWholeNumber);
+        mResultText.displayResult(initDisplayPrec, msd, leastDigPos, truncatedWholeNumber);
         if (mCurrentState != CalculatorState.INPUT) { // in EVALUATE or INIT state
             onResult(mCurrentState != CalculatorState.INIT);
         }