Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 83f278ed authored by Hans Boehm's avatar Hans Boehm
Browse files

Correctly track Activity in Evaluator

Bug: 33703223

Evaluator was remembering the Activity with which it had been
created, not necessarily the current one. This interfered with
proper display of things like timeout messages that rely on the
remembered Activity.

Use the Application rather than Activity context for the DB helper,
since it outlives the Activity.

Remove unused mActivity from ExpressionDB.

Also acouple of trivial cleanups for minor annoyances found while
debugging:

1. Add some missing final declarations.

2. Fix a comment.

Change-Id: Iefe1fb588f66a1c77c82164680306377917c07af
parent 97228c68
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -977,7 +977,7 @@ class CalculatorExpr {
     * Is the current expression worth evaluating?
     */
    public boolean hasInterestingOps() {
        int last = trailingBinaryOpsStart();
        final int last = trailingBinaryOpsStart();
        int first = 0;
        if (last > first && isOperatorUnchecked(first, R.id.op_sub)) {
            // Leading minus is not by itself interesting.
+9 −8
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
package com.android.calculator2;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.AsyncTask;
@@ -101,8 +102,9 @@ public class Evaluator implements CalculatorExpr.ExprResolver {

    public static Evaluator getInstance(Calculator calculator) {
        if (evaluator == null) {
            evaluator = new Evaluator(calculator);
            evaluator = new Evaluator(calculator.getApplicationContext());
        }
        evaluator.mCalculator = calculator;
        return evaluator;
    }

@@ -253,7 +255,7 @@ public class Evaluator implements CalculatorExpr.ExprResolver {
    // estimating exponent size for truncating short representation.
    private static final int EXP_COST = 3;

    private final Calculator mCalculator;
    private Calculator mCalculator;

    //  A hopefully unique name associated with mSaved.
    private String mSavedName;
@@ -334,14 +336,13 @@ public class Evaluator implements CalculatorExpr.ExprResolver {
        mExprs.put(MAIN_INDEX, expr);
    }

    Evaluator(Calculator calculator) {
        mCalculator = calculator;
    Evaluator(Context context) {
        setMainExpr(new ExprInfo(new CalculatorExpr(), false));
        mSavedName = "none";
        mTimeoutHandler = new Handler();

        mExprDB = new ExpressionDB(mCalculator);
        mSharedPrefs = PreferenceManager.getDefaultSharedPreferences(calculator);
        mExprDB = new ExpressionDB(context);
        mSharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
        mMainExpr.mDegreeMode = mSharedPrefs.getBoolean(KEY_PREF_DEGREE_MODE, false);
        long savedIndex = mSharedPrefs.getLong(KEY_PREF_SAVED_INDEX, 0L);
        long memoryIndex = mSharedPrefs.getLong(KEY_PREF_MEMORY_INDEX, 0L);
@@ -536,7 +537,7 @@ public class Evaluator implements CalculatorExpr.ExprResolver {
         * Is a computed result too big for decimal conversion?
         */
        private boolean isTooBig(UnifiedReal res) {
            int maxBits = mRequired ? getMaxResultBits(mExprInfo.mLongTimeout)
            final int maxBits = mRequired ? getMaxResultBits(mExprInfo.mLongTimeout)
                    : QUICK_MAX_RESULT_BITS;
            return res.approxWholeNumberBitsGreaterThan(maxBits);
        }
@@ -1194,7 +1195,7 @@ public class Evaluator implements CalculatorExpr.ExprResolver {
                    && ((AsyncEvaluator)(ei.mEvaluator)).mRequired) {
                // Duplicate request; ignore.
            } else {
                // Restart evaluator in requested mode, i.e. with longer timeout.
                // (Re)start evaluator in requested mode, i.e. with longer timeout.
                cancel(ei, true);
                evaluateResult(index, listener, cmi, true);
            }
+2 −5
Original line number Diff line number Diff line
@@ -205,11 +205,8 @@ public class ExpressionDB {
    // initialization.
    private Object mLock = new Object();

    private Activity mActivity;

    public ExpressionDB(Activity activity) {
        mActivity = activity;
        mExpressionDBHelper = new ExpressionDBHelper(activity);
    public ExpressionDB(Context context) {
        mExpressionDBHelper = new ExpressionDBHelper(context);
        AsyncInitializer initializer = new AsyncInitializer();
        // All calls that create background database accesses are made from the UI thread, and
        // use a SERIAL_EXECUTOR. Thus they execute in order.