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));
  }
 @Override
 protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_calculator);
   savedInstanceState = savedInstanceState == null ? Bundle.EMPTY : savedInstanceState;
   initialize(savedInstanceState);
   mEvaluator.evaluate(mFormulaEditText.getCleanText(), this);
 }
 private void onEquals() {
   String text = mFormulaEditText.getCleanText();
   if (mCurrentState == CalculatorState.INPUT) {
     switch (mEqualButton.getState()) {
       case EQUALS:
         setState(CalculatorState.EVALUATE);
         mEvaluator.evaluate(text, this);
         break;
       case NEXT:
         mFormulaEditText.next();
         break;
     }
   } else if (mCurrentState == CalculatorState.GRAPHING) {
     setState(CalculatorState.EVALUATE);
     onEvaluate(text, "", INVALID_RES_ID);
   }
 }
  @Override
  protected void onResume() {
    super.onResume();

    // Load up to date history
    mPersist = new Persist(this);
    mPersist.load();
    mHistory = mPersist.getHistory();
    incrementGroupId();

    // When history is open, the display is saved as a Display Entry. Cache it if it exists.
    HistoryEntry displayEntry = null;
    if (mHistoryAdapter != null) {
      displayEntry = mHistoryAdapter.getDisplayEntry();
    }

    // Create a new History Adapter (with the up-to-date history)
    mHistoryAdapter = new HistoryAdapter(this, mEvaluator.getSolver(), mHistory);
    mHistoryAdapter.setOnItemClickListener(
        new HistoryAdapter.OnItemClickListener() {
          @Override
          public void onItemClick(final HistoryEntry entry) {
            mDisplayView.collapse(
                new AnimationFinishedListener() {
                  @Override
                  public void onAnimationFinished() {
                    mFormulaEditText.setText(entry.getFormula());
                  }
                });
          }
        });
    mHistoryAdapter.setOnItemLongclickListener(
        new HistoryAdapter.OnItemLongClickListener() {
          @Override
          public void onItemLongClick(HistoryEntry entry) {
            Clipboard.copy(getBaseContext(), entry.getResult());
          }
        });

    // Restore the Display Entry (if it existed)
    if (displayEntry != null) {
      mHistoryAdapter.setDisplayEntry(displayEntry.getFormula(), displayEntry.getResult());
    }

    // Observe! Set! Typical adapter stuff.
    mHistory.setObserver(
        new History.Observer() {
          @Override
          public void notifyDataSetChanged() {
            mHistoryAdapter.notifyDataSetChanged();
          }
        });
    mDisplayView.setAdapter(mHistoryAdapter);
    mDisplayView.attachToRecyclerView(
        new ItemTouchHelper(
            new ItemTouchHelper.SimpleCallback(
                ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT,
                ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
              @Override
              public boolean onMove(
                  RecyclerView recyclerView,
                  RecyclerView.ViewHolder viewHolder,
                  RecyclerView.ViewHolder target) {
                return false;
              }

              @Override
              public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
                if (viewHolder.getAdapterPosition() < mHistory.getEntries().size()) {
                  HistoryEntry item = mHistory.getEntries().get(viewHolder.getAdapterPosition());
                  mHistory.remove(item);
                  mHistoryAdapter.notifyItemRemoved(viewHolder.getAdapterPosition());
                }
              }
            }));
    mDisplayView.scrollToMostRecent();
  }