/**
 * @author Carlos Antonio
 * @version 0.1.0
*/
package com.bridge;

import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CallbackContext;

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

import android.net.Uri;
import android.content.Context;

import com.facebook.crypto.Crypto;
import com.facebook.crypto.keychain.SharedPrefsBackedKeyChain;
import com.facebook.crypto.util.SystemNativeCryptoLibrary;

/**
 * This class echoes a string called from JavaScript.
 */
public class Safe extends CordovaPlugin {

  public static final String ENCRYPT_ACTION = "encrypt";
  public static final String DECRYPT_ACTION = "decrypt";

    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
      if (action.equals(ENCRYPT_ACTION)) {
        String path = args.getString(0);
        this.encryptFile(path, callbackContext);
        return true;
      }

      if (action.equals(DECRYPT_ACTION)) {
        String path = args.getString(0);
        this.decryptFile(path, callbackContext);
        return true;
      }

      return false;
    }

    private void encryptFile(String path, CallbackContext callbackContext) {
      if (path != null && path.length() > 0) {
        callbackContext.success(path);
      } else {
        callbackContext.error(2);
      }
    }

    private void decryptFile(String path, CallbackContext callbackContext) {
      if (path != null && path.length() > 0) {
        callbackContext.success(path);
      } else {
        callbackContext.error(2);
      }
    }
}

@@ -65,16 +65,203 @@
 ridge;%0A%0A
+import java.io.BufferedOutputStream;%0Aimport java.io.File;%0Aimport java.io.FileNotFoundException;%0Aimport java.io.FileOutputStream;%0Aimport java.io.IOException;%0Aimport java.io.OutputStream;%0A%0A
 import o
@@ -332,25 +332,24 @@
 ackContext;%0A
-%0A
 import org.j
@@ -397,36 +397,8 @@
 ion;
-%0Aimport org.json.JSONObject;
 %0A%0Aim
@@ -487,16 +487,175 @@
 Crypto;%0A
+import com.facebook.crypto.Entity;%0Aimport com.facebook.crypto.exception.CryptoInitializationException;%0Aimport com.facebook.crypto.exception.KeyChainException;%0A
 import c
@@ -1606,38 +1606,1624 @@
     
-callbackContext.success(path);
+Entity entity = null;%0A        Context context = cordova.getActivity().getApplicationContext();%0A%0A        Uri uri = Uri.parse(path);%0A        String fileName = uri.getLastPathSegment();%0A%0A        File file = new File(uri.getPath());%0A        File tmpFile = null;%0A        try %7B%0A          tmpFile = File.createTempFile(fileName, null, context.getCacheDir());%0A        %7D catch (IOException e) %7B%0A          callbackContext.error(0);%0A          e.printStackTrace();%0A        %7D%0A%0A        OutputStream fileStream = null;%0A        try %7B%0A          fileStream = new BufferedOutputStream(%0A              new FileOutputStream(tmpFile));%0A        %7D catch (FileNotFoundException e) %7B%0A          callbackContext.error(4);%0A          e.printStackTrace();%0A        %7D%0A%0A        Crypto crypto = new Crypto(%0A            new SharedPrefsBackedKeyChain(context),%0A            new SystemNativeCryptoLibrary());%0A%0A        if (!crypto.isAvailable()) %7B%0A          callbackContext.error(1);%0A          return;%0A        %7D%0A%0A        try %7B%0A          OutputStream outputStream = crypto.getCipherOutputStream(%0A            fileStream,%0A            entity);%0A%0A          // TODO: Write contents of file to encryption stream%0A          // outputStream.write(contents);%0A          outputStream.close();%0A%0A          callbackContext.success(path);%0A        %7D catch (IOException e) %7B%0A          callbackContext.error(0);%0A          e.printStackTrace();%0A        %7D catch (CryptoInitializationException e) %7B%0A          callbackContext.error(1);%0A          e.printStackTrace();%0A        %7D catch (KeyChainException e) %7B%0A          callbackContext.error(3);%0A          e.printStackTrace();%0A        %7D
 %0A   

Add initial crypto logic