Search in sources :

Example 21 with PluginResult

use of org.apache.cordova.api.PluginResult in project cordova-android-chromeview by thedracle.

the class AccelListener method fail.

// Sends an error back to JS
private void fail(int code, String message) {
    // Error object
    JSONObject errorObj = new JSONObject();
    try {
        errorObj.put("code", code);
        errorObj.put("message", message);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    PluginResult err = new PluginResult(PluginResult.Status.ERROR, errorObj);
    err.setKeepCallback(true);
    callbackContext.sendPluginResult(err);
}
Also used : JSONObject(org.json.JSONObject) PluginResult(org.apache.cordova.api.PluginResult) JSONException(org.json.JSONException)

Example 22 with PluginResult

use of org.apache.cordova.api.PluginResult in project cordova-android-chromeview by thedracle.

the class AccelListener method win.

private void win() {
    // Success return object
    PluginResult result = new PluginResult(PluginResult.Status.OK, this.getAccelerationJSON());
    result.setKeepCallback(true);
    callbackContext.sendPluginResult(result);
}
Also used : PluginResult(org.apache.cordova.api.PluginResult)

Example 23 with PluginResult

use of org.apache.cordova.api.PluginResult in project cordova-android-chromeview by thedracle.

the class AudioHandler method execute.

/**
     * Executes the request and returns PluginResult.
     * @param action 		The action to execute.
     * @param args 			JSONArry of arguments for the plugin.
     * @param callbackContext		The callback context used when calling back into JavaScript.
     * @return 				A PluginResult object with a status and message.
     */
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
    PluginResult.Status status = PluginResult.Status.OK;
    String result = "";
    if (action.equals("startRecordingAudio")) {
        this.startRecordingAudio(args.getString(0), FileHelper.stripFileProtocol(args.getString(1)));
    } else if (action.equals("stopRecordingAudio")) {
        this.stopRecordingAudio(args.getString(0));
    } else if (action.equals("startPlayingAudio")) {
        this.startPlayingAudio(args.getString(0), FileHelper.stripFileProtocol(args.getString(1)));
    } else if (action.equals("seekToAudio")) {
        this.seekToAudio(args.getString(0), args.getInt(1));
    } else if (action.equals("pausePlayingAudio")) {
        this.pausePlayingAudio(args.getString(0));
    } else if (action.equals("stopPlayingAudio")) {
        this.stopPlayingAudio(args.getString(0));
    } else if (action.equals("setVolume")) {
        try {
            this.setVolume(args.getString(0), Float.parseFloat(args.getString(1)));
        } catch (NumberFormatException nfe) {
        //no-op
        }
    } else if (action.equals("getCurrentPositionAudio")) {
        float f = this.getCurrentPositionAudio(args.getString(0));
        callbackContext.sendPluginResult(new PluginResult(status, f));
        return true;
    } else if (action.equals("getDurationAudio")) {
        float f = this.getDurationAudio(args.getString(0), args.getString(1));
        callbackContext.sendPluginResult(new PluginResult(status, f));
        return true;
    } else if (action.equals("create")) {
        String id = args.getString(0);
        String src = FileHelper.stripFileProtocol(args.getString(1));
        AudioPlayer audio = new AudioPlayer(this, id, src);
        this.players.put(id, audio);
    } else if (action.equals("release")) {
        boolean b = this.release(args.getString(0));
        callbackContext.sendPluginResult(new PluginResult(status, b));
        return true;
    } else {
        // Unrecognized action.
        return false;
    }
    callbackContext.sendPluginResult(new PluginResult(status, result));
    return true;
}
Also used : PluginResult(org.apache.cordova.api.PluginResult)

Example 24 with PluginResult

use of org.apache.cordova.api.PluginResult in project cordova-android-chromeview by thedracle.

the class Capture method onActivityResult.

/**
     * Called when the video view exits.
     *
     * @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").
     * @throws JSONException
     */
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    // Result received okay
    if (resultCode == Activity.RESULT_OK) {
        // An audio clip was requested
        if (requestCode == CAPTURE_AUDIO) {
            // Get the uri of the audio clip
            Uri data = intent.getData();
            // create a file object from the uri
            results.put(createMediaFile(data));
            if (results.length() >= limit) {
                // Send Uri back to JavaScript for listening to audio
                this.callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, results));
            } else {
                // still need to capture more audio clips
                captureAudio();
            }
        } else if (requestCode == CAPTURE_IMAGE) {
            // To work around it I had to grab the code from CameraLauncher.java
            try {
                // Create entry in media store for image
                // (Don't use insertImage() because it uses default compression setting of 50 - no way to change it)
                ContentValues values = new ContentValues();
                values.put(android.provider.MediaStore.Images.Media.MIME_TYPE, IMAGE_JPEG);
                Uri uri = null;
                try {
                    uri = this.cordova.getActivity().getContentResolver().insert(android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
                } catch (UnsupportedOperationException e) {
                    LOG.d(LOG_TAG, "Can't write to external media storage.");
                    try {
                        uri = this.cordova.getActivity().getContentResolver().insert(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI, values);
                    } catch (UnsupportedOperationException ex) {
                        LOG.d(LOG_TAG, "Can't write to internal media storage.");
                        this.fail(createErrorObject(CAPTURE_INTERNAL_ERR, "Error capturing image - no media storage found."));
                        return;
                    }
                }
                FileInputStream fis = new FileInputStream(DirectoryManager.getTempDirectoryPath(this.cordova.getActivity()) + "/Capture.jpg");
                OutputStream os = this.cordova.getActivity().getContentResolver().openOutputStream(uri);
                byte[] buffer = new byte[4096];
                int len;
                while ((len = fis.read(buffer)) != -1) {
                    os.write(buffer, 0, len);
                }
                os.flush();
                os.close();
                fis.close();
                // Add image to results
                results.put(createMediaFile(uri));
                checkForDuplicateImage();
                if (results.length() >= limit) {
                    // Send Uri back to JavaScript for viewing image
                    this.callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, results));
                } else {
                    // still need to capture more images
                    captureImage();
                }
            } catch (IOException e) {
                e.printStackTrace();
                this.fail(createErrorObject(CAPTURE_INTERNAL_ERR, "Error capturing image."));
            }
        } else if (requestCode == CAPTURE_VIDEO) {
            // Get the uri of the video clip
            Uri data = intent.getData();
            // create a file object from the uri
            results.put(createMediaFile(data));
            if (results.length() >= limit) {
                // Send Uri back to JavaScript for viewing video
                this.callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, results));
            } else {
                // still need to capture more video clips
                captureVideo(duration);
            }
        }
    } else // If canceled
    if (resultCode == Activity.RESULT_CANCELED) {
        // If we have partial results send them back to the user
        if (results.length() > 0) {
            this.callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, results));
        } else // user canceled the action
        {
            this.fail(createErrorObject(CAPTURE_NO_MEDIA_FILES, "Canceled."));
        }
    } else // If something else
    {
        // If we have partial results send them back to the user
        if (results.length() > 0) {
            this.callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, results));
        } else // something bad happened
        {
            this.fail(createErrorObject(CAPTURE_NO_MEDIA_FILES, "Did not complete!"));
        }
    }
}
Also used : ContentValues(android.content.ContentValues) PluginResult(org.apache.cordova.api.PluginResult) OutputStream(java.io.OutputStream) IOException(java.io.IOException) Uri(android.net.Uri) FileInputStream(java.io.FileInputStream)

Example 25 with PluginResult

use of org.apache.cordova.api.PluginResult in project cordova-android-chromeview by thedracle.

the class CompassListener method execute.

/**
     * Executes the request and returns PluginResult.
     *
     * @param action                The action to execute.
     * @param args          	    JSONArry of arguments for the plugin.
     * @param callbackS=Context     The callback id used when calling back into JavaScript.
     * @return              	    True if the action was valid.
     * @throws JSONException 
     */
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
    if (action.equals("start")) {
        this.start();
    } else if (action.equals("stop")) {
        this.stop();
    } else if (action.equals("getStatus")) {
        int i = this.getStatus();
        callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, i));
    } else if (action.equals("getHeading")) {
        // If not running, then this is an async call, so don't worry about waiting
        if (this.status != CompassListener.RUNNING) {
            int r = this.start();
            if (r == CompassListener.ERROR_FAILED_TO_START) {
                callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.IO_EXCEPTION, CompassListener.ERROR_FAILED_TO_START));
                return true;
            }
            // Set a timeout callback on the main thread.
            Handler handler = new Handler(Looper.getMainLooper());
            handler.postDelayed(new Runnable() {

                public void run() {
                    CompassListener.this.timeout();
                }
            }, 2000);
        }
        callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, getCompassHeading()));
    } else if (action.equals("setTimeout")) {
        this.setTimeout(args.getLong(0));
    } else if (action.equals("getTimeout")) {
        long l = this.getTimeout();
        callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, l));
    } else {
        // Unsupported action
        return false;
    }
    return true;
}
Also used : PluginResult(org.apache.cordova.api.PluginResult) Handler(android.os.Handler)

Aggregations

PluginResult (org.apache.cordova.api.PluginResult)27 JSONException (org.json.JSONException)10 JSONObject (org.json.JSONObject)9 IOException (java.io.IOException)5 FileNotFoundException (java.io.FileNotFoundException)4 AlertDialog (android.app.AlertDialog)3 DialogInterface (android.content.DialogInterface)3 MalformedURLException (java.net.MalformedURLException)3 CordovaInterface (org.apache.cordova.api.CordovaInterface)3 Intent (android.content.Intent)2 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 FileOutputStream (java.io.FileOutputStream)2 OutputStream (java.io.OutputStream)2 HttpURLConnection (java.net.HttpURLConnection)2 URL (java.net.URL)2 HostnameVerifier (javax.net.ssl.HostnameVerifier)2 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)2 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)2 JSONArray (org.json.JSONArray)2