<commit_before>package fr.free.nrw.commons.auth;

import android.accounts.Account;
import android.accounts.AccountAuthenticatorActivity;
import android.accounts.AccountAuthenticatorResponse;
import android.accounts.AccountManager;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.ContentResolver;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v4.app.NavUtils;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.io.IOException;

import fr.free.nrw.commons.CommonsApplication;
import fr.free.nrw.commons.EventLog;
import fr.free.nrw.commons.R;
import fr.free.nrw.commons.WelcomeActivity;
import fr.free.nrw.commons.contributions.ContributionsContentProvider;
import fr.free.nrw.commons.modifications.ModificationsContentProvider;


public class LoginActivity extends AccountAuthenticatorActivity {

    public static final String PARAM_USERNAME = "fr.free.nrw.commons.login.username";

    private CommonsApplication app;

    Button loginButton;
    Button signupButton;
    EditText usernameEdit;
    EditText passwordEdit;

    private class LoginTask extends AsyncTask<String, String, String> {

        Activity context;
        ProgressDialog dialog;
        String username;
        String password;

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            Log.d("Commons", "Login done!");

            EventLog.schema(CommonsApplication.EVENT_LOGIN_ATTEMPT)
                    .param("username", username)
                    .param("result", result)
                    .log();

            if (result.equals("Success")) {
                dialog.dismiss();
                Toast successToast = Toast.makeText(context, R.string.login_success, Toast.LENGTH_SHORT);
                successToast.show();
                Account account = new Account(username, WikiAccountAuthenticator.COMMONS_ACCOUNT_TYPE);
                boolean accountCreated = AccountManager.get(context).addAccountExplicitly(account, password, null);

                Bundle extras = context.getIntent().getExtras();
                if (extras != null) {
                    if (accountCreated) { // Pass the new account back to the account manager
                        AccountAuthenticatorResponse response = extras.getParcelable(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE);
                        Bundle authResult = new Bundle();
                        authResult.putString(AccountManager.KEY_ACCOUNT_NAME, username);
                        authResult.putString(AccountManager.KEY_ACCOUNT_TYPE, WikiAccountAuthenticator.COMMONS_ACCOUNT_TYPE);
                        response.onResult(authResult);
                    }
                }
                // FIXME: If the user turns it off, it shouldn't be auto turned back on
                ContentResolver.setSyncAutomatically(account, ContributionsContentProvider.AUTHORITY, true); // Enable sync by default!
                ContentResolver.setSyncAutomatically(account, ModificationsContentProvider.AUTHORITY, true); // Enable sync by default!
                context.finish();
            } else {
                int response;
                if(result.equals("NetworkFailure")) {
                    response = R.string.login_failed_network;
                } else if(result.equals("NotExists") || result.equals("Illegal") || result.equals("NotExists")) {
                    response = R.string.login_failed_username;
                    passwordEdit.setText("");
                } else if(result.equals("EmptyPass") || result.equals("WrongPass") || result.equals("WrongPluginPass")) {
                    response = R.string.login_failed_password;
                    passwordEdit.setText("");
                } else if(result.equals("Throttled")) {
                    response = R.string.login_failed_throttled;
                } else if(result.equals("Blocked")) {
                    response = R.string.login_failed_blocked;
                } else {
                    // Should never really happen
                    Log.d("Commons", "Login failed with reason: " + result);
                    response = R.string.login_failed_generic;
                }
                Toast.makeText(getApplicationContext(), response, Toast.LENGTH_LONG).show();
                dialog.cancel();
            }

        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            dialog = new ProgressDialog(context);
            dialog.setIndeterminate(true);
            dialog.setTitle(getString(R.string.logging_in_title));
            dialog.setMessage(getString(R.string.logging_in_message));
            dialog.setCanceledOnTouchOutside(false);
            dialog.show();
        }

        LoginTask(Activity context) {
            this.context = context;
        }

        @Override
        protected String doInBackground(String... params) {
            username = params[0];
            password = params[1];
            try {
                return app.getApi().login(username, password);
            } catch (IOException e) {
                // Do something better!
                return "NetworkFailure";
            }
        }

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        app = (CommonsApplication) this.getApplicationContext();
        setContentView(R.layout.activity_login);
        loginButton = (Button) findViewById(R.id.loginButton);
        signupButton = (Button) findViewById(R.id.signupButton);
        usernameEdit = (EditText) findViewById(R.id.loginUsername);
        passwordEdit = (EditText) findViewById(R.id.loginPassword);
        final LoginActivity that = this;

        TextWatcher loginEnabler = new TextWatcher() {
            public void beforeTextChanged(CharSequence charSequence, int start, int count, int after) { }

            public void onTextChanged(CharSequence charSequence, int start, int count, int after) { }

            public void afterTextChanged(Editable editable) {
                if(usernameEdit.getText().length() != 0 && passwordEdit.getText().length() != 0) {
                    loginButton.setEnabled(true);
                } else {
                    loginButton.setEnabled(false);
                }
            }
        };

        usernameEdit.addTextChangedListener(loginEnabler);
        passwordEdit.addTextChangedListener(loginEnabler);
        passwordEdit.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
                if (loginButton.isEnabled()) {
                    if (actionId == EditorInfo.IME_ACTION_DONE) {
                        performLogin();
                        return true;
                    } else if ((keyEvent != null) && keyEvent.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
                        performLogin();
                        return true;
                    }
                }
                return false;
            }
        });

        loginButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                that.performLogin();
            }
        });

        if (savedInstanceState == null) {
            Bundle extras = getIntent().getExtras();
            // Only load welcome screen if we weren't redirected from SignupActivity
            if (extras == null || extras.getBoolean("Redirected") != true ) {
                Log.d("SignupActivity", "Redirected? " + Boolean.toString(extras.getBoolean("Redirected")));
                Intent welcomeIntent = new Intent(this, WelcomeActivity.class);
                startActivity(welcomeIntent);
            }
        }
    }

    private void performLogin() {
        String username = usernameEdit.getText().toString();
        // Because Mediawiki is upercase-first-char-then-case-sensitive :)
        String canonicalUsername = username.substring(0,1).toUpperCase() + username.substring(1);

        String password = passwordEdit.getText().toString();<commit_msg>Add null check and simplify if statement<commit_after>package fr.free.nrw.commons.auth;

import android.accounts.Account;
import android.accounts.AccountAuthenticatorActivity;
import android.accounts.AccountAuthenticatorResponse;
import android.accounts.AccountManager;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.ContentResolver;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v4.app.NavUtils;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.io.IOException;

import fr.free.nrw.commons.CommonsApplication;
import fr.free.nrw.commons.EventLog;
import fr.free.nrw.commons.R;
import fr.free.nrw.commons.WelcomeActivity;
import fr.free.nrw.commons.contributions.ContributionsContentProvider;
import fr.free.nrw.commons.modifications.ModificationsContentProvider;


public class LoginActivity extends AccountAuthenticatorActivity {

    public static final String PARAM_USERNAME = "fr.free.nrw.commons.login.username";

    private CommonsApplication app;

    Button loginButton;
    Button signupButton;
    EditText usernameEdit;
    EditText passwordEdit;

    private class LoginTask extends AsyncTask<String, String, String> {

        Activity context;
        ProgressDialog dialog;
        String username;
        String password;

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            Log.d("Commons", "Login done!");

            EventLog.schema(CommonsApplication.EVENT_LOGIN_ATTEMPT)
                    .param("username", username)
                    .param("result", result)
                    .log();

            if (result.equals("Success")) {
                dialog.dismiss();
                Toast successToast = Toast.makeText(context, R.string.login_success, Toast.LENGTH_SHORT);
                successToast.show();
                Account account = new Account(username, WikiAccountAuthenticator.COMMONS_ACCOUNT_TYPE);
                boolean accountCreated = AccountManager.get(context).addAccountExplicitly(account, password, null);

                Bundle extras = context.getIntent().getExtras();
                if (extras != null) {
                    if (accountCreated) { // Pass the new account back to the account manager
                        AccountAuthenticatorResponse response = extras.getParcelable(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE);
                        Bundle authResult = new Bundle();
                        authResult.putString(AccountManager.KEY_ACCOUNT_NAME, username);
                        authResult.putString(AccountManager.KEY_ACCOUNT_TYPE, WikiAccountAuthenticator.COMMONS_ACCOUNT_TYPE);
                        response.onResult(authResult);
                    }
                }
                // FIXME: If the user turns it off, it shouldn't be auto turned back on
                ContentResolver.setSyncAutomatically(account, ContributionsContentProvider.AUTHORITY, true); // Enable sync by default!
                ContentResolver.setSyncAutomatically(account, ModificationsContentProvider.AUTHORITY, true); // Enable sync by default!
                context.finish();
            } else {
                int response;
                if(result.equals("NetworkFailure")) {
                    response = R.string.login_failed_network;
                } else if(result.equals("NotExists") || result.equals("Illegal") || result.equals("NotExists")) {
                    response = R.string.login_failed_username;
                    passwordEdit.setText("");
                } else if(result.equals("EmptyPass") || result.equals("WrongPass") || result.equals("WrongPluginPass")) {
                    response = R.string.login_failed_password;
                    passwordEdit.setText("");
                } else if(result.equals("Throttled")) {
                    response = R.string.login_failed_throttled;
                } else if(result.equals("Blocked")) {
                    response = R.string.login_failed_blocked;
                } else {
                    // Should never really happen
                    Log.d("Commons", "Login failed with reason: " + result);
                    response = R.string.login_failed_generic;
                }
                Toast.makeText(getApplicationContext(), response, Toast.LENGTH_LONG).show();
                dialog.cancel();
            }

        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            dialog = new ProgressDialog(context);
            dialog.setIndeterminate(true);
            dialog.setTitle(getString(R.string.logging_in_title));
            dialog.setMessage(getString(R.string.logging_in_message));
            dialog.setCanceledOnTouchOutside(false);
            dialog.show();
        }

        LoginTask(Activity context) {
            this.context = context;
        }

        @Override
        protected String doInBackground(String... params) {
            username = params[0];
            password = params[1];
            try {
                return app.getApi().login(username, password);
            } catch (IOException e) {
                // Do something better!
                return "NetworkFailure";
            }
        }

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        app = (CommonsApplication) this.getApplicationContext();
        setContentView(R.layout.activity_login);
        loginButton = (Button) findViewById(R.id.loginButton);
        signupButton = (Button) findViewById(R.id.signupButton);
        usernameEdit = (EditText) findViewById(R.id.loginUsername);
        passwordEdit = (EditText) findViewById(R.id.loginPassword);
        final LoginActivity that = this;

        TextWatcher loginEnabler = new TextWatcher() {
            public void beforeTextChanged(CharSequence charSequence, int start, int count, int after) { }

            public void onTextChanged(CharSequence charSequence, int start, int count, int after) { }

            public void afterTextChanged(Editable editable) {
                if(usernameEdit.getText().length() != 0 && passwordEdit.getText().length() != 0) {
                    loginButton.setEnabled(true);
                } else {
                    loginButton.setEnabled(false);
                }
            }
        };

        usernameEdit.addTextChangedListener(loginEnabler);
        passwordEdit.addTextChangedListener(loginEnabler);
        passwordEdit.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
                if (loginButton.isEnabled()) {
                    if (actionId == EditorInfo.IME_ACTION_DONE) {
                        performLogin();
                        return true;
                    } else if ((keyEvent != null) && keyEvent.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
                        performLogin();
                        return true;
                    }
                }
                return false;
            }
        });

        loginButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                that.performLogin();
            }
        });

        if (savedInstanceState == null) {
            Bundle extras = getIntent().getExtras();
            // Only load welcome screen if we weren't redirected from SignupActivity
            if (extras == null || !extras.getBoolean("Redirected")) {
                if (extras != null) {
                    Log.d("SignupActivity", "Redirected? " + Boolean.toString(extras.getBoolean("Redirected")));
                }
                Intent welcomeIntent = new Intent(this, WelcomeActivity.class);
                startActivity(welcomeIntent);
            }
        }
    }

    private void performLogin() {
        String username = usernameEdit.getText().toString();
        // Because Mediawiki is upercase-first-char-then-case-sensitive :)
        String canonicalUsername = username.substring(0,1).toUpperCase() + username.substring(1);

        String password = passwordEdit.getText().toString();
{'fraction_non_alphanumeric': 0.09501064625654601, 'fraction_numerical': 0.0009207573228980837, 'mean_word_length': 2.188623853211009, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3107485', 'n_tokens_mistral': 4393, 'n_tokens_neox': 4154, 'n_words': 855}