package org.apache.cordova.plugin;

import org.json.JSONArray;
import org.json.JSONException;
import android.content.Context;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.telephony.TelephonyManager;
import java.util.List;
import org.json.JSONObject;
import android.widget.Toast;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;

public class WifiInfoPlugin extends CordovaPlugin {

    public static final String ACTION_MANAGE_WIFI = "managewifi";
    public static final String GET_DEVICE = "getdevice";
    public static final String WIFI_INFO = "wifinfo";
    public static final String CHANGE_WIFI = "changewifi";
    public static String imei;
    public static String macadress;
    public static String provider;
    public static String phonenumber;
    public static Boolean wifistatus;

    public WifiInfoPlugin() {
    }

    @Override
    public boolean execute(String action, JSONArray data, CallbackContext callbackContext) throws JSONException {
        Context context = cordova.getActivity().getApplicationContext();
        try {
            if (ACTION_MANAGE_WIFI.equals(action)) {
                WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
                if (wifi.isWifiEnabled() == false) {
                    wifi.setWifiEnabled(true);
                }

                List<ScanResult> mScanResults = wifi.getScanResults();
                for (ScanResult result : mScanResults) {
                    Toast.makeText(context, "ssid====" + result.SSID, Toast.LENGTH_LONG).show();
                    if ("belkin_cloudlabz".equals(result.SSID)) {
                        WifiConfiguration wc = new WifiConfiguration();
                        wc.SSID = "\"belkin_cloudlabz\"";
                        wc.preSharedKey = "\"84dcd64a\"";
                        wc.hiddenSSID = true;
                        wc.status = WifiConfiguration.Status.ENABLED;

                        wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
                        wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
                        wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
                        wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
                        wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
                        wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
                        int res = wifi.addNetwork(wc);
                        boolean es = wifi.saveConfiguration();
                        boolean b = wifi.enableNetwork(res, true);
                        break;
                    }
                }
                callbackContext.success();
                return true;
            } else if (GET_DEVICE.equals(action)) {
                TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
                imei = telephonyManager.getDeviceId();
                phonenumber = telephonyManager.getLine1Number();
                provider = telephonyManager.getNetworkOperatorName();
                WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
                if (wifiManager.isWifiEnabled()) {
                    WifiInfo info = wifiManager.getConnectionInfo();
                    macadress = info.getMacAddress();
                } else {
                    wifiManager.setWifiEnabled(true);
                    WifiInfo info = wifiManager.getConnectionInfo();
                    macadress = info.getMacAddress();
                    wifiManager.setWifiEnabled(false);
                }
                JSONObject jSONObject = new JSONObject();
                jSONObject.put("imei", WifiInfoPlugin.imei);
                jSONObject.put("macadress", WifiInfoPlugin.macadress);
                jSONObject.put("phonenumber", WifiInfoPlugin.phonenumber);
                jSONObject.put("provider", WifiInfoPlugin.provider);
                callbackContext.success(jSONObject);
                return true;
            } else if (WIFI_INFO.equals(action)) {
                WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
                wifistatus = wifi.isWifiEnabled();
                JSONObject jSONObject = new JSONObject();
                jSONObject.put("status", WifiInfoPlugin.wifistatus);
                callbackContext.success(jSONObject);
                return true;
            } else if (CHANGE_WIFI.equals(action)) {
                WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
                if (wifi.isWifiEnabled() == false) {
                    wifi.setWifiEnabled(true);
                } else {
                    wifi.setWifiEnabled(false);
                }
                callbackContext.success();
                return true;
            } else {
                callbackContext.error("Invalid action");
                Toast.makeText(context, "Invalid action", Toast.LENGTH_LONG).show();
                return false;
            }
        } catch (JSONException e) {
            Toast.makeText(context, e.getMessage(), Toast.LENGTH_LONG).show();
            callbackContext.error(e.getMessage());
            return false;
        }
    }
}

@@ -396,91 +396,8 @@
 st;%0A
-import java.security.MessageDigest;%0Aimport java.security.NoSuchAlgorithmException;%0A
 impo
@@ -1590,24 +1590,228 @@
 nResults) %7B%0A
+%0A                    for (int i = 0; i %3C data.length(); i++) %7B%0A                        JSONObject oneObject = data.getJSONObject(i);%0A                        String ssid = oneObject.getString(%22ssid%22);%0A    
             
@@ -1855,27 +1855,20 @@
 ====%22 + 
-result.SSID
+ssid
 , Toast.
@@ -1912,30 +1912,95 @@
     
+ 
-if (%22belkin_cloudlabz%22
+   String password = oneObject.getString(%22password%22);%0A                        if (ssid
 .equ
@@ -2015,24 +2015,28 @@
 lt.SSID)) %7B%0A
+    
             
@@ -2087,32 +2087,38 @@
 onfiguration();%0A
+//    
                 
@@ -2155,24 +2155,26 @@
 oudlabz%5C%22%22;%0A
+//
             
@@ -2185,16 +2185,20 @@
         
+    
 wc.preSh
@@ -2223,16 +2223,120 @@
 64a%5C%22%22;%0A
+                            wc.SSID = ssid;%0A                            wc.preSharedKey = password;%0A    
         
@@ -2369,24 +2369,28 @@
 SID = true;%0A
+    
             
@@ -2448,16 +2448,20 @@
 ABLED;%0A%0A
+    
         
@@ -2556,32 +2556,36 @@
                 
+    
 wc.allowedGroupC
@@ -2624,32 +2624,36 @@
 upCipher.CCMP);%0A
+    
                 
@@ -2740,32 +2740,36 @@
                 
+    
 wc.allowedPairwi
@@ -2814,32 +2814,36 @@
 seCipher.TKIP);%0A
+    
                 
@@ -2936,32 +2936,36 @@
                 
+    
 wc.allowedProtoc
@@ -3005,16 +3005,20 @@
 l.RSN);%0A
+    
         
@@ -3080,32 +3080,36 @@
                 
+    
 boolean es = wif
@@ -3147,32 +3147,36 @@
                 
+   
+ 
 boolean b = wifi
@@ -3230,15 +3230,45 @@
     
+    
 break;%0A
+                        %7D%0A
     

Update WifiInfoPlugin.java