<commit_before>/**
 * Hubroid - A GitHub app for Android
 * 
 * Copyright (c) 2011 Idlesoft LLC.
 * 
 * Licensed under the New BSD License.
 */

package net.idlesoft.android.apps.github.activities;

import com.flurry.android.FlurryAgent;

import net.idlesoft.android.apps.github.R;

import org.idlesoft.libraries.ghapi.GitHubAPI;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.ImageButton;
import android.widget.TextView;

import java.io.File;

public class CommitChangeViewer extends Activity {
    private GitHubAPI mGapi = new GitHubAPI();

    public JSONObject mJson;

    private SharedPreferences.Editor mEditor;

    public Intent mIntent;

    private SharedPreferences mPrefs;

    public String mRepositoryName;

    public String mRepositoryOwner;

    private String mPassword;

    private String mUsername;

    @Override
    public void onCreate(final Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.commit_view);

        mPrefs = getSharedPreferences(Hubroid.PREFS_NAME, 0);
        mEditor = mPrefs.edit();

        mUsername = mPrefs.getString("username", "");
        mPassword = mPrefs.getString("password", "");

        mGapi.authenticate(mUsername, mPassword);

        ((ImageButton) findViewById(R.id.btn_search)).setOnClickListener(new OnClickListener() {
            public void onClick(final View v) {
                startActivity(new Intent(CommitChangeViewer.this, Search.class));
            }
        });

        final TextView title = (TextView) findViewById(R.id.tv_page_title);
        title.setText("Commit Diff");

        final Bundle extras = getIntent().getExtras();
        if (extras != null) {
            mRepositoryName = extras.getString("repo_name");
            mRepositoryOwner = extras.getString("repo_owner");
            try {
                mJson = new JSONObject(extras.getString("json"));

                /*
                 * This new method of displaying file diffs was inspired by iOctocat's approach.
                 * Thanks to <NAME> (dbloete on GitHub) for creating iOctocat and
                 * making me realize Android needed some GitHub love too. ;-)
                 */
                final WebView webView = (WebView) findViewById(R.id.wv_commitView_diff);

                /*
                 * Prepare CSS for diff:
                 * Added lines are green, removed lines are red, and the special lines that specify
                 * how many lines were affected in the chunk are a light blue.
                 */
                String content =
                        "<style type=\"text/css\">"
                        + "div {"
                        + "margin-right: 100%25;"
                        + "font-family: monospace;"
                        + "white-space: nowrap;"
                        + "display: inline-block;"
                        + "}"
                        + ".lines {"
                        + "background-color: #EAF2F5;"
                        + "}"
                        + ".added {"
                        + "background-color: #DDFFDD;"
                        + "}"
                        + ".removed {"
                        + "background-color: #FFDDDD;"
                        + "}"
                        + "</style>";

                String[] splitDiff = mJson.getString("diff").split("\n");
                for (int i = 0; i < splitDiff.length; i++) {
                    // Replace all tabs with four non-breaking spaces (most browsers truncate "\t+" to " ").
                    splitDiff[i] = splitDiff[i].replaceAll("\t", "&nbsp;&nbsp;&nbsp;&nbsp;");

                    // Replace any sequence of two or more spaces with &nbsps (most browsers truncate " +" to " ").<commit_msg>When formatting a diff, made sure to HTML encode any HTML special characters, otherwise files containing HTML elements would have them interpreted by the browser when, leading to broken and truncated diffs.<commit_after>/**
 * Hubroid - A GitHub app for Android
 * 
 * Copyright (c) 2011 Idlesoft LLC.
 * 
 * Licensed under the New BSD License.
 */

package net.idlesoft.android.apps.github.activities;

import com.flurry.android.FlurryAgent;

import net.idlesoft.android.apps.github.R;

import org.idlesoft.libraries.ghapi.GitHubAPI;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Environment;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.ImageButton;
import android.widget.TextView;

import java.io.File;

public class CommitChangeViewer extends Activity {
    private GitHubAPI mGapi = new GitHubAPI();

    public JSONObject mJson;

    private SharedPreferences.Editor mEditor;

    public Intent mIntent;

    private SharedPreferences mPrefs;

    public String mRepositoryName;

    public String mRepositoryOwner;

    private String mPassword;

    private String mUsername;

    @Override
    public void onCreate(final Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.commit_view);

        mPrefs = getSharedPreferences(Hubroid.PREFS_NAME, 0);
        mEditor = mPrefs.edit();

        mUsername = mPrefs.getString("username", "");
        mPassword = mPrefs.getString("password", "");

        mGapi.authenticate(mUsername, mPassword);

        ((ImageButton) findViewById(R.id.btn_search)).setOnClickListener(new OnClickListener() {
            public void onClick(final View v) {
                startActivity(new Intent(CommitChangeViewer.this, Search.class));
            }
        });

        final TextView title = (TextView) findViewById(R.id.tv_page_title);
        title.setText("Commit Diff");

        final Bundle extras = getIntent().getExtras();
        if (extras != null) {
            mRepositoryName = extras.getString("repo_name");
            mRepositoryOwner = extras.getString("repo_owner");
            try {
                mJson = new JSONObject(extras.getString("json"));

                /*
                 * This new method of displaying file diffs was inspired by iOctocat's approach.
                 * Thanks to <NAME> (dbloete on GitHub) for creating iOctocat and
                 * making me realize Android needed some GitHub love too. ;-)
                 */
                final WebView webView = (WebView) findViewById(R.id.wv_commitView_diff);

                /*
                 * Prepare CSS for diff:
                 * Added lines are green, removed lines are red, and the special lines that specify
                 * how many lines were affected in the chunk are a light blue.
                 */
                String content =
                        "<style type=\"text/css\">"
                        + "div {"
                        + "margin-right: 100%25;"
                        + "font-family: monospace;"
                        + "white-space: nowrap;"
                        + "display: inline-block;"
                        + "}"
                        + ".lines {"
                        + "background-color: #EAF2F5;"
                        + "}"
                        + ".added {"
                        + "background-color: #DDFFDD;"
                        + "}"
                        + ".removed {"
                        + "background-color: #FFDDDD;"
                        + "}"
                        + "</style>";

                String[] splitDiff = mJson.getString("diff").split("\n");
                for (int i = 0; i < splitDiff.length; i++) {
                    // HTML encode any elements, else any diff containing "<div>" or any HTML element will be interpreted as one by the browser
                    splitDiff[i] = TextUtils.htmlEncode(splitDiff[i]);

                    // Replace all tabs with four non-breaking spaces (most browsers truncate "\t+" to " ").
                    splitDiff[i] = splitDiff[i].replaceAll("\t", "&nbsp;&nbsp;&nbsp;&nbsp;");

                    // Replace any sequence of two or more spaces with &nbsps (most browsers truncate " +" to " ").
{'fraction_non_alphanumeric': 0.10263827817634807, 'fraction_numerical': 0.003008562832677621, 'mean_word_length': 1.90814266487214, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 10, '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': '3752363', 'n_tokens_mistral': 2310, 'n_tokens_neox': 2134, 'n_words': 596}