Example usage for org.apache.cordova PluginResult PluginResult

List of usage examples for org.apache.cordova PluginResult PluginResult

Introduction

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

Prototype

public PluginResult(Status status, List<PluginResult> multipartMessages) 

Source Link

Usage

From source file:com.foxit.cordova.plugin.FoxitPdf.java

private boolean getFormInfo(CallbackContext callbackContext) {
    if (ReaderActivity.pdfViewCtrl == null || ReaderActivity.pdfViewCtrl.getDoc() == null) {
        callbackContext.error("Please open document first.");
        return false;
    }/*from w w  w .java  2  s .  c om*/

    PDFDoc pdfDoc = ReaderActivity.pdfViewCtrl.getDoc();
    try {
        if (!pdfDoc.hasForm()) {
            callbackContext.error("The current document does not have interactive form.");
            return false;
        }
        Form form = new Form(pdfDoc);
        int alignment = form.getAlignment();
        boolean needConstructAppearances = form.needConstructAppearances();
        JSONObject obj = new JSONObject();
        obj.put("alignment", alignment);
        obj.put("needConstructAppearances", needConstructAppearances);
        DefaultAppearance da = form.getDefaultAppearance();
        JSONObject defaultApObj = new JSONObject();
        defaultApObj.put("flags", da.getFlags());
        defaultApObj.put("textColor", da.getText_color());
        defaultApObj.put("textSize", da.getText_size());
        obj.put("defaultAppearance", defaultApObj);

        PluginResult result = new PluginResult(PluginResult.Status.OK, obj);
        result.setKeepCallback(true);
        callbackContext.sendPluginResult(result);
        return true;
    } catch (PDFException e) {
        callbackContext.error(e.getMessage() + ", Error code = " + e.getLastError());
    } catch (JSONException e) {
        callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION));
    }
    return false;
}

From source file:com.foxit.cordova.plugin.FoxitPdf.java

private boolean getAllFormFields(CallbackContext callbackContext) {
    if (ReaderActivity.pdfViewCtrl == null || ReaderActivity.pdfViewCtrl.getDoc() == null) {
        callbackContext.error("Please open document first.");
        return false;
    }//from   w  w w  .j  a  v a 2  s.c  om

    PDFDoc pdfDoc = ReaderActivity.pdfViewCtrl.getDoc();
    try {
        if (!pdfDoc.hasForm()) {
            callbackContext.error("The current document does not have interactive form.");
            return false;
        }
        Form form = new Form(pdfDoc);
        int fieldCount = form.getFieldCount(null);
        if (fieldCount == 0) {
            callbackContext.error("The current document does not have form fields.");
            return false;
        }
        JSONArray infos = new JSONArray();
        for (int i = 0; i < fieldCount; i++) {
            Field field = form.getField(i, null);
            int type = field.getType();
            JSONObject obj = new JSONObject();
            obj.put("fieldIndex", i);
            obj.put("fieldType", type);
            obj.put("fieldFlag", field.getFlags());
            obj.put("name", field.getName());
            obj.put("defValue", field.getDefaultValue());
            obj.put("value", field.getValue());
            obj.put("alignment", field.getAlignment());
            obj.put("alternateName", field.getAlternateName());
            obj.put("mappingName", field.getMappingName());
            obj.put("maxLength", field.getMaxLength());
            obj.put("topVisibleIndex", field.getTopVisibleIndex());
            DefaultAppearance da = field.getDefaultAppearance();
            JSONObject defaultApObj = new JSONObject();
            defaultApObj.put("flags", da.getFlags());
            defaultApObj.put("textColor", da.getText_color());
            defaultApObj.put("textSize", da.getText_size());
            obj.put("defaultAppearance", defaultApObj);

            if (type == Field.e_TypeComboBox || type == Field.e_TypeListBox) {
                ChoiceOptionArray options = field.getOptions();
                long optionCount = options.getSize();
                if (optionCount > 0) {
                    JSONArray optArray = new JSONArray();
                    for (int j = 0; j < optionCount; j++) {
                        JSONObject optObj = new JSONObject();
                        ChoiceOption option = options.getAt(j);
                        optObj.put("optionValue", option.getOption_value());
                        optObj.put("optionLabel", option.getOption_label());
                        optObj.put("selected", option.getDefault_selected());
                        optObj.put("defaultSelected", option.getSelected());
                        optArray.put(optObj);
                    }
                    obj.put("choiceOptions", optArray);
                }

            }

            infos.put(obj);
        }

        PluginResult result = new PluginResult(PluginResult.Status.OK, infos);
        result.setKeepCallback(true);
        callbackContext.sendPluginResult(result);
        return true;
    } catch (PDFException e) {
        callbackContext.error(e.getMessage() + ", Error code = " + e.getLastError());
    } catch (JSONException e) {
        callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION));
    }
    return false;
}

From source file:com.foxit.cordova.plugin.FoxitPdf.java

private boolean getPageControls(int pageIndex, CallbackContext callbackContext) {
    if (ReaderActivity.pdfViewCtrl == null || ReaderActivity.pdfViewCtrl.getDoc() == null) {
        callbackContext.error("Please open document first.");
        return false;
    }//from w w  w  . j  ava  2 s  .  co  m

    PDFDoc pdfDoc = ReaderActivity.pdfViewCtrl.getDoc();
    try {
        if (!pdfDoc.hasForm()) {
            callbackContext.error("The current document does not have interactive form.");
            return false;
        }
        Form form = new Form(pdfDoc);
        PDFPage page = pdfDoc.getPage(pageIndex);
        if (!page.isParsed()) {
            page.startParse(PDFPage.e_ParsePageNormal, null, false);
        }
        int controlCount = form.getControlCount(page);
        if (controlCount == 0) {
            callbackContext.error("The current document does not have field controls.");
            return false;
        }
        JSONArray infos = new JSONArray();
        for (int i = 0; i < controlCount; i++) {
            Control control = form.getControl(page, i);
            JSONObject obj = new JSONObject();
            obj.put("controlIndex", i);
            obj.put("exportValue", control.getExportValue());
            obj.put("isChecked", control.isChecked());
            obj.put("isDefaultChecked", control.isDefaultChecked());
            infos.put(obj);
        }

        PluginResult result = new PluginResult(PluginResult.Status.OK, infos);
        result.setKeepCallback(true);
        callbackContext.sendPluginResult(result);
        return true;
    } catch (PDFException e) {
        callbackContext.error(e.getMessage() + ", Error code = " + e.getLastError());
    } catch (JSONException e) {
        callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION));
    }
    return false;
}

From source file:com.foxit.cordova.plugin.FoxitPdf.java

private boolean addControl(int pageIndex, String fieldName, int fieldType,
        com.foxit.sdk.common.fxcrt.RectF rectF, CallbackContext callbackContext) {
    if (ReaderActivity.pdfViewCtrl == null || ReaderActivity.pdfViewCtrl.getDoc() == null) {
        callbackContext.error("Please open document first.");
        return false;
    }//from  w w  w  . jav  a 2 s  . c o m

    PDFDoc pdfDoc = ReaderActivity.pdfViewCtrl.getDoc();
    try {
        // if (!pdfDoc.hasForm()) {
        //     callbackContext.error("The current document does not have interactive form.");
        //     return false;
        // }
        Form form = new Form(pdfDoc);
        PDFPage page = pdfDoc.getPage(pageIndex);
        if (!page.isParsed()) {
            page.startParse(PDFPage.e_ParsePageNormal, null, false);
        }

        Control control = form.addControl(page, fieldName, fieldType, rectF);
        JSONObject obj = new JSONObject();
        obj.put("controlIndex", form.getControlCount(page) - 1);
        obj.put("exportValue", control.getExportValue());
        obj.put("isChecked", control.isChecked());
        obj.put("isDefaultChecked", control.isDefaultChecked());

        ((UIExtensionsManager) ReaderActivity.pdfViewCtrl.getUIExtensionsManager()).getDocumentManager()
                .setDocModified(true);
        PluginResult result = new PluginResult(PluginResult.Status.OK, obj);
        result.setKeepCallback(true);
        callbackContext.sendPluginResult(result);
        return true;
    } catch (PDFException e) {
        callbackContext.error(e.getMessage() + ", Error code = " + e.getLastError());
    } catch (JSONException e) {
        callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION));
    }
    return false;
}

From source file:com.foxit.cordova.plugin.FoxitPdf.java

private boolean getFieldByControl(int pageIndex, int controlIndex, CallbackContext callbackContext) {
    if (ReaderActivity.pdfViewCtrl == null || ReaderActivity.pdfViewCtrl.getDoc() == null) {
        callbackContext.error("Please open document first.");
        return false;
    }/*from   w w w. j  a  v  a2 s  .com*/

    PDFDoc pdfDoc = ReaderActivity.pdfViewCtrl.getDoc();
    try {
        if (!pdfDoc.hasForm()) {
            callbackContext.error("The current document does not have interactive form.");
            return false;
        }
        Form form = new Form(pdfDoc);
        PDFPage page = pdfDoc.getPage(pageIndex);
        if (!page.isParsed()) {
            page.startParse(PDFPage.e_ParsePageNormal, null, false);
        }

        Control control = form.getControl(page, controlIndex);
        Field field = control.getField();
        int type = field.getType();
        JSONObject obj = new JSONObject();
        int fieldCount = form.getFieldCount(null);
        for (int i = 0; i < fieldCount; i++) {
            Field other = form.getField(i, null);
            if (field.getDict().getObjNum() == other.getDict().getObjNum()) {
                obj.put("fieldIndex", i);
                break;
            }
        }
        obj.put("fieldType", type);
        obj.put("fieldFlag", field.getFlags());
        obj.put("name", field.getName());
        obj.put("defValue", field.getDefaultValue());
        obj.put("value", field.getValue());
        obj.put("alignment", field.getAlignment());
        obj.put("alternateName", field.getAlternateName());
        obj.put("mappingName", field.getMappingName());
        obj.put("maxLength", field.getMaxLength());
        obj.put("topVisibleIndex", field.getTopVisibleIndex());
        DefaultAppearance da = field.getDefaultAppearance();
        JSONObject defaultApObj = new JSONObject();
        defaultApObj.put("flags", da.getFlags());
        defaultApObj.put("textColor", da.getText_color());
        defaultApObj.put("textSize", da.getText_size());
        obj.put("defaultAppearance", defaultApObj);

        if (type == Field.e_TypeComboBox || type == Field.e_TypeListBox) {
            ChoiceOptionArray options = field.getOptions();
            long optionCount = options.getSize();
            if (optionCount > 0) {
                JSONArray optArray = new JSONArray();
                for (int j = 0; j < optionCount; j++) {
                    JSONObject optObj = new JSONObject();
                    ChoiceOption option = options.getAt(j);
                    optObj.put("optionValue", option.getOption_value());
                    optObj.put("optionLabel", option.getOption_label());
                    optObj.put("selected", option.getDefault_selected());
                    optObj.put("defaultSelected", option.getSelected());
                    optArray.put(optObj);
                }
                obj.put("choiceOptions", optArray);
            }

        }
        PluginResult result = new PluginResult(PluginResult.Status.OK, obj);
        result.setKeepCallback(true);
        callbackContext.sendPluginResult(result);
        return true;
    } catch (PDFException e) {
        callbackContext.error(e.getMessage() + ", Error code = " + e.getLastError());
    } catch (JSONException e) {
        callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION));
    }
    return false;
}

From source file:com.foxit.cordova.plugin.FoxitPdf.java

private boolean getFieldControls(int fieldIndex, CallbackContext callbackContext) {
    if (ReaderActivity.pdfViewCtrl == null || ReaderActivity.pdfViewCtrl.getDoc() == null) {
        callbackContext.error("Please open document first.");
        return false;
    }//from   www  .j ava 2  s.  com

    PDFDoc pdfDoc = ReaderActivity.pdfViewCtrl.getDoc();
    try {
        if (!pdfDoc.hasForm()) {
            callbackContext.error("The current document does not have interactive form.");
            return false;
        }
        Form form = new Form(pdfDoc);
        Field field = form.getField(fieldIndex, null);
        int controlCount = field.getControlCount();
        if (controlCount == 0) {
            callbackContext.error("The specified form field does not have field controls.");
            return false;
        }
        JSONArray infos = new JSONArray();
        for (int i = 0; i < controlCount; i++) {
            Control control = field.getControl(i);
            JSONObject obj = new JSONObject();
            obj.put("controlIndex", i);
            obj.put("exportValue", control.getExportValue());
            obj.put("isChecked", control.isChecked());
            obj.put("isDefaultChecked", control.isDefaultChecked());
            infos.put(obj);
        }

        PluginResult result = new PluginResult(PluginResult.Status.OK, infos);
        result.setKeepCallback(true);
        callbackContext.sendPluginResult(result);
        return true;
    } catch (PDFException e) {
        callbackContext.error(e.getMessage() + ", Error code = " + e.getLastError());
    } catch (JSONException e) {
        callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION));
    }
    return false;
}

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

@Override
public void sendPluginResult(String strEventName) {
    PluginResult pr = new PluginResult(PluginResult.Status.OK, strEventName);
    pr.setKeepCallback(true);/*from   ww w. j  ava  2 s .c  om*/
    this.getCallbackContextKeepCallback().sendPluginResult(pr);
}

From source file:com.gabfiocchi.cordova.plugin.browsertab.BrowserTab.java

License:Open Source License

private void isAvailable(CallbackContext callbackContext) {
    String browserPackage = findCustomTabBrowser();
    Log.d(LOG_TAG, "browser package: " + browserPackage);
    callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, browserPackage != null));
}

From source file:com.gft.cordova.plugins.trustdevice.TrustedDevicePlugin.java

License:Appverse Public License

/**
 * Execution entry point from javascript world.
 *
 * @param action          The action to execute ("isTrusted").
 * @param args            The exec() arguments (Ignored, no arguments).
 * @param callbackContext The callback context used when calling back into JavaScript.
 * @return resultStatus/*from   w ww .  j  av a2 s . c om*/
 * @throws JSONException
 */
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
    boolean resultStatus = false;
    if ("isTrusted".equals(action)) {
        callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, !isDeviceRooted()));
        resultStatus = true;
    }
    return resultStatus;
}

From source file:com.glasgowtiger.inappbrowser.InAppBrowser.java

License:Apache License

/**
 * Executes the request and returns PluginResult.
 *
 * @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.
 *//*ww w  . ja  v a  2s.co m*/
public boolean execute(String action, CordovaArgs args, final CallbackContext callbackContext)
        throws JSONException {
    if (action.equals("open")) {
        this.callbackContext = callbackContext;
        final String url = args.getString(0);
        String t = args.optString(1);
        if (t == null || t.equals("") || t.equals(NULL)) {
            t = SELF;
        }
        final String target = t;
        final HashMap<String, Boolean> features = parseFeature(args.optString(2));

        Log.d(LOG_TAG, "target = " + target);

        this.cordova.getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                String result = "";
                // SELF
                if (SELF.equals(target)) {
                    Log.d(LOG_TAG, "in self");
                    /* This code exists for compatibility between 3.x and 4.x versions of Cordova.
                     * Previously the Config class had a static method, isUrlWhitelisted(). That
                     * responsibility has been moved to the plugins, with an aggregating method in
                     * PluginManager.
                     */
                    Boolean shouldAllowNavigation = null;
                    if (url.startsWith("javascript:")) {
                        shouldAllowNavigation = true;
                    }
                    if (shouldAllowNavigation == null) {
                        try {
                            Method iuw = Config.class.getMethod("isUrlWhiteListed", String.class);
                            shouldAllowNavigation = (Boolean) iuw.invoke(null, url);
                        } catch (NoSuchMethodException e) {
                        } catch (IllegalAccessException e) {
                        } catch (InvocationTargetException e) {
                        }
                    }
                    if (shouldAllowNavigation == null) {
                        try {
                            Method gpm = webView.getClass().getMethod("getPluginManager");
                            PluginManager pm = (PluginManager) gpm.invoke(webView);
                            Method san = pm.getClass().getMethod("shouldAllowNavigation", String.class);
                            shouldAllowNavigation = (Boolean) san.invoke(pm, url);
                        } catch (NoSuchMethodException e) {
                        } catch (IllegalAccessException e) {
                        } catch (InvocationTargetException e) {
                        }
                    }
                    // load in webview
                    if (Boolean.TRUE.equals(shouldAllowNavigation)) {
                        Log.d(LOG_TAG, "loading in webview");
                        webView.loadUrl(url);
                    }
                    //Load the dialer
                    else if (url.startsWith(WebView.SCHEME_TEL)) {
                        try {
                            Log.d(LOG_TAG, "loading in dialer");
                            Intent intent = new Intent(Intent.ACTION_DIAL);
                            intent.setData(Uri.parse(url));
                            cordova.getActivity().startActivity(intent);
                        } catch (android.content.ActivityNotFoundException e) {
                            LOG.e(LOG_TAG, "Error dialing " + url + ": " + e.toString());
                        }
                    }
                    // load in InAppBrowser
                    else {
                        Log.d(LOG_TAG, "loading in InAppBrowser");
                        result = showWebPage(url, features);
                    }
                }
                // SYSTEM
                else if (SYSTEM.equals(target)) {
                    Log.d(LOG_TAG, "in system");
                    result = openExternal(url);
                }
                // BLANK - or anything else
                else {
                    Log.d(LOG_TAG, "in blank");
                    result = showWebPage(url, features);
                }

                PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, result);
                pluginResult.setKeepCallback(true);
                callbackContext.sendPluginResult(pluginResult);
            }
        });
    } else if (action.equals("close")) {
        closeDialog();
    } else if (action.equals("injectScriptCode")) {
        String jsWrapper = null;
        if (args.getBoolean(1)) {
            jsWrapper = String.format("prompt(JSON.stringify([eval(%%s)]), 'gap-iab://%s')",
                    callbackContext.getCallbackId());
        }
        injectDeferredObject(args.getString(0), jsWrapper);
    } else if (action.equals("injectScriptFile")) {
        String jsWrapper;
        if (args.getBoolean(1)) {
            jsWrapper = String.format(
                    "(function(d) { var c = d.createElement('script'); c.src = %%s; c.onload = function() { prompt('', 'gap-iab://%s'); }; d.body.appendChild(c); })(document)",
                    callbackContext.getCallbackId());
        } else {
            jsWrapper = "(function(d) { var c = d.createElement('script'); c.src = %s; d.body.appendChild(c); })(document)";
        }
        injectDeferredObject(args.getString(0), jsWrapper);
    } else if (action.equals("injectStyleCode")) {
        String jsWrapper;
        if (args.getBoolean(1)) {
            jsWrapper = String.format(
                    "(function(d) { var c = d.createElement('style'); c.innerHTML = %%s; d.body.appendChild(c); prompt('', 'gap-iab://%s');})(document)",
                    callbackContext.getCallbackId());
        } else {
            jsWrapper = "(function(d) { var c = d.createElement('style'); c.innerHTML = %s; d.body.appendChild(c); })(document)";
        }
        injectDeferredObject(args.getString(0), jsWrapper);
    } else if (action.equals("injectStyleFile")) {
        String jsWrapper;
        if (args.getBoolean(1)) {
            jsWrapper = String.format(
                    "(function(d) { var c = d.createElement('link'); c.rel='stylesheet'; c.type='text/css'; c.href = %%s; d.head.appendChild(c); prompt('', 'gap-iab://%s');})(document)",
                    callbackContext.getCallbackId());
        } else {
            jsWrapper = "(function(d) { var c = d.createElement('link'); c.rel='stylesheet'; c.type='text/css'; c.href = %s; d.head.appendChild(c); })(document)";
        }
        injectDeferredObject(args.getString(0), jsWrapper);
    } else if (action.equals("show")) {
        this.cordova.getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                dialog.show();
            }
        });
        PluginResult pluginResult = new PluginResult(PluginResult.Status.OK);
        pluginResult.setKeepCallback(true);
        this.callbackContext.sendPluginResult(pluginResult);
    } else {
        return false;
    }
    return true;
}