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();
    }

    class PInfo {
        private String appname = "";
        private String pname = "";
        private String versionName = "";
        private int versionCode = 0;
        private Drawable icon;
        private void prettyPrint() {
            Log.v(appname + "   " + pname + "   " + versionName + " " + versionCode);
        }
    }

    private ArrayList<PInfo> getPackages() {
        ArrayList<PInfo> apps = getInstalledApps(false); /* false = no system packages */
        final int max = apps.size();
        for (int i=0; i<max; i++) {
            apps.get(i).prettyPrint();
        }
        return apps;
    }

    private ArrayList<PInfo> getInstalledApps(boolean getSysPackages) {
        ArrayList<PInfo> res = new ArrayList<PInfo>();        
        List<PackageInfo> packs = getPackageManager().getInstalledPackages(0);
        for(int i=0;i<packs.size();i++) {
            PackageInfo p = packs.get(i);
            if ((!getSysPackages) && (p.versionName == null)) {
                continue ;
            }
            PInfo newInfo = new PInfo();
            newInfo.appname = p.applicationInfo.loadLabel(getPackageManager()).toString();
            newInfo.pname = p.packageName;
            newInfo.versionName = p.versionName;
            newInfo.versionCode = p.versionCode;
            newInfo.icon = p.applicationInfo.loadIcon(getPackageManager());
            res.add(newInfo);
        }
        return new JSONArray(res);
    }

    /**
     * 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,699 +816,310 @@
     
-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
+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
 %0A   
@@ -1137,18 +1137,21 @@
 ist%3C
-PInfo%3E res
+String%3E list 
  = n
@@ -1167,378 +1167,170 @@
 ist%3C
-PInfo%3E();        %0A        List%3CPackageInfo%3E packs = getPackageManager().getInstalledPackages(0);%0A        for(int i=0;i%3Cpacks.size();i++) %7B%0A            PackageInfo p
+String%3E();%0A        for (ResolveInfo resolve : resovleInfos) %7B%0A            String packageName
  = 
-p
 ac
-ks.get(i);%0A            if ((!getSysPackages) && (p.versionName == null)) %7B%0A                continue ;%0A            %7D%0A            PInfo newInfo = new PInfo();%0A            newInfo.appn
+tivityInfo.packageName;%0A            String strAppN
 ame 
+ 
 = 
-p.application
+activity
 Info
@@ -1340,35 +1340,26 @@
 adLabel(
-getP
+p
 ackageM
-anager()
+gr
 ).toStri
@@ -1380,26 +1380,17 @@
     
-newInfo.pname = p.
+list.add(
 pack
@@ -1388,32 +1388,33 @@
 .add(packageName
+)
 ;%0A            ne
@@ -1415,43 +1415,28 @@
     
-newInfo.versionName = p.version
+list.add(strApp
 Name
+)
 ;%0A  
@@ -1445,164 +1445,97 @@
     
-    newInfo.versionCode = p.versionCode;%0A            newInfo.icon = p.applicationInfo.loadIcon(getPackageManager());%0A            res.add(newInfo);%0A        %7D
+%7D%0A        List%3CString%3E ulist = new ArrayList%3CString%3E(new HashSet%3CString%3E(list));%0A        
 %0A   
@@ -1560,19 +1560,21 @@
 ONArray(
-res
+ulist
 );%0A    %7D

Update Apps.java