<commit_before>package org.wikipedia.editing;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.webkit.WebView;
import android.widget.*;
import com.github.kevinsawicki.http.HttpRequest;
import com.squareup.picasso.Callback;
import com.squareup.picasso.Picasso;
import org.json.JSONException;
import org.json.JSONObject;
import org.mediawiki.api.json.Api;
import org.mediawiki.api.json.RequestBuilder;
import org.wikipedia.*;
import org.wikipedia.login.LoginTask;
import org.wikipedia.login.LogoutTask;
import org.wikipedia.login.User;
import org.wikipedia.page.Section;

public class EditSectionActivity extends ActionBarActivity {
    public static final String ACTION_EDIT_SECTION = "org.wikipedia.edit_section";
    public static final String EXTRA_TITLE = "org.wikipedia.edit_section.title";
    public static final String EXTRA_SECTION = "org.wikipedia.edit_section.section";

    private WikipediaApp app;

    private PageTitle title;
    private Section section;

    private String sectionWikitext;

    private EditText sectionText;
    private View sectionProgress;
    private View sectionContainer;
    private View sectionError;
    private Button sectionErrorRetry;
    private View captchaContainer;
    private View captchaProgress;
    private ImageView captchaImage;
    private EditText captchaText;

    private View abusefilterContainer;
    private WebView abusefilterWebView;
    private CommunicationBridge abusefilterBridge;
    private View abuseFilterBackAction;

    private CaptchaEditResult captchaEditResult;
    private AbuseFilterEditResult abusefilterEditResult;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_section);

        getActionBar().setDisplayHomeAsUpEnabled(true);

        if (!getIntent().getAction().equals(ACTION_EDIT_SECTION)) {
            throw new RuntimeException("Much wrong action. Such exception. Wow");
        }

        app = (WikipediaApp)getApplicationContext();

        title = getIntent().getParcelableExtra(EXTRA_TITLE);
        section = getIntent().getParcelableExtra(EXTRA_SECTION);

        progressDialog = new ProgressDialog(this);

        getActionBar().setTitle(getString(R.string.editsection_activity_title));

        sectionText = (EditText) findViewById(R.id.edit_section_text);
        sectionProgress = findViewById(R.id.edit_section_load_progress);
        sectionContainer = findViewById(R.id.edit_section_container);
        sectionError = findViewById(R.id.edit_section_error);
        sectionErrorRetry = (Button) findViewById(R.id.edit_section_error_retry);

        captchaContainer = findViewById(R.id.edit_section_captcha_container);
        captchaImage = (ImageView) findViewById(R.id.edit_section_captcha_image);
        captchaText = (EditText) findViewById(R.id.edit_section_captcha_text);
        captchaProgress = findViewById(R.id.edit_section_captcha_image_progress);

        abusefilterContainer = findViewById(R.id.edit_section_abusefilter_container);
        abusefilterWebView = (WebView) findViewById(R.id.edit_section_abusefilter_webview);
        abusefilterBridge = new CommunicationBridge(abusefilterWebView, "file:///android_asset/abusefilter.html");
        abuseFilterBackAction = findViewById(R.id.edit_section_abusefilter_back);

        if (savedInstanceState != null && savedInstanceState.containsKey("sectionWikitext")) {
            sectionWikitext = savedInstanceState.getString("sectionWikitext");
        }

        if (savedInstanceState != null && savedInstanceState.containsKey("captcha")) {
            captchaEditResult = savedInstanceState.getParcelable("captcha");
            handleCaptcha();
        }

        if (savedInstanceState != null && savedInstanceState.containsKey("abusefilter")) {
            abusefilterEditResult = savedInstanceState.getParcelable("abusefilter");
            handleAbuseFilter();
        }

        sectionErrorRetry.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Utils.crossFade(sectionError, sectionProgress);
                fetchSectionText();
            }
        });

        captchaImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new RefreshCaptchaTask(EditSectionActivity.this, title) {
                    @Override
                    public void onBeforeExecute() {
                        Utils.crossFade(captchaImage, captchaProgress);
                    }

                    @Override
                    public void onFinish(CaptchaEditResult result) {
                        captchaEditResult = result;
                        handleCaptcha(true);
                    }
                }.execute();

            }
        });

        abuseFilterBackAction.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cancelAbuseFilter();
            }
        });

        fetchSectionText();
    }

    private ProgressDialog progressDialog;
    private void doSave() {
        progressDialog.setIndeterminate(true);
        progressDialog.setCancelable(false);
        progressDialog.setMessage(getString(R.string.dialog_saving_in_progress));
        if (captchaEditResult != null) {
            // Show the wikitext in the background when captcha is being saved=
            Utils.crossFade(captchaContainer, sectionContainer);
        }
        app.getEditTokenStorage().get(title.getSite(), new EditTokenStorage.TokenRetreivedCallback() {
            @Override
            public void onTokenRetreived(final String token) {

                new DoEditTask(EditSectionActivity.this, title, sectionText.getText().toString(), section.getId(), token) {
                    @Override
                    public void onBeforeExecute() {
                        progressDialog.show();
                    }

                    @Override
                    public RequestBuilder buildRequest(Api api) {
                        RequestBuilder builder = super.buildRequest(api);
                        if (captchaEditResult != null) {
                            builder.param("captchaid", captchaEditResult.getCaptchaId())
                                    .param("captchaword", captchaText.getText().toString());
                        }
                        return builder;
                    }

                    @Override
                    public void onCatch(Throwable caught) {
                        if (caught instanceof EditingException) {
                            EditingException ee = (EditingException) caught;
                            if (app.getUserInfoStorage().isLoggedIn() && ee.getCode() == "badtoken") {
                                // looks like our session expired.
                                app.getEditTokenStorage().clearAllTokens();
                                app.getCookieManager().clearAllCookies();

                                User user = app.getUserInfoStorage().getUser();
                                new LoginTask(app, app.getPrimarySite(), user.getUsername(), user.getPassword()) {
                                    @Override
                                    public void onFinish(String result) {
                                        doSave();
                                    }
                                }.execute();
                                return;
                            }
                        }
                        if (!(caught instanceof HttpRequest.HttpRequestException)) {
                            throw new RuntimeException(caught);
                        }
                        Log.d("Wikipedia", caught.toString());
                        final AlertDialog retryDialog = new AlertDialog.Builder(EditSectionActivity.this)<commit_msg>Do not crash when user has changed password<commit_after>package org.wikipedia.editing;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.webkit.WebView;
import android.widget.*;
import com.github.kevinsawicki.http.HttpRequest;
import com.squareup.picasso.Callback;
import com.squareup.picasso.Picasso;
import org.json.JSONException;
import org.json.JSONObject;
import org.mediawiki.api.json.Api;
import org.mediawiki.api.json.RequestBuilder;
import org.wikipedia.*;
import org.wikipedia.login.LoginTask;
import org.wikipedia.login.LogoutTask;
import org.wikipedia.login.User;
import org.wikipedia.page.Section;

public class EditSectionActivity extends ActionBarActivity {
    public static final String ACTION_EDIT_SECTION = "org.wikipedia.edit_section";
    public static final String EXTRA_TITLE = "org.wikipedia.edit_section.title";
    public static final String EXTRA_SECTION = "org.wikipedia.edit_section.section";

    private WikipediaApp app;

    private PageTitle title;
    private Section section;

    private String sectionWikitext;

    private EditText sectionText;
    private View sectionProgress;
    private View sectionContainer;
    private View sectionError;
    private Button sectionErrorRetry;
    private View captchaContainer;
    private View captchaProgress;
    private ImageView captchaImage;
    private EditText captchaText;

    private View abusefilterContainer;
    private WebView abusefilterWebView;
    private CommunicationBridge abusefilterBridge;
    private View abuseFilterBackAction;

    private CaptchaEditResult captchaEditResult;
    private AbuseFilterEditResult abusefilterEditResult;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_section);

        getActionBar().setDisplayHomeAsUpEnabled(true);

        if (!getIntent().getAction().equals(ACTION_EDIT_SECTION)) {
            throw new RuntimeException("Much wrong action. Such exception. Wow");
        }

        app = (WikipediaApp)getApplicationContext();

        title = getIntent().getParcelableExtra(EXTRA_TITLE);
        section = getIntent().getParcelableExtra(EXTRA_SECTION);

        progressDialog = new ProgressDialog(this);

        getActionBar().setTitle(getString(R.string.editsection_activity_title));

        sectionText = (EditText) findViewById(R.id.edit_section_text);
        sectionProgress = findViewById(R.id.edit_section_load_progress);
        sectionContainer = findViewById(R.id.edit_section_container);
        sectionError = findViewById(R.id.edit_section_error);
        sectionErrorRetry = (Button) findViewById(R.id.edit_section_error_retry);

        captchaContainer = findViewById(R.id.edit_section_captcha_container);
        captchaImage = (ImageView) findViewById(R.id.edit_section_captcha_image);
        captchaText = (EditText) findViewById(R.id.edit_section_captcha_text);
        captchaProgress = findViewById(R.id.edit_section_captcha_image_progress);

        abusefilterContainer = findViewById(R.id.edit_section_abusefilter_container);
        abusefilterWebView = (WebView) findViewById(R.id.edit_section_abusefilter_webview);
        abusefilterBridge = new CommunicationBridge(abusefilterWebView, "file:///android_asset/abusefilter.html");
        abuseFilterBackAction = findViewById(R.id.edit_section_abusefilter_back);

        if (savedInstanceState != null && savedInstanceState.containsKey("sectionWikitext")) {
            sectionWikitext = savedInstanceState.getString("sectionWikitext");
        }

        if (savedInstanceState != null && savedInstanceState.containsKey("captcha")) {
            captchaEditResult = savedInstanceState.getParcelable("captcha");
            handleCaptcha();
        }

        if (savedInstanceState != null && savedInstanceState.containsKey("abusefilter")) {
            abusefilterEditResult = savedInstanceState.getParcelable("abusefilter");
            handleAbuseFilter();
        }

        sectionErrorRetry.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Utils.crossFade(sectionError, sectionProgress);
                fetchSectionText();
            }
        });

        captchaImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new RefreshCaptchaTask(EditSectionActivity.this, title) {
                    @Override
                    public void onBeforeExecute() {
                        Utils.crossFade(captchaImage, captchaProgress);
                    }

                    @Override
                    public void onFinish(CaptchaEditResult result) {
                        captchaEditResult = result;
                        handleCaptcha(true);
                    }
                }.execute();

            }
        });

        abuseFilterBackAction.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cancelAbuseFilter();
            }
        });

        fetchSectionText();
    }

    private ProgressDialog progressDialog;
    private void doSave() {
        progressDialog.setIndeterminate(true);
        progressDialog.setCancelable(false);
        progressDialog.setMessage(getString(R.string.dialog_saving_in_progress));
        if (captchaEditResult != null) {
            // Show the wikitext in the background when captcha is being saved=
            Utils.crossFade(captchaContainer, sectionContainer);
        }
        app.getEditTokenStorage().get(title.getSite(), new EditTokenStorage.TokenRetreivedCallback() {
            @Override
            public void onTokenRetreived(final String token) {

                new DoEditTask(EditSectionActivity.this, title, sectionText.getText().toString(), section.getId(), token) {
                    @Override
                    public void onBeforeExecute() {
                        progressDialog.show();
                    }

                    @Override
                    public RequestBuilder buildRequest(Api api) {
                        RequestBuilder builder = super.buildRequest(api);
                        if (captchaEditResult != null) {
                            builder.param("captchaid", captchaEditResult.getCaptchaId())
                                    .param("captchaword", captchaText.getText().toString());
                        }
                        return builder;
                    }

                    @Override
                    public void onCatch(Throwable caught) {
                        if (caught instanceof EditingException) {
                            EditingException ee = (EditingException) caught;
                            if (app.getUserInfoStorage().isLoggedIn() && ee.getCode() == "badtoken") {
                                // looks like our session expired.
                                app.getEditTokenStorage().clearAllTokens();
                                app.getCookieManager().clearAllCookies();

                                User user = app.getUserInfoStorage().getUser();
                                new LoginTask(app, app.getPrimarySite(), user.getUsername(), user.getPassword()) {
                                    @Override
                                    public void onFinish(String result) {
                                        if (result.equals("Success")) {
                                            doSave();
                                        } else {
                                            progressDialog.dismiss();
                                            Utils.crossFade(sectionText, sectionError);
                                            sectionError.setVisibility(View.VISIBLE);
                                        }
                                    }
                                }.execute();
                                return;
                            }
                        }
                        if (!(caught instanceof HttpRequest.HttpRequestException)) {
                            throw new RuntimeException(caught);
                        }
                        Log.d("Wikipedia", caught.toString());
                        final AlertDialog retryDialog = new AlertDialog.Builder(EditSectionActivity.this)
{'fraction_non_alphanumeric': 0.08192139737991266, 'fraction_numerical': 0.00011644832605531295, 'mean_word_length': 2.127458120903132, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 28, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5218709', 'n_tokens_mistral': 4212, 'n_tokens_neox': 4008, 'n_words': 765}