Do not use HISTORY_MAIN_INDEX as clipboard or memory index

Bug: 34697529

We were potentially saving HISTORY_MAIN_INDEX (-1) as the expression
index for the clipboard or memory expression, without actually saving
expression at that index.

Deal with the fact that sharedpreferences or the database may have
been corrupted with such spurious -1 values.

As part of this, we also make the memory operations much more
robust against trying to use an erroneous answer. This seems like
a good idea anyway, since there are probably other, also extremely
unlikely, cases in which this can occur. (Recall that some expressions
evaluate to an error only when the precision is increased
sufficiently.)

Test: Unit tests pass. Installed old calculator and arranged to
corrupt saved location as well as some history entries. The bad
history entries show up with an answer of "Bad Expression".
Trying th use the "Bad Expression" result no longer crashes for me.

Change-Id: I0b1a897ce018353c4b67248f84fccde82fc43e92
(cherry picked from commit 7451da4b6bb1cb40440fb755e8344f90bc38ecb5)
diff --git a/src/com/android/calculator2/CalculatorExpr.java b/src/com/android/calculator2/CalculatorExpr.java
index 18bb6cf..75ab1c9 100644
--- a/src/com/android/calculator2/CalculatorExpr.java
+++ b/src/com/android/calculator2/CalculatorExpr.java
@@ -373,7 +373,16 @@
             case CONSTANT:
                 return new Constant(in);
             case PRE_EVAL:
-                return new PreEval(in);
+                PreEval pe = new PreEval(in);
+                if (pe.mIndex == -1) {
+                    // Database corrupted by earlier bug.
+                    // Return a conspicuously wrong placeholder that won't lead to a crash.
+                    Constant result = new Constant();
+                    result.add(R.id.dec_point);
+                    return result;
+                } else {
+                    return pe;
+                }
             default: throw new IOException("Bad save file format");
             }
         } else {