<commit_before>package me.cvhc.equationsolver;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.inputmethodservice.Keyboard;
import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.InputFilter;
import android.text.InputType;
import android.text.Spanned;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.github.brnunes.swipeablerecyclerview.SwipeableRecyclerViewTouchListener;

import java.util.ArrayList;
import java.util.Arrays;

public class MainFragment extends Fragment {
    private final String LOG_TAG = MainFragment.class.getSimpleName();
    private final int HISTORY_SIZE = 10;

    private ActionBar mActionBar;
    private RecyclerView mRecyclerView;
    private RecyclerViewAdapter mRecyclerViewAdapter;
    private RecyclerView.LayoutManager mLayoutManager;
    private AutoCompleteTextView mEditInputNewExpression;

    private ArrayAdapter<String> mAutocompleteAssignmentAdapter;
    private ArrayAdapter<String> mAutocompleteEquationAdapter;

    private ToggleButton mToggleInputType;
    private Button mButtonSolve;
    private Button mButtonAdd;
    private ExpressionKeypad mExpressionKeypad;
    private View mDummy;

    private Toast mToast;
    private boolean mDoubleBackToExitPressedOnce = false;

    public MainFragment() {
    }

    private class CustomTextWatcher implements TextWatcher {
        @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
        @Override public void onTextChanged(CharSequence s, int start, int before, int count) {}

        @Override
        public void afterTextChanged(Editable s) {
            String str = s.toString();

            mButtonAdd.setVisibility(str.length() > 0 ? View.VISIBLE : View.GONE);
            if (mToggleInputType.isChecked() || str.length() == 0) {
                return;
            }

            char id = str.charAt(0);
            String prefix = id + " = ";

            if (!Character.isLowerCase(id)) {
                makeToast(String.format(getString(R.string.error_invalid_id), id));
                mEditInputNewExpression.setText("");
                return;
            }

            if (str.length() == 1) {
                mEditInputNewExpression.setText(prefix);
                mEditInputNewExpression.setSelection(prefix.length());
            } else if (str.length() > 1) {
                if (str.length() < prefix.length()) {
                    mEditInputNewExpression.setText("");
                } else if (!str.startsWith(prefix)) {
                    mEditInputNewExpression.setText(prefix);
                    mEditInputNewExpression.setSelection(prefix.length());
                }
            }
        }
    }

    private class SimpleInputFilter implements InputFilter {
        public CharSequence filter(CharSequence source, int start, int end,
                                   Spanned dest, int dstart, int dend) {
            boolean meetEqual = dest.toString().indexOf('=') != -1;

            for (int i = start; i < end; i++) {
                char c = source.charAt(i);
                if (c == '=') {
                    if (meetEqual) {
                        makeToast(R.string.error_multiple_equal_sign);
                        return "";
                    }
                } else if (!Character.isLetter(c) && !Character.isDigit(c)
                        && ".=+-*/^() ".indexOf(c) == -1) {
                    makeToast(R.string.error_illegal_char);
                    return "";
                }
            }
            return null;
        }
    }


    private class SwipeListener implements SwipeableRecyclerViewTouchListener.SwipeListener {
        @Override
        public boolean canSwipeLeft(int position) {
            return true;
        }

        @Override
        public boolean canSwipeRight(int position) {
            return true;
        }

        private void helper(int[] reverseSortedPositions) {
            for (int position : reverseSortedPositions) {
                mRecyclerViewAdapter.removeItem(position);
            }
        }

        @Override
        public void onDismissedBySwipeLeft(RecyclerView recyclerView, int[] reverseSortedPositions) {
            helper(reverseSortedPositions);
        }

        @Override
        public void onDismissedBySwipeRight(RecyclerView recyclerView, int[] reverseSortedPositions) {
            helper(reverseSortedPositions);
        }
    }

    private class FinishEditListener implements EditText.OnEditorActionListener {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_DONE || event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
                return submitNewExpression(v);
            }

            return false;
        }
    }

    private boolean submitNewExpression(TextView v) {
        if (mRecyclerViewAdapter.newItem(v.getText(), mToggleInputType.isChecked())) {
            mRecyclerView.smoothScrollToPosition(mRecyclerViewAdapter.getItemCount() - 1);

            if (mToggleInputType.isChecked()) {
                mAutocompleteEquationAdapter.insert(v.getText().toString(), 0);
            } else {
                mAutocompleteAssignmentAdapter.insert(v.getText().toString(), 0);
            }

            v.setText("");
            return true;
        } else {<commit_msg>Prevent user input x as first character when assign<commit_after>package me.cvhc.equationsolver;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.inputmethodservice.Keyboard;
import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.InputFilter;
import android.text.InputType;
import android.text.Spanned;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.github.brnunes.swipeablerecyclerview.SwipeableRecyclerViewTouchListener;

import java.util.ArrayList;
import java.util.Arrays;

public class MainFragment extends Fragment {
    private final String LOG_TAG = MainFragment.class.getSimpleName();
    private final int HISTORY_SIZE = 10;

    private ActionBar mActionBar;
    private RecyclerView mRecyclerView;
    private RecyclerViewAdapter mRecyclerViewAdapter;
    private RecyclerView.LayoutManager mLayoutManager;
    private AutoCompleteTextView mEditInputNewExpression;

    private ArrayAdapter<String> mAutocompleteAssignmentAdapter;
    private ArrayAdapter<String> mAutocompleteEquationAdapter;

    private ToggleButton mToggleInputType;
    private Button mButtonSolve;
    private Button mButtonAdd;
    private ExpressionKeypad mExpressionKeypad;
    private View mDummy;

    private Toast mToast;
    private boolean mDoubleBackToExitPressedOnce = false;

    public MainFragment() {
    }

    private class CustomTextWatcher implements TextWatcher {
        @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
        @Override public void onTextChanged(CharSequence s, int start, int before, int count) {}

        @Override
        public void afterTextChanged(Editable s) {
            String str = s.toString();

            mButtonAdd.setVisibility(str.length() > 0 ? View.VISIBLE : View.GONE);
            if (mToggleInputType.isChecked() || str.length() == 0) {
                return;
            }

            char id = str.charAt(0);
            String prefix = id + " = ";

            if (!Character.isLowerCase(id) || id == 'x') {
                makeToast(String.format(getString(R.string.error_invalid_id), id));
                mEditInputNewExpression.setText("");
                return;
            }

            if (str.length() == 1) {
                mEditInputNewExpression.setText(prefix);
                mEditInputNewExpression.setSelection(prefix.length());
            } else if (str.length() > 1) {
                if (str.length() < prefix.length()) {
                    mEditInputNewExpression.setText("");
                } else if (!str.startsWith(prefix)) {
                    mEditInputNewExpression.setText(prefix);
                    mEditInputNewExpression.setSelection(prefix.length());
                }
            }
        }
    }

    private class SimpleInputFilter implements InputFilter {
        public CharSequence filter(CharSequence source, int start, int end,
                                   Spanned dest, int dstart, int dend) {
            boolean meetEqual = dest.toString().indexOf('=') != -1;

            for (int i = start; i < end; i++) {
                char c = source.charAt(i);
                if (c == '=') {
                    if (meetEqual) {
                        makeToast(R.string.error_multiple_equal_sign);
                        return "";
                    }
                } else if (!Character.isLetter(c) && !Character.isDigit(c)
                        && ".=+-*/^() ".indexOf(c) == -1) {
                    makeToast(R.string.error_illegal_char);
                    return "";
                }
            }
            return null;
        }
    }

    private class SwipeListener implements SwipeableRecyclerViewTouchListener.SwipeListener {
        @Override
        public boolean canSwipeLeft(int position) {
            return true;
        }

        @Override
        public boolean canSwipeRight(int position) {
            return true;
        }

        private void helper(int[] reverseSortedPositions) {
            for (int position : reverseSortedPositions) {
                mRecyclerViewAdapter.removeItem(position);
            }
        }

        @Override
        public void onDismissedBySwipeLeft(RecyclerView recyclerView, int[] reverseSortedPositions) {
            helper(reverseSortedPositions);
        }

        @Override
        public void onDismissedBySwipeRight(RecyclerView recyclerView, int[] reverseSortedPositions) {
            helper(reverseSortedPositions);
        }
    }

    private class FinishEditListener implements EditText.OnEditorActionListener {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_DONE ||
                    event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
                return submitNewExpression(v);
            } else {
                return false;
            }
        }
    }

    private boolean submitNewExpression(TextView v) {
        if (mRecyclerViewAdapter.newItem(v.getText(), mToggleInputType.isChecked())) {
            mRecyclerView.smoothScrollToPosition(mRecyclerViewAdapter.getItemCount() - 1);

            if (mToggleInputType.isChecked()) {
                mAutocompleteEquationAdapter.insert(v.getText().toString(), 0);
            } else {
                mAutocompleteAssignmentAdapter.insert(v.getText().toString(), 0);
            }

            v.setText("");
            return true;
        } else {
{'fraction_non_alphanumeric': 0.08975762314308053, 'fraction_numerical': 0.0026583268178264267, 'mean_word_length': 2.46639566395664, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4890009', 'n_tokens_mistral': 3437, 'n_tokens_neox': 3272, 'n_words': 703}