/**
 * 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 java.io.File;

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)) {
      // get path arg
      String path = args.getString(0);
      // if path is set execute plugin methods
      if (path != null && path.length() > 0) {
        this.previewFile(path, callbackContext);
      } else {
        callbackContext.error(2);
      }
      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) {
    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);
    }
  }

  private File downloadFile(String url, CallbackContext callbackContext) {
  }
}

@@ -439,17 +439,16 @@
 ontext;%0A
-%0A
 import o
@@ -522,16 +522,48 @@
 et.Uri;%0A
+import android.content.Context;%0A
 import a
@@ -669,16 +669,113 @@
 ception;
+%0Aimport android.webkit.URLUtil;%0Aimport android.os.AsyncTask;%0Aimport android.webkit.CookieManager;
 %0A%0A/**%0A *
@@ -1098,30 +1098,8 @@
 ) %7B%0A
-      // get path arg%0A
     
@@ -1143,191 +1143,67 @@
     
-// 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
+  new FileDownloadAsyncTask(path, callbackContext).execute(
 );%0A 
@@ -1232,32 +1232,8 @@
 ue;%0A
-    %7D%0A    return false;%0A
   %7D%0A
@@ -1873,24 +1873,71 @@
 kContext) %7B%0A
+    if (path != null && path.length() %3E 0) %7B%0A  
     try %7B%0A  
@@ -1934,16 +1934,18 @@
   try %7B%0A
+  
       Ur
@@ -1971,24 +1971,26 @@
 ath);%0A      
+  
 String mime 
@@ -2020,20 +2020,18 @@
     
+  
 Intent 
-fileI
+i
 nten
@@ -2070,20 +2070,91 @@
 W);%0A
-%0A
+  
       
-fileI
+Context context = cordova.getActivity().getApplicationContext();%0A%0A        i
 nten
@@ -2200,37 +2200,25 @@
 ;%0A      
-cordova.getActivity()
+  context
 .startAc
@@ -2228,13 +2228,9 @@
 ity(
-fileI
+i
 nten
@@ -2230,24 +2230,26 @@
 y(intent);%0A%0A
+  
       callba
@@ -2273,16 +2273,18 @@
 ();%0A    
+ 
+ 
 %7D catch 
@@ -2311,24 +2311,26 @@
 eption e) %7B%0A
+  
       e.prin
@@ -2346,24 +2346,26 @@
 ce();%0A      
+  
 callbackCont
@@ -2378,97 +2378,695 @@
 ror(
-1);%0A    %7D%0A  %7D%0A%0A  private File downloadFile(String url, CallbackContext callbackContext) %7B
+e.getMessage());%0A      %7D%0A    %7D else %7B%0A      callbackContext.error(2);%0A    %7D%0A  %7D%0A%0A  private class FileDownloadAsyncTask extends AsyncTask%3CVoid, Void, File%3E %7B%0A    private final CallbackContext callbackContext;%0A    private final String url;%0A%0A    public FileDownloadAsyncTask(String url, CallbackContext callbackContext) %7B%0A      super();%0A      this.callbackContext = callbackContext;%0A      this.url = url;%0A    %7D%0A%0A    @Override%0A    protected File doInBackground(Void... arg0) %7B%0A      // File file = downloadFile(url);%0A      return file;%0A    %7D%0A%0A    @Override%0A    protected void onPostExecute(File result) %7B%0A      String path = result.toString();%0A      previewFile(path, callbackContext);%0A    %7D
 %0A  %7D

Allow for file downloads