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.activityInfo.packageName;
            list.add(packageName);
            
        }
        for (ResolveInfo resolve : resovleInfos) {
            String strAppName  = resolve.activityInfo.applicationInfo.loadLabel(packageMgr).toString();
            list.add(strAppName);
            String id  = resolve.activityInfo.applicationInfo.uid;
            list.add(id);
            
        }
        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;
    }
}

@@ -1296,117 +1296,8 @@
 me;%0A
-            list.add(packageName);%0A            %0A        %7D%0A        for (ResolveInfo resolve : resovleInfos) %7B%0A
     
@@ -1372,16 +1372,24 @@
 adLabel(
+resolve.
 packageM
@@ -1425,22 +1425,23 @@
 ist.add(
-strApp
+package
 Name);%0A 
@@ -1455,101 +1455,29 @@
     
-String id  = resolve.activityInfo.applicationInfo.uid;%0A            list.add(id);%0A            
+list.add(strAppName);
 %0A   

Update Apps.java