<commit_before>package in.testpress.testpress.authenticator;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.inject.Inject;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import in.testpress.testpress.Injector;
import in.testpress.testpress.R;
import in.testpress.testpress.R.id;
import in.testpress.testpress.TestpressApplication;
import in.testpress.testpress.core.Constants;
import in.testpress.testpress.core.TestpressService;
import in.testpress.testpress.models.DaoSession;
import in.testpress.testpress.models.InstituteSettings;
import in.testpress.testpress.models.InstituteSettingsDao;
import in.testpress.testpress.models.RegistrationSuccessResponse;
import in.testpress.testpress.models.RegistrationErrorDetails;
import in.testpress.testpress.ui.TextWatcherAdapter;
import in.testpress.testpress.util.InternetConnectivityChecker;
import in.testpress.testpress.util.SafeAsyncTask;
import retrofit.RetrofitError;

import static android.view.inputmethod.EditorInfo.IME_ACTION_DONE;
import static in.testpress.testpress.authenticator.LoginActivity.REQUEST_CODE_REGISTER_USER;
import static in.testpress.testpress.authenticator.RegisterActivity.VerificationMethod.EMAIL;
import static in.testpress.testpress.authenticator.RegisterActivity.VerificationMethod.MOBILE;

public class RegisterActivity extends AppCompatActivity {

    @Inject TestpressService testpressService;
    @InjectView(id.et_username) EditText usernameText;
    @InjectView(id.et_password) EditText passwordText;
    @InjectView(id.et_password_confirm) EditText confirmPasswordText;
    @InjectView(id.et_email) EditText emailText;
    @InjectView(id.et_phone) EditText phoneText;
    @InjectView(id.phone_layout) TextInputLayout phoneLayout;
    @InjectView(id.tv_fill_all_details) TextView fillAllDetailsText;
    @InjectView(id.b_register) Button registerButton;
    @InjectView(id.register_layout) LinearLayout registerLayout;
    @InjectView(R.id.success_complete) LinearLayout successContainer;
    @InjectView(R.id.success_description) TextView successDescription;

    private final TextWatcher watcher = validationTextWatcher();
    private RegistrationSuccessResponse registrationSuccessResponse;
    private MaterialDialog progressDialog;
    private InternetConnectivityChecker internetConnectivityChecker = new InternetConnectivityChecker(this);
    private VerificationMethod verificationMethod;
    enum VerificationMethod { MOBILE, EMAIL }

    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        Injector.inject(this);
        setContentView(R.layout.register_activity);
        ButterKnife.inject(this);
        DaoSession daoSession = ((TestpressApplication) getApplicationContext()).getDaoSession();
        InstituteSettingsDao instituteSettingsDao = daoSession.getInstituteSettingsDao();
        List<InstituteSettings> instituteSettingsList = instituteSettingsDao.queryBuilder()
                .where(InstituteSettingsDao.Properties.BaseUrl.eq(Constants.Http.URL_BASE)).list();
        if (instituteSettingsList.size() != 0) {
            InstituteSettings instituteSettings = instituteSettingsList.get(0);
            verificationMethod =
                    instituteSettings.getVerificationMethod().equals("M") ? MOBILE : EMAIL;
        } else {
            // Never happen, just for a safety.
            finish();
        }
        confirmPasswordText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            public boolean onEditorAction(final TextView v, final int actionId,
                                          final KeyEvent event) {
                if (actionId == IME_ACTION_DONE && registerButton.isEnabled()) {
                    register();
                    return true;
                }
                return false;
            }
        });
        usernameText.addTextChangedListener(watcher);
        passwordText.addTextChangedListener(watcher);
        emailText.addTextChangedListener(watcher);
        if (verificationMethod.equals(MOBILE)) {
            phoneText.addTextChangedListener(watcher);
            phoneLayout.setVisibility(View.VISIBLE);
        } else {
            phoneLayout.setVisibility(View.GONE);
        }
        confirmPasswordText.addTextChangedListener(watcher);
    }

    void postDetails(){
        progressDialog = new MaterialDialog.Builder(this)
                .title(R.string.loading)
                .content(R.string.please_wait)
                .widgetColorRes(R.color.primary)
                .progress(true, 0)
                .show();
        new SafeAsyncTask<Boolean>() {
            public Boolean call() throws Exception {
                registrationSuccessResponse = testpressService.register(usernameText.getText().toString(), emailText.getText().toString(), passwordText.getText().toString(), phoneText.getText().toString());
                return true;
            }

            @Override
            protected void onException(final Exception e) throws RuntimeException {
                // Retrofit Errors are handled
                if((e instanceof RetrofitError)) {
                    RegistrationErrorDetails registrationErrorDetails = (RegistrationErrorDetails)((RetrofitError) e).getBodyAs(RegistrationErrorDetails.class);
                    if(!registrationErrorDetails.getUsername().isEmpty()) {
                        usernameText.setError(registrationErrorDetails.getUsername().get(0));
                        usernameText.requestFocus();
                    }
                    if(!registrationErrorDetails.getEmail().isEmpty()) {
                        emailText.setError(registrationErrorDetails.getEmail().get(0));
                        emailText.requestFocus();
                    }
                    if(!registrationErrorDetails.getPassword().isEmpty()) {
                        passwordText.setError(registrationErrorDetails.getPassword().get(0));
                        passwordText.requestFocus();
                    }
                    if(!registrationErrorDetails.getPhone().isEmpty()) {
                        phoneText.setError(registrationErrorDetails.getPhone().get(0));
                        phoneText.requestFocus();
                    }
                }
                progressDialog.dismiss();
            }<commit_msg>Prevent multiple account creation<commit_after>package in.testpress.testpress.authenticator;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.inject.Inject;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import in.testpress.testpress.Injector;
import in.testpress.testpress.R;
import in.testpress.testpress.R.id;
import in.testpress.testpress.TestpressApplication;
import in.testpress.testpress.core.Constants;
import in.testpress.testpress.core.TestpressService;
import in.testpress.testpress.models.DaoSession;
import in.testpress.testpress.models.InstituteSettings;
import in.testpress.testpress.models.InstituteSettingsDao;
import in.testpress.testpress.models.RegistrationSuccessResponse;
import in.testpress.testpress.models.RegistrationErrorDetails;
import in.testpress.testpress.ui.TextWatcherAdapter;
import in.testpress.testpress.util.InternetConnectivityChecker;
import in.testpress.testpress.util.SafeAsyncTask;
import retrofit.RetrofitError;

import static android.view.inputmethod.EditorInfo.IME_ACTION_DONE;
import static in.testpress.testpress.authenticator.LoginActivity.REQUEST_CODE_REGISTER_USER;
import static in.testpress.testpress.authenticator.RegisterActivity.VerificationMethod.EMAIL;
import static in.testpress.testpress.authenticator.RegisterActivity.VerificationMethod.MOBILE;

public class RegisterActivity extends AppCompatActivity {

    @Inject TestpressService testpressService;
    @InjectView(id.et_username) EditText usernameText;
    @InjectView(id.et_password) EditText passwordText;
    @InjectView(id.et_password_confirm) EditText confirmPasswordText;
    @InjectView(id.et_email) EditText emailText;
    @InjectView(id.et_phone) EditText phoneText;
    @InjectView(id.phone_layout) TextInputLayout phoneLayout;
    @InjectView(id.tv_fill_all_details) TextView fillAllDetailsText;
    @InjectView(id.b_register) Button registerButton;
    @InjectView(id.register_layout) LinearLayout registerLayout;
    @InjectView(R.id.success_complete) LinearLayout successContainer;
    @InjectView(R.id.success_description) TextView successDescription;

    private final TextWatcher watcher = validationTextWatcher();
    private RegistrationSuccessResponse registrationSuccessResponse;
    private MaterialDialog progressDialog;
    private InternetConnectivityChecker internetConnectivityChecker = new InternetConnectivityChecker(this);
    private VerificationMethod verificationMethod;
    enum VerificationMethod { MOBILE, EMAIL }

    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        Injector.inject(this);
        setContentView(R.layout.register_activity);
        ButterKnife.inject(this);
        DaoSession daoSession = ((TestpressApplication) getApplicationContext()).getDaoSession();
        InstituteSettingsDao instituteSettingsDao = daoSession.getInstituteSettingsDao();
        List<InstituteSettings> instituteSettingsList = instituteSettingsDao.queryBuilder()
                .where(InstituteSettingsDao.Properties.BaseUrl.eq(Constants.Http.URL_BASE)).list();
        if (instituteSettingsList.size() != 0) {
            InstituteSettings instituteSettings = instituteSettingsList.get(0);
            verificationMethod =
                    instituteSettings.getVerificationMethod().equals("M") ? MOBILE : EMAIL;
        } else {
            // Never happen, just for a safety.
            finish();
        }
        confirmPasswordText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            public boolean onEditorAction(final TextView v, final int actionId,
                                          final KeyEvent event) {
                if (actionId == IME_ACTION_DONE && registerButton.isEnabled()) {
                    register();
                    return true;
                }
                return false;
            }
        });
        usernameText.addTextChangedListener(watcher);
        passwordText.addTextChangedListener(watcher);
        emailText.addTextChangedListener(watcher);
        if (verificationMethod.equals(MOBILE)) {
            phoneText.addTextChangedListener(watcher);
            phoneLayout.setVisibility(View.VISIBLE);
        } else {
            phoneLayout.setVisibility(View.GONE);
        }
        confirmPasswordText.addTextChangedListener(watcher);
    }

    void postDetails(){
        registerButton.setEnabled(false);
        progressDialog = new MaterialDialog.Builder(this)
                .title(R.string.loading)
                .content(R.string.please_wait)
                .widgetColorRes(R.color.primary)
                .progress(true, 0)
                .cancelable(false)
                .show();
        new SafeAsyncTask<Boolean>() {
            public Boolean call() throws Exception {
                registrationSuccessResponse = testpressService.register(usernameText.getText().toString(), emailText.getText().toString(), passwordText.getText().toString(), phoneText.getText().toString());
                return true;
            }

            @Override
            protected void onException(final Exception e) throws RuntimeException {
                registerButton.setEnabled(true);
                // Retrofit Errors are handled
                if((e instanceof RetrofitError)) {
                    RegistrationErrorDetails registrationErrorDetails = (RegistrationErrorDetails)((RetrofitError) e).getBodyAs(RegistrationErrorDetails.class);
                    if(!registrationErrorDetails.getUsername().isEmpty()) {
                        usernameText.setError(registrationErrorDetails.getUsername().get(0));
                        usernameText.requestFocus();
                    }
                    if(!registrationErrorDetails.getEmail().isEmpty()) {
                        emailText.setError(registrationErrorDetails.getEmail().get(0));
                        emailText.requestFocus();
                    }
                    if(!registrationErrorDetails.getPassword().isEmpty()) {
                        passwordText.setError(registrationErrorDetails.getPassword().get(0));
                        passwordText.requestFocus();
                    }
                    if(!registrationErrorDetails.getPhone().isEmpty()) {
                        phoneText.setError(registrationErrorDetails.getPhone().get(0));
                        phoneText.requestFocus();
                    }
                }
                progressDialog.dismiss();
            }
{'fraction_non_alphanumeric': 0.08759590792838874, 'fraction_numerical': 0.0012787723785166241, 'mean_word_length': 3.0684971098265894, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '19376', 'n_tokens_mistral': 3561, 'n_tokens_neox': 3485, 'n_words': 564}