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 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 = "NaturalForms";

    @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;
            }

            String state = Environment.getExternalStorageState();
            if (Environment.MEDIA_MOUNTED.equals(state)) {
                File JNDir = new File(Environment.getExternalStorageDirectory(), "JobNimbus");
                if(JNDir.mkdirs()){}
                String fileName = "/nf-data" + System.currentTimeMillis() + ".csv";
                try {
                    FileOutputStream overWrite = new FileOutputStream(JNDir.toString() + fileName, false);
                    overWrite.write(args.getString(0).getBytes());
                    overWrite.flush();
                    overWrite.close();

                    LaunchIntent.setDataAndType(Uri.parse("file://" + JNDir.toString() + fileName), "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 ioe) {
                    callback.error("Could not send naturalForms data");
                }
                catch(Exception e){
                    callback.error("Failed to launch naturalForms");
                }
            } else callback.error("Cannot access external storage");

            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;
    }
}
@@ -1097,289 +1097,95 @@
     
-String state = Environment.getExternalStorageState();%0A            if (Environment.MEDIA_MOUNTED.equals(state)) %7B%0A                File JNDir = new File(Environment.getExternalStorageDirectory(), %22JobNimbus%22);%0A                if(JNDir.mkdirs())%7B%7D%0A                String fileName =
+File nfData = new File(cordova.getActivity().getExternalCacheDir() + File.separator +
  %22
-/
 nf-d
@@ -1226,22 +1226,19 @@
 + %22.csv%22
+)
 ;%0A
-    
         
@@ -1246,32 +1246,65 @@
    try %7B%0A       
+   
+      nfData.createNewFile();%0A
              Fil
@@ -1296,24 +1296,27 @@
             
+   
 FileOutputSt
@@ -1353,21 +1353,22 @@
 tStream(
-JNDir
+nfData
 .toStrin
@@ -1370,27 +1370,16 @@
 String()
- + fileName
 , false)
@@ -1380,20 +1380,16 @@
 false);%0A
-    
         
@@ -1451,36 +1451,32 @@
                 
-   
- 
 overWrite.flush(
@@ -1474,28 +1474,24 @@
 te.flush();%0A
-    
             
@@ -1522,36 +1522,32 @@
                 
-   
- 
 LaunchIntent.set
@@ -1584,13 +1584,14 @@
 %22 + 
-JNDir
+nfData
 .toS
@@ -1601,19 +1601,8 @@
 ng()
- + fileName
 ), %22
@@ -1626,28 +1626,24 @@
             
-    
 ResolveInfo 
@@ -1705,28 +1705,24 @@
 alforms%22);%0A%0A
-    
             
@@ -1850,20 +1850,16 @@
         
-    
 cordova.
@@ -1910,28 +1910,24 @@
             
-   
- 
 %7D catch (IOE
@@ -1938,28 +1938,24 @@
 tion ioe) %7B%0A
-    
             
@@ -2026,35 +2026,16 @@
     
-    %7D%0A               
+%7D
  catch
+ 
 (Exc
@@ -2043,22 +2043,19 @@
 ption e)
+ 
 %7B%0A
-    
         
@@ -2127,82 +2127,9 @@
     
-    %7D%0A            %7D else callback.error(%22Cannot access external storage%22);
+%7D
 %0A%0A  

use external app cache directory on android