Example usage for org.json JSONArray optLong

List of usage examples for org.json JSONArray optLong

Introduction

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

Prototype

public long optLong(int index) 

Source Link

Document

Get the optional long value associated with an index.

Usage

From source file:com.remobile.file.FileUtils.java

public boolean execute(String action, final JSONArray args, final CallbackContext callbackContext) {
    if (!configured) {
        callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR,
                "File plugin is not configured. Please see the README.md file for details on how to update config.xml"));
        return true;
    }//from  ww  w. j  av  a2  s. c o m
    if (action.equals("testSaveLocationExists")) {
        threadhelper(new FileOp() {
            public void run(JSONArray args) {
                boolean b = DirectoryManager.testSaveLocationExists();
                callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, b));
            }
        }, args, callbackContext);
    } else if (action.equals("getFreeDiskSpace")) {
        threadhelper(new FileOp() {
            public void run(JSONArray args) {
                long l = DirectoryManager.getFreeDiskSpace(false);
                callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, l));
            }
        }, args, callbackContext);
    } else if (action.equals("testFileExists")) {
        threadhelper(new FileOp() {
            public void run(JSONArray args) throws JSONException {
                String fname = args.getString(0);
                boolean b = DirectoryManager.testFileExists(fname);
                callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, b));
            }
        }, args, callbackContext);
    } else if (action.equals("testDirectoryExists")) {
        threadhelper(new FileOp() {
            public void run(JSONArray args) throws JSONException {
                String fname = args.getString(0);
                boolean b = DirectoryManager.testFileExists(fname);
                callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, b));
            }
        }, args, callbackContext);
    } else if (action.equals("readAsText")) {
        threadhelper(new FileOp() {
            public void run(JSONArray args) throws JSONException, MalformedURLException {
                String encoding = args.getString(1);
                int start = args.getInt(2);
                int end = args.getInt(3);
                String fname = args.getString(0);
                readFileAs(fname, start, end, callbackContext, encoding, PluginResult.MESSAGE_TYPE_STRING);
            }
        }, args, callbackContext);
    } else if (action.equals("readAsDataURL")) {
        threadhelper(new FileOp() {
            public void run(JSONArray args) throws JSONException, MalformedURLException {
                int start = args.getInt(1);
                int end = args.getInt(2);
                String fname = args.getString(0);
                readFileAs(fname, start, end, callbackContext, null, -1);
            }
        }, args, callbackContext);
    } else if (action.equals("readAsArrayBuffer")) {
        threadhelper(new FileOp() {
            public void run(JSONArray args) throws JSONException, MalformedURLException {
                int start = args.getInt(1);
                int end = args.getInt(2);
                String fname = args.getString(0);
                readFileAs(fname, start, end, callbackContext, null, PluginResult.MESSAGE_TYPE_ARRAYBUFFER);
            }
        }, args, callbackContext);
    } else if (action.equals("readAsBinaryString")) {
        threadhelper(new FileOp() {
            public void run(JSONArray args) throws JSONException, MalformedURLException {
                int start = args.getInt(1);
                int end = args.getInt(2);
                String fname = args.getString(0);
                readFileAs(fname, start, end, callbackContext, null, PluginResult.MESSAGE_TYPE_BINARYSTRING);
            }
        }, args, callbackContext);
    } else if (action.equals("write")) {
        threadhelper(new FileOp() {
            public void run(JSONArray args)
                    throws JSONException, FileNotFoundException, IOException, NoModificationAllowedException {
                String fname = args.getString(0);
                String data = args.getString(1);
                int offset = args.getInt(2);
                Boolean isBinary = args.getBoolean(3);
                long fileSize = write(fname, data, offset, isBinary);
                callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, fileSize));
            }
        }, args, callbackContext);
    } else if (action.equals("truncate")) {
        threadhelper(new FileOp() {
            public void run(JSONArray args)
                    throws JSONException, FileNotFoundException, IOException, NoModificationAllowedException {
                String fname = args.getString(0);
                int offset = args.getInt(1);
                long fileSize = truncateFile(fname, offset);
                callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, fileSize));
            }
        }, args, callbackContext);
    } else if (action.equals("requestAllFileSystems")) {
        threadhelper(new FileOp() {
            public void run(JSONArray args) throws IOException, JSONException {
                callbackContext.success(requestAllFileSystems());
            }
        }, args, callbackContext);
    } else if (action.equals("requestAllPaths")) {
        cordova.getThreadPool().execute(new Runnable() {
            public void run() {
                try {
                    callbackContext.success(requestAllPaths());
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
    } else if (action.equals("requestFileSystem")) {
        threadhelper(new FileOp() {
            public void run(JSONArray args) throws IOException, JSONException {
                int fstype = args.getInt(0);
                long size = args.optLong(1);
                if (size != 0 && size > (DirectoryManager.getFreeDiskSpace(true) * 1024)) {
                    callbackContext.sendPluginResult(
                            new PluginResult(PluginResult.Status.ERROR, FileUtils.QUOTA_EXCEEDED_ERR));
                } else {
                    JSONObject obj = requestFileSystem(fstype);
                    callbackContext.success(obj);
                }
            }
        }, args, callbackContext);
    } else if (action.equals("resolveLocalFileSystemURI")) {
        threadhelper(new FileOp() {
            public void run(JSONArray args) throws IOException, JSONException {
                String fname = args.getString(0);
                JSONObject obj = resolveLocalFileSystemURI(fname);
                callbackContext.success(obj);
            }
        }, args, callbackContext);
    } else if (action.equals("getFileMetadata")) {
        threadhelper(new FileOp() {
            public void run(JSONArray args) throws FileNotFoundException, JSONException, MalformedURLException {
                String fname = args.getString(0);
                JSONObject obj = getFileMetadata(fname);
                callbackContext.success(obj);
            }
        }, args, callbackContext);
    } else if (action.equals("getParent")) {
        threadhelper(new FileOp() {
            public void run(JSONArray args) throws JSONException, IOException {
                String fname = args.getString(0);
                JSONObject obj = getParent(fname);
                callbackContext.success(obj);
            }
        }, args, callbackContext);
    } else if (action.equals("getDirectory")) {
        threadhelper(new FileOp() {
            public void run(JSONArray args) throws FileExistsException, IOException, TypeMismatchException,
                    EncodingException, JSONException {
                String dirname = args.getString(0);
                String path = args.getString(1);
                JSONObject obj = getFile(dirname, path, args.optJSONObject(2), true);
                callbackContext.success(obj);
            }
        }, args, callbackContext);
    } else if (action.equals("getFile")) {
        threadhelper(new FileOp() {
            public void run(JSONArray args) throws FileExistsException, IOException, TypeMismatchException,
                    EncodingException, JSONException {
                String dirname = args.getString(0);
                String path = args.getString(1);
                JSONObject obj = getFile(dirname, path, args.optJSONObject(2), false);
                callbackContext.success(obj);
            }
        }, args, callbackContext);
    } else if (action.equals("remove")) {
        threadhelper(new FileOp() {
            public void run(JSONArray args) throws JSONException, NoModificationAllowedException,
                    InvalidModificationException, MalformedURLException {
                String fname = args.getString(0);
                boolean success = remove(fname);
                if (success) {
                    callbackContext.success();
                } else {
                    callbackContext.error(FileUtils.NO_MODIFICATION_ALLOWED_ERR);
                }
            }
        }, args, callbackContext);
    } else if (action.equals("removeRecursively")) {
        threadhelper(new FileOp() {
            public void run(JSONArray args) throws JSONException, FileExistsException, MalformedURLException,
                    NoModificationAllowedException {
                String fname = args.getString(0);
                boolean success = removeRecursively(fname);
                if (success) {
                    callbackContext.success();
                } else {
                    callbackContext.error(FileUtils.NO_MODIFICATION_ALLOWED_ERR);
                }
            }
        }, args, callbackContext);
    } else if (action.equals("moveTo")) {
        threadhelper(new FileOp() {
            public void run(JSONArray args) throws JSONException, NoModificationAllowedException, IOException,
                    InvalidModificationException, EncodingException, FileExistsException {
                String fname = args.getString(0);
                String newParent = args.getString(1);
                String newName = args.getString(2);
                JSONObject entry = transferTo(fname, newParent, newName, true);
                callbackContext.success(entry);
            }
        }, args, callbackContext);
    } else if (action.equals("copyTo")) {
        threadhelper(new FileOp() {
            public void run(JSONArray args) throws JSONException, NoModificationAllowedException, IOException,
                    InvalidModificationException, EncodingException, FileExistsException {
                String fname = args.getString(0);
                String newParent = args.getString(1);
                String newName = args.getString(2);
                JSONObject entry = transferTo(fname, newParent, newName, false);
                callbackContext.success(entry);
            }
        }, args, callbackContext);
    } else if (action.equals("readEntries")) {
        threadhelper(new FileOp() {
            public void run(JSONArray args) throws FileNotFoundException, JSONException, MalformedURLException {
                String fname = args.getString(0);
                JSONArray entries = readEntries(fname);
                callbackContext.success(entries);
            }
        }, args, callbackContext);
    } else if (action.equals("_getLocalFilesystemPath")) {
        // Internal method for testing: Get the on-disk location of a local filesystem url.
        // [Currently used for testing file-transfer]
        threadhelper(new FileOp() {
            public void run(JSONArray args) throws FileNotFoundException, JSONException, MalformedURLException {
                String localURLstr = args.getString(0);
                String fname = filesystemPathForURL(localURLstr);
                callbackContext.success(fname);
            }
        }, args, callbackContext);
    } else {
        return false;
    }
    return true;
}

From source file:org.catnut.plugin.zhihu.Zhihu.java

@Override
public ContentValues convert(JSONArray array) {
    long now = System.currentTimeMillis();

    ContentValues item = new ContentValues();

    // ??item???/*ww  w .  java  2  s .  c o m*/
    item.put(BaseColumns._ID, now);

    // 
    item.put(STATUS, array.optString(1));
    item.put(ANSWER, array.optString(2));
    item.put(LAST_ALTER_DATE, array.optLong(4) * 1000);
    item.put(ANSWER_ID, array.optLong(5));

    // 
    JSONArray user = array.optJSONArray(6);
    if (user != null) {
        item.put(NICK, user.optString(0));
        item.put(UID, user.optString(1));
        item.put(AVATAR, user.optInt(2));
    }

    // 
    JSONArray question = array.optJSONArray(7);
    if (question != null) {
        item.put(TITLE, question.optString(1, null));
        item.put(DESCRIPTION, question.optString(2));
        item.put(QUESTION_ID, question.optLong(3));
    }

    return item;
}

From source file:edu.stanford.mobisocial.dungbeetle.feed.objects.DeleteObj.java

public void handleDirectMessage(Context context, Contact from, JSONObject json) {
    DBHelper dbh = DBHelper.getGlobal(context);
    try {//from ww w .j  a v  a  2  s.  c o  m
        long[] hashes;
        if (json.has(HASHES)) {
            JSONArray jsonHashes = json.optJSONArray(HASHES);
            hashes = new long[jsonHashes.length()];
            for (int i = 0; i < jsonHashes.length(); i++) {
                hashes[i] = jsonHashes.optLong(i);
            }
        } else if (json.has(HASH)) {
            hashes = new long[] { json.optLong(HASH) };
        } else {
            Log.d(TAG, "DeleteObj with no hashes!");
            return;
        }
        dbh.markOrDeleteObjs(hashes);
    } finally {
        dbh.close();
    }
}

From source file:edu.stanford.mobisocial.dungbeetle.feed.objects.DeleteObj.java

@Override
public void afterDbInsertion(Context context, DbObj obj) {
    Uri feedUri = obj.getContainingFeed().getUri();
    DBHelper dbh = DBHelper.getGlobal(context);
    try {// w w  w  .  j ava 2s  . c  o  m
        JSONObject json = obj.getJson();
        long[] hashes;
        if (json.optJSONArray(HASHES) != null) {
            JSONArray jsonHashes = json.optJSONArray(HASHES);
            hashes = new long[jsonHashes.length()];
            for (int i = 0; i < jsonHashes.length(); i++) {
                hashes[i] = jsonHashes.optLong(i);
            }
        } else if (json.has(HASH)) {
            hashes = new long[] { json.optLong(HASH) };
        } else {
            Log.d(TAG, "DeleteObj with no hashes!");
            return;
        }
        Log.d(TAG, "marking or deleting " + hashes.length);
        dbh.markOrDeleteFeedObjs(feedUri, hashes, (json.has(FORCE) && json.optBoolean(FORCE)));
    } finally {
        dbh.close();
    }
}