<commit_before>package com.martin.knowledgebase;

import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import java.security.GeneralSecurityException;

import static com.tozny.crypto.android.AesCbcWithIntegrity.generateSalt;
import static com.tozny.crypto.android.AesCbcWithIntegrity.saltString;


public class LoginActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        setTitle(getString(R.string.title_activity_login));
        if (savedInstanceState == null) {
            SharedPreferences prefs = getSharedPreferences("KB", MODE_PRIVATE);
            if (prefs.contains("salt")) {
                getFragmentManager().beginTransaction().add(R.id.container, new FragmentCheckPassword()).commit();
            } else {
                getFragmentManager().beginTransaction().add(R.id.container, new FragmentSetPassword()).commit();
            }
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_login, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            // TODO: Remove debug code
            SharedPreferences prefs = getSharedPreferences("KB", MODE_PRIVATE);
            SharedPreferences.Editor editor = prefs.edit();
            editor.clear();
            editor.commit();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public static class FragmentSetPassword extends Fragment {

        private EditText mFirst, mSecond;
        private Button mGenerate;
        public FragmentSetPassword() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_setpassword, container, false);
            mFirst = (EditText) rootView.findViewById(R.id.etFirst);
            mSecond = (EditText) rootView.findViewById(R.id.etSecond);
            mGenerate = (Button) rootView.findViewById(R.id.bEnter);
            return rootView;
        }

        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);

            if (savedInstanceState != null) {
                mFirst.setText(savedInstanceState.getString("first"));
                mSecond.setText(savedInstanceState.getString("second"));
            }

            mGenerate.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mFirst.getText().toString().contentEquals(mSecond.getText().toString()) && !mFirst.getText().toString().contentEquals("")) {
                        try {
                            String salt = saltString(generateSalt());
                            SharedPreferences prefs = getActivity().getSharedPreferences("KB", MODE_PRIVATE);
                            SharedPreferences.Editor editor = prefs.edit();
                            editor.putString("salt", salt);
                            editor.commit();
                            Util.stringEncrypt(getActivity(), mFirst.getText().toString(), "pwcheck", "This should equal itself.");
                            Intent i = new Intent(getActivity(), MainActivity.class);
                            i.putExtra("password", mFirst.getText().toString());
                            startActivity(i);
                            getActivity().finish();
                        } catch (GeneralSecurityException e) {
                            e.printStackTrace();
                        }
                    } else {
                        // TODO: Have snackbar pop up
                    }
                }
            });
        }

        @Override
        public void onSaveInstanceState(Bundle outState) {
            super.onSaveInstanceState(outState);
            outState.putString("first", mFirst.getText().toString());
            outState.putString("second", mSecond.getText().toString());
        }
    }

    public static class FragmentCheckPassword extends Fragment {

        private EditText mFirst;
        private Button mLogin;
        public FragmentCheckPassword() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_checkpassword, container, false);
            mFirst = (EditText) rootView.findViewById(R.id.etFirst);
            mLogin = (Button) rootView.findViewById(R.id.bEnter);
            return rootView;
        }

        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);

            if (savedInstanceState != null) {
                mFirst.setText(savedInstanceState.getString("first"));
            }

            // TODO: Check password integrity
            mLogin.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (!mFirst.getText().toString().contentEquals("")) {
                        Intent i = new Intent(getActivity(), MainActivity.class);
                        i.putExtra("password", mFirst.getText().toString());
                        startActivity(i);
                        getActivity().finish();
                    } else {
                        // TODO: snackbar
                    }
                }
            });
        }

        @Override
        public void onSaveInstanceState(Bundle outState) {
            super.onSaveInstanceState(outState);
            outState.putString("first", mFirst.getText().toString());<commit_msg>Implemented primitive method to check password for correctness<commit_after>package com.martin.knowledgebase;

import android.app.Activity;
import android.app.Fragment;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.security.GeneralSecurityException;

import static com.tozny.crypto.android.AesCbcWithIntegrity.generateSalt;
import static com.tozny.crypto.android.AesCbcWithIntegrity.saltString;


public class LoginActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        setTitle(getString(R.string.title_activity_login));
        if (savedInstanceState == null) {
            SharedPreferences prefs = getSharedPreferences("KB", MODE_PRIVATE);
            if (prefs.contains("salt")) {
                getFragmentManager().beginTransaction().add(R.id.container, new FragmentCheckPassword()).commit();
            } else {
                getFragmentManager().beginTransaction().add(R.id.container, new FragmentSetPassword()).commit();
            }
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_login, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            // TODO: Remove debug code
            SharedPreferences prefs = getSharedPreferences("KB", MODE_PRIVATE);
            SharedPreferences.Editor editor = prefs.edit();
            editor.clear();
            editor.commit();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public static class FragmentSetPassword extends Fragment {

        private EditText mFirst, mSecond;
        private Button mGenerate;

        public FragmentSetPassword() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_setpassword, container, false);
            mFirst = (EditText) rootView.findViewById(R.id.etFirst);
            mSecond = (EditText) rootView.findViewById(R.id.etSecond);
            mGenerate = (Button) rootView.findViewById(R.id.bEnter);
            return rootView;
        }

        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);

            if (savedInstanceState != null) {
                mFirst.setText(savedInstanceState.getString("first"));
                mSecond.setText(savedInstanceState.getString("second"));
            }

            mGenerate.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mFirst.getText().toString().contentEquals(mSecond.getText().toString()) && !mFirst.getText().toString().contentEquals("")) {
                        try {
                            String salt = saltString(generateSalt());
                            SharedPreferences prefs = getActivity().getSharedPreferences("KB", MODE_PRIVATE);
                            SharedPreferences.Editor editor = prefs.edit();
                            editor.putString("salt", salt);
                            editor.commit();
                            // TODO: More efficient way of checking password, e.g. hashing function
                            final ProgressDialog progress = ProgressDialog.show(getActivity(), "Saving", "Saving password", true);
                            new Thread() {

                                @Override
                                public void run() {
                                    super.run();
                                    Util.stringEncrypt(getActivity(), mFirst.getText().toString(), "pwcheck", "This should equal itself.");
                                    getActivity().runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            progress.dismiss();
                                            Intent i = new Intent(getActivity(), MainActivity.class);
                                            i.putExtra("password", mFirst.getText().toString());
                                            startActivity(i);
                                            getActivity().finish();
                                        }
                                    });
                                }

                            }.start();
                        } catch (GeneralSecurityException e) {
                            e.printStackTrace();
                        }
                    } else {
                        // TODO: Have snackbar pop up
                    }
                }
            });
        }

        @Override
        public void onSaveInstanceState(Bundle outState) {
            super.onSaveInstanceState(outState);
            outState.putString("first", mFirst.getText().toString());
            outState.putString("second", mSecond.getText().toString());
        }
    }

    public static class FragmentCheckPassword extends Fragment {

        private EditText mFirst;
        private Button mLogin;

        public FragmentCheckPassword() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_checkpassword, container, false);
            mFirst = (EditText) rootView.findViewById(R.id.etFirst);
            mLogin = (Button) rootView.findViewById(R.id.bEnter);
            return rootView;
        }

        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);

            if (savedInstanceState != null) {
                mFirst.setText(savedInstanceState.getString("first"));
            }

            mLogin.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (!mFirst.getText().toString().contentEquals("")) {
                        final ProgressDialog progress = ProgressDialog.show(getActivity(), "Checking", "Checking password", true);
                        new Thread() {

                            @Override
                            public void run() {
                                super.run();
                                final String decrypted = Util.stringDecrypt(getActivity(), mFirst.getText().toString(), "pwcheck");
                                getActivity().runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        progress.dismiss();
                                        if (decrypted.contentEquals("This should equal itself.")) {
                                            Intent i = new Intent(getActivity(), MainActivity.class);
                                            i.putExtra("password", mFirst.getText().toString());
                                            startActivity(i);
                                            getActivity().finish();
                                        }
                                        else {
                                            // TODO: snackbar
                                            Toast.makeText(getActivity(), "Wrong password", Toast.LENGTH_SHORT).show();
                                        }
                                    }
                                });
                            }

                        }.start();
                    } else {
                        // TODO: snackbar
                    }
                }
            });
        }

        @Override
        public void onSaveInstanceState(Bundle outState) {
            super.onSaveInstanceState(outState);
            outState.putString("first", mFirst.getText().toString());
{'fraction_non_alphanumeric': 0.09159082168952698, 'fraction_numerical': 0.0, 'mean_word_length': 1.6535714285714285, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 38, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6959310', 'n_tokens_mistral': 3569, 'n_tokens_neox': 3312, 'n_words': 746}