package com.naturefeng.cordova.plugin.TrafficStat;

import android.net.TrafficStats;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


/**
 * This class gets data usage from mobile.
 */
public class TrafficStat extends CordovaPlugin {
    
    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
        if (action.equals("GSMtx")) { 
            this.GSMtx(callbackContext);
            return true;
        }
        else if(action.equals("GSMrx")) {
            this.GSMrx(callbackContext);
            return true;
        }
        else if(action.equals("Totaltx")){
            this.Totaltx(callbackContext);
            return true;
        }
        else if(action.equals("Totalrx")) {
            this.Totalrx(callbackContext);
            return true;
        }
        return false;
    }

    /*
     * GSM total transmit bytes 
     */
    private void GSMtx(CallbackContext callbackContext) {
        JSONObject mStartTX = new JSONObject();
        long TX = 0;
        try{
            TX = TrafficStats.getMobileTxBytes();
            if(TX != TrafficStats.UNSUPPORTED){
                mStartTX.put("TXBytes", TX);
                callbackContext.success(mStartTX);
            } else {
                callbackContext.error("Stats Not Possible");
            }
        }
        catch(JSONException ex)
        {
            
            callbackContext.error("Exception");
        }
    }
    
    /*
     * GSM Rx total bytes
     */
    private void GSMrx(CallbackContext callbackContext) {
        JSONObject mStartRX = new JSONObject();
        long RX = 0;
        try{
            RX = TrafficStats.getMobileRxBytes();
            if(RX != TrafficStats.UNSUPPORTED){
                mStartRX.put("RXBytes", RX);
                callbackContext.success(mStartRX);
            } else {
                callbackContext.error("Stats Not Possible");
            }
        }
        catch(JSONException ex)
        {
            callbackContext.error("Exception");
        }
    }

    
    /*
     * Total Rx total bytes
     */
    private void Totaltx(CallbackContext callbackContext) {
        JSONObject mStartTX = new JSONObject();
        long TX = 0;
        try{
            TX = TrafficStats.getTotalTxBytes();
            if(TX != TrafficStats.UNSUPPORTED){
                mStartTX.put("TTXBytes", TX);
                callbackContext.success(mStartTX);
            } else {
                callbackContext.error("Stats Not Possible");
            }
        }
        catch(JSONException ex)
        {
            callbackContext.error("Exception");
        }
    }
    
    /*
     * Total tx total bytes
     */
    private void Totalrx(CallbackContext callbackContext) {
        JSONObject mStartRX = new JSONObject();
        long RX = 0;
        try{
            RX = TrafficStats.getTotalRxBytes();
            if(RX != TrafficStats.UNSUPPORTED){
                mStartRX.put("TRXBytes", RX);
                callbackContext.success(mStartRX);
            } else {
                callbackContext.error("Stats Not Possible");
            }
        }
        catch(JSONException ex)
        {
            callbackContext.error("Exception");
        }
    }
}
@@ -479,24 +479,51 @@
 Exception %7B%0A
+        Log.i(%22test%22,%221%22);%0A
         if (
@@ -2911,32 +2911,59 @@
 lbackContext) %7B%0A
+        Log.i(%22test%22,%222%22);%0A
         JSONObje
@@ -2986,32 +2986,59 @@
 w JSONObject();%0A
+        Log.i(%22test%22,%223%22);%0A
         long RX 
@@ -3047,32 +3047,63 @@
 0;%0A        try%7B%0A
+            Log.i(%22test%22,%224%22);%0A
             RX =
@@ -3127,32 +3127,63 @@
 TotalRxBytes();%0A
+            Log.i(%22test%22,%225%22);%0A
             if(R
@@ -3206,32 +3206,67 @@
 s.UNSUPPORTED)%7B%0A
+                Log.i(%22test%22,%226%22);%0A
                 
@@ -3287,32 +3287,67 @@
 TRXBytes%22, RX);%0A
+                Log.i(%22test%22,%227%22);%0A
                 
@@ -3373,32 +3373,32 @@
 cess(mStartRX);%0A
-
             %7D el
@@ -3394,32 +3394,67 @@
        %7D else %7B%0A
+                Log.i(%22test%22,%228%22);%0A
                 

fix bug