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

          final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
          mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
          final List pkgAppsList = packageMgr.queryIntentActivities( mainIntent, 0);
          ArrayList<String> list  = new ArrayList<String>();
          for (Object object : pkgAppsList) 
          {
            ResolveInfo info = (ResolveInfo) object;
            //Drawable icon    = getBaseContext().getPackageManager().getApplicationIcon(info.activityInfo.applicationInfo);
            String strAppName   = info.activityInfo.applicationInfo.loadLabel(packageMgr).toString();
            String strPackageName  = info.activityInfo.applicationInfo.packageName.toString();
            //final String title  = (String)((info != null) ? packageMgr.getApplicationLabel(info.activityInfo.applicationInfo) : "???");
            list.add(strAppName);
            list.add(strPackageName);
        }     


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

@@ -847,29 +847,16 @@
         
-%0A            
 PackageM
@@ -900,25 +900,24 @@
 ager();%0A
-%0A
         
   final 
@@ -908,24 +908,16 @@
         
-  final 
 Intent m
@@ -974,18 +974,16 @@
         
-  
 mainInte
@@ -1036,32 +1036,38 @@
     
-  
-final List pkgAppsList
+List%3CResolveInfo%3E resovleInfos
  = p
@@ -1098,17 +1098,16 @@
 ivities(
- 
 mainInte
@@ -1114,25 +1114,25 @@
 nt, 0);%0A
- 
+%0A
         
  ArrayLi
@@ -1123,17 +1123,16 @@
         
- 
 ArrayLis
@@ -1186,69 +1186,13 @@
     
-  
 for (
-Object object : pkgAppsList) %0A          %7B%0A            
 Reso
@@ -1203,36 +1203,33 @@
 nfo 
-info = (R
+resolve : r
 eso
-l
 v
+l
 eInfo
+s
 ) 
-object;
+%7B
 %0A   
@@ -1241,119 +1241,61 @@
     
-//Drawable icon    = getBaseContext().getPackageManager().getApplicationIcon(info.activityInfo.applicationInfo)
+String packageName = resolve.activityInfo.packageName
 ;%0A  
@@ -1323,23 +1323,25 @@
 ppName  
- 
 = 
-info
+resolve
 .activit
@@ -1346,32 +1346,16 @@
 ityInfo.
-applicationInfo.
 loadLabe
@@ -1355,16 +1355,19 @@
 oadLabel
+Res
 (package
@@ -1399,317 +1399,74 @@
     
-String strPackageName  = info.activityInfo.applicationInfo.packageName.toString();%0A            //final String title  = (String)((info != null) ? packageMgr.getApplicationLabel(info.activityInfo.applicationInfo) : %22???%22);%0A            list.add(strAppName);%0A            list.add(strPackageName);%0A        %7D     %0A%0A
+list.add(packageName);%0A            list.add(strAppName);%0A        %7D
 %0A   

Update Apps.java