package io.happie.cordovaCamera;

import android.content.Context;
import android.content.Intent;

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

import java.io.File;

import android.os.Environment;

public class HappieCamera extends CordovaPlugin {

    public static CallbackContext callbackContext;
    public static Context context;
    protected HappieCameraRoll camRoll = new HappieCameraRoll();

    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
        this.callbackContext = callbackContext;
        if (action.equals("openCamera") || action.equals("getCameraRoll")) {
            try {
                if (action.equals("openCamera")) { //run thread safe camera
                    cordova.getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            openCamera();
                        }
                    });
                } else if (action.equals("getCameraRoll")) {
                    cordova.getThreadPool().execute(new Runnable() {
                        @Override
                        public void run() {
                            camRoll.getAndroidCameraRoll();
                        }
                    });
                }
            } catch (IllegalArgumentException e) {
                callbackContext.error("Illegal Argument Exception");
                PluginResult r = new PluginResult(PluginResult.Status.ERROR);
                callbackContext.sendPluginResult(r);
                return true;
            }

            PluginResult r = new PluginResult(PluginResult.Status.NO_RESULT);
            r.setKeepCallback(true);
            callbackContext.sendPluginResult(r);
            return true;
        }
        return false;
    }

    public void openCamera() {
        context = this.cordova.getActivity().getApplicationContext();
        Intent pictureIntent = new Intent(context, io.happie.cordovaCamera.HappieCameraActivity.class);
        pictureIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(pictureIntent);
    }

    public static void sessionFinished(String JSON) {
        if (JSON != null && JSON.length() > 0) callbackContext.success(JSON);
         else callbackContext.error("no json");
    }
}
@@ -27,16 +27,41 @@
 amera;%0A%0A
+import android.Manifest;%0A
 import a
@@ -80,24 +80,24 @@
 nt.Context;%0A
-
 import andro
@@ -321,16 +321,58 @@
 .File;%0A%0A
+import android.content.pm.PackageManager;%0A
 import a
@@ -596,16 +596,130 @@
 aRoll();
+%0A    public static String currentAction = %22%22;%0A%0A    public static final String CAMERA = Manifest.permission.CAMERA;
 %0A%0A    pu
@@ -870,24 +870,293 @@
 ackContext;%0A
+        currentAction = action;%0A%0A        if(cordova.hasPermission(CAMERA)) %7B%0A            return executeLogic(action);%0A        %7D%0A        else %7B%0A            getCamPermission(0);%0A            return false;%0A        %7D%0A    %7D%0A%0A%0A    public boolean executeLogic(String action) %7B%0A
         if (
@@ -2729,32 +2729,744 @@
 Intent);%0A    %7D%0A%0A
+    public void getCamPermission(int requestCode)%7B%0A        cordova.requestPermission(this, requestCode, CAMERA);%0A    %7D%0A%0A    public void onRequestPermissionResult(int requestCode, String%5B%5D permissions,%0A                                          int%5B%5D grantResults) throws JSONException%0A    %7B%0A        for(int r:grantResults)%0A        %7B%0A            if(r == PackageManager.PERMISSION_DENIED)%0A            %7B%0A                this.callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, %22Permission Denied%22));%0A                return;%0A            %7D%0A        %7D%0A        switch(requestCode)%0A        %7B%0A            case 0:%0A                executeLogic(currentAction);%0A                break;%0A        %7D%0A    %7D%0A%0A
     public stati
@@ -3581,17 +3581,16 @@
 (JSON);%0A
- 
         

add android 6 permissions