<commit_before>package fuzion24.device.vulnerability.test.ui;

import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.nowsecure.android.vts.BuildConfig;
import com.nowsecure.android.vts.R;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import fuzion24.device.vulnerability.test.ResultsCallback;
import fuzion24.device.vulnerability.test.ShareViaGoogleForm;
import fuzion24.device.vulnerability.test.VulnerabilityTestResult;
import fuzion24.device.vulnerability.test.VulnerabilityTestRunner;
import fuzion24.device.vulnerability.test.adapter.RecyclerAdapter;
import fuzion24.device.vulnerability.util.DeviceInfo;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "VULN_TEST";

    private DeviceInfo devInfo;
    private List<VulnerabilityTestResult> testResults;
    private RecyclerView recyclerView;
    private TextView emptyView;
    private CoordinatorLayout coordinatorLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitleTextColor(Color.WHITE);
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle(R.string.app_name);

        coordinatorLayout = (CoordinatorLayout) findViewById(R.id.coordinatorLayout);
        emptyView = (TextView) findViewById(R.id.emptyView);
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);

        final TextView tvKernelVersion = (TextView) findViewById(R.id.kernelVersion);
        final TextView tvBuildFingerPrint = (TextView) findViewById(R.id.buildFingerPrint);
        final TextView tvBuildID = (TextView) findViewById(R.id.buildID);
        final TextView tvBuildBrand = (TextView) findViewById(R.id.buildBrand);
        final TextView tvBuildManuf = (TextView) findViewById(R.id.buildManufacturer);
        final TextView tvBuildModel = (TextView) findViewById(R.id.buildModel);
        final TextView tvBuildRelease = (TextView) findViewById(R.id.buildRelease);
        final TextView tvBuildSDK = (TextView) findViewById(R.id.buildSDK);

        final TextView tvBuildCPUABI = (TextView) findViewById(R.id.buildCPUABI);
        final TextView tvBuildCPUABI2 = (TextView) findViewById(R.id.buildCPUABI2);
        final TextView tvBuildABIList = (TextView) findViewById(R.id.buildABIList);


        devInfo = DeviceInfo.getDeviceInfo();
        tvBuildFingerPrint.setText(devInfo.getBuildFingerPrint());
        tvBuildID.setText(devInfo.getBuildID());
        tvKernelVersion.setText(devInfo.getKernelVersion());
        tvBuildBrand.setText(devInfo.getBuildBrand());
        tvBuildManuf.setText(devInfo.getBuildManufacturer());
        tvBuildModel.setText(devInfo.getBuildModel());
        tvBuildRelease.setText(devInfo.getBuildRelease());
        tvBuildSDK.setText(devInfo.getBuildSDK());
        tvBuildCPUABI.setText(devInfo.getBuildCpuABI());
        tvBuildCPUABI2.setText(devInfo.getBuildCpuABI2());

        StringBuilder sb = new StringBuilder();
        for(String s : devInfo.getSupportedABIS()){
            sb.append(s);
            sb.append(" ");
        }

        tvBuildABIList.setText(sb.toString());

        FloatingActionButton fabStart = (FloatingActionButton) findViewById(R.id.fabStart);

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main_menu, menu);

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int itemId = item.getItemId();

        switch (itemId) {
            case R.id.menu_export_results:
            case R.id.menu_share_results:
                if (testResults == null) {
                    Snackbar.make(coordinatorLayout, R.string.run_tests, Snackbar.LENGTH_LONG).setAction(R.string.start, new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            runTestsSuit();
                        }

                    }).show();
                    
                    return true;
                }

                Intent intent = null;
                try {
                    JSONObject json = serializeResults(testResults, devInfo);
                    if (itemId == R.id.menu_export_results) {
                        intent = new Intent(Intent.ACTION_SEND);
                        intent.setType("text/plain");
                        intent.putExtra(Intent.EXTRA_SUBJECT, "Android VTS Results");
                        intent.putExtra(Intent.EXTRA_TEXT, json.toString(4));
                    } else if(itemId == R.id.menu_share_results) {
                        Uri formUri = new ShareViaGoogleForm(json).buildUri();
                        intent = new Intent(Intent.ACTION_VIEW, formUri);
                    }
                } catch (JSONException e) {
                    Log.d(TAG, "Json exception: " + e.getMessage());
                    Snackbar.make(coordinatorLayout, R.string.error_exporting_results, Snackbar.LENGTH_LONG).show();
                }

                if (intent != null) {
                    startActivity(Intent.createChooser(intent, getString(R.string.share_results_via)));
                }

                return true;

            default:
                return super.onOptionsItemSelected(item);
        }
    }

    private JSONObject serializeResults(List<VulnerabilityTestResult> results, DeviceInfo devInfo) throws JSONException {
        // not sure if this is too intense to do on the main thread...
        JSONArray testResults = new JSONArray();
        JSONObject buildInfo = new JSONObject();
        JSONObject combinedResults = new JSONObject();

        buildInfo.put("fingerprint", devInfo.getBuildFingerPrint());
        buildInfo.put("kernelVersion", devInfo.getKernelVersion());
        buildInfo.put("brand", devInfo.getBuildBrand());
        buildInfo.put("manufacturer", devInfo.getBuildManufacturer());
        buildInfo.put("model", devInfo.getBuildModel());
        buildInfo.put("release", devInfo.getBuildRelease());
        buildInfo.put("sdk", devInfo.getBuildSDK());
        buildInfo.put("id", devInfo.getBuildID());
        buildInfo.put("versionCode", BuildConfig.VERSION_CODE);
        buildInfo.put("versionName", BuildConfig.VERSION_NAME);

        for (VulnerabilityTestResult s : results) {
            JSONObject res = new JSONObject();<commit_msg>Add abi info to the shared json structure<commit_after>package fuzion24.device.vulnerability.test.ui;

import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.nowsecure.android.vts.BuildConfig;
import com.nowsecure.android.vts.R;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import fuzion24.device.vulnerability.test.ResultsCallback;
import fuzion24.device.vulnerability.test.ShareViaGoogleForm;
import fuzion24.device.vulnerability.test.VulnerabilityTestResult;
import fuzion24.device.vulnerability.test.VulnerabilityTestRunner;
import fuzion24.device.vulnerability.test.adapter.RecyclerAdapter;
import fuzion24.device.vulnerability.util.DeviceInfo;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "VULN_TEST";

    private DeviceInfo devInfo;
    private List<VulnerabilityTestResult> testResults;
    private RecyclerView recyclerView;
    private TextView emptyView;
    private CoordinatorLayout coordinatorLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitleTextColor(Color.WHITE);
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle(R.string.app_name);

        coordinatorLayout = (CoordinatorLayout) findViewById(R.id.coordinatorLayout);
        emptyView = (TextView) findViewById(R.id.emptyView);
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);

        final TextView tvKernelVersion = (TextView) findViewById(R.id.kernelVersion);
        final TextView tvBuildFingerPrint = (TextView) findViewById(R.id.buildFingerPrint);
        final TextView tvBuildID = (TextView) findViewById(R.id.buildID);
        final TextView tvBuildBrand = (TextView) findViewById(R.id.buildBrand);
        final TextView tvBuildManuf = (TextView) findViewById(R.id.buildManufacturer);
        final TextView tvBuildModel = (TextView) findViewById(R.id.buildModel);
        final TextView tvBuildRelease = (TextView) findViewById(R.id.buildRelease);
        final TextView tvBuildSDK = (TextView) findViewById(R.id.buildSDK);

        final TextView tvBuildCPUABI = (TextView) findViewById(R.id.buildCPUABI);
        final TextView tvBuildCPUABI2 = (TextView) findViewById(R.id.buildCPUABI2);
        final TextView tvBuildABIList = (TextView) findViewById(R.id.buildABIList);


        devInfo = DeviceInfo.getDeviceInfo();
        tvBuildFingerPrint.setText(devInfo.getBuildFingerPrint());
        tvBuildID.setText(devInfo.getBuildID());
        tvKernelVersion.setText(devInfo.getKernelVersion());
        tvBuildBrand.setText(devInfo.getBuildBrand());
        tvBuildManuf.setText(devInfo.getBuildManufacturer());
        tvBuildModel.setText(devInfo.getBuildModel());
        tvBuildRelease.setText(devInfo.getBuildRelease());
        tvBuildSDK.setText(devInfo.getBuildSDK());
        tvBuildCPUABI.setText(devInfo.getBuildCpuABI());
        tvBuildCPUABI2.setText(devInfo.getBuildCpuABI2());

        StringBuilder sb = new StringBuilder();
        for(String s : devInfo.getSupportedABIS()){
            sb.append(s);
            sb.append(" ");
        }

        tvBuildABIList.setText(sb.toString());

        FloatingActionButton fabStart = (FloatingActionButton) findViewById(R.id.fabStart);

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main_menu, menu);

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int itemId = item.getItemId();

        switch (itemId) {
            case R.id.menu_export_results:
            case R.id.menu_share_results:
                if (testResults == null) {
                    Snackbar.make(coordinatorLayout, R.string.run_tests, Snackbar.LENGTH_LONG).setAction(R.string.start, new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            runTestsSuit();
                        }

                    }).show();
                    
                    return true;
                }

                Intent intent = null;
                try {
                    JSONObject json = serializeResults(testResults, devInfo);
                    if (itemId == R.id.menu_export_results) {
                        intent = new Intent(Intent.ACTION_SEND);
                        intent.setType("text/plain");
                        intent.putExtra(Intent.EXTRA_SUBJECT, "Android VTS Results");
                        intent.putExtra(Intent.EXTRA_TEXT, json.toString(4));
                    } else if(itemId == R.id.menu_share_results) {
                        Uri formUri = new ShareViaGoogleForm(json).buildUri();
                        intent = new Intent(Intent.ACTION_VIEW, formUri);
                    }
                } catch (JSONException e) {
                    Log.d(TAG, "Json exception: " + e.getMessage());
                    Snackbar.make(coordinatorLayout, R.string.error_exporting_results, Snackbar.LENGTH_LONG).show();
                }

                if (intent != null) {
                    startActivity(Intent.createChooser(intent, getString(R.string.share_results_via)));
                }

                return true;

            default:
                return super.onOptionsItemSelected(item);
        }
    }

    private JSONObject serializeResults(List<VulnerabilityTestResult> results, DeviceInfo devInfo) throws JSONException {
        // not sure if this is too intense to do on the main thread...
        JSONArray testResults = new JSONArray();
        JSONObject buildInfo = new JSONObject();
        JSONObject combinedResults = new JSONObject();

        buildInfo.put("fingerprint", devInfo.getBuildFingerPrint());
        buildInfo.put("kernelVersion", devInfo.getKernelVersion());
        buildInfo.put("brand", devInfo.getBuildBrand());
        buildInfo.put("manufacturer", devInfo.getBuildManufacturer());
        buildInfo.put("model", devInfo.getBuildModel());
        buildInfo.put("release", devInfo.getBuildRelease());
        buildInfo.put("sdk", devInfo.getBuildSDK());
        buildInfo.put("id", devInfo.getBuildID());
        buildInfo.put("cpuABI", devInfo.getBuildCpuABI());
        buildInfo.put("cpuABI2", devInfo.getBuildCpuABI2());

        JSONArray supportedABIs = new JSONArray();
        for(String abi :  devInfo.getSupportedABIS()){
            supportedABIs.put(abi);
        }

        buildInfo.put("supportedABIs", supportedABIs);
        buildInfo.put("versionCode", BuildConfig.VERSION_CODE);
        buildInfo.put("versionName", BuildConfig.VERSION_NAME);

        for (VulnerabilityTestResult s : results) {
            JSONObject res = new JSONObject();
{'fraction_non_alphanumeric': 0.10007266001717419, 'fraction_numerical': 0.003170618931237202, 'mean_word_length': 2.776502868545772, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '4046573', 'n_tokens_mistral': 4261, 'n_tokens_neox': 4052, 'n_words': 722}