Example usage for org.apache.cordova CordovaArgs isNull

List of usage examples for org.apache.cordova CordovaArgs isNull

Introduction

In this page you can find the example usage for org.apache.cordova CordovaArgs isNull.

Prototype

public boolean isNull(int index) 

Source Link

Usage

From source file:com.google.cordova.ChromeStorage.java

License:Open Source License

private JSONObject getStoredValuesForKeys(CordovaArgs args, boolean useDefaultValues) {
    JSONObject ret = new JSONObject();
    try {/*from ww  w.  j a v a 2s . c o  m*/
        boolean sync = args.getBoolean(0);
        JSONObject jsonObject = (JSONObject) args.optJSONObject(1);
        JSONArray jsonArray = args.optJSONArray(1);
        boolean isNull = args.isNull(1);
        List<String> keys = new ArrayList<String>();

        if (jsonObject != null) {
            keys = JSONUtils.toStringList(jsonObject.names());
            // Ensure default values of keys are maintained
            if (useDefaultValues) {
                ret = jsonObject;
            }
        } else if (jsonArray != null) {
            keys = JSONUtils.toStringList(jsonArray);
        } else if (isNull) {
            keys = null;
        }

        if (keys != null && keys.isEmpty()) {
            ret = new JSONObject();
        } else {
            JSONObject storage = getStorage(sync);

            if (keys == null) {
                // return the whole storage if the key given is null
                ret = storage;
            } else {
                // return the storage for the keys specified
                for (String key : keys) {
                    if (storage.has(key)) {
                        Object value = storage.get(key);
                        ret.put(key, value);
                    }
                }
            }
        }
    } catch (Exception e) {
        Log.e(LOG_TAG, "Could not retrieve storage", e);
        ret = null;
    }

    return ret;
}

From source file:com.google.cordova.ChromeStorage.java

License:Open Source License

private void remove(final CordovaArgs args, final CallbackContext callbackContext) {
    executorService.execute(new Runnable() {
        @Override//  ww w . j  a  va2 s. co  m
        public void run() {
            try {
                boolean sync = args.getBoolean(0);
                JSONObject jsonObject = (JSONObject) args.optJSONObject(1);
                JSONArray jsonArray = args.optJSONArray(1);
                boolean isNull = args.isNull(1);
                List<String> keys = new ArrayList<String>();
                JSONObject oldValues = new JSONObject();

                if (jsonObject != null) {
                    keys = JSONUtils.toStringList(jsonObject.names());
                } else if (jsonArray != null) {
                    keys = JSONUtils.toStringList(jsonArray);
                } else if (isNull) {
                    keys = null;
                }

                if (keys != null && !keys.isEmpty()) {
                    JSONObject storage = getStorage(sync);
                    for (String key : keys) {
                        Object oldValue = storage.opt(key);
                        if (oldValue != null) {
                            oldValues.put(key, oldValue);
                        }
                        storage.remove(key);
                    }
                    setStorage(sync, storage);
                }
                callbackContext.success(oldValues);
            } catch (Exception e) {
                Log.e(LOG_TAG, "Could not update storage", e);
                callbackContext.error("Could not update storage");
            }
        }
    });
}

From source file:org.chromium.ChromeStorage.java

License:Open Source License

private JSONObject getStoredValuesForKeys(CordovaArgs args, boolean useDefaultValues) {
    JSONObject ret = new JSONObject();
    try {//from ww w  .  j  a  v  a  2s  . co  m
        String namespace = args.getString(0);
        JSONObject jsonObject = (JSONObject) args.optJSONObject(1);
        JSONArray jsonArray = args.optJSONArray(1);
        boolean isNull = args.isNull(1);
        List<String> keys = new ArrayList<String>();

        if (jsonObject != null) {
            keys = toStringList(jsonObject.names());
            // Ensure default values of keys are maintained
            if (useDefaultValues) {
                ret = jsonObject;
            }
        } else if (jsonArray != null) {
            keys = toStringList(jsonArray);
        } else if (isNull) {
            keys = null;
        }

        if (keys != null && keys.isEmpty()) {
            ret = new JSONObject();
        } else {
            JSONObject storage = getStorage(namespace);

            if (keys == null) {
                // return the whole storage if the key given is null
                ret = storage;
            } else {
                // return the storage for the keys specified
                for (String key : keys) {
                    if (storage.has(key)) {
                        Object value = storage.get(key);
                        ret.put(key, value);
                    }
                }
            }
        }
    } catch (JSONException e) {
        Log.e(LOG_TAG, "Storage is corrupted!", e);
        ret = null;
    } catch (IOException e) {
        Log.e(LOG_TAG, "Could not retrieve storage", e);
        ret = null;
    }

    return ret;
}

From source file:org.chromium.ChromeStorage.java

License:Open Source License

private void remove(final CordovaArgs args, final CallbackContext callbackContext) {
    cordova.getThreadPool().execute(new Runnable() {
        @Override//from w ww .  ja v a 2 s  .c om
        public void run() {
            try {
                String namespace = args.getString(0);
                JSONObject jsonObject = (JSONObject) args.optJSONObject(1);
                JSONArray jsonArray = args.optJSONArray(1);
                boolean isNull = args.isNull(1);
                List<String> keys = new ArrayList<String>();
                JSONObject oldValues = new JSONObject();

                if (jsonObject != null) {
                    keys = toStringList(jsonObject.names());
                } else if (jsonArray != null) {
                    keys = toStringList(jsonArray);
                } else if (isNull) {
                    keys = null;
                }

                if (keys != null && !keys.isEmpty()) {
                    // Use a lock to serialize updates to storage, to
                    // ensure data is written consistently with
                    // concurrent writers
                    writeLock.lock();

                    try {
                        JSONObject storage = getStorage(namespace);
                        for (String key : keys) {
                            Object oldValue = storage.opt(key);
                            if (oldValue != null) {
                                oldValues.put(key, oldValue);
                            }
                            storage.remove(key);
                        }
                        setStorage(namespace, storage);
                    } finally {
                        writeLock.unlock();
                    }
                }
                callbackContext.success(oldValues);
            } catch (Exception e) {
                Log.e(LOG_TAG, "Could not update storage", e);
                callbackContext.error("Could not update storage");
            }
        }
    });
}