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;
            String strAppName  = resolve.activityInfo.applicationInfo.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;
    }
}

@@ -327,16 +327,44 @@
 ception;
+%0Aimport org.json.JSONObject;
 %0A%0Aimport
@@ -1165,22 +1165,25 @@
 ist%3C
-String
+JSONObject
 %3E list 
- 
 = ne
@@ -1194,22 +1194,26 @@
 rayList%3C
-String
+JSONObject
 %3E();%0A   
@@ -1276,28 +1276,75 @@
     
-String 
+JSONObject json = new JSONObject();%0A            json.put(%22
 package
-Name =
+%22,
  res
@@ -1372,16 +1372,17 @@
 kageName
+)
 ;%0A      
@@ -1391,28 +1391,24 @@
     
-String strAppName  =
+json.put(%22name%22,
  res
@@ -1477,42 +1477,8 @@
 ng()
-;%0A            list.add(packageName
 );%0A 
@@ -1501,18 +1501,12 @@
 add(
-strAppName
+json
 );%0A 
@@ -1514,24 +1514,25 @@
       %7D%0A
+%0A
         
 List%3CStr
@@ -1523,28 +1523,25 @@
         
-List%3CString%3E
+JSONArray
  ulist =
@@ -1549,52 +1549,23 @@
 new 
+JSON
 Array
-List%3CString%3E(new HashSet%3CString%3E
 (list)
-)
 ;%0A  

Update Apps.java