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() {
        List<PackageInfo> apps = getPackageManager().getInstalledPackages(0)

        
        ArrayList<AppInfo> res = new ArrayList<AppInfo>();
            for(int i=0;i<apps.size();i++) {
                        PackageInfo p = apps.get(i);
         
                        AppInfo newInfo = new AppInfo();
                        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());
                        list.add(newInfo);
                        }
                    }
         
        class AppInfo {
            String appname = "";
            String pname = "";
            String versionName = "";
            int versionCode = 0;
            Drawable icon;
         
        }

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

@@ -851,154 +851,284 @@
     
-List%3C
 Package
-Info%3E apps = getPackageManager().getInstalledPackages(0)%0A%0A        %0A        ArrayList%3CAppInfo%3E res = new ArrayList%3CAppInfo%3E(
+Manager 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
         
     
@@ -1127,120 +1127,110 @@
     
-    for(int i=0;i%3Capps.size();i++) %7B%0A                        PackageInfo p = apps.get(i);%0A         %0A            
+ArrayList%3CString%3E list  = new ArrayList%3CString%3E();%0A        for (ResolveInfo resolve : resovleInfos) %7B%0A
     
@@ -1286,140 +1286,46 @@
     
-    
-        newInfo.appname = p.applicationInfo.loadLabel(getPackageManager()).toString();%0A                        newInfo.pname = p
+newInfo.package = resolve.activityInfo
 .pac
@@ -1350,204 +1350,79 @@
     
- 
-           newInfo.versionName = p.versionName;%0A                        newInfo.versionCode = p.versionCode;%0A                        newInfo.icon = p.applicationInfo.loadIcon(getPackageManager
+newInfo.name    = resolve.activityInfo.loadLabel(packageMgr).toString
 ()
-)
 ;%0A  
@@ -1431,28 +1431,16 @@
         
-    
-        
 list.add
@@ -1462,263 +1462,9 @@
     
-                %7D%0A                    %7D%0A         %0A        class AppInfo %7B%0A            String appname = %22%22;%0A            String pname = %22%22;%0A            String versionName = %22%22;%0A            int versionCode = 0;%0A            Drawable icon;%0A         %0A        %7D%0A
+%7D
 %0A   

Update Apps.java