Example usage for org.apache.cordova PluginResult setKeepCallback

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

Introduction

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

Prototype

public void setKeepCallback(boolean b) 

Source Link

Usage

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;
    }//  w  ww. jav  a2s  .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;
    }/* ww w . ja  v a  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);
        }

        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;
    }/*w w w. j  a v a  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);
        }

        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 ww w .j  a  va 2s.  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);
        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);
    this.getCallbackContextKeepCallback().sendPluginResult(pr);
}

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.
 *///from w  w w  . j av  a 2  s .c  o 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;
}

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

License:Apache License

/**
 * Create a new plugin result and send it back to JavaScript
 *
 * @param obj a JSONObject contain event payload information
 * @param status the status code to return to the JavaScript environment
 *///from  w ww.j a v a 2s  .  c o  m
private void sendUpdate(JSONObject obj, boolean keepCallback, PluginResult.Status status) {
    if (callbackContext != null) {
        PluginResult result = new PluginResult(status, obj);
        result.setKeepCallback(keepCallback);
        callbackContext.sendPluginResult(result);
        if (!keepCallback) {
            callbackContext = null;
        }
    }
}

From source file:com.google.zxing.BarcodeScanner.java

License:BSD License

/**
* Executes the request and returns PluginResult.
*
* @param action        The action to execute.
* @param args          JSONArray 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.
*//*from w w w.ja  v a2  s. c om*/
public PluginResult execute(String action, JSONArray args, String callbackId) {
    this.callback = callbackId;

    if (action.equals(SCAN)) {
        scan();
    } else {
        return new PluginResult(PluginResult.Status.INVALID_ACTION);
    }
    PluginResult r = new PluginResult(PluginResult.Status.NO_RESULT);
    r.setKeepCallback(true);
    return r;
}

From source file:com.gotojmp.cordova.NativeAd.NativeAd.java

License:Apache License

public void onClose(int id) {
    PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, id);
    pluginResult.setKeepCallback(true);
    this.onCloseCallbackContext.sendPluginResult(pluginResult);
}

From source file:com.gotojmp.cordova.NativeAd.NativeAd.java

License:Apache License

public void onClick(int id, String pos) {
    try {/*w w  w  .j  a va2 s.co  m*/
        JSONObject res = new JSONObject();
        res.put("id", id);
        res.put("pos", pos);
        PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, res);
        pluginResult.setKeepCallback(true);
        this.onClickCallbackContext.sendPluginResult(pluginResult);
    } catch (JSONException ex) {
        LOG.e(LOG_TAG, "JSON error.");
    }
}