<commit_before>package de.danoeh.antennapod.activity.gpoddernet;

import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.ViewFlipper;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

import de.danoeh.antennapod.BuildConfig;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.activity.MainActivity;
import de.danoeh.antennapod.core.gpoddernet.GpodnetService;
import de.danoeh.antennapod.core.gpoddernet.GpodnetServiceException;
import de.danoeh.antennapod.core.gpoddernet.model.GpodnetDevice;
import de.danoeh.antennapod.core.preferences.GpodnetPreferences;
import de.danoeh.antennapod.core.preferences.UserPreferences;
import de.danoeh.antennapod.core.service.GpodnetSyncService;

/**
 * Guides the user through the authentication process
 * Step 1: Request username and password from user
 * Step 2: Choose device from a list of available devices or create a new one
 * Step 3: Choose from a list of actions
 */
public class GpodnetAuthenticationActivity extends ActionBarActivity {
    private static final String TAG = "GpodnetAuthActivity";

    private static final String CURRENT_STEP = "current_step";

    private ViewFlipper viewFlipper;

    private static final int STEP_DEFAULT = -1;
    private static final int STEP_LOGIN = 0;
    private static final int STEP_DEVICE = 1;
    private static final int STEP_FINISH = 2;

    private int currentStep = -1;

    private GpodnetService service;
    private volatile String username;
    private volatile String password;
    private volatile GpodnetDevice selectedDevice;

    View[] views;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setTheme(UserPreferences.getTheme());
        super.onCreate(savedInstanceState);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        setContentView(R.layout.gpodnetauth_activity);
        service = new GpodnetService();

        viewFlipper = (ViewFlipper) findViewById(R.id.viewflipper);
        LayoutInflater inflater = (LayoutInflater)
                getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        views = new View[]{
                inflater.inflate(R.layout.gpodnetauth_credentials, viewFlipper, false),
                inflater.inflate(R.layout.gpodnetauth_device, viewFlipper, false),
                inflater.inflate(R.layout.gpodnetauth_finish, viewFlipper, false)
        };
        for (View view : views) {
            viewFlipper.addView(view);
        }
        advance();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (service != null) {
            service.shutdown();
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
    }

    private void setupLoginView(View view) {
        final EditText username = (EditText) view.findViewById(R.id.etxtUsername);
        final EditText password = (EditText) view.findViewById(R.id.etxtPassword);
        final Button login = (Button) view.findViewById(R.id.butLogin);
        final TextView txtvError = (TextView) view.findViewById(R.id.txtvError);
        final ProgressBar progressBar = (ProgressBar) view.findViewById(R.id.progBarLogin);

        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                final String usernameStr = username.getText().toString();
                final String passwordStr = password.getText().toString();

                if (BuildConfig.DEBUG) Log.d(TAG, "Checking login credentials");
                AsyncTask<GpodnetService, Void, Void> authTask = new AsyncTask<GpodnetService, Void, Void>() {

                    volatile Exception exception;

                    @Override
                    protected void onPreExecute() {
                        super.onPreExecute();
                        login.setEnabled(false);
                        progressBar.setVisibility(View.VISIBLE);
                        txtvError.setVisibility(View.GONE);

                    }

                    @Override
                    protected void onPostExecute(Void aVoid) {
                        super.onPostExecute(aVoid);
                        login.setEnabled(true);
                        progressBar.setVisibility(View.GONE);

                        if (exception == null) {
                            advance();
                        } else {
                            txtvError.setText(exception.getMessage());
                            txtvError.setVisibility(View.VISIBLE);
                        }
                    }

                    @Override
                    protected Void doInBackground(GpodnetService... params) {
                        try {
                            params[0].authenticate(usernameStr, passwordStr);
                            GpodnetAuthenticationActivity.this.username = usernameStr;
                            GpodnetAuthenticationActivity.this.password = <PASSWORD>;
                        } catch (GpodnetServiceException e) {
                            e.printStackTrace();
                            exception = e;
                        }
                        return null;
                    }
                };<commit_msg>Display error more nicely<commit_after>package de.danoeh.antennapod.activity.gpoddernet;

import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.ViewFlipper;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

import de.danoeh.antennapod.BuildConfig;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.activity.MainActivity;
import de.danoeh.antennapod.core.gpoddernet.GpodnetService;
import de.danoeh.antennapod.core.gpoddernet.GpodnetServiceException;
import de.danoeh.antennapod.core.gpoddernet.model.GpodnetDevice;
import de.danoeh.antennapod.core.preferences.GpodnetPreferences;
import de.danoeh.antennapod.core.preferences.UserPreferences;
import de.danoeh.antennapod.core.service.GpodnetSyncService;

/**
 * Guides the user through the authentication process
 * Step 1: Request username and password from user
 * Step 2: Choose device from a list of available devices or create a new one
 * Step 3: Choose from a list of actions
 */
public class GpodnetAuthenticationActivity extends ActionBarActivity {
    private static final String TAG = "GpodnetAuthActivity";

    private static final String CURRENT_STEP = "current_step";

    private ViewFlipper viewFlipper;

    private static final int STEP_DEFAULT = -1;
    private static final int STEP_LOGIN = 0;
    private static final int STEP_DEVICE = 1;
    private static final int STEP_FINISH = 2;

    private int currentStep = -1;

    private GpodnetService service;
    private volatile String username;
    private volatile String password;
    private volatile GpodnetDevice selectedDevice;

    View[] views;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setTheme(UserPreferences.getTheme());
        super.onCreate(savedInstanceState);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        setContentView(R.layout.gpodnetauth_activity);
        service = new GpodnetService();

        viewFlipper = (ViewFlipper) findViewById(R.id.viewflipper);
        LayoutInflater inflater = (LayoutInflater)
                getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        views = new View[]{
                inflater.inflate(R.layout.gpodnetauth_credentials, viewFlipper, false),
                inflater.inflate(R.layout.gpodnetauth_device, viewFlipper, false),
                inflater.inflate(R.layout.gpodnetauth_finish, viewFlipper, false)
        };
        for (View view : views) {
            viewFlipper.addView(view);
        }
        advance();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (service != null) {
            service.shutdown();
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
    }

    private void setupLoginView(View view) {
        final EditText username = (EditText) view.findViewById(R.id.etxtUsername);
        final EditText password = (EditText) view.findViewById(R.id.etxtPassword);
        final Button login = (Button) view.findViewById(R.id.butLogin);
        final TextView txtvError = (TextView) view.findViewById(R.id.txtvError);
        final ProgressBar progressBar = (ProgressBar) view.findViewById(R.id.progBarLogin);

        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                final String usernameStr = username.getText().toString();
                final String passwordStr = password.getText().toString();

                if (BuildConfig.DEBUG) Log.d(TAG, "Checking login credentials");
                AsyncTask<GpodnetService, Void, Void> authTask = new AsyncTask<GpodnetService, Void, Void>() {

                    volatile Exception exception;

                    @Override
                    protected void onPreExecute() {
                        super.onPreExecute();
                        login.setEnabled(false);
                        progressBar.setVisibility(View.VISIBLE);
                        txtvError.setVisibility(View.GONE);

                    }

                    @Override
                    protected void onPostExecute(Void aVoid) {
                        super.onPostExecute(aVoid);
                        login.setEnabled(true);
                        progressBar.setVisibility(View.GONE);

                        if (exception == null) {
                            advance();
                        } else {
                            txtvError.setText(exception.getCause().getMessage());
                            txtvError.setVisibility(View.VISIBLE);
                        }
                    }

                    @Override
                    protected Void doInBackground(GpodnetService... params) {
                        try {
                            params[0].authenticate(usernameStr, passwordStr);
                            GpodnetAuthenticationActivity.this.username = usernameStr;
                            GpodnetAuthenticationActivity.this.password = <PASSWORD>;
                        } catch (GpodnetServiceException e) {
                            e.printStackTrace();
                            exception = e;
                        }
                        return null;
                    }
                };
{'fraction_non_alphanumeric': 0.08280879587999673, 'fraction_numerical': 0.0016349219324777243, 'mean_word_length': 2.2271168557108942, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '2502573', 'n_tokens_mistral': 3239, 'n_tokens_neox': 3098, 'n_words': 675}