/**
  * Inserts text into the formula EditText. If an equation was recently solved, it will replace the
  * formula's text instead of appending.
  */
 protected void insert(String text) {
   // Add left parenthesis after functions.
   if (mCurrentState.equals(CalculatorState.INPUT)
       || mCurrentState.equals(CalculatorState.GRAPHING)
       || mFormulaEditText.isCursorModified()) {
     mFormulaEditText.insert(text);
   } else {
     mFormulaEditText.setText(text);
     incrementGroupId();
   }
 }
  @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));
  }