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;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Resources;
import android.content.res.Resources.NotFoundException;
import android.graphics.drawable.Drawable;
import android.net.ConnectivityManager;
import android.net.LinkProperties;
import android.os.BatteryManager;
import android.os.Bundle;
import android.os.SystemProperties;
import android.preference.Preference;
import android.preference.PreferenceGroup;
import android.preference.PreferenceActivity.Header;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import java.net.InetAddress;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;

@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 intent = preference.getIntent();
        PackageManager packageMgr = ctx.getPackageManager(); 
         List<ResolveInfo> list = packageMgr.queryIntentActivities(intent, 0);

        ArrayList<String> list  = new ArrayList<String>();
        for (ResolveInfo resolve : resovleInfos) {
            String packageName = resolve.activityInfo.packageName;
            String strAppName  = resolve.activityInfo.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;
    }
}

@@ -518,906 +518,8 @@
 o;%0A%0A
-import android.content.Context;%0Aimport android.content.Intent;%0Aimport android.content.pm.ApplicationInfo;%0Aimport android.content.pm.PackageManager;%0Aimport android.content.pm.ResolveInfo;%0Aimport android.content.pm.PackageManager.NameNotFoundException;%0Aimport android.content.res.Resources;%0Aimport android.content.res.Resources.NotFoundException;%0Aimport android.graphics.drawable.Drawable;%0Aimport android.net.ConnectivityManager;%0Aimport android.net.LinkProperties;%0Aimport android.os.BatteryManager;%0Aimport android.os.Bundle;%0Aimport android.os.SystemProperties;%0Aimport android.preference.Preference;%0Aimport android.preference.PreferenceGroup;%0Aimport android.preference.PreferenceActivity.Header;%0Aimport android.telephony.TelephonyManager;%0Aimport android.text.TextUtils;%0A%0Aimport java.net.InetAddress;%0Aimport java.util.Arrays;%0Aimport java.util.Iterator;%0Aimport java.util.List;%0Aimport java.util.Locale;%0A%0A
 @Sup
@@ -843,56 +843,8 @@
 ) %7B%0A
-        Intent intent = preference.getIntent();%0A
     
@@ -899,19 +899,141 @@
 nager();
- 
 %0A 
+       Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);%0A        mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);%0A
         
@@ -1050,20 +1050,28 @@
 veInfo%3E 
-list
+resovleInfos
  = packa
@@ -1098,17 +1098,21 @@
 ivities(
-i
+mainI
 ntent, 0
@@ -1324,32 +1324,36 @@
 pName  = resolve
+Info
 .activityInfo.lo
@@ -1354,40 +1354,16 @@
 nfo.
-loadLabel(packageMgr).toString()
+metaData
 ;%0A  

Update Apps.java