/*
 * PhoneGap is available under *either* the terms of the modified BSD license *or* the
 * MIT License (2008). See http://opensource.org/licenses/alphabetical for full text.
 *
 * Copyright (c) 2005-2010, Nitobi Software Inc.
 * Copyright (c) 2011, IBM Corporation
 */

package org.apache.cordova.fileopener;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

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

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

import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.PluginResult;

public class FileOpener extends CordovaPlugin {
    
    public PluginResult execute(String action, JSONArray args, String callbackId) {
        PluginResult.Status status = PluginResult.Status.OK;
        String result = "";
        
        try {
            if (action.equals("openFile")) {
                openFile(args.getString(0));
            }
            else {
                status = PluginResult.Status.INVALID_ACTION;
            }
            return new PluginResult(status, result);
        } catch (JSONException e) {
            return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
        } catch (IOException e) {
            return new PluginResult(PluginResult.Status.IO_EXCEPTION);
        }
    }
    
    private void openFile(String url) throws IOException {
        // Create URI
        Uri uri = Uri.parse(url);
        
        Intent intent = null;
        // Check what kind of file you are trying to open, by comparing the url with extensions.
        // When the if condition is matched, plugin sets the correct intent (mime) type,
        // so Android knew what application to use to open the file
        
        if (url.contains(".doc") || url.contains(".docx")) {
            // Word document
            intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(uri, "application/msword");
        } else if(url.contains(".pdf")) {
            // PDF file
            intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(uri, "application/pdf");
        } else if(url.contains(".ppt") || url.contains(".pptx")) {
            // Powerpoint file
            intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(uri, "application/vnd.ms-powerpoint");
        } else if(url.contains(".xls") || url.contains(".xlsx")) {
            // Excel file
            intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(uri, "application/vnd.ms-excel");
        } else if(url.contains(".rtf")) {
            // RTF file
            intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(uri, "application/rtf");
        } else if(url.contains(".wav")) {
            // WAV audio file
            intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(uri, "audio/x-wav");
        } else if(url.contains(".gif")) {
            // GIF file
            intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(uri, "image/gif");
        } else if(url.contains(".jpg") || url.contains(".jpeg")) {
            // JPG file
            intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(uri, "image/jpeg");
        } else if(url.contains(".txt")) {
            // Text file
            intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(uri, "text/plain");
        } else if(url.contains(".mpg") || url.contains(".mpeg") || url.contains(".mpe") || url.contains(".mp4") || url.contains(".avi")) {
            // Video files
            intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(uri, "video/*");
        }
        
        //if you want you can also define the intent type for any other file
        
        //additionally use else clause below, to manage other unknown extensions
        //in this case, Android will show all applications installed on the device
        //so you can choose which application to use
        
        else {
            intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(uri, "*/*");
        }
        
        this.cordova.getActivity().startActivity(intent);
    }
    
}

@@ -1,424 +1,70 @@
-/*%0A * PhoneGap is available under *either* the terms of the modified BSD license *or* the%0A * MIT License (2008). See http://opensource.org/licenses/alphabetical for full text.%0A *%0A * Copyright (c) 2005-2010, Nitobi Software Inc.%0A * Copyright (c) 2011, IBM Corporation%0A */%0A%0Apackage org.apache.cordova.fileopener;%0A%0Aimport java.io.File;%0Aimport java.io.FileOutputStream;%0Aimport java.io.IOException;%0Aimport java.io.InputStream
+package org.apache.cordova.fileopener;%0A%0Aimport java.io.IOException
 ;%0A%0Ai
@@ -125,40 +125,8 @@
 n;%0A%0A
-import android.content.Context;%0A
 impo
@@ -176,16 +176,90 @@
 et.Uri;%0A
+import android.os.Environment;%0A%0Aimport org.apache.cordova.CallbackContext;
 %0Aimport 
@@ -295,48 +295,8 @@
 gin;
-%0Aimport org.apache.cordova.PluginResult;
 %0A%0Apu
@@ -350,31 +350,23 @@
    %0A
-    
+%09
 public 
-PluginResult
+boolean
  exe
@@ -405,615 +405,366 @@
 gs, 
-String callbackId) %7B%0A        PluginResult.Status status = PluginResult.Status.OK;%0A        String result = %22%22;%0A        %0A        try %7B%0A            if (action.equals(%22openFile%22)) %7B%0A                openFile(args.getString(0));%0A            %7D%0A            else %7B%0A                status = PluginResult.Status.INVALID_ACTION;%0A            %7D%0A            return new PluginResult(status, result);%0A        %7D catch (JSONException e) %7B%0A            return new PluginResult(PluginResult.Status.JSON_EXCEPTION);%0A        %7D catch (IOException e) %7B%0A            return new PluginResult(PluginResult.Status.IO_EXCEPTION);%0A        %7D
+CallbackContext callbackContext) throws JSONException%7B%0A        if (action.equals(%22openFile%22)) %7B%0A            try %7B%0A%09%09%09%09openFile(Environment.getExternalStorageDirectory()+%22/sw/www/%22+args.getString(0));%0A%09%09%09%7D catch (IOException e) %7B%0A%09%09%09%09e.printStackTrace();%0A%09%09%09%09return false;%0A%09%09%09%7D%0A        %7D%0A        else %7B%0A            return false;%0A        %7D%0A        return true;
 %0A   
@@ -937,253 +937,33 @@
     
-// Check what kind of file you are trying to open, by comparing the url with extensions.%0A        // When the if condition is matched, plugin sets the correct intent (
+%0A        //guess 
 mime
-) 
 type
-,%0A        // so Android knew what application to use to open the file
 %0A   
@@ -2975,329 +2975,8 @@
   %7D%0A
-        %0A        //if you want you can also define the intent type for any other file%0A        %0A        //additionally use else clause below, to manage other unknown extensions%0A        //in this case, Android will show all applications installed on the device%0A        //so you can choose which application to use%0A        %0A
     
@@ -3096,25 +3096,16 @@
       %7D%0A
-        %0A
         

fix android