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, long defaultValue) 

Source Link

Document

Get the optional long value associated with an index.

Usage

From source file:com.samsung.spen.SpenPlugin.java

/**
 * //  w  w  w .  ja v  a2  s .  c o m
 * @param args
 *                 JSON array of options sent from the script.
 * @param surfaceType
 *                int
 * @param callbackContext
 *                CallbackContext
 * @return   options
 *                SpenTrayBarOptions
 * @throws JSONException
 */
private SpenTrayBarOptions createTrayBarOptions(JSONArray args, int surfaceType,
        CallbackContext callbackContext) throws JSONException {
    if (Log.isLoggable(Utils.SPEN, Log.DEBUG)) {
        Log.d(TAG, "Inside createTrayBarOptions");
    }

    String tempId = args.getString(ID);
    if (tempId != null) {
        tempId = tempId.trim();
        if (tempId.length() > MAX_ID_LENGTH) {
            tempId = tempId.substring(0, MAX_ID_LENGTH);
        }
    }

    final String id = tempId;
    if (id == null || id.equals("") || id.equals("null") || !id.matches("^[ !#-)+-.0-9;=@-Z^-{}~]+$")) {
        SpenException.sendPluginResult(SpenExceptionType.INVALID_SURFACE_ID, callbackContext);
        return null;
    }

    int sPenFlags = args.optInt(SPEN_FLAGS, Integer.MIN_VALUE);
    if (sPenFlags == Integer.MIN_VALUE || sPenFlags > Utils.MAX_FLAGS_VALUE
            || sPenFlags < Utils.MIN_FLAGS_VALUE) {
        SpenException.sendPluginResult(SpenExceptionType.INVALID_FLAGS, callbackContext);
        return null;
    }

    int returnType = args.optInt(RETURN_TYPE);
    if (returnType != Utils.RETURN_TYPE_IMAGE_DATA && returnType != Utils.RETURN_TYPE_IMAGE_URI
            && returnType != Utils.RETURN_TYPE_TEXT) {

        SpenException.sendPluginResult(SpenExceptionType.INVALID_RETURN_TYPE, callbackContext);
        return null;
    }

    String backgroundColor = args.getString(BACKGROUND_COLOR);

    String imagePath = args.getString(IMAGE_PATH);
    if (imagePath.equals("") || imagePath.equals("null")) {
        imagePath = null;
    } else {
        imagePath = Uri.decode(imagePath);
        String truncatedPath = truncateQueryPart(imagePath);
        File file = new File(truncatedPath);
        if (file.exists()) {
            imagePath = truncatedPath;
        }
    }

    int bgImageScaleType = args.optInt(BACKGROUND_IMAGE_SCALE_TYPE);
    if (bgImageScaleType != Utils.BACKGROUND_IMAGE_MODE_CENTER
            && bgImageScaleType != Utils.BACKGROUND_IMAGE_MODE_FIT
            && bgImageScaleType != Utils.BACKGROUND_IMAGE_MODE_STRETCH
            && bgImageScaleType != Utils.BACKGROUND_IMAGE_MODE_TILE) {
        bgImageScaleType = Utils.BACKGROUND_IMAGE_MODE_FIT;
    }

    int imageUriScaleType = args.optInt(IMAGE_URI_SCALE_TYPE);
    if (imageUriScaleType != Utils.IMAGE_URI_MODE_CENTER && imageUriScaleType != Utils.IMAGE_URI_MODE_FIT
            && imageUriScaleType != Utils.IMAGE_URI_MODE_TILE
            && imageUriScaleType != Utils.IMAGE_URI_MODE_STRETCH) {
        imageUriScaleType = Utils.IMAGE_URI_MODE_FIT;
    }

    if (surfaceType == Utils.SURFACE_INLINE) {
        if ((sPenFlags & Utils.FLAG_ADD_PAGE) == Utils.FLAG_ADD_PAGE) {
            if (Log.isLoggable(Utils.SPEN, Log.DEBUG)) {
                Log.d(TAG, "Add Page is not supported in Inline");
            }
            sPenFlags = sPenFlags & ~Utils.FLAG_ADD_PAGE;
        }
    } else if (surfaceType == Utils.SURFACE_POPUP) {

        if ((sPenFlags & Utils.FLAG_EDIT) == Utils.FLAG_EDIT) {
            if (Log.isLoggable(Utils.SPEN, Log.DEBUG)) {
                Log.d(TAG, "Edit Page is not supported in Popup");
            }
            sPenFlags = sPenFlags & ~Utils.FLAG_EDIT;
        }

        if ((sPenFlags & Utils.FLAG_PEN) == Utils.FLAG_PEN) {
            if (Log.isLoggable(Utils.SPEN, Log.DEBUG)) {
                Log.d(TAG, "Pen option is provided by default, is not configurable in Popup");
            }
            sPenFlags = sPenFlags & ~Utils.FLAG_PEN;

        }

        if ((sPenFlags & Utils.FLAG_ERASER) == Utils.FLAG_ERASER) {
            if (Log.isLoggable(Utils.SPEN, Log.DEBUG)) {
                Log.d(TAG, "Eraser option is provided by default, is not configurable in Popup");
            }
            sPenFlags = sPenFlags & ~Utils.FLAG_ERASER;

        }

        if ((sPenFlags & Utils.FLAG_UNDO_REDO) == Utils.FLAG_UNDO_REDO) {
            if (Log.isLoggable(Utils.SPEN, Log.DEBUG)) {
                Log.d(TAG, "Undo Redo option is provided by default, is not configurable in Popup");
            }
            sPenFlags = sPenFlags & ~Utils.FLAG_UNDO_REDO;

        }
    } else {
        SpenException.sendPluginResult(SpenExceptionType.INVALID_SURFACE_TYPE, callbackContext);
        return null;
    }

    if ((sPenFlags & Utils.FLAG_TEXT_RECOGNITION) == Utils.FLAG_TEXT_RECOGNITION
            || (sPenFlags & Utils.FLAG_SHAPE_RECOGNITION) == Utils.FLAG_SHAPE_RECOGNITION) {
        sPenFlags = sPenFlags | Utils.FLAG_SELECTION;
    }

    SpenTrayBarOptions options = new SpenTrayBarOptions(sPenFlags);
    options.setId(id);
    options.setIsfeatureEnabled(mSpenState == SPEN_AND_HAND_SUPPORTED ? true : false);
    options.setColor(backgroundColor);
    options.setBgImageScaleType(bgImageScaleType);
    options.setImageUriScaleType(imageUriScaleType);
    options.setReturnType(returnType);
    options.setSurfaceType(surfaceType);
    options.setImagePath(imagePath);
    options.setDensity(mActivity.getApplicationContext().getResources().getDisplayMetrics().density);

    if (surfaceType == Utils.SURFACE_INLINE) {
        long xRect = 0, yRect = 0, width = 0, height = 0, xBodyRect = 0, yBodyRect = 0;
        if (args.isNull(RECTANGLE_X_VALUE) || args.isNull(RECTANGLE_Y_VALUE) || args.isNull(WIDTH)
                || args.isNull(HEIGHT)) {
            SpenException.sendPluginResult(SpenExceptionType.INVALID_INLINE_CORDINATES, callbackContext);
            return null;
        } else {
            xRect = args.optLong(RECTANGLE_X_VALUE, Integer.MIN_VALUE);
            yRect = args.optLong(RECTANGLE_Y_VALUE, Integer.MIN_VALUE);
            width = args.optLong(WIDTH, Integer.MIN_VALUE);
            height = args.optLong(HEIGHT, Integer.MIN_VALUE);
            xBodyRect = args.optLong(BODY_RECTANGLE_X_VALUE, Integer.MIN_VALUE);
            yBodyRect = args.optLong(BODY_RECTANGLE_Y_VALUE, Integer.MAX_VALUE);
            if (xRect == Integer.MIN_VALUE || yRect == Integer.MIN_VALUE || width == Integer.MIN_VALUE
                    || height == Integer.MIN_VALUE || xBodyRect == Integer.MIN_VALUE
                    || yBodyRect == Integer.MIN_VALUE || xRect > (long) Integer.MAX_VALUE
                    || yRect > (long) Integer.MAX_VALUE || width > (long) Integer.MAX_VALUE
                    || height > (long) Integer.MAX_VALUE || xBodyRect > (long) Integer.MAX_VALUE
                    || yBodyRect > (long) Integer.MAX_VALUE) {
                SpenException.sendPluginResult(SpenExceptionType.INVALID_INLINE_CORDINATES, callbackContext);
                return null;
            }
        }
        SurfacePosition surfacePosition = new SurfacePosition(mActivity.getApplicationContext(), (int) width,
                (int) height, (int) xRect - (int) xBodyRect, (int) yRect - (int) yBodyRect);
        if (!surfacePosition.isSurfaceValid(options, mActivity.getApplicationContext())) {
            SpenException.sendPluginResult(SpenExceptionType.INVALID_INLINE_CORDINATES, callbackContext);
            return null;
        }
        options.setSurfacePosition(surfacePosition);
    } else if (surfaceType == Utils.SURFACE_POPUP) {
        long popupWidth = 0, popupHeight = 0;
        popupWidth = args.optLong(POPUP_WIDTH, Integer.MIN_VALUE);
        popupHeight = args.optLong(POPUP_HEIGHT, Integer.MIN_VALUE);
        SurfacePosition surfacePosition = new SurfacePosition(mActivity.getApplicationContext(),
                (int) popupWidth, (int) popupHeight);
        options.setSurfacePosition(surfacePosition);
    }
    return options;
}