Advertisement
Guest User

Untitled

a guest
Aug 12th, 2017
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 17.63 KB | None | 0 0
  1. public class BalanceEquationFragment extends Fragment {
  2.     // TODO: Rename parameter arguments, choose names that match
  3.     // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
  4.     private static final String ARG_PARAM1 = "param1";
  5.     private static final String ARG_PARAM2 = "param2";
  6.  
  7.     // TODO: Rename and change types of parameters
  8.     private String mParam1;
  9.     private String mParam2;
  10.  
  11.     private Activity activity;
  12.  
  13.     private Toast toast = null;
  14.     private ConstraintLayout layout;
  15.     private EditText LHSEqn;
  16.     private EditText RHSEqn;
  17.     private TextView eqnText;
  18.     private Button pasteEquationButton;
  19.     private Button clearButton;
  20.     private Button balanceButton;
  21.     private Button plusButton;
  22.     private Button parenthesesButton;
  23.     private Button pasteLeftButton;
  24.     private Button pasteRightButton;
  25.     private Button copyButton;
  26.     private View eqnView;
  27.     private PopupWindow eqnPopupWindow;
  28.     private ClipboardManager clipboard;
  29.  
  30.     private OnFragmentInteractionListener mListener;
  31.  
  32.     public BalanceEquationFragment() {
  33.         // Required empty public constructor
  34.     }
  35.  
  36.     /**
  37.      * Use this factory method to create a new instance of
  38.      * this fragment using the provided parameters.
  39.      *
  40.      * @param param1 Parameter 1.
  41.      * @param param2 Parameter 2.
  42.      * @return A new instance of fragment BalanceEquationFragment.
  43.      */
  44.     // TODO: Rename and change types and number of parameters
  45.     public static BalanceEquationFragment newInstance(String param1, String param2) {
  46.         BalanceEquationFragment fragment = new BalanceEquationFragment();
  47.         Bundle args = new Bundle();
  48.         args.putString(ARG_PARAM1, param1);
  49.         args.putString(ARG_PARAM2, param2);
  50.         fragment.setArguments(args);
  51.         return fragment;
  52.     }
  53.  
  54.     @Override
  55.     public void onCreate(Bundle savedInstanceState) {
  56.         super.onCreate(savedInstanceState);
  57.         if (getArguments() != null) {
  58.             mParam1 = getArguments().getString(ARG_PARAM1);
  59.             mParam2 = getArguments().getString(ARG_PARAM2);
  60.         }
  61.     }
  62.  
  63.     // TODO: Rename method, update argument and hook method into UI event
  64.     public void onButtonPressed(Uri uri) {
  65.         if (mListener != null) {
  66.             mListener.onFragmentInteraction(uri);
  67.         }
  68.     }
  69.  
  70.     @Override
  71.     public void onAttach(Context context) {
  72.         super.onAttach(context);
  73.         if (context instanceof OnFragmentInteractionListener) {
  74.             mListener = (OnFragmentInteractionListener) context;
  75.         } else {
  76.             throw new RuntimeException(context.toString()
  77.                     + " must implement OnFragmentInteractionListener");
  78.         }
  79.         activity = (Activity) context;
  80.     }
  81.  
  82.     @Override
  83.     public void onDetach() {
  84.         super.onDetach();
  85.         mListener = null;
  86.     }
  87.  
  88.     /**
  89.      * This interface must be implemented by activities that contain this
  90.      * fragment to allow an interaction in this fragment to be communicated
  91.      * to the activity and potentially other fragments contained in that
  92.      * activity.
  93.      * <p>
  94.      * See the Android Training lesson <a href=
  95.      * "http://developer.android.com/training/basics/fragments/communicating.html"
  96.      * >Communicating with Other Fragments</a> for more information.
  97.      */
  98.     public interface OnFragmentInteractionListener {
  99.         // TODO: Update argument type and name
  100.         void onFragmentInteraction(Uri uri);
  101.     }
  102.  
  103.     @Override
  104.     public View onCreateView(LayoutInflater inflater, ViewGroup container,
  105.                              Bundle savedInstanceState) {
  106.         // Inflate the layout for this fragment
  107.         View rootView = inflater.inflate(R.layout.fragment_balance_equation, container, false);
  108.  
  109.         // initialize main fragment assets
  110.         layout = (ConstraintLayout) rootView.findViewById(R.id.balEqnFragment_layout);
  111.         LHSEqn = (EditText) rootView.findViewById(R.id.leftEquation);
  112.         RHSEqn = (EditText) rootView.findViewById(R.id.rightEquation);
  113.         pasteEquationButton = (Button) rootView.findViewById(R.id.equationpaste_button);
  114.         clearButton = (Button) rootView.findViewById(R.id.clear_button);
  115.         balanceButton = (Button) rootView.findViewById(R.id.balance_button);
  116.         plusButton = (Button) rootView.findViewById(R.id.plus_button);
  117.         parenthesesButton = (Button) rootView.findViewById(R.id.parentheses_button);
  118.         pasteLeftButton = (Button) rootView.findViewById(R.id.leftpaste_button);
  119.         pasteRightButton = (Button) rootView.findViewById(R.id.rightpaste_button);
  120.         clipboard = (ClipboardManager) getActivity().getSystemService(Context.CLIPBOARD_SERVICE);
  121.  
  122.         // initialize PopupWindow assets
  123.         eqnView = inflater.inflate(R.layout.popupwindow_equation, null);
  124.         eqnPopupWindow = new PopupWindow(eqnView, 1000,
  125.                 ConstraintLayout.LayoutParams.WRAP_CONTENT, false);
  126.         eqnText = (TextView) eqnView.findViewById(R.id.balanced_equation);
  127.         copyButton = (Button) eqnView.findViewById(R.id.copy_button);
  128.  
  129.         // set some view parameters
  130.         clearButton.setVisibility(View.GONE);
  131.         eqnText.setTextIsSelectable(true);
  132.         eqnText.setTextColor(Color.BLACK);
  133.         eqnPopupWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
  134.  
  135.         balanceButton.setOnClickListener(new View.OnClickListener() {
  136.             @Override
  137.             public void onClick(View view) {
  138.                 String equation = LHSEqn.getText().toString() + "=" + RHSEqn.getText().toString(); // string operations in separate class
  139.                 try {
  140.                     equation = EquationBalance.balanceEquation(equation);
  141.                 } catch (Exception e) {
  142.                     if (toast != null){
  143.                         toast.cancel();
  144.                     }
  145.                     toast = Toast.makeText(getActivity(), "Invalid Equation!", Toast.LENGTH_SHORT);
  146.                     toast.show();
  147.                     return;
  148.                 }
  149.                 if (getActivity().getCurrentFocus() != null){
  150.                     getActivity().getCurrentFocus().clearFocus();
  151.                 }
  152.  
  153.                 eqnText.setText(equation);
  154.                 eqnPopupWindow.showAtLocation(clearButton, Gravity.CENTER, 0, 0);
  155.             }
  156.         });
  157.  
  158.         copyButton.setOnClickListener(new View.OnClickListener() {
  159.             @Override
  160.             public void onClick(View view) {
  161.                 String equation = ((TextView) eqnView.findViewById(R.id.balanced_equation)).getText().toString();
  162.                 ClipData clip = ClipData.newPlainText("balanced equation", equation);
  163.                 clipboard.setPrimaryClip(clip);
  164.                 if (toast != null){
  165.                     toast.cancel();
  166.                 }
  167.                 toast = Toast.makeText(activity, "Equation Copied", Toast.LENGTH_SHORT);
  168.                 toast.show();
  169.             }
  170.         });
  171.  
  172.         clearButton.setOnClickListener(new View.OnClickListener() {
  173.             @Override
  174.             public void onClick(View view) {
  175.                 LHSEqn.setText("");
  176.                 RHSEqn.setText("");
  177.                 if (toast != null){
  178.                     toast.cancel();
  179.                 }
  180.                 toast = Toast.makeText(getActivity(), "Equation Cleared", Toast.LENGTH_SHORT);
  181.                 toast.show();
  182.             }
  183.         });
  184.  
  185.         plusButton.setOnClickListener(new View.OnClickListener() {
  186.             @Override
  187.             public void onClick(View view) {
  188.                 if (LHSEqn.hasFocus()){
  189.                     LHSEqn.getText().insert(LHSEqn.getSelectionStart(), "+");
  190.                 }
  191.                 if (RHSEqn.hasFocus()){
  192.                     RHSEqn.getText().insert(RHSEqn.getSelectionStart(), "+");
  193.                 }
  194.             }
  195.         });
  196.  
  197.         parenthesesButton.setOnClickListener(new View.OnClickListener() {
  198.             @Override
  199.             public void onClick(View view) {
  200.                 if (LHSEqn.hasFocus()){
  201.                     LHSEqn.getText().insert(LHSEqn.getSelectionStart(), "()");
  202.                     LHSEqn.setSelection(LHSEqn.getSelectionStart() - 1);
  203.                 }
  204.                 if (RHSEqn.hasFocus()){
  205.                     RHSEqn.getText().insert(RHSEqn.getSelectionStart(), "()");
  206.                     RHSEqn.setSelection(RHSEqn.getSelectionStart() - 1);
  207.                 }
  208.             }
  209.         });
  210.  
  211.         pasteLeftButton.setOnClickListener(new View.OnClickListener() {
  212.             @Override
  213.             public void onClick(View view) {
  214.                 if (!(clipboard.hasPrimaryClip())) {
  215.                     if (toast != null){
  216.                         toast.cancel();
  217.                     }
  218.                     toast = Toast.makeText(getActivity(), "Clipboard doesn't contain anything!", Toast.LENGTH_SHORT);
  219.                     toast.show();
  220.                 } else if (!(clipboard.getPrimaryClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN) ||
  221.                         clipboard.getPrimaryClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_HTML))) {
  222.                     if (toast != null){
  223.                         toast.cancel();
  224.                     }
  225.                     toast = Toast.makeText(getActivity(), "Clipboard doesn't contain usable text!", Toast.LENGTH_SHORT);
  226.                     toast.show();
  227.                 } else {
  228.                     ClipData.Item item = clipboard.getPrimaryClip().getItemAt(0);
  229.                     LHSEqn.setText(item.coerceToText(getActivity()).toString().replaceAll("[^A-Za-z0-9\\+\\(\\)\\[\\] ]", ""));
  230.                 }
  231.             }
  232.         });
  233.  
  234.         pasteRightButton.setOnClickListener(new View.OnClickListener() {
  235.             @Override
  236.             public void onClick(View view) {
  237.                 if (!(clipboard.hasPrimaryClip())) {
  238.                     if (toast != null){
  239.                         toast.cancel();
  240.                     }
  241.                     toast = Toast.makeText(getActivity(), "Clipboard doesn't contain anything!", Toast.LENGTH_SHORT);
  242.                     toast.show();
  243.                 } else if (!(clipboard.getPrimaryClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN) ||
  244.                         clipboard.getPrimaryClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_HTML))) {
  245.                     if (toast != null){
  246.                         toast.cancel();
  247.                     }
  248.                     toast = Toast.makeText(getActivity(), "Clipboard doesn't contain usable text!", Toast.LENGTH_SHORT);
  249.                     toast.show();
  250.                 } else {
  251.                     ClipData.Item item = clipboard.getPrimaryClip().getItemAt(0);
  252.                     RHSEqn.setText(item.coerceToText(getActivity()).toString().replaceAll("[^A-Za-z0-9\\+\\(\\)\\[\\] ]", ""));
  253.                 }
  254.             }
  255.         });
  256.  
  257.         pasteEquationButton.setOnClickListener(new View.OnClickListener() {
  258.             @Override
  259.             public void onClick(View view) {
  260.                 if (!(clipboard.hasPrimaryClip())) {
  261.                     if (toast != null){
  262.                         toast.cancel();
  263.                     }
  264.                     toast = Toast.makeText(getActivity(), "Clipboard doesn't contain anything!", Toast.LENGTH_SHORT);
  265.                     toast.show();
  266.                 } else if (!(clipboard.getPrimaryClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN) ||
  267.                         clipboard.getPrimaryClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_HTML))) {
  268.                     if (toast != null){
  269.                         toast.cancel();
  270.                     }
  271.                     toast = Toast.makeText(getActivity(), "Clipboard doesn't contain usable text!", Toast.LENGTH_SHORT);
  272.                     toast.show();
  273.                 } else {
  274.                     ClipData.Item item = clipboard.getPrimaryClip().getItemAt(0);
  275.                     String equation = item.coerceToText(getActivity()).toString();
  276.                     System.out.println(equation);
  277.                     equation = equation.replaceAll("-+", "=").replaceAll("<+","=").replaceAll(">+","=")
  278.                             .replaceAll("→+", "=").replaceAll("←+", "=")
  279.                             .replaceAll("↔+", "=").replaceAll("⇄+", "=")
  280.                             .replaceAll("⇌+", "=");
  281.                     System.out.println(equation);
  282.                     equation = equation.replaceAll("[^A-Za-z0-9\\+\\(\\)\\[\\]=]", "");
  283.                     System.out.println(equation);
  284.                     equation = equation.replaceAll("=+","=");
  285.                     String[] eqnHS = equation.split("=");
  286.                     if (eqnHS.length != 2) {
  287.                         if (toast != null) {
  288.                             toast.cancel();
  289.                         }
  290.                         toast = Toast.makeText(getActivity(), "Invalid equation!", Toast.LENGTH_SHORT);
  291.                         toast.show();
  292.                         return;
  293.                     }
  294.                     LHSEqn.setText(eqnHS[0]);
  295.                     RHSEqn.setText(eqnHS[1]);
  296.                 }
  297.             }
  298.         });
  299.  
  300.         layout.setOnTouchListener(new View.OnTouchListener(){
  301.             @Override
  302.             public boolean onTouch(View view, MotionEvent event){
  303.                 if (event.getAction() == MotionEvent.ACTION_UP){
  304.                     layout.requestFocus();
  305.                     InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
  306.                     imm.hideSoftInputFromWindow(layout.getWindowToken(), 0);
  307.                     return true;
  308.                 }
  309.                 return false;
  310.             }
  311.         });
  312.  
  313.         LHSEqn.addTextChangedListener(new TextWatcher() {
  314.             @Override
  315.             public void beforeTextChanged(CharSequence s, int start, int count, int after) {
  316.                 // nothing
  317.             }
  318.  
  319.             @Override
  320.             public void onTextChanged(CharSequence s, int start, int before, int count) {
  321.                 // nothing
  322.             }
  323.  
  324.             @Override
  325.             public void afterTextChanged(Editable s) {
  326.                 if (!LHSEqn.getText().toString().isEmpty() || !RHSEqn.getText().toString().isEmpty()){
  327.                     pasteEquationButton.setVisibility(View.GONE);
  328.                     clearButton.setVisibility(View.VISIBLE);
  329.                 }
  330.                 else{
  331.                     clearButton.setVisibility(View.GONE);
  332.                     pasteEquationButton.setVisibility(View.VISIBLE);
  333.                 }
  334.  
  335.                 for (int i = 0; i < s.length(); ++i){
  336.                     if (i == 0 && Character.isLowerCase(s.charAt(i))){
  337.                         String str = s.subSequence(i, i + 1).toString().toUpperCase();
  338.                         s.replace(i, i + 1, str);
  339.                     }
  340.                     else if (i > 0) {
  341.                         if (Character.isLowerCase(s.charAt(i)) && (s.charAt(i - 1) == '(' || s.charAt(i - 1) == ')' || s.charAt(i - 1) == ' '
  342.                                 || s.charAt(i - 1) == '+' || Character.isLowerCase(s.charAt(i - 1))
  343.                                 || Character.isDigit(s.charAt(i - 1)))) {
  344.                             String str = s.subSequence(i, i + 1).toString().toUpperCase();
  345.                             s.replace(i, i + 1, str);
  346.                         }
  347.                     }
  348.                 }
  349.             }
  350.         });
  351.  
  352.         RHSEqn.addTextChangedListener(new TextWatcher() {
  353.             @Override
  354.             public void beforeTextChanged(CharSequence s, int start, int count, int after) {
  355.                 // nothing
  356.             }
  357.  
  358.             @Override
  359.             public void onTextChanged(CharSequence s, int start, int before, int count) {
  360.                 // nothing
  361.             }
  362.  
  363.             @Override
  364.             public void afterTextChanged(Editable s) {
  365.                 if (!LHSEqn.getText().toString().isEmpty() || !RHSEqn.getText().toString().isEmpty()){
  366.                     pasteEquationButton.setVisibility(View.GONE);
  367.                     clearButton.setVisibility(View.VISIBLE);
  368.                 }
  369.                 else{
  370.                     clearButton.setVisibility(View.GONE);
  371.                     pasteEquationButton.setVisibility(View.VISIBLE);
  372.                 }
  373.  
  374.                 for (int i = 0; i < s.length(); ++i){
  375.                     if (i == 0 && Character.isLowerCase(s.charAt(i))){
  376.                         String str = s.subSequence(i, i + 1).toString().toUpperCase();
  377.                         s.replace(i, i + 1, str);
  378.                     }
  379.                     else if (i > 0) {
  380.                         if (Character.isLowerCase(s.charAt(i)) && (s.charAt(i - 1) == '(' || s.charAt(i - 1) == ')' || s.charAt(i - 1) == ' '
  381.                                 || s.charAt(i - 1) == '+' || Character.isLowerCase(s.charAt(i - 1))
  382.                                 || Character.isDigit(s.charAt(i - 1)))) {
  383.                             String str = s.subSequence(i, i + 1).toString().toUpperCase();
  384.                             s.replace(i, i + 1, str);
  385.                         }
  386.                     }
  387.                 }
  388.             }
  389.         });
  390.  
  391.         RHSEqn.setOnEditorActionListener(new TextView.OnEditorActionListener() {
  392.             @Override
  393.             public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
  394.                 boolean handled = false;
  395.                 if (actionId == EditorInfo.IME_ACTION_GO) {
  396.                     balanceButton.performClick();
  397.                     handled = true;
  398.                 }
  399.                 return handled;
  400.             }
  401.         });
  402.  
  403.         return rootView;
  404.     }
  405. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement