Example usage for org.apache.cordova CallbackContext success

List of usage examples for org.apache.cordova CallbackContext success

Introduction

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

Prototype

public void success() 

Source Link

Document

Helper for success callbacks that just returns the Status.OK by default

Usage

From source file:BarcodeReaderPlugin.java

private void createReader(final CallbackContext callbackContext) {
    Log.v(TAG, "createReader: start");

    if ((manager != null) || (barcodeReader != null)) {
        Log.w(TAG, "createReader: AidcManager and/or BarcodeReader objects already created");
        callbackContext.error("reader already created");
        return;//from w w w  . j  a  v  a 2s  .  c  o  m
    }

    // CreatedCallback.onCreated() callback occurs on different thread so use countdown latch to sync
    final CountDownLatch createCompleted = new CountDownLatch(1);

    // Create AIDC Manager object
    Log.v(TAG, "createReader: creating AidcManager");
    AidcManager.create(this.cordova.getActivity().getApplicationContext(), new AidcManager.CreatedCallback() {
        @Override
        public void onCreated(AidcManager aidcManager) {
            Log.v(TAG, "createReader.CreatedCallback: start");
            manager = aidcManager;
            Log.v(TAG, "createReader.CreatedCallback: end");
            createCompleted.countDown();
        }
    });

    // Wait here for onCreated() to complete
    try {
        createCompleted.await();
    } catch (InterruptedException ex) {
    }
    ;
    Log.v(TAG, "createReader: AidcManager created");

    // Get BarcodeReader object from AIDC Manager
    Log.v(TAG, "createReader: creating BarcodeReader");
    barcodeReader = manager.createBarcodeReader();

    if (barcodeReader != null) {
        Log.v(TAG, "createReader: BarcodeReader created");

        try {
            // Set trigger mode to "auto" so we don't have to handle the trigger ourselves
            Log.v(TAG, "createReader: setting trigger property to auto");
            barcodeReader.setProperty(BarcodeReader.PROPERTY_TRIGGER_CONTROL_MODE,
                    BarcodeReader.TRIGGER_CONTROL_MODE_AUTO_CONTROL);
        } catch (UnsupportedPropertyException ex) {
            Log.e(TAG, "createReader: failed to set trigger control to auto");
            closeReader();
            callbackContext.error("failed to set trigger control to auto");
            return;
        }

        try {
            // Claim the scanner now
            Log.v(TAG, "createReader: claiming barcode reader");
            barcodeReader.claim();
        } catch (ScannerUnavailableException ex) {
            Log.e(TAG, "createReader: failed to claim barcode reader");
            closeReader();
            callbackContext.error("failed to claim barcode reader");
            return;
        }

        Log.v(TAG, "createReader: successfully created and initialized barcode reader");
        callbackContext.success();
    } else {
        Log.e(TAG, "createReader: failed to create BarcodeReader object");
        callbackContext.error("failed to create barcode reader");
        return;
    }

    Log.v(TAG, "createReader: end");
}

From source file:BarcodeReaderPlugin.java

private void setProperties(CallbackContext callbackContext, JSONArray args) {
    Log.v(TAG, "setProperties");

    // Return now if barcode reader object not created yet
    if (barcodeReader == null) {
        Log.e(TAG, "setProperties: barcodeReader is null");
        callbackContext.error("Barcode reader not created");
        return;//from  w  w w .  ja  va2 s .  co m
    }

    // We are expecting one or more name,value pairs so args.length should be an even number
    int length = args.length();
    Log.v(TAG, "setProperties: args.length=" + length);
    if ((length % 2) != 0) {
        Log.e(TAG, "setProperties: odd number of elements in argument array");
        callbackContext.error("odd number of elements in argument array");
        return;
    }

    // Convert JSONArray into HashMap of pair,value elements representing properties to set
    Map<String, Object> properties = new HashMap<String, Object>();
    String propName;
    Object propValue;
    try {
        for (int i = 0; i < length; /*empty*/) {
            // Name should always be a string
            propName = args.getString(i++);
            Log.v(TAG, "setProperties: propName=" + propName);

            // Value could be boolean, integer or string
            propValue = args.get(i++);
            Log.v(TAG, "setProperties: propValue=" + propValue);

            // We don't currently expose trigger events so do not allow changes to trigger control mode
            if (propName.equals(BarcodeReader.PROPERTY_TRIGGER_CONTROL_MODE)) {
                Log.w(TAG, "PROPERTY_TRIGGER_CONTROL_MODE property ignored");
                continue;
            }

            // Add name,value pair into map
            properties.put(propName, propValue);
            Log.v(TAG, "setProperties: property added to map");
        }
    } catch (JSONException ex) {
        Log.e(TAG, "error occurred converting JSONArray to HashMap - " + ex.getMessage());
        callbackContext.error("invalid elements in argument array");
        return;
    }

    // Apply properties using map
    // BarcodeReader.setProperties() seems to ignore invalid property names
    barcodeReader.setProperties(properties);
    Log.v(TAG, "setProperties: barcode reader properties set");
    callbackContext.success();
    return;
}

From source file:ai.api.ApiAiPlugin.java

License:Apache License

public void init(final JSONObject argObject, CallbackContext callbackContext) {
    try {/*from ww  w. jav  a 2s .  co m*/

        final String baseURL = argObject.optString("baseURL", "https://api.api.ai/v1/");
        final String clientAccessToken = argObject.getString("clientAccessToken");
        final String subscriptionKey = argObject.getString("subscriptionKey");
        final String language = argObject.optString("lang", "en");
        final boolean debugMode = argObject.optBoolean("debug", false);
        final String version = argObject.optString("version", "20150910");

        final AIConfiguration.SupportedLanguages lang = AIConfiguration.SupportedLanguages
                .fromLanguageTag(language);
        final AIConfiguration config = new AIConfiguration(clientAccessToken, subscriptionKey, lang,
                AIConfiguration.RecognitionEngine.System);

        if (!TextUtils.isEmpty(version)) {
            config.setProtocolVersion(version);
        }

        aiService = AIService.getService(this.cordova.getActivity().getApplicationContext(), config);
        aiService.setListener(this);

        if (aiService instanceof GoogleRecognitionServiceImpl) {
            ((GoogleRecognitionServiceImpl) aiService)
                    .setRecognitionResultsListener(new RecognitionResultsListener() {
                        @Override
                        public void onPartialResults(final List<String> partialResults) {
                            ApiAiPlugin.this.onPartialResults(partialResults);
                        }

                        @Override
                        public void onRecognitionResults(final List<String> recognitionResults) {
                            ApiAiPlugin.this.onRecognitionResults(recognitionResults);
                        }
                    });
        }

        callbackContext.success();
    } catch (Exception ex) {
        Log.e(TAG, "Init", ex);
        callbackContext.error(ex.toString());
    }
}

From source file:ai.api.ApiAiPlugin.java

License:Apache License

public void stopListening(CallbackContext callbackContext) {
    try {/*from ww w .  ja  va 2s .  com*/
        aiService.stopListening();
        callbackContext.success();
    } catch (Exception ex) {
        Log.e(TAG, "stopListening", ex);
        callbackContext.error(ex.getMessage());
    }
}

From source file:ai.api.ApiAiPlugin.java

License:Apache License

public void cancelAllRequests(CallbackContext callbackContext) {
    try {//from   w  w  w . j  a v  a  2s. c o  m
        aiService.cancel();
        callbackContext.success();
    } catch (Exception ex) {
        Log.e(TAG, "cancelAllRequests", ex);
        callbackContext.error(ex.getMessage());
    }
}

From source file:au.com.micropacific.cordova.CipherlabRS30Plugin.java

License:Open Source License

public void initialise(CallbackContext callbackContext) {
    Log.v("CipherlabRS30Plugin", "CipherlabRS30Plugin.initialise()");
    try {/*from   ww w  . ja v  a2  s .  c  o m*/
        Log.v("CipherlabRS30Plugin", "com.cipherlab.barcode.GeneralString.Intent_SOFTTRIGGER_DATA: "
                + com.cipherlab.barcode.GeneralString.Intent_SOFTTRIGGER_DATA);
        if (cordova.getActivity() == null) {
            Log.v("CipherlabRS30Plugin", "cordova.getActivity() is null");
        } else {
            Log.v("CipherlabRS30Plugin", "cordova.getActivity() is something");
        }

        filter = new IntentFilter();
        filter.addAction(com.cipherlab.barcode.GeneralString.Intent_SOFTTRIGGER_DATA);
        filter.addAction(com.cipherlab.barcode.GeneralString.Intent_PASS_TO_APP);
        filter.addAction(com.cipherlab.barcode.GeneralString.Intent_READERSERVICE_CONNECTED);

        mReaderManager = ReaderManager.InitInstance(cordova.getActivity());
        mDataReceiver = new DataReceiver(this, this.mReaderManager);
        cordova.getActivity().registerReceiver(mDataReceiver, filter);

        Log.v("CipherlabRS30Plugin", "Got reader");
    } catch (Exception e) {
        StringWriter writer = new StringWriter();
        PrintWriter printWriter = new PrintWriter(writer);
        e.printStackTrace(printWriter);
        printWriter.flush();

        String stackTrace = writer.toString();

        Log.v("CipherlabRS30Plugin", "Error starting reader manager: " + stackTrace);
    }

    Log.v("CipherlabRS30Plugin", "CipherlabRS30Plugin.initialise() Done");

    callbackContext.success();
}

From source file:br.com.brunogrossi.MediaScannerPlugin.MediaScannerPlugin.java

License:Open Source License

@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
    try {/*from w  w w.ja va2  s  .  c  om*/
        if (action.equals("scanFile")) {
            String fileUri = args.optString(0);
            if (fileUri != null && !fileUri.equals("")) {
                Uri contentUri = Uri.parse(fileUri);

                Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                mediaScanIntent.setData(contentUri);
                this.cordova.getActivity().sendBroadcast(mediaScanIntent);

                callbackContext.success();

                return true;
            } else {
                Log.w(TAG, "No action param provided: " + action);
                callbackContext.error("No action param provided: " + action);
                return false;
            }
        } else {
            Log.w(TAG, "Wrong action was provided: " + action);
            return false;
        }
    } catch (RuntimeException e) {
        e.printStackTrace();
        callbackContext.error(e.getMessage());
        return false;
    }
}

From source file:br.com.calangodev.aacdecoder.AacDecoder.java

License:Apache License

@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
    if (this.cordova.getActivity().isFinishing())
        return true;
    /*(if (action.equals("mediaPlayer")) {
    Log.v(TAG,"ESTAMOS NA ACTION mediaPlayer");
    return true;//from  www  .  j a  va  2 s  .c om
    }
    else {
    return false;
    }*/
    if (action.equals("mediaPlayer")) {
        Log.v(TAG, "ESTAMOS AQUI em MEdia player: URL: " + args.getString(0));
        //            try{
        //                multiPlayer = new MultiPlayer();
        //                multiPlayer.playAsync(args.getString(0));
        //            } catch (Throwable t){
        //                Log.w(TAG, "Cannot play url - " + t );
        //            }
        startMediaPlayer(args.getString(0));

    } else if (action.equals("stopMediaPlayer")) {
        stop();
    } else if (action.equals("readyState")) {
        // verificar se tem um buffer maior que 2

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

From source file:br.com.futuring.cordova.plugins.GooglePlayGame.java

License:Apache License

private void executeSignOut(final CallbackContext callbackContext) {
    Log.d(LOGTAG, "executeSignOut");

    cordova.getActivity().runOnUiThread(new Runnable() {
        @Override/*  ww w .  j  a  va 2s .  co  m*/
        public void run() {
            gameHelper.signOut();
            callbackContext.success();
        }
    });
}

From source file:br.com.futuring.cordova.plugins.GooglePlayGame.java

License:Apache License

private void executeSubmitScore(final JSONObject options, final CallbackContext callbackContext)
        throws JSONException {
    Log.d(LOGTAG, "executeSubmitScore");

    cordova.getActivity().runOnUiThread(new Runnable() {
        @Override//ww w.  j  av  a2  s  .  c  om
        public void run() {
            try {
                if (gameHelper.isSignedIn()) {
                    Games.Leaderboards.submitScore(gameHelper.getApiClient(),
                            options.getString("leaderboardId"), options.getInt("score"));
                    callbackContext.success();
                } else {
                    callbackContext.error("executeSubmitScore: not yet signed in");
                }
            } catch (JSONException e) {
                Log.w(LOGTAG, "executeSubmitScore: unexpected error", e);
                callbackContext.error("executeSubmitScore: error while submitting score");
            }
        }
    });
}