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.google.cordova.ChromeStorage.java

License:Open Source License

private void getBytesInUse(final CordovaArgs args, final CallbackContext callbackContext) {
    executorService.execute(new Runnable() {
        @Override// ww w . j  a  va  2  s.  co  m
        public void run() {
            //Don't use default values as the keys that don't have values in storage don't affect size
            JSONObject storage = getStoredValuesForKeys(args, /*useDefaultValues*/ false);

            if (storage == null) {
                callbackContext.error("Could not retrieve storage");
            } else {
                callbackContext.success(storage.toString().getBytes().length);
            }
        }
    });
}

From source file:com.google.cordova.ChromeStorage.java

License:Open Source License

private void set(final CordovaArgs args, final CallbackContext callbackContext) {
    executorService.execute(new Runnable() {
        @Override/*from   w ww  . j  av  a  2  s  . c om*/
        public void run() {
            try {
                boolean sync = args.getBoolean(0);
                JSONObject jsonObject = (JSONObject) args.getJSONObject(1);
                JSONArray keyArray = jsonObject.names();
                JSONObject oldValues = new JSONObject();

                if (keyArray != null) {
                    List<String> keys = JSONUtils.toStringList(keyArray);
                    JSONObject storage = getStorage(sync);
                    for (String key : keys) {
                        Object oldValue = storage.opt(key);
                        if (oldValue != null) {
                            oldValues.put(key, oldValue);
                        }
                        storage.put(key, jsonObject.get(key));
                    }
                    setStorage(sync, storage);
                }
                callbackContext.success(oldValues);
            } catch (Exception e) {
                Log.e(LOG_TAG, "Could not update storage", e);
                callbackContext.error("Could not update storage");
            }
        }
    });
}

From source file:com.google.cordova.ChromeStorage.java

License:Open Source License

private void remove(final CordovaArgs args, final CallbackContext callbackContext) {
    executorService.execute(new Runnable() {
        @Override/*ww  w  . j a v a  2 s.  c o m*/
        public void run() {
            try {
                boolean sync = args.getBoolean(0);
                JSONObject jsonObject = (JSONObject) args.optJSONObject(1);
                JSONArray jsonArray = args.optJSONArray(1);
                boolean isNull = args.isNull(1);
                List<String> keys = new ArrayList<String>();
                JSONObject oldValues = new JSONObject();

                if (jsonObject != null) {
                    keys = JSONUtils.toStringList(jsonObject.names());
                } else if (jsonArray != null) {
                    keys = JSONUtils.toStringList(jsonArray);
                } else if (isNull) {
                    keys = null;
                }

                if (keys != null && !keys.isEmpty()) {
                    JSONObject storage = getStorage(sync);
                    for (String key : keys) {
                        Object oldValue = storage.opt(key);
                        if (oldValue != null) {
                            oldValues.put(key, oldValue);
                        }
                        storage.remove(key);
                    }
                    setStorage(sync, storage);
                }
                callbackContext.success(oldValues);
            } catch (Exception e) {
                Log.e(LOG_TAG, "Could not update storage", e);
                callbackContext.error("Could not update storage");
            }
        }
    });
}

From source file:com.google.cordova.ChromeStorage.java

License:Open Source License

private void clear(final CordovaArgs args, final CallbackContext callbackContext) {
    executorService.execute(new Runnable() {
        @Override//from w ww . j a va  2  s  . c  om
        public void run() {
            try {
                boolean sync = args.getBoolean(0);
                JSONObject oldValues = getStorage(sync);
                setStorage(sync, new JSONObject());
                callbackContext.success(oldValues);
            } catch (Exception e) {
                Log.e(LOG_TAG, "Could not clear storage", e);
                callbackContext.error("Could not update storage");
            }
        }
    });
}

From source file:com.google.zxing.client.android.plugin.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.
 *//*  ww w.ja v  a  2  s .  c  om*/
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {

    this.cbContext = callbackContext;

    if (action.equals(SCAN)) {
        String mode = "";
        if (args.length() > 0) {
            try {
                mode = args.getString(0);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        scan(mode);

    } else {
        callbackContext.error("Invalid Action");

        return false;
    }

    return true;
}

From source file:com.jingle.zxing.plugin.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  www  . j  a  v a  2  s .c om
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {

    this.cbContext = callbackContext;

    if (action.equals(SCAN)) {
        String mode = All_MODE;
        if (args.length() > 0) {
            try {
                mode = args.getString(0);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        mode = ONE_MODE;
        scan(mode);

    } else {
        callbackContext.error("Invalid Action");

        return false;
    }

    return true;
}

From source file:com.mclinic.plugin.MuzimaPlugin.java

License:Apache License

protected void initialize(final String server, final String username, final String password,
        final CallbackContext callbackContext) {

    Log.i(TAG, "Initializing internal structure of the plugins.");
    if (FileUtils.storageReady()) {
        MuzimaModule muzimaModule = new MuzimaModule(lucenePath, defaultKey);
        muzimaModule.setServer(server);/* w  w w.  j  a  v a 2  s.c  o m*/
        muzimaModule.setUsername(username);
        muzimaModule.setPassword(password);
        Context.initialize(muzimaModule);

        AdministrativeService administrativeService = Context.getInstance(AdministrativeService.class);
        administrativeService.initializeRepository(configurationPath);
        Log.i(TAG, "Internal structure of plugins is initialized.");
        // initialize with the current user
        // TODO: need to make sure the timeout is short when performing any download this!
        administrativeService.downloadUsers(username);
        // validate the user
        UserService userService = Context.getInstance(UserService.class);
        User user = userService.getUserByUsername(username);
        if (user != null) {
            if (StringUtil.isEmpty(user.getSalt()) && StringUtil.isEmpty(user.getPassword())) {
                String salt = UUID.randomUUID().toString();
                String hashedPassword = DigestUtils.generateChecksum(password + ":" + salt);
                user.setSalt(salt);
                user.setPassword(hashedPassword);
                userService.updateUser(user);
                callbackContext.success(hashedPassword);
            } else {
                String hashedPassword = DigestUtils.generateChecksum(password + ":" + user.getSalt());
                if (StringUtil.equals(hashedPassword, user.getPassword()))
                    callbackContext.success(hashedPassword);
                else
                    callbackContext.error("User is not authorized to access this app!");
                userService.deleteUser(user);
            }
        } else {
            callbackContext.error("User is not authorized to access this app!");
        }
    }
}

From source file:com.phonegap.plugins.ExternalAppLauncher.ExternalAppLauncher.java

License:BSD License

@Override
public boolean execute(String action, JSONArray data, CallbackContext callbackContext) {

    Context context = cordova.getActivity().getApplicationContext();
    PackageManager pm = cordova.getActivity().getPackageManager();

    String packageName;/* w w w.ja  v  a2  s.co m*/
    Boolean result = true;

    try {
        packageName = data.getString(0);
    } catch (JSONException e) {
        return false;
    }

    if (LAUNCH_APP.equals(action)) {
        result = launchApp(context, pm, packageName);
    } else if (LAUNCH_MARKET.equals(action)) {
        result = launchMarket(context, packageName);
    }

    if (result == true) {
        callbackContext.success(0);
        return true;
    } else {
        callbackContext.error(0);
        return false;
    }
}

From source file:com.phonegap.plugins.FloatingActivityPlugin.FloatingActivityPlugin.java

License:MIT License

@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
    // TODO Auto-generated method stub
    Context context = cordova.getActivity().getApplicationContext();
    PackageManager pm = cordova.getActivity().getPackageManager();
    _checkerService = null;/*from w ww  .j  a v a2  s  .co m*/
    String packageName;
    Boolean result = true;

    try {
        packageName = args.getString(0);
    } catch (JSONException e) {
        return false;
    }
    result = launch(pm, context, packageName, context);
    if (result == true) {
        callbackContext.success(0);
        return true;
    } else {
        callbackContext.error(0);
        return false;
    }
}

From source file:com.phonegap.plugins.speech.SpeechRecognizer.java

License:Open Source License

public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {

    this.cbContext = callbackContext;

    // Dispatcher
    if (ACTION_INIT.equals(action)) {

        if (DoInit())
            return true;
        else/*from ww w .j a  va  2  s. c o m*/
            this.cbContext.error("Speech Not Initialized or Unavailable");
        return false;

    } else if (ACTION_SPEECH_RECOGNIZE.equals(action)) {
        // recognize speech
        if (!recognizerPresent) {
            callbackContext.error(NOT_PRESENT_MESSAGE);
            return false;
        }
        if (!this.speechRecognizerCallbackId.equals("")) {
            callbackContext.error("Speech recognition is in progress.");
            return false;
        }

        startSpeechRecognitionActivity(args);

    } else {
        // Invalid action
        this.cbContext.error("Unknown action: " + action);
        return false;
    }
    return true;
}