/**
 * Open.java
 *
 * Copyright (C) 2014 Carlos Antonio
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 */

package com.bridge;

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

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

import android.net.Uri;
import android.content.Intent;
import android.webkit.MimeTypeMap;
import android.content.ActivityNotFoundException;

/**
 * This class starts an activity for an intent to view files
 */
public class Open extends CordovaPlugin {

  public static final String OPEN_ACTION = "open";

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

  /**
   * Returns the MIME type of the file.
   *
   * @param path
   * @return
   */
  private static String getMimeType(String path) {
    String mimeType = null;

    String extension = MimeTypeMap.getFileExtensionFromUrl(path);
    if (extension != null) {
      MimeTypeMap mime = MimeTypeMap.getSingleton();
      mimeType = mime.getMimeTypeFromExtension(extension);
    }

    System.out.println("Mime type: " + mimeType);

    return mimeType;
  }

  /**
   * Creates an intent for the data of mime type
   *
   * @param path
   * @param callbackContext
   */
  private void previewFile(String path, CallbackContext callbackContext) {
    if (path != null && path.length() > 0) {
      try {
        Uri uri = Uri.parse(path);
        String mime = getMimeType(path);
        Intent fileIntent = new Intent(Intent.ACTION_VIEW);

        fileIntent.setDataAndTypeAndNormalize(uri, mime);
        cordova.getActivity().startActivity(fileIntent);

        callbackContext.success();
      } catch (ActivityNotFoundException e) {
        e.printStackTrace();
        callbackContext.error(1);
      }
    } else {
      callbackContext.error(2);
    }
  }
}

@@ -332,16 +332,37 @@
 .bridge;
+%0Aimport java.io.File;
 %0A%0Aimport
@@ -962,24 +962,46 @@
 _ACTION)) %7B%0A
+      // get path arg%0A
       String
@@ -1037,48 +1037,201 @@
     
-this.previewFile(path, callbackContext);
+// if path is set execute plugin methods%0A      if (path != null && path.length() %3E 0) %7B%0A        this.previewFile(path, callbackContext);%0A      %7D else %7B%0A        callbackContext.error(2);%0A      %7D
 %0A   
@@ -1923,55 +1923,8 @@
 ) %7B%0A
-    if (path != null && path.length() %3E 0) %7B%0A  
     
@@ -1929,18 +1929,16 @@
   try %7B%0A
-  
       Ur
@@ -1964,26 +1964,24 @@
 ath);%0A      
-  
 String mime 
@@ -1997,26 +1997,24 @@
 Type(path);%0A
-  
       Intent
@@ -2066,18 +2066,16 @@
 %0A%0A      
-  
 fileInte
@@ -2116,18 +2116,16 @@
  mime);%0A
-  
       co
@@ -2174,26 +2174,24 @@
 nt);%0A%0A      
- 
- 
 callbackCont
@@ -2201,26 +2201,24 @@
 .success();%0A
-  
     %7D catch 
@@ -2255,18 +2255,16 @@
 %7B%0A      
- 
- 
 e.printS
@@ -2272,26 +2272,24 @@
 ackTrace();%0A
-  
       callba
@@ -2316,62 +2316,89 @@
     
-  
 %7D%0A  
- 
- %7D else %7B%0A      callbackContext.error(2);%0A    %7D
+%7D%0A%0A  private File downloadFile(String url, CallbackContext callbackContext) %7B
 %0A  %7D

Check for argument validity in root method