Example usage for org.apache.cordova.api CallbackContext error

List of usage examples for org.apache.cordova.api CallbackContext error

Introduction

In this page you can find the example usage for org.apache.cordova.api CallbackContext error.

Prototype

public void error(int message) 

Source Link

Document

Helper for error callbacks that just returns the Status.ERROR by default

Usage

From source file:com.polychrom.cordova.ActionBarPlugin.java

License:BSD License

@Override
public boolean execute(final String action, final JSONArray args, final CallbackContext callbackContext)
        throws JSONException {
    if (!plugin_actions.contains(action)) {
        return false;
    }//from w  ww  .  j a va  2  s  .  c o m

    final Activity ctx = (Activity) cordova;

    if ("isAvailable".equals(action)) {
        JSONObject result = new JSONObject();
        result.put("value", ctx.getWindow().hasFeature(Window.FEATURE_ACTION_BAR));
        callbackContext.success(result);
        return true;
    }

    final ActionBar bar = ctx.getActionBar();
    if (bar == null) {
        Window window = ctx.getWindow();
        if (!window.hasFeature(Window.FEATURE_ACTION_BAR)) {
            callbackContext
                    .error("ActionBar feature not available, Window.FEATURE_ACTION_BAR must be enabled!");
        } else {
            callbackContext.error("Failed to get ActionBar");
        }

        return true;
    }

    if (menu == null) {
        callbackContext.error("Options menu not initialised");
        return true;
    }

    final StringBuffer error = new StringBuffer();
    JSONObject result = new JSONObject();

    if ("isShowing".equals(action)) {
        result.put("value", bar.isShowing());
    } else if ("getHeight".equals(action)) {
        result.put("value", bar.getHeight());
    } else if ("getDisplayOptions".equals(action)) {
        result.put("value", bar.getDisplayOptions());
    } else if ("getNavigationMode".equals(action)) {
        result.put("value", bar.getNavigationMode());
    } else if ("getSelectedNavigationItem".equals(action)) {
        result.put("value", bar.getSelectedNavigationIndex());
    } else if ("getSubtitle".equals(action)) {
        result.put("value", bar.getSubtitle());
    } else if ("getTitle".equals(action)) {
        result.put("value", bar.getTitle());
    } else {
        try {
            JSONException exception = new Runnable() {
                public JSONException exception = null;

                public void run() {
                    try {
                        // This is a bit of a hack (should be specific to the request, not global)
                        bases = new String[] { removeFilename(webView.getOriginalUrl()),
                                removeFilename(webView.getUrl()) };

                        if ("show".equals(action)) {
                            bar.show();
                        } else if ("hide".equals(action)) {
                            bar.hide();
                        } else if ("setMenu".equals(action)) {
                            if (args.isNull(0)) {
                                error.append("menu can not be null");
                                return;
                            }

                            menu_definition = args.getJSONArray(0);

                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                                ctx.invalidateOptionsMenu();
                            }
                        } else if ("setTabs".equals(action)) {
                            if (args.isNull(0)) {
                                error.append("menu can not be null");
                                return;
                            }

                            bar.removeAllTabs();
                            tab_callbacks.clear();

                            if (!buildTabs(bar, args.getJSONArray(0))) {
                                error.append("Invalid tab bar definition");
                            }
                        } else if ("setDisplayHomeAsUpEnabled".equals(action)) {
                            if (args.isNull(0)) {
                                error.append("showHomeAsUp can not be null");
                                return;
                            }

                            bar.setDisplayHomeAsUpEnabled(args.getBoolean(0));
                        } else if ("setDisplayOptions".equals(action)) {
                            if (args.isNull(0)) {
                                error.append("options can not be null");
                                return;
                            }

                            final int options = args.getInt(0);
                            bar.setDisplayOptions(options);
                        } else if ("setDisplayShowHomeEnabled".equals(action)) {
                            if (args.isNull(0)) {
                                error.append("showHome can not be null");
                                return;
                            }

                            bar.setDisplayShowHomeEnabled(args.getBoolean(0));
                        } else if ("setDisplayShowTitleEnabled".equals(action)) {
                            if (args.isNull(0)) {
                                error.append("showTitle can not be null");
                                return;
                            }

                            bar.setDisplayShowTitleEnabled(args.getBoolean(0));
                        } else if ("setDisplayUseLogoEnabled".equals(action)) {
                            if (args.isNull(0)) {
                                error.append("useLogo can not be null");
                                return;
                            }

                            bar.setDisplayUseLogoEnabled(args.getBoolean(0));
                        } else if ("setHomeButtonEnabled".equals(action)) {
                            if (args.isNull(0)) {
                                error.append("enabled can not be null");
                                return;
                            }

                            bar.setHomeButtonEnabled(args.getBoolean(0));
                        } else if ("setIcon".equals(action)) {
                            if (args.isNull(0)) {
                                error.append("icon can not be null");
                                return;
                            }

                            Drawable drawable = getDrawableForURI(args.getString(0));
                            bar.setIcon(drawable);
                        } else if ("setListNavigation".equals(action)) {
                            JSONArray items = null;
                            if (args.isNull(0) == false) {
                                items = args.getJSONArray(0);
                            }

                            navigation_adapter.setItems(items);
                            bar.setListNavigationCallbacks(navigation_adapter, navigation_listener);
                        } else if ("setLogo".equals(action)) {
                            if (args.isNull(0)) {
                                error.append("logo can not be null");
                                return;
                            }

                            Drawable drawable = getDrawableForURI(args.getString(0));
                            bar.setLogo(drawable);
                        } else if ("setNavigationMode".equals(action)) {
                            if (args.isNull(0)) {
                                error.append("mode can not be null");
                                return;
                            }

                            final int mode = args.getInt(0);
                            bar.setNavigationMode(mode);
                        } else if ("setSelectedNavigationItem".equals(action)) {
                            if (args.isNull(0)) {
                                error.append("position can not be null");
                                return;
                            }

                            bar.setSelectedNavigationItem(args.getInt(0));
                        } else if ("setSubtitle".equals(action)) {
                            if (args.isNull(0)) {
                                error.append("subtitle can not be null");
                                return;
                            }

                            bar.setSubtitle(args.getString(0));
                        } else if ("setTitle".equals(action)) {
                            if (args.isNull(0)) {
                                error.append("title can not be null");
                                return;
                            }

                            bar.setTitle(args.getString(0));
                        }
                    } catch (JSONException e) {
                        exception = e;
                    } finally {
                        synchronized (this) {
                            this.notify();
                        }
                    }
                }

                // Run task synchronously
                {
                    synchronized (this) {
                        ctx.runOnUiThread(this);
                        this.wait();
                    }
                }
            }.exception;

            if (exception != null) {
                throw exception;
            }
        } catch (InterruptedException e) {
            error.append("Function interrupted on UI thread");
        }
    }

    if (error.length() == 0) {
        if (result.length() > 0) {
            callbackContext.success(result);
        } else {
            callbackContext.success();
        }
    } else {
        callbackContext.error(error.toString());
    }

    return true;
}

From source file:com.urucas.plugins.GmailsenderPlugin.java

License:Open Source License

@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {

    /**//from w  w  w  .ja v a  2  s.c  o m
     *  Javascript code
     *    
     *  cordova.exec(
     *     function(a){}, 
     *     function(err) {}, 
     *     "GmailsenderPlugin", 
     *     "send",
     *     [
     *        ["mail1@gmail.com", "mail2@gmail.com"],
     *        "This is the subject",     
     *        "This is the body"
     *     ]
     *  );
     * 
     */

    if (action.equals("send")) {

        JSONArray toJSON = args.getJSONArray(0);
        String[] toArr = toJSON.join(",").split(",");

        m.setTo(toArr);
        m.setFrom("wooo@wooo.com");
        m.setSubject(args.getString(1));
        m.setBody(args.getString(2));

        try {
            if (m.send()) {
                callbackContext.success();
            } else {
                callbackContext.error("Error sending the email");
            }
        } catch (Exception e) {
            callbackContext.error(e.toString());
        }
        return true;
    }
    return false;
}

From source file:cz.cvut.sedekpav.cordova.bleplugin.BLEPlugin.java

@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
    try {/*from   w  w  w.  ja v  a 2s.co m*/
        if (ACTION_LIST_DEVICES.equals(action)) {
            JSONObject arg_object = args.getJSONObject(0);
            BLE ble = new BLE(cordova.getActivity());
            /*            Intent calIntent = new Intent(Intent.ACTION_EDIT)
             .setType("vnd.android.cursor.item/event")
             .putExtra("beginTime", arg_object.getLong("startTimeMillis"))
             .putExtra("endTime", arg_object.getLong("endTimeMillis"))
             .putExtra("title", arg_object.getString("title"))
             .putExtra("description", arg_object.getString("description"))
             .putExtra("eventLocation", arg_object.getString("eventLocation"));
             this.cordova.getActivity().startActivity(calIntent);*/
            List<BluetoothDevice> devs = ble.listBluetoothDevices();

            JSONArray jArray = new JSONArray();
            for (BluetoothDevice dev : devs) {
                JSONObject json = new JSONObject();
                json.put("address", dev.getAddress().toString());
                json.put("name", dev.getName());
                jArray.put(json);
            }

            callbackContext.success(jArray);
            return true;
        }
        callbackContext.error("Invalid action");
        return false;
    } catch (Exception e) {
        System.err.println("Exception: " + e.getMessage());
        callbackContext.error(e.getMessage());
        return false;
    }
}

From source file:it.nacios.app.dbloader.DBLoader.java

License:Open Source License

private void loadDB(String dbFileName, CallbackContext callbackContext) {
    InputStream in = null;/*from   w w  w . j  a  v  a 2  s  .  co  m*/
    OutputStream out = null;
    if (dbFileName != null && dbFileName.length() > 0) {
        try {
            in = this.webView.getContext().getAssets().open(dbFileName);
            File file = new File(DATA_FOLDER + dbFileName);
            if (file.exists() && file.isFile()) {
                file.delete();
                file.createNewFile();
                out = new FileOutputStream(file);
                byte[] buffer = new byte[4096];
                int bytesRead;
                while ((bytesRead = in.read(buffer)) != -1)
                    out.write(buffer, 0, bytesRead);
                in.close();
                out.close();
                callbackContext.success();
            }

        } catch (IOException e) {
            callbackContext.error("Error: " + e.getMessage());
        }
    } else {
        callbackContext.error("Error: dbFileName not defined");
    }
}

From source file:org.threemusketeers.cordova.plugin.browserpush.BrowserPush.java

License:Apache License

@Override
public boolean execute(String action, JSONArray args, final CallbackContext callbackContext)
        throws JSONException {
    if (action.equals("EventSource.constructor")) {
        EventSource es = new EventSource(args.getString(0), group,
                new CordovaEventSourceNotification(cordova, callbackContext));
        sources.put(args.getString(1), es);
        return true;
    } else if (action.equals("EventSource.close")) {
        EventSource es = sources.get(args.getString(0));
        if (es != null) {
            es.close();/* w w  w .  j  a  v a  2  s .com*/
        } else {
            callbackContext.error("Unable to find EventSource associated");
            Log.d("BrowserPush", "Error finding open source ");
        }
        sources.remove(args.getString(0));
        return true;
    } else if (action.equals("WebSocket.constructor")) {
        WebSocket webSocket = new WebSocket(args.getString(0), group,
                new CordovaWebSocketNotification(cordova, callbackContext));
        webSockets.put(args.getString(1), webSocket);
        return true;
    } else if (action.equals("WebSocket.close")) {
        WebSocket webSocket = webSockets.get(args.getString(0));
        if (webSocket != null) {
            webSocket.close();
        } else {
            callbackContext.error("Unable to find WebSocket associated");
            Log.d("BrowserPush", "Error finding websocket ");
        }
        webSockets.remove(args.getString(0));
        return true;
    } else if (action.equals("WebSocket.send")) {
        WebSocket webSocket = webSockets.get(args.getString(1));
        if (webSocket != null) {
            webSocket.send(args.getString(0));
        } else {
            callbackContext.error("Unable to find WebSocket associated");
            Log.d("BrowserPush", "Error finding websocket ");
        }
        return true;
    }

    return false;
}