<commit_before>package org.opendroidphp.app;


import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockActivity;

import org.opendroidphp.app.common.utils.ShellOutputFormatter;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;


public class SQLShellActivity extends SherlockActivity {

    private Button runCommand;
    private TextView shellResult;

    private static Process proc;
    private static InputStream stdout;
    private static OutputStream stdin;

    //private static Shell.Interactive sqlSession;

    private Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sqlshell);
        handler = new Handler();
        runCommand = (Button) findViewById(R.id.run_cmd);
        shellResult = (TextView) findViewById(R.id.shell_result);

        if (proc == null) {
            initialize();
        }

        runCommand.setOnClickListener(executeQuery);
        (new SyncSQLResultTask()).execute();
    }

    protected void initialize() {

        if (proc != null) {
            return;
        }

        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);

        String username = preferences.getString("mysql_username", "root");
        String password = preferences.getString("mysql_password", "");

        String[] baseShell = new String[]{
                Constants.MYSQL_MONITOR_SBIN_LOCATION, "-h",
                "127.0.0.1", "-T", "-f", "-r", "-t", "-E", "--disable-pager",
                "-n", "--user=" + username, "--password=" + password,
                "--default-character-set=utf8", "-L"};

        try {

            proc = new ProcessBuilder(baseShell).
                    redirectErrorStream(true).
                    start();

            stdin = proc.getOutputStream();
            stdout = proc.getInputStream();

        } catch (IOException e) {
            e.printStackTrace();

        }

        /*sqlSession = new Shell.Builder().
                useSH().
                //setShell(baseShell).
                setWantSTDERR(true).
                setWatchdogTimeout(5).
                setMinimalLogging(true).
                open(new Shell.OnCommandResultListener() {


                    @Override
                    public void onCommandResult(int commandCode, final int exitCode, final List<String> output) {

                        if (exitCode != Shell.OnCommandResultListener.SHELL_RUNNING) {
                            handler.post(new Runnable() {
                                @Override
                                public void run() {

                                    if (output != null) {
                                        for (String error : output) {
                                            shellResult.append(error);
                                        }
                                    }
                                }
                            });

                        } else {

                        }

                    }
                });*/
    }

    private class SyncSQLResultTask extends AsyncTask<Void, String, Void> {

        @Override
        protected Void doInBackground(Void... params) {
            BufferedReader buff = new BufferedReader(
                    new InputStreamReader(stdout));

            try {
                while (true) {

                    String READ = ShellOutputFormatter.toHTML(buff.readLine());
                    if (READ == null) {
                        break;
                    }
                    publishProgress(READ);

                }
                buff.close();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onProgressUpdate(String... values) {
            super.onProgressUpdate(values);
            shellResult.append(ShellOutputFormatter.toHTML(values[0]));
        }
    }


    private final View.OnClickListener executeQuery = new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            try {

                String command = ((EditText) findViewById(R.id.command))
                        .getText()
                        .toString();

                try {
                    stdin.write((command + "\r\n").getBytes());
                    stdin.flush();
                } catch (Exception e) {

                }

              /*  sqlSession.addCommand(command, new Shell.OnCommandResultListener() {
                    @Override
                    public void onCommandResult(int commandCode, int exitCode, final List<String> output) {
                        handler.post(new Runnable() {
                            @Override
                            public void run() {

                                if (output != null) {
                                    for (String error : output) {
                                        shellResult.append(error);
                                    }
                                }
                            }
                        });
                    }
                });*/
            } catch (NullPointerException e) {

            }


        }
    };
}<commit_msg>fixed "activity" from being crash when no mysql daemon is running<commit_after>package org.opendroidphp.app;


import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockActivity;

import org.opendroidphp.R;
import org.opendroidphp.app.common.utils.ShellOutputFormatter;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;


public class SQLShellActivity extends SherlockActivity {

    private static Process proc;
    private static InputStream stdout;
    private static OutputStream stdin;
    private final View.OnClickListener executeQuery = new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            try {

                String command = ((EditText) findViewById(R.id.command))
                        .getText()
                        .toString();

                try {
                    stdin.write((command + "\r\n").getBytes());
                    stdin.flush();
                } catch (Exception e) {

                }

              /*  sqlSession.addCommand(command, new Shell.OnCommandResultListener() {
                    @Override
                    public void onCommandResult(int commandCode, int exitCode, final List<String> output) {
                        handler.post(new Runnable() {
                            @Override
                            public void run() {

                                if (output != null) {
                                    for (String error : output) {
                                        shellResult.append(error);
                                    }
                                }
                            }
                        });
                    }
                });*/
            } catch (NullPointerException e) {

            }


        }
    };
    private Button runCommand;

    //private static Shell.Interactive sqlSession;
    private TextView shellResult;
    private Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sqlshell);
        handler = new Handler();
        runCommand = (Button) findViewById(R.id.run_cmd);
        shellResult = (TextView) findViewById(R.id.shell_result);

        if (proc == null) {
            initialize();
        }

        runCommand.setOnClickListener(executeQuery);
        (new SyncSQLResultTask()).execute();
    }

    protected void initialize() {

        if (proc != null) {
            return;
        }

        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);

        String username = preferences.getString("mysql_username", "root");
        String password = preferences.getString("mysql_password", "");

        String[] baseShell = new String[]{
                Constants.MYSQL_MONITOR_SBIN_LOCATION, "-h",
                "127.0.0.1", "-T", "-f", "-r", "-t", "-E", "--disable-pager",
                "-n", "--user=" + username, "--password=" + password,
                "--default-character-set=utf8", "-L"};

        try {

            proc = new ProcessBuilder(baseShell).
                    redirectErrorStream(true).
                    start();

            stdin = proc.getOutputStream();
            stdout = proc.getInputStream();

        } catch (IOException e) {
            e.printStackTrace();

        }

        /*sqlSession = new Shell.Builder().
                useSH().
                //setShell(baseShell).
                setWantSTDERR(true).
                setWatchdogTimeout(5).
                setMinimalLogging(true).
                open(new Shell.OnCommandResultListener() {


                    @Override
                    public void onCommandResult(int commandCode, final int exitCode, final List<String> output) {

                        if (exitCode != Shell.OnCommandResultListener.SHELL_RUNNING) {
                            handler.post(new Runnable() {
                                @Override
                                public void run() {

                                    if (output != null) {
                                        for (String error : output) {
                                            shellResult.append(error);
                                        }
                                    }
                                }
                            });

                        } else {

                        }

                    }
                });*/
    }

    private class SyncSQLResultTask extends AsyncTask<Void, String, Void> {

        @Override
        protected Void doInBackground(Void... params) {

            if (stdout == null) {
                return null;
            }

            BufferedReader buff = new BufferedReader(
                    new InputStreamReader(stdout));

            try {
                while (true) {

                    String READ = ShellOutputFormatter.toHTML(buff.readLine());
                    if (READ == null) {
                        break;
                    }
                    publishProgress(READ);

                }
                buff.close();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onProgressUpdate(String... values) {
            super.onProgressUpdate(values);
            shellResult.append(ShellOutputFormatter.toHTML(values[0]));
        }
    }
}
{'fraction_non_alphanumeric': 0.09351047521716914, 'fraction_numerical': 0.0015329586101175269, 'mean_word_length': 1.3230464886251236, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 22, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6880325', 'n_tokens_mistral': 2799, 'n_tokens_neox': 2568, 'n_words': 575}