package io.happie.naturalforms;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.ActivityNotFoundException;
import android.content.pm.ResolveInfo;
import android.os.Environment;
import android.net.Uri;
import android.util.Log;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

public class NaturalForms extends CordovaPlugin {

    public static final String TAG = "NaturalFormsPlugin";

    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callback) throws JSONException {
        try {
            PackageManager manager = cordova.getActivity().getApplicationContext().getPackageManager();
            Intent LaunchIntent = manager.getLaunchIntentForPackage("net.expedata.naturalforms");
            if (LaunchIntent == null) {
                callback.error("naturalForms not installed");
                return true;
            }

            File nfData = new File(cordova.getActivity().getExternalCacheDir() + File.separator + "nf-data" + System.currentTimeMillis() + ".csv");
            Log.w(TAG, "PATH: " + nfData.toString());
            try {
                nfData.createNewFile();
                FileOutputStream overWrite = new FileOutputStream(nfData.toString(), false);
                overWrite.write(args.getString(0).getBytes());
                overWrite.flush();
                overWrite.close();

                LaunchIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

                LaunchIntent.setDataAndType(Uri.parse("file://" + nfData.toString()), "text/csv");

                ResolveInfo best = getPackageInfo(LaunchIntent, "net.expedata.naturalforms");

                LaunchIntent.setClassName(best.activityInfo.packageName, "net.expedata.naturalforms.ui.CsvImportActivity");

                cordova.getActivity().startActivity(LaunchIntent);

            } catch (IOException e) {
                callback.error(e.getMessage());
            } catch (Exception e) {
                callback.error(e.getMessage());
            }

            callback.success();
        } catch (ActivityNotFoundException e) {
            callback.error("naturalForms not installed");
        }
        return true;
    }

    private ResolveInfo getPackageInfo(final Intent intent, final String packageName) {
        final PackageManager pm = cordova.getActivity().getPackageManager();
        final List<ResolveInfo> matches = pm.queryIntentActivities(intent, 0);

        ResolveInfo best = null;
        for (final ResolveInfo info : matches) {
            if (info.activityInfo.packageName.contains(packageName)) {
                best = info;
            }
        }
        return best;
    }
}
@@ -192,39 +192,8 @@
 fo;%0A
-import android.os.Environment;%0A
 impo
@@ -487,16 +487,86 @@
 .List;%0A%0A
+import static android.support.v4.content.FileProvider.getUriForFile;%0A%0A
 public c
@@ -1143,32 +1143,306 @@
 %0A            %7D%0A%0A
+            File nfCache = new File(cordova.getActivity().getExternalCacheDir() + %22/nf%22);%0A%0A            //ensure the nf directory exists%0A            if (nfCache.mkdirs()) Log.d(TAG, %22media directory created%22);%0A            else Log.d(TAG, %22media directory already created%22);%0A%0A
             File
@@ -1522,16 +1522,40 @@
 arator +
+ %22nf%22 + File.separator +
  %22nf-dat
@@ -1593,24 +1593,25 @@
  + %22.csv%22);%0A
+%0A
             
@@ -1648,24 +1648,25 @@
 oString());%0A
+%0A
             
@@ -1934,24 +1934,171 @@
 e.close();%0A%0A
+                Uri contentUri = getUriForFile(cordova.getActivity().getApplicationContext(), %22android.support.v4.content.FileProvider%22, nfData);%0A%0A
             
@@ -2212,48 +2212,18 @@
 ype(
-Uri.parse(%22file://%22 + nfData.toString())
+contentUri
 , %22t

update to passing via Content://