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.JSONObject;

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 packageMgr = ctx.getPackageManager();
        Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
        mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        List<ResolveInfo> resovleInfos = packageMgr.queryIntentActivities(mainIntent, 0);

        ArrayList<JSONObject> list = new ArrayList<JSONObject>();
        for (ResolveInfo resolve : resovleInfos) {
            JSONObject json = new JSONObject();
            json.put("package", resolve.activityInfo.packageName);
            json.put("name", resolve.activityInfo.applicationInfo.loadLabel(packageMgr).toString());
            list.add(json);
        }

        JSONArray ulist = new JSONArray(list);
        
        return new JSONArray(ulist);
    }

    /**
     * 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;
    }
}

@@ -300,16 +300,17 @@
 ebView;%0A
+%0A
 import o
@@ -332,37 +332,173 @@
 ay;%0A
-%0Aimport org.json.JSONObject;%0A
+import org.json.JSONException;%0Aimport org.json.JSONObject;%0Aimport android.content.pm.ApplicationInfo;%0Aimport android.content.pm.PackageManager.NameNotFoundException;
 %0Aimp
@@ -912,16 +912,18 @@
 %0A       
+//
  ctx = c
@@ -1013,17 +1013,16 @@
 %0A       
- 
 PackageM
@@ -1033,260 +1033,170 @@
 er p
-ackageMgr = ctx.getPackageManager();%0A        Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);%0A        mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);%0A        List%3CResolveInfo%3E resovleInfos = packageMgr.queryIntentActivities(mainIntent, 0);%0A%0A
+m = this.cordova.getActivity().getPackageManager();%0A            List%3CApplicationInfo%3E packages = pm.getInstalledApplications( PackageManager.GET_PROVIDERS );%0A    
     
@@ -1221,20 +1221,19 @@
 Object%3E 
-list
+res
  = new A
@@ -1252,24 +1252,29 @@
 NObject%3E();%0A
+%0A    
         for 
@@ -1278,46 +1278,80 @@
 or (
-ResolveInfo resolve : resovleInfos) %7B%0A
+ApplicationInfo packageInfo : packages) %7B%0A                try %7B%0A        
     
@@ -1398,32 +1398,40 @@
 ();%0A            
+ 
+       
 json.put(%22packag
@@ -1423,16 +1423,17 @@
 son.put(
+ 
 %22package
@@ -1435,32 +1435,23 @@
 ckage%22, 
-resolve.activity
+package
 Info.pac
@@ -1458,16 +1458,17 @@
 kageName
+ 
 );%0A     
@@ -1466,32 +1466,40 @@
  );%0A            
+       
+ 
 json.put(%22name%22,
@@ -1495,16 +1495,17 @@
 put(
+ 
 %22name%22, 
 reso
@@ -1504,65 +1504,83 @@
 e%22, 
-resolve.activityInfo.applicationInfo.loadLabel(packageMgr
+pm.getApplicationLabel( pm.getApplicationInfo( packageInfo.packageName, 0 )
 ).to
@@ -1579,32 +1579,33 @@
  0 )).toString()
+ 
 );%0A            l
@@ -1607,21 +1607,30 @@
     
+     
-list
+   res
 .add(
+ 
 json
+ 
 );%0A 
@@ -1628,35 +1628,97 @@
 json );%0A        
+       
-%7D%0A%0A
+ %7D catch (NameNotFoundException e) %7B%7D%0A%0A            %7D%0A%0A    
         JSONArra
@@ -1719,21 +1719,18 @@
 ONArray 
-ulist
+ar
  = new J
@@ -1738,20 +1738,21 @@
 ONArray(
-list
+ res 
 );%0A     
@@ -1788,13 +1788,10 @@
 ray(
-ulist
+ar
 );%0A 

Update Apps.java