Avoid starting long uninterruptible computations

Bug: 21957088

Shorten timeouts.

We usually do short computations first, so this helps to avoid
starting absurdly big ones.

Check size of result before converting to decimal.  If the decimal
conversion is likely to time out anyway, just pretent we timed out.
This turns out to avoid a lot of problems, since BigInteger decimal
conversion is the clear bottleneck for something like 100000!
And its uninterruptible.

Remove the timeout in one case in which we had previously forgotten.

Check for interrupts in a couple of more places in BoundedRational.
One of these caused log(10^100000) to hang for a long time.

One or two trivial cleanups in code that was touched anyway.

Change-Id: I3494a8ed28f681fb26634ecd90042e2f2a8008a8
diff --git a/src/com/android/calculator2/BoundedRational.java b/src/com/android/calculator2/BoundedRational.java
index 70c21af..2b3d1ed 100644
--- a/src/com/android/calculator2/BoundedRational.java
+++ b/src/com/android/calculator2/BoundedRational.java
@@ -87,6 +87,11 @@
         return CR.valueOf(mNum).divide(CR.valueOf(mDen));
     }
 
+    // Approximate number of bits to left of binary point.
+    public int wholeNumberBits() {
+        return mNum.bitLength() - mDen.bitLength();
+    }
+
     private boolean tooBig() {
         if (mDen.equals(BigInteger.ONE)) return false;
         return (mNum.bitLength() + mDen.bitLength() > MAX_SIZE);
@@ -418,6 +423,9 @@
         // This algorithm is very naive, but we doubt it matters.
         long count = 0;
         while (n.mod(BigInteger.TEN).equals(BigInteger.ZERO)) {
+            if (Thread.interrupted()) {
+                throw new CR.AbortedException();
+            }
             n = n.divide(BigInteger.TEN);
             ++count;
         }
@@ -502,6 +510,9 @@
         if (r.mDen.equals(BigInteger.ONE)) return 0;
         r = r.reduce();
         BigInteger den = r.mDen;
+        if (den.bitLength() > MAX_SIZE) {
+            return Integer.MAX_VALUE;
+        }
         while (!den.testBit(0)) {
             ++powers_of_two;
             den = den.shiftRight(1);