package com.vliesaputra.cordova.plugins;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.PluginResult;

import org.json.JSONArray;

import android.content.Context;
import android.telephony.TelephonyManager;
import android.accounts.Account;
import android.accounts.AccountManager;

public class DeviceInformation extends CordovaPlugin {

    private String checkValue(String str) {
        if ((str == null) || (str.length() == 0)) {
            return "\"TM.ERROR\"";
        }

        return "\"" + str + "\"";
    }

    private String getAccount(AccountManager am) {
        String str = "";

        if (am != null) {
            Account[] accounts = am.getAccounts();

            for (int i = 0; i < accounts.length; i++) {
                if (str.length() > 0) {
                    str += ",";
                }

                str += "\"account" + i + "Name\": " + checkValue(accounts[i].name) + ","
                        + "\"account" + i + "Type\": " + checkValue(accounts[i].type);
            }
        }

        return str;
    }

    private String getTelephone(TelephonyManager tm) {
        String str = "";

        if (tm != null) {
            str = "\"deviceID\": " + checkValue(tm.getDeviceId()) + ","
                    + "\"phoneNo\": " + checkValue(tm.getLine1Number()) + ","
                    + "\"netCountry\": " + checkValue(tm.getNetworkCountryIso()) + ","
                    + "\"netName\": " + checkValue(tm.getNetworkOperatorName()) + ","
                    + "\"simNo\": " + checkValue(tm.getSimSerialNumber()) + ","
                    + "\"simCountry\": " + checkValue(tm.getSimCountryIso()) + ","
                    + "\"simName\": " + checkValue(tm.getSimOperatorName());
        }

        return str;
    }

    private String getDetails(TelephonyManager tm, AccountManager am) {
        String acc = getAccount(am);
        String tel = getTelephone(tm);

        String str = "";
        if ((acc.length() != 0) || (tel.length() != 0)) {
            str += "{" + acc;

            if (str.length() > 1) {
                str += ",";
            }

            str += tel + "}";
        }

        return str;
    }

    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {
        try {
            if (action.equals("get")) {
                TelephonyManager tm = (TelephonyManager) this.cordova.getActivity().getSystemService(Context.TELEPHONY_SERVICE);
                AccountManager am = AccountManager.get(this.cordova.getActivity());

                String result = getDetails(tm,am);
                if (result != null) {
                    callbackContext.success(result);
                    return true;
                }
            }
            callbackContext.error("Invalid action");
            return false;
        } catch (Exception e) {
            String s = "Exception: " + e.getMessage();

            System.err.println(s);
            callbackContext.error(s);

            return false;
        }
    }
}

@@ -727,24 +727,135 @@
 ccounts();%0A%0A
+            if (str.length() %3E 0) %7B%0A                str += %22,%22;%0A            %7D%0A            str += %22accounts:%5B%22;%0A
             
@@ -914,36 +914,25 @@
         if (
-str.length()
+i
  %3E 0) %7B%0A    
@@ -969,33 +969,32 @@
               %7D%0A
-%0A
                 
@@ -1005,30 +1005,16 @@
 += %22
+%7B
 %5C%22
-account%22 + i + %22Name
+email
 %5C%22: 
@@ -1085,25 +1085,9 @@
  %22%5C%22
-account%22 + i + %22T
+t
 ype%5C
@@ -1121,16 +1121,22 @@
 i%5D.type)
+ + %22%7D%22
 ;%0A      
@@ -1135,32 +1135,55 @@
 ;%0A            %7D%0A
+            str += %22%5D%22%0A
         %7D%0A%0A     

Convert accounts to JSON array of objects