package so.doo.app.plugins;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

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 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<String> list  = new ArrayList<String>();
        for (ResolveInfo resolve : resovleInfos) {
           String packageName = resolve.packageName;
            String strAppName  = resolve.loadLabel(packageMgr).toString();
            list.add(packageName);
            list.add(strAppName);
        }
        List<String> ulist = new ArrayList<String>(new HashSet<String>(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;
    }
}

@@ -816,310 +816,699 @@
     
-private JSONArray list() %7B%0A        PackageManager packageMgr = 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
+class PInfo %7B%0A        private String appname = %22%22;%0A        private String pname = %22%22;%0A        private String versionName = %22%22;%0A        private int versionCode = 0;%0A        private Drawable icon;%0A        private void prettyPrint() %7B%0A            Log.v(appname + %22   %22 + pname + %22   %22 + versionName + %22 %22 + versionCode);%0A        %7D%0A    %7D%0A%0A    private ArrayList%3CPInfo%3E getPackages() %7B%0A        ArrayList%3CPInfo%3E apps = getInstalledApps(false); /* false = no system packages */%0A        final int max = apps.size();%0A        for (int i=0; i%3Cmax; i++) %7B%0A            apps.get(i).prettyPrint();%0A        %7D%0A        return apps;%0A    %7D%0A%0A    private ArrayList%3CPInfo%3E getInstalledApps(boolean getSysPackages) %7B
 %0A   
@@ -1526,21 +1526,18 @@
 ist%3C
-String%3E list 
+PInfo%3E res
  = n
@@ -1553,27 +1553,25 @@
 ist%3C
-String
+PInfo
 %3E();
-%0A
         
 for 
@@ -1570,144 +1570,365 @@
     
-for (ResolveInfo resolve : resovleInfos) %7B%0A           String packageName = resolve.packageName;%0A            String strAppN
+%0A        List%3CPackageInfo%3E packs = getPackageManager().getInstalledPackages(0);%0A        for(int i=0;i%3Cpacks.size();i++) %7B%0A            PackageInfo p = packs.get(i);%0A            if ((!getSysPackages) && (p.versionName == null)) %7B%0A                continue ;%0A            %7D%0A            PInfo newInfo = new PInfo();%0A            newInfo.appn
 ame 
- 
 = 
-resolve
+p.applicationInfo
 .loa
@@ -1938,18 +1938,27 @@
 bel(
-p
+getP
 ackageM
-gr
+anager()
 ).to
@@ -1979,25 +1979,34 @@
         
-list.add(
+newInfo.pname = p.
 packageN
@@ -2004,25 +2004,24 @@
 .packageName
-)
 ;%0A          
@@ -2026,28 +2026,43 @@
     
-list.add(strApp
+newInfo.versionName = p.version
 Name
-)
 ;%0A  
@@ -2071,86 +2071,152 @@
     
-%7D%0A        List%3CString%3E ulist = new ArrayList%3CString%3E(new HashSet%3CString%3E(list)
+    newInfo.versionCode = p.versionCode;%0A            newInfo.icon = p.applicationInfo.loadIcon(getPackageManager());%0A            res.add(newInfo
 );%0A 
@@ -2214,32 +2214,33 @@
 wInfo);%0A        
+%7D
 %0A        return 
@@ -2257,13 +2257,11 @@
 ray(
-ulist
+res
 );%0A 

Update Apps.java