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.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  w w. j a v a 2 s  .  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

/**
* Called when the barcode scanner intent completes
*
* @param requestCode       The request code originally supplied to startActivityForResult(),
*                          allowing you to identify who this result came from.
* @param resultCode        The integer result code returned by the child activity through its setResult().
* @param intent            An Intent, which can return result data to the caller (various data can be attached to Intent "extras").
*///  www.j a v a2  s .  co m
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    if (requestCode == REQUEST_CODE) {
        if (resultCode == Activity.RESULT_OK) {
            JSONObject obj = new JSONObject();
            try {
                obj.put(TEXT, intent.getStringExtra("SCAN_RESULT"));
                obj.put(FORMAT, intent.getStringExtra("SCAN_RESULT_FORMAT"));
                obj.put(CANCELLED, false);
            } catch (JSONException e) {
                //Log.d(LOG_TAG, "This should never happen");
            }
            this.success(new PluginResult(PluginResult.Status.OK, obj), this.callback);
        }
        if (resultCode == Activity.RESULT_CANCELED) {
            JSONObject obj = new JSONObject();
            try {
                obj.put(TEXT, "");
                obj.put(FORMAT, "");
                obj.put(CANCELLED, true);
            } catch (JSONException e) {
                //Log.d(LOG_TAG, "This should never happen");
            }
            this.success(new PluginResult(PluginResult.Status.OK, obj), this.callback);
        } else {
            this.error(new PluginResult(PluginResult.Status.ERROR), this.callback);
        }
    }
}

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);//from   w ww .jav a 2s . c  o m
    this.onCloseCallbackContext.sendPluginResult(pluginResult);
}

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

License:Apache License

public void onClick(int id, String pos) {
    try {/*from   www. j  a v a2s. c  om*/
        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.");
    }
}

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

License:Apache License

/**
 * Executes the request and returns PluginResult.
 *
 * @param action the action to execute.//from   w  w w.  ja  v a  2  s.c  o  m
 * @param args JSONArry of arguments for the plugin.
 * @param callbackContext the callbackContext used when calling back into JavaScript.
 * @return A PluginResult object with a status and message.
 */
public boolean execute(String action, CordovaArgs args, final CallbackContext callbackContext)
        throws JSONException {
    if (action.equals("open")) {
        final String html = args.getString(0);
        final int adw = this.dpToPixels(args.getInt(1));
        final int adh = this.dpToPixels(args.getInt(2));
        final String showAt = args.optString(3);
        final String closeAt = args.optString(4);
        final int cw = this.dpToPixels(60);
        final int ch = this.dpToPixels(20);
        cordova.getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                NativeAdView adView = new NativeAdView(cordova.getActivity(), getNativeAd(), adw, adh, cw, ch,
                        closeAt);
                adView.id = ++_adId;
                adView.load(html);

                FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
                        ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                if (showAt.equals("center")) {
                    layoutParams.gravity = Gravity.CENTER;
                } else if (showAt.equals("top")) {
                    layoutParams.gravity = Gravity.TOP;
                } else {
                    layoutParams.gravity = Gravity.BOTTOM;
                }
                cordova.getActivity().addContentView(adView, layoutParams);
                ads.put("ad-" + adView.id, adView);
                PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, adView.id);
                pluginResult.setKeepCallback(true);
                callbackContext.sendPluginResult(pluginResult);
            }
        });
    } else if (action.equals("close")) {
        final int id = args.getInt(0);
        if (id > 0) {
            cordova.getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    NativeAdView adView = ads.get("ad-" + id);
                    if (adView != null) {
                        adView.closeAdView(false);
                    }
                }
            });
        }
    } else if (action.equals("onClose")) {
        this.onCloseCallbackContext = callbackContext;
        PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, 0);
        pluginResult.setKeepCallback(true);
        callbackContext.sendPluginResult(pluginResult);
    } else if (action.equals("onClick")) {
        this.onClickCallbackContext = callbackContext;
        PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, 0);
        pluginResult.setKeepCallback(true);
        callbackContext.sendPluginResult(pluginResult);
    } else if (action.equals("openUrl")) {
        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) {
                            LOG.d(LOG_TAG, e.getLocalizedMessage());
                        } catch (IllegalAccessException e) {
                            LOG.d(LOG_TAG, e.getLocalizedMessage());
                        } catch (InvocationTargetException e) {
                            LOG.d(LOG_TAG, e.getLocalizedMessage());
                        }
                    }
                    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) {
                            LOG.d(LOG_TAG, e.getLocalizedMessage());
                        } catch (IllegalAccessException e) {
                            LOG.d(LOG_TAG, e.getLocalizedMessage());
                        } catch (InvocationTargetException e) {
                            LOG.d(LOG_TAG, e.getLocalizedMessage());
                        }
                    }
                    // 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 NativeAd
                    else {
                        LOG.d(LOG_TAG, "loading in NativeAd");
                        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("openDeepLink")) {
        this.callbackContext = callbackContext;
        final String deepLink = args.getString(0);
        final String url = args.getString(1);

        final HashMap<String, Boolean> features = parseFeature(args.optString(2));

        this.cordova.getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                String result = "";
                final Intent customSchemeIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(deepLink));
                final PackageManager packageManager = cordova.getActivity().getApplicationContext()
                        .getPackageManager();
                final List<ResolveInfo> resolvedActivities = packageManager
                        .queryIntentActivities(customSchemeIntent, 0);
                if (resolvedActivities.size() > 0) {
                    cordova.getActivity().startActivity(customSchemeIntent);
                } else {
                    result = showWebPage(url, features);
                }
                PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, result);
                pluginResult.setKeepCallback(true);
                callbackContext.sendPluginResult(pluginResult);
            }
        });
    } else {
        return false;
    }
    return true;
}

From source file:com.gotojmp.cordova.uid.UID.java

License:MIT License

/**
 * Executes the request and returns PluginResult.
 *
 * @param action            The action to execute.
 * @param args              JSONArry of arguments for the plugin.
 * @param callbackContext   The callback id used when calling back into JavaScript.
 * @return                  True if the action was valid, false if not.
 *//*  w ww  . j a  va2s  .  c o m*/
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
    this.callbackContext = callbackContext;
    if (action.equals("getUID")) {
        JSONObject r = new JSONObject();
        r.put("UUID", UID.uuid);
        r.put("IMEI", UID.imei);
        r.put("IMSI", UID.imsi);
        r.put("ICCID", UID.iccid);
        r.put("MAC", UID.mac);

        PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, r);
        pluginResult.setKeepCallback(true);
        callbackContext.sendPluginResult(pluginResult);
        return true;
    } else {
        return false;
    }
}

From source file:com.grumpysailor.cordova.devicerotationvector.RotationVectorListener.java

License:Apache License

/**
 * Executes the request./*  w ww  .  j av a  2s.  c o  m*/
 *
 * @param action        The action to execute.
 * @param args          The exec() arguments.
 * @param callbackId    The callback id used when calling back into JavaScript.
 * @return              Whether the action was valid.
 */
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {
    if (action.equals("start")) {
        this.callbackContext = callbackContext;
        if (this.status != RotationVectorListener.RUNNING) {
            // If not running, then this is an async call, so don't worry about waiting
            // We drop the callback onto our stack, call start, and let start and the sensor callback fire off the callback down the road
            this.start();
        }
    } else if (action.equals("stop")) {
        if (this.status == RotationVectorListener.RUNNING) {
            this.stop();
        }
    } else {
        // Unsupported action
        return false;
    }

    PluginResult result = new PluginResult(PluginResult.Status.NO_RESULT, "");
    result.setKeepCallback(true);
    callbackContext.sendPluginResult(result);
    return true;
}

From source file:com.grumpysailor.cordova.devicerotationvector.RotationVectorListener.java

License:Apache License

private void win() {
    // Success return object
    PluginResult result = new PluginResult(PluginResult.Status.OK, this.getRotationJSON());
    result.setKeepCallback(true);/*w  w w  .  ja  v  a 2 s. c  om*/
    callbackContext.sendPluginResult(result);
}

From source file:com.grumpysailor.cordova.magnetsensor.MagnetSensor.java

License:Apache License

/**
 * Executes the request./*w w w  .  j  a v  a2 s.  c o m*/
 *
 * @param action        The action to execute.
 * @param args          The exec() arguments.
 * @param callbackId    The callback id used when calling back into JavaScript.
 * @return              Whether the action was valid.
 */
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {
    if (action.equals("start")) {
        this.callbackContext = callbackContext;
        if (this.status != MagnetSensor.RUNNING) {
            // If not running, then this is an async call, so don't worry about waiting
            // We drop the callback onto our stack, call start, and let start and the sensor callback fire off the callback down the road
            this.start();
        }
    } else if (action.equals("stop")) {
        if (this.status == MagnetSensor.RUNNING) {
            this.stop();
        }
    } else {
        // Unsupported action
        return false;
    }

    PluginResult result = new PluginResult(PluginResult.Status.NO_RESULT, "");
    result.setKeepCallback(true);
    callbackContext.sendPluginResult(result);
    return true;
}

From source file:com.grumpysailor.cordova.magnetsensor.MagnetSensor.java

License:Apache License

private void win() {
    // Success return object
    PluginResult result = new PluginResult(PluginResult.Status.OK, "");
    result.setKeepCallback(true);/*ww  w.  j  a v  a 2  s  . c  o m*/
    callbackContext.sendPluginResult(result);
}