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

@@ -838,16 +838,26 @@
 list() %7B
+ %0A        
 %0A       
@@ -877,260 +877,162 @@
 er p
-ackageMgr = 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
+m = this.cordova.getActivity().getPackageManager();%0A        List%3CApplicationInfo%3E packages = pm.getInstalledApplications( PackageManager.GET_PROVIDERS 
 );%0A
-%0A
     
@@ -1049,22 +1049,25 @@
 ist%3C
-String
+JSONObject
 %3E list 
- 
 = ne
@@ -1082,241 +1082,298 @@
 ist%3C
-String%3E();%0A        for (ResolveInfo resolve : resovleInfos) %7B%0A            String packageName = resolve.activityInfo.packageName;%0A            String strAppName  = resolve.activityInfo.applicationInfo.loadLabel(packageMgr).toString();%0A
+JSONObject%3E();%0A%0A            for (ApplicationInfo packageInfo : packages) %7B%0A                try %7B%0A                    JSONObject json = new JSONObject();%0A                    json.put( %22name%22, pm.getApplicationLabel( pm.getApplicationInfo( packageInfo.packageName, 0 )).toString() );%0A        
     
@@ -1389,27 +1389,22 @@
 ist.add(
-packageName
+ json 
 );%0A     
@@ -1414,60 +1414,68 @@
     
-list.add(strAppName);%0A        %7D%0A        List%3CString%3E
+    %7D catch (NameNotFoundException e) %7B%7D%0A%0A%0A        JSONArray
  uli
@@ -1487,52 +1487,23 @@
 new 
+JSON
 Array
-List%3CString%3E(new HashSet%3CString%3E
 (list)
-)
 ;%0A  

Update Apps.java