package fuzion24.device.vulnerability.broadcastreceiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.os.AsyncTask;
import android.os.Bundle;

import org.json.JSONObject;

import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

import fuzion24.device.vulnerability.test.VulnerabilityTestResult;
import fuzion24.device.vulnerability.util.DeviceInfo;
import fuzion24.device.vulnerability.vulnerabilities.VulnerabilityOrganizer;
import fuzion24.device.vulnerability.vulnerabilities.VulnerabilityResultSerialzier;
import fuzion24.device.vulnerability.vulnerabilities.VulnerabilityTest;

/**
 * Created by fuzion24 on 11/25/15.
 */
public class ScanRunnerBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(final Context context, Intent intent) {

        //Only allow this code to be ran on debug builds, since it accepts and writes to arbitrary file
        //paths, which would allow another app to arbitrarily write anywhere in this app's context.

        boolean isDebuggable = ( 0 != ( context.getApplicationInfo().flags &= ApplicationInfo.FLAG_DEBUGGABLE ) );
        if(!isDebuggable){
            return;
        }

        Bundle intentExtras = intent.getExtras();
        final String writeResultPath = intentExtras.getString("RESULT_PATH");

        new AsyncTask<Void,Void,Void>(){
            @Override
            protected Void doInBackground(Void... params) {
                List<VulnerabilityTest> tests = VulnerabilityOrganizer.getTests(context);
                List<VulnerabilityTestResult> results = new ArrayList<VulnerabilityTestResult>();
                for(VulnerabilityTest vt : tests){
                    boolean vulnerable = false;
                    Exception x = null;
                    try {
                        vulnerable = vt.isVulnerable(context);
                    }catch(Exception e){
                      x = e;
                    }
                    results.add(new VulnerabilityTestResult(vt, vulnerable, x));

                }

                try {
                    JSONObject jobj = VulnerabilityResultSerialzier.serializeResultsToJson(results, DeviceInfo.getDeviceInfo());
                    FileOutputStream fos = new FileOutputStream(writeResultPath);
                    fos.write(jobj.toString(2).getBytes());
                    fos.close();
                }catch(Exception e){
                    e.printStackTrace();
                }

                return null;
            }
        }.execute();

    }
}

@@ -1123,16 +1123,112 @@
 ontext.%0A
+        // http://android-developers.blogspot.com/2010/09/securing-android-lvl-applications.html
 %0A       

Add link to description of debuggable check