Example usage for org.json JSONArray getBoolean

List of usage examples for org.json JSONArray getBoolean

Introduction

In this page you can find the example usage for org.json JSONArray getBoolean.

Prototype

public boolean getBoolean(int index) throws JSONException 

Source Link

Document

Get the boolean value associated with an index.

Usage

From source file:com.polychrom.cordova.AccountManagerPlugin.java

@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
    if (manager == null) {
        manager = AccountManager.get(cordova.getActivity());
    }/*from   ww w . j  a  va  2  s .  c  om*/

    try {
        if ("getAccountsByType".equals(action)) {
            Account[] account_list = manager.getAccountsByType(args.isNull(0) ? null : args.getString(0));
            JSONArray result = new JSONArray();

            for (Account account : account_list) {
                Integer index = indexForAccount(account);
                accounts.put(index, account);

                JSONObject account_object = new JSONObject();
                account_object.put("_index", (int) index);
                account_object.put("name", account.name);
                account_object.put("type", account.type);
                result.put(account_object);
            }

            callbackContext.success(result);
            return true;
        } else if ("addAccountExplicitly".equals(action)) {
            if (args.isNull(0) || args.getString(0).length() == 0) {
                callbackContext.error("accountType can not be null or empty");
                return true;
            } else if (args.isNull(1) || args.getString(1).length() == 0) {
                callbackContext.error("username can not be null or empty");
                return true;
            } else if (args.isNull(2) || args.getString(2).length() == 0) {
                callbackContext.error("password can not be null or empty");
                return true;
            }

            Account account = new Account(args.getString(1), args.getString(0));
            Integer index = indexForAccount(account);

            Bundle userdata = new Bundle();
            if (!args.isNull(3)) {
                JSONObject userdata_json = args.getJSONObject(3);
                if (userdata_json != null) {
                    Iterator<String> keys = userdata_json.keys();
                    while (keys.hasNext()) {
                        String key = keys.next();
                        userdata.putString(key, userdata_json.getString(key));
                    }
                }
            }

            if (false == manager.addAccountExplicitly(account, args.getString(2), userdata)) {
                callbackContext.error("Account with username already exists!");
                return true;
            }

            accounts.put(index, account);

            JSONObject result = new JSONObject();
            result.put("_index", (int) index);
            result.put("name", account.name);
            result.put("type", account.type);

            callbackContext.success(result);
            return true;
        } else if ("updateCredentials".equals(action)) {
            if (args.isNull(0)) {
                callbackContext.error("account can not be null");
                return true;
            }

            Account account = accounts.get(args.getInt(0));
            if (account == null) {
                callbackContext.error("Invalid account");
                return true;
            }

            callbackContext.error("Not yet implemented");
            return true;
        } else if ("clearPassword".equals(action)) {
            if (args.isNull(0)) {
                callbackContext.error("account can not be null");
                return true;
            }

            Account account = accounts.get(args.getInt(0));
            if (account == null) {
                callbackContext.error("Invalid account");
                return true;
            }

            manager.clearPassword(account);
            callbackContext.success();
            return true;
        } else if ("removeAccount".equals(action)) {
            if (args.isNull(0)) {
                callbackContext.error("account can not be null");
                return true;
            }

            int index = args.getInt(0);
            Account account = accounts.get(index);
            if (account == null) {
                callbackContext.error("Invalid account");
                return true;
            }

            // TODO: Add support for AccountManager (callback)
            AccountManagerFuture<Boolean> future = manager.removeAccount(account, null, null);
            try {
                if (future.getResult() == true) {
                    accounts.remove(index);
                    callbackContext.success();
                } else {
                    callbackContext.error("Failed to remove account");
                }
            } catch (OperationCanceledException e) {
                callbackContext.error("Operation canceled: " + e.getLocalizedMessage());
            } catch (AuthenticatorException e) {
                callbackContext.error("Authenticator error: " + e.getLocalizedMessage());
            } catch (IOException e) {
                callbackContext.error("IO error: " + e.getLocalizedMessage());
            }

            return true;
        } else if ("setAuthToken".equals(action)) {
            if (args.isNull(0)) {
                callbackContext.error("account can not be null");
                return true;
            } else if (args.isNull(1) || args.getString(1).length() == 0) {
                callbackContext.error("authTokenType can not be null or empty");
                return true;
            } else if (args.isNull(2) || args.getString(2).length() == 0) {
                callbackContext.error("authToken can not be null or empty");
                return true;
            }

            Account account = accounts.get(args.getInt(0));
            if (account == null) {
                callbackContext.error("Invalid account");
                return true;
            }

            manager.setAuthToken(account, args.getString(1), args.getString(2));
            callbackContext.success();
            return true;
        } else if ("peekAuthToken".equals(action)) {
            if (args.isNull(0)) {
                callbackContext.error("account can not be null");
                return true;
            } else if (args.isNull(1) || args.getString(1).length() == 0) {
                callbackContext.error("authTokenType can not be null or empty");
                return true;
            }

            Account account = accounts.get(args.getInt(0));
            if (account == null) {
                callbackContext.error("Invalid account");
                return true;
            }

            JSONObject result = new JSONObject();
            result.put("value", manager.peekAuthToken(account, args.getString(1)));
            callbackContext.success(result);
            return true;
        } else if ("getAuthToken".equals(action)) {
            if (args.isNull(0)) {
                callbackContext.error("account can not be null");
                return true;
            } else if (args.isNull(1) || args.getString(1).length() == 0) {
                callbackContext.error("authTokenType can not be null or empty");
                return true;
            } else if (args.isNull(3)) {
                callbackContext.error("notifyAuthFailure can not be null");
                return true;
            }

            Account account = accounts.get(args.getInt(0));
            if (account == null) {
                callbackContext.error("Invalid account");
                return true;
            }

            Bundle options = new Bundle();
            // TODO: Options support (will be relevent when we support AccountManagers)

            // TODO: AccountManager support
            AccountManagerFuture<Bundle> future = manager.getAuthToken(account, args.getString(1), options,
                    args.getBoolean(3), null, null);
            try {
                JSONObject result = new JSONObject();
                result.put("value", future.getResult().getString(AccountManager.KEY_AUTHTOKEN));
                callbackContext.success(result);
            } catch (OperationCanceledException e) {
                callbackContext.error("Operation canceled: " + e.getLocalizedMessage());
            } catch (AuthenticatorException e) {
                callbackContext.error("Authenticator error: " + e.getLocalizedMessage());
            } catch (IOException e) {
                callbackContext.error("IO error: " + e.getLocalizedMessage());
            }

            return true;
        } else if ("setPassword".equals(action)) {
            if (args.isNull(0)) {
                callbackContext.error("account can not be null");
                return true;
            } else if (args.isNull(1) || args.getString(1).length() == 0) {
                callbackContext.error("password can not be null or empty");
                return true;
            }

            Account account = accounts.get(args.getInt(0));
            if (account == null) {
                callbackContext.error("Invalid account");
                return true;
            }

            manager.setPassword(account, args.getString(1));
            callbackContext.success();
            return true;
        } else if ("getPassword".equals(action)) {
            if (args.isNull(0)) {
                callbackContext.error("account can not be null");
                return true;
            }

            Account account = accounts.get(args.getInt(0));
            if (account == null) {
                callbackContext.error("Invalid account");
                return true;
            }

            JSONObject result = new JSONObject();
            result.put("value", manager.getPassword(account));
            callbackContext.success(result);
            return true;
        } else if ("setUserData".equals(action)) {
            if (args.isNull(0)) {
                callbackContext.error("account can not be null");
                return true;
            } else if (args.isNull(1) || args.getString(1).length() == 0) {
                callbackContext.error("key can not be null or empty");
                return true;
            } else if (args.isNull(2) || args.getString(2).length() == 0) {
                callbackContext.error("value can not be null or empty");
                return true;
            }

            Account account = accounts.get(args.getInt(0));
            if (account == null) {
                callbackContext.error("Invalid account");
                return true;
            }

            manager.setUserData(account, args.getString(1), args.getString(2));
            callbackContext.success();
            return true;
        } else if ("getUserData".equals(action)) {
            if (args.isNull(0)) {
                callbackContext.error("account can not be null");
                return true;
            } else if (args.isNull(1) || args.getString(1).length() == 0) {
                callbackContext.error("key can not be null or empty");
                return true;
            }

            Account account = accounts.get(args.getInt(0));
            if (account == null) {
                callbackContext.error("Invalid account");
                return true;
            }

            JSONObject result = new JSONObject();
            result.put("value", manager.getUserData(account, args.getString(1)));
            callbackContext.success(result);
            return true;
        }
    } catch (SecurityException e) {
        callbackContext.error("Access denied");
        return true;
    }

    return false;
}

From source file:com.phonegap.NetworkManager.java

/**
 * Executes the request and returns PluginResult.
 * /*w w w.j a va 2  s. c  o m*/
 * @param action       The action to execute.
 * @param args          JSONArry of arguments for the plugin.
 * @param callbackId   The callback id used when calling back into JavaScript.
 * @return             A PluginResult object with a status and message.
 */
public PluginResult execute(String action, JSONArray args, String callbackId) {
    PluginResult.Status status = PluginResult.Status.OK;
    String result = "";
    try {
        if (action.equals("isAvailable")) {
            boolean b = this.isAvailable();
            return new PluginResult(status, b);
        } else if (action.equals("isWifiActive")) {
            boolean b = this.isWifiActive();
            return new PluginResult(status, b);
        } else if (action.equals("isReachable")) {
            int i = this.isReachable(args.getString(0), args.getBoolean(1));
            return new PluginResult(status, i);
        }
        return new PluginResult(status, result);
    } catch (JSONException e) {
        return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
    }
}

From source file:com.cranberrygame.cordova.plugin.ad.admob.Plugin.java

private void setUp(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
    //Activity activity=cordova.getActivity();
    //webView/*from  w  w  w. j  a v  a  2 s  . co m*/
    //args.length()
    //args.getString(0)
    //args.getString(1)
    //args.getInt(0)
    //args.getInt(1)
    //args.getBoolean(0)
    //args.getBoolean(1)
    //JSONObject json = args.optJSONObject(0);
    //json.optString("bannerAdUnit")
    //json.optString("interstitialAdUnit")
    //JSONObject inJson = json.optJSONObject("inJson");
    //final String bannerAdUnit = args.getString(0);
    //final String interstitialAdUnit = args.getString(1);            
    //final boolean isOverlap = args.getBoolean(2);            
    //final boolean isTest = args.getBoolean(3);
    //final String[] zoneIds = new String[args.getJSONArray(4).length()];
    //for (int i = 0; i < args.getJSONArray(4).length(); i++) {
    //   zoneIds[i] = args.getJSONArray(4).getString(i);
    //}         
    //Log.d(LOG_TAG, String.format("%s", bannerAdUnit));         
    //Log.d(LOG_TAG, String.format("%s", interstitialAdUnit));
    //Log.d(LOG_TAG, String.format("%b", isOverlap));
    //Log.d(LOG_TAG, String.format("%b", isTest));      
    final String bannerAdUnit = args.getString(0);
    final String interstitialAdUnit = args.getString(1);
    final boolean isOverlap = args.getBoolean(2);
    final boolean isTest = args.getBoolean(3);
    Log.d(LOG_TAG, String.format("%s", bannerAdUnit));
    Log.d(LOG_TAG, String.format("%s", interstitialAdUnit));
    Log.d(LOG_TAG, String.format("%b", isOverlap));
    Log.d(LOG_TAG, String.format("%b", isTest));

    callbackContextKeepCallback = callbackContext;

    if (isOverlap)
        pluginDelegate = new AdmobOverlap(this);
    else
        pluginDelegate = new AdmobSplit(this);

    cordova.getActivity().runOnUiThread(new Runnable() {
        @Override
        public void run() {
            _setUp(bannerAdUnit, interstitialAdUnit, isOverlap, isTest);
        }
    });
}

From source file:com.sesamtv.cordova.chromecast.ChromeCast.java

private boolean executeActions(final String action, final JSONArray args, final CallbackContext callbackContext)
        throws JSONException {
    switch (Actions.valueOf(action)) {
    case sendMessage:
        cordova.getThreadPool().execute(new Runnable() {
            public void run() {
                try {
                    String channelName = args.getString(0);
                    String msg = args.getString(1);
                    sendMessage(channelName, msg, callbackContext);
                } catch (Exception e) {
                    callbackContext.error(e.getMessage());
                }//  w w  w. j  av  a 2  s .c om
            }
        });
        break;

    case loadMedia:
        loadMediaAction(args.getJSONObject(0), callbackContext);
        break;
    case pauseMedia:
        pauseMediaAction(callbackContext);
        break;
    case stopMedia:
        stopMediaAction(callbackContext, args);
        break;
    case playMedia:
        playMediaAction(callbackContext);
        break;
    case seekMediaBy:
        try {
            seekMediaByAction(args.getLong(0));
            callbackContext.success();
        } catch (IOException e) {
            callbackContext.error(e.getMessage());
        }
        break;
    case seekMedia:
        try {
            seekMediaAction(args.getLong(0));
            callbackContext.success();
        } catch (IOException e) {
            callbackContext.error(e.getMessage());
        }
        break;
    case setDeviceVolume:
        final double vol = args.getDouble(0);
        Log.d(TAG, "setVolume " + vol);
        cordova.getThreadPool().execute(new Runnable() {
            public void run() {
                setVolumeAction(vol, callbackContext);
            }
        });
        break;
    case setDeviceVolumeBy:
        cordova.getThreadPool().execute(new Runnable() {
            public void run() {
                try {
                    setVolumeByAction(args.getDouble(0), callbackContext);
                } catch (JSONException e) {
                    callbackContext.error(e.getMessage());
                }
            }
        });
        break;
    case setMuted:
        final boolean muted = args.getBoolean(0);
        Log.d(TAG, "setMuted " + muted);
        cordova.getThreadPool().execute(new Runnable() {
            public void run() {
                setDeviceMutedAction(muted, callbackContext);
            }
        });
        break;
    case toggleMuted:
        Log.d(TAG, "toggleMuted ");
        cordova.getThreadPool().execute(new Runnable() {
            public void run() {
                toggleDeviceMutedAction(callbackContext);
            }
        });
        break;
    case getMediaStatus:
        Log.d(TAG, "getMediaStatus");
        callbackContext.sendPluginResult(getMediaStatus());
        break;
    case stopApplication:
        try {
            Log.d(TAG, "stopCast");
            stopApplication();
            callbackContext.success();
        } catch (Exception e) {
            callbackContext.error("stop cast failed :" + e.getMessage());
            return false;
        }
        break;

    default:
        callbackContext.error("Invalid action: " + action);

    }
    return true;
}