@Override
  protected void onSaveInstanceState(@NonNull Bundle outState) {
    // If there's an animation in progress, cancel it first to ensure our state is up-to-date.
    if (mCurrentAnimator != null) {
      mCurrentAnimator.cancel();
    }

    super.onSaveInstanceState(outState);
    outState.putInt(KEY_CURRENT_STATE, mCurrentState.ordinal());
    outState.putString(
        KEY_CURRENT_EXPRESSION,
        mTokenizer.getNormalizedExpression(mFormulaEditText.getCleanText()));
  }
  protected void initialize(Bundle savedInstanceState) {
    // Rebuild constants. If the user changed their locale, it won't kill the app
    // but it might change a decimal point from . to ,
    Constants.rebuildConstants();

    mDisplayView = (DisplayOverlay) findViewById(R.id.display);
    mDisplayView.setFade(findViewById(R.id.history_fade));
    mDisplayForeground = (ViewGroup) findViewById(R.id.the_clear_animation);
    mFormulaEditText = (CalculatorEditText) findViewById(R.id.formula);
    mResultEditText = (CalculatorEditText) findViewById(R.id.result);
    mPadViewPager = (CalculatorPadView) findViewById(R.id.pad_pager);
    mDeleteButton = findViewById(R.id.del);
    mClearButton = findViewById(R.id.clr);
    mEqualButton = (EqualsImageButton) findViewById(R.id.pad_numeric).findViewById(R.id.eq);

    if (mEqualButton == null || mEqualButton.getVisibility() != View.VISIBLE) {
      mEqualButton = (EqualsImageButton) findViewById(R.id.pad_operator).findViewById(R.id.eq);
    }

    mTokenizer = new CalculatorExpressionTokenizer(this);
    mEvaluator = new CalculatorExpressionEvaluator(mTokenizer);

    setState(
        CalculatorState.values()[
            savedInstanceState.getInt(KEY_CURRENT_STATE, CalculatorState.INPUT.ordinal())]);

    mFormulaEditText.setSolver(mEvaluator.getSolver());
    mResultEditText.setSolver(mEvaluator.getSolver());
    mFormulaEditText.setText(
        mTokenizer.getLocalizedExpression(
            savedInstanceState.getString(KEY_CURRENT_EXPRESSION, "")));
    mFormulaEditText.addTextChangedListener(mFormulaTextWatcher);
    mFormulaEditText.setOnKeyListener(mFormulaOnKeyListener);
    mFormulaEditText.setOnTextSizeChangeListener(this);
    mDeleteButton.setOnLongClickListener(this);
    mResultEditText.setEnabled(false);
    findViewById(R.id.lparen).setOnLongClickListener(this);
    findViewById(R.id.rparen).setOnLongClickListener(this);
    findViewById(R.id.fun_sin).setOnLongClickListener(this);
    findViewById(R.id.fun_cos).setOnLongClickListener(this);
    findViewById(R.id.fun_tan).setOnLongClickListener(this);

    // Disable IME for this application
    getWindow()
        .setFlags(
            WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,
            WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);

    Button dot = (Button) findViewById(R.id.dec_point);
    dot.setText(String.valueOf(Constants.DECIMAL_POINT));
  }
 protected String getNormalizedExpression(String expr) {
   return mTokenizer.getNormalizedExpression(expr);
 }
 protected String cleanExpression(String expr) {
   expr = EquationFormatter.appendParenthesis(expr);
   expr = Solver.clean(expr);
   expr = mTokenizer.getLocalizedExpression(expr);
   return expr;
 }