package so.doo.app.plugins;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import org.json.JSONException;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CordovaWebView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;

@SuppressLint("HandlerLeak")
public class Apps extends CordovaPlugin {
    
    private Context ctx;

     public void initialize(CordovaInterface cordova, CordovaWebView webView) {
        super.initialize(cordova, webView);
       // ctx = cordova.getActivity().getApplicationContext();
    }

    private JSONArray list() {
       PackageManager pm = this.cordova.getActivity().getPackageManager();
            List<ApplicationInfo> packages = pm.getInstalledApplications( PackageManager.GET_PROVIDERS );
            ArrayList<JSONObject> res = new ArrayList<JSONObject>();

            for (ApplicationInfo packageInfo : packages) {
                try {
                    JSONObject json = new JSONObject();
                    json.put( "package", packageInfo.packageName );
                    json.put( "name", pm.getApplicationLabel( pm.getApplicationInfo( packageInfo.packageName, 0 )).toString() );
                    res.add( json );
                } catch (NameNotFoundException e) {}

            }

            JSONArray ar = new JSONArray( res );
        
        return new JSONArray(ar);
    }

    /**
     * Executes the request and returns PluginResult.
     *
     * @param action            The action to execute.
     * @param args              JSONArry of arguments for the plugin.
     * @param callbackContext   The callback id used when calling back into JavaScript.
     * @return                  True if the action was valid, false otherwise.
     * @throws JSONException 
     */
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
        // list
        if (action.equals("list")) {
            JSONArray json = list();
            callbackContext.success(json.toString());
            
            return true;
        }

        return false;
    }
}

@@ -103,38 +103,8 @@
 st;%0A
-import org.json.JSONException;
 %0Aimp
@@ -270,17 +270,16 @@
 ebView;%0A
-%0A
 import o
@@ -332,142 +332,8 @@
 on;%0A
-import org.json.JSONObject;%0Aimport android.content.pm.ApplicationInfo;%0Aimport android.content.pm.PackageManager.NameNotFoundException;
 %0Aimp
@@ -747,18 +747,16 @@
 %0A       
-//
  ctx = c
@@ -846,16 +846,17 @@
 %0A       
+ 
 PackageM
@@ -867,101 +867,217 @@
 er p
-m = this.cordova.getActivity().getPackageManager();%0A            List%3CApplication
+ackageMgr = ctx.getPackageManager();%0A        Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);%0A        mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);%0A        List%3CResolveInfo%3E resovle
 Info
-%3E
+s =
  package
 s = 
@@ -1076,77 +1076,51 @@
 kage
-s = pm.getInstalledApplications( PackageManager.GET_PROVIDERS );%0A    
+Mgr.queryIntentActivities(mainIntent, 0);%0A%0A
     
@@ -1137,23 +1137,21 @@
 ist%3C
-JSONObject%3E res
+String%3E list 
  = n
@@ -1167,209 +1167,119 @@
 ist%3C
-JSONObject%3E();%0A%0A            for (ApplicationInfo packageInfo : packages) %7B%0A                try %7B%0A                    JSONObject json = new JSONObject();%0A                    json.put( %22package%22, package
+String%3E();%0A        for (ResolveInfo resolve : resovleInfos) %7B%0A            String packageName = resolve.activity
 Info
@@ -1290,18 +1290,16 @@
 kageName
- )
 ;%0A      
@@ -1308,109 +1308,86 @@
     
-        json.put( %22name%22, pm.getApplicationLabel( pm.getApplicationInfo( packageInfo.packageName, 0 )
+String strAppName  = resolve.activityInfo.applicationInfo.loadLabel(packageMgr
 ).to
@@ -1394,18 +1394,16 @@
 String()
- )
 ;%0A      
@@ -1412,97 +1412,73 @@
     
-        res.add( json );%0A                %7D catch (NameNotFoundException e) %7B%7D%0A%0A  
+list.add(packageName);%0A            list.add(strAppName);%0A
         
   %7D%0A
@@ -1469,28 +1469,25 @@
 e);%0A        
-  %7D%0A
+%7D
 %0A           
@@ -1487,46 +1487,76 @@
     
-    JSONArray ar
+List%3CString%3E ulist
  = new 
-JSON
 Array
-( res 
+List%3CString%3E(new HashSet%3CString%3E(list)
 );%0A 
@@ -1592,18 +1592,21 @@
 ONArray(
-ar
+ulist
 );%0A    %7D

Update Apps.java