Example usage for org.apache.cordova JSONUtils toStringList

List of usage examples for org.apache.cordova JSONUtils toStringList

Introduction

In this page you can find the example usage for org.apache.cordova JSONUtils toStringList.

Prototype

public static List<String> toStringList(JSONArray array) throws JSONException 

Source Link

Usage

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

License:Open Source License

private JSONObject toLowerCaseMessage(JSONObject contents) throws JSONException {
    List<String> messages = JSONUtils.toStringList(contents.names());
    for (String message : messages) {
        JSONObject value = contents.getJSONObject(message);
        contents.remove(message);/*from   www. jav  a 2  s.  co  m*/
        contents.put(message.toLowerCase(), value);
    }
    return contents;
}

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

License:Open Source License

private List<String> loadScopesFromManifest() throws IOException, JSONException {
    Context context = this.cordova.getActivity();
    InputStream is = context.getAssets().open("www/manifest.json");
    //Small trick to get the scanner to pull the entire input stream in one go
    Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
    String contents = s.hasNext() ? s.next() : "";

    JSONObject manifestContents = new JSONObject(contents);
    List<String> scopes = new ArrayList<String>();

    JSONObject oAuthObj = manifestContents.optJSONObject("oauth2");
    if (oAuthObj != null) {
        JSONArray oAuthScopes = oAuthObj.optJSONArray("scopes");
        if (oAuthScopes != null) {
            scopes = JSONUtils.toStringList(oAuthScopes);
        } else {/*w ww  .j a  va 2  s  .c om*/
            throw new IllegalArgumentException("scopes missing from manifest.json");
        }
    } else {
        throw new IllegalArgumentException("oauth2 missing from manifest.json");
    }

    return scopes;
}

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  w  ww  .java2  s.co  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 set(final CordovaArgs args, final CallbackContext callbackContext) {
    executorService.execute(new Runnable() {
        @Override//  w w w.ja  va  2s .  co m
        public void run() {
            try {
                boolean sync = args.getBoolean(0);
                JSONObject jsonObject = (JSONObject) args.getJSONObject(1);
                JSONArray keyArray = jsonObject.names();
                JSONObject oldValues = new JSONObject();

                if (keyArray != null) {
                    List<String> keys = JSONUtils.toStringList(keyArray);
                    JSONObject storage = getStorage(sync);
                    for (String key : keys) {
                        Object oldValue = storage.opt(key);
                        if (oldValue != null) {
                            oldValues.put(key, oldValue);
                        }
                        storage.put(key, jsonObject.get(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:com.google.cordova.ChromeStorage.java

License:Open Source License

private void remove(final CordovaArgs args, final CallbackContext callbackContext) {
    executorService.execute(new Runnable() {
        @Override// w w w .  ja v  a  2 s. c  o 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");
            }
        }
    });
}