Make onSaveInstanceState wait for database writes

Bug: 34051652

Add a mechanism to wait for (asynchronous) database writes to
complete.

Invoke it in Calculator.onSaveInstanceState(). This avoids a potential
situation in which the saved instance state bundle includes a reference
to an expression that never made it to the database. This may be more
likely than it appears, since we run all background tasks on a single
thread. A database save operation can get hung up behind a computation.

Note that in almost all cases, the wait operation will return
immediately, as evidenced by the fact that this very rarely
leads to crashes without the wait operation.

Change-Id: I7c2bdcd9bf19f6cb4bd653fb9244bca0f6b25435
3 files changed