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() {
        Intent homeintent = new Intent(Intent.ACTION_MAIN);
        homeintent.addCategory(Intent.CATEGORY_HOME);
        homeintent.addCategory(Intent.CATEGORY_DEFAULT);// seems not needed here since This is a synonym for
        // including
        // the CATEGORY_DEFAULT in your supplied Intent per doc
        this.resolveInfo = pm.resolveActivity(homeintent, PackageManager.MATCH_DEFAULT_ONLY);
        ActivityInfo activityInfo = resolveInfo.activityInfo;
        userHomeLauncherPackage = activityInfo.packageName;
        userHomeLauncherClass = activityInfo.name;
        userHomeLauncherName = activityInfo.loadLabel(pm).toString();
return new JSONArray(userHomeLauncherName);
    }

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

@@ -835,24 +835,85 @@
 ay list() %7B%0A
+        PackageManager packageMgr = ctx.getPackageManager();%0A
         Inte
@@ -915,21 +915,21 @@
  Intent 
-homei
+mainI
 ntent = 
@@ -957,16 +957,22 @@
 ION_MAIN
+, null
 );%0A     
@@ -966,37 +966,37 @@
  null);%0A        
-homei
+mainI
 ntent.addCategor
@@ -1017,12 +1017,16 @@
 ORY_
-HOME
+LAUNCHER
 );%0A 
@@ -1036,193 +1036,149 @@
     
-homeintent.addCatego
+List%3CResolveInfo%3E resovleInfos = packageMgr.que
 ry
-(
 Intent
-.CATEGORY_DEFAULT);// seems not needed here since This is a synonym for%0A        // including%0A        // the CATEGORY_DEFAULT in your supplied Intent per doc
+Activities(mainIntent, 0);%0A%0A        ArrayList%3CString%3E list  = new ArrayList%3CString%3E();
 %0A   
@@ -1186,14 +1186,14 @@
     
-this.r
+for (R
 esol
@@ -1203,110 +1203,64 @@
 nfo 
-= pm.
 resolve
-Activity(homeintent, PackageManager.MATCH_DEFAULT_ONLY);%0A        ActivityInfo activityInfo
+ : resovleInfos) %7B%0A            String packageName
  = r
@@ -1265,20 +1265,16 @@
  resolve
-Info
 .activit
@@ -1282,57 +1282,121 @@
 Info
-;%0A        userHomeLauncherPackage = activityInfo.
+.packageName;%0A            String strAppName  = resolve.loadLabel(packageManager).toString()%0A            list.add(
 pack
@@ -1394,32 +1394,33 @@
 .add(packageName
+)
 ;%0A        userHo
@@ -1417,49 +1417,32 @@
     
+ 
-userHomeLauncherClass = activityInfo.n
+   list.add(strAppN
 ame
+)
 ;%0A  
@@ -1451,70 +1451,106 @@
     
-userHomeLauncherName = activityInfo.loadLabel(pm).toString();%0A
+%7D%0A        List%3CString%3E ulist = new ArrayList%3CString%3E(new HashSet%3CString%3E(list));%0A        %0A        
 retu
@@ -1571,27 +1571,12 @@
 ay(u
-serHomeLauncherName
+list
 );%0A 

Update Apps.java