/**
 * @author Carlos Antonio
 * @version 0.2.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.Intent;
import android.content.Context;
import android.webkit.MimeTypeMap;
import android.content.ActivityNotFoundException;

/**
 * This class echoes a string called from JavaScript.
 */
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 url = args.getString(0);
      this.chooseIntent(url, callbackContext);
      return true;
    }
    return false;
  }

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

    String extension = MimeTypeMap.getFileExtensionFromUrl(url);
    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 url
   * @param callbackContext
   */
  private void chooseIntent(String url, CallbackContext callbackContext) {
    if (url != null && url.length() > 0) {
      // callbackContext.success("Successfully opened file.");
    } else {
      // callbackContext.error("Expected one non-empty string argument.");
    }
  }
}

@@ -1555,86 +1555,382 @@
     
-// callbackContext.success(%22Successfully opened file.%22);%0A    %7D else %7B
+try %7B%0A        Uri uri = Uri.parse(url);%0A        String mime = getMimeType(url);%0A        Intent fileIntent = new Intent(Intent.ACTION_VIEW);%0A%0A        fileIntent.setDataAndTypeAndNormalize(uri, mime);%0A        cordova.getActivity().startActivity(fileIntent);%0A%0A        callbackContext.success();%0A      %7D catch (ActivityNotFoundException e) %7B%0A        e.printStackTrace();
 %0A      
-//
+ 
  cal
@@ -1952,49 +1952,62 @@
 ror(
-%22Expected one non-empty string argument.%22
+1);%0A      %7D%0A    %7D else %7B%0A      callbackContext.error(2
 );%0A 

Create intent for URI and MIME