Search in sources :

Example 6 with PluginResult

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

the class NativeToJsMessageQueueTest method testPopAndEncode.

//This test is for the undocumented encoding system setup for the bridge
//TODO: Document how the non-Javascript bridges are supposed to work
@Test
public void testPopAndEncode() {
    NativeToJsMessageQueue.BridgeMode bridge;
    bridge = new NativeToJsMessageQueue.NoOpBridgeMode();
    queue.addBridgeMode(bridge);
    queue.setBridgeMode(0);
    PluginResult result = new PluginResult(PluginResult.Status.OK);
    queue.addPluginResult(result, TEST_CALLBACK_ID);
    assertFalse(queue.isEmpty());
    String resultString = queue.popAndEncode(false);
    String[] results = resultString.split(" ");
    assertEquals(TEST_CALLBACK_ID, results[2]);
}
Also used : NativeToJsMessageQueue(org.apache.cordova.NativeToJsMessageQueue) PluginResult(org.apache.cordova.PluginResult) Test(org.junit.Test)

Example 7 with PluginResult

use of org.apache.cordova.PluginResult in project TelephoneNumberPlugin by macdonst.

the class TelephoneNumber method execute.

public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {
    if (action.equals("get")) {
        TelephonyManager telephonyManager = (TelephonyManager) this.cordova.getActivity().getSystemService(Context.TELEPHONY_SERVICE);
        String result = telephonyManager.getLine1Number();
        if (result != null) {
            callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, result));
            return true;
        }
    }
    callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR));
    return false;
}
Also used : PluginResult(org.apache.cordova.PluginResult) TelephonyManager(android.telephony.TelephonyManager)

Example 8 with PluginResult

use of org.apache.cordova.PluginResult in project jpHolo by teusink.

the class InAppBrowser method sendUpdate.

/**
     * 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
     */
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;
        }
    }
}
Also used : PluginResult(org.apache.cordova.PluginResult)

Example 9 with PluginResult

use of org.apache.cordova.PluginResult in project jpHolo by teusink.

the class InAppChromeClient method onJsPrompt.

/**
     * Tell the client to display a prompt dialog to the user.
     * If the client returns true, WebView will assume that the client will
     * handle the prompt dialog and call the appropriate JsPromptResult method.
     *
     * The prompt bridge provided for the InAppBrowser is capable of executing any
     * oustanding callback belonging to the InAppBrowser plugin. Care has been
     * taken that other callbacks cannot be triggered, and that no other code
     * execution is possible.
     *
     * To trigger the bridge, the prompt default value should be of the form:
     *
     * gap-iab://<callbackId>
     *
     * where <callbackId> is the string id of the callback to trigger (something
     * like "InAppBrowser0123456789")
     *
     * If present, the prompt message is expected to be a JSON-encoded value to
     * pass to the callback. A JSON_EXCEPTION is returned if the JSON is invalid.
     *
     * @param view
     * @param url
     * @param message
     * @param defaultValue
     * @param result
     */
@Override
public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, JsPromptResult result) {
    // See if the prompt string uses the 'gap-iab' protocol. If so, the remainder should be the id of a callback to execute.
    if (defaultValue != null && defaultValue.startsWith("gap")) {
        if (defaultValue.startsWith("gap-iab://")) {
            PluginResult scriptResult;
            String scriptCallbackId = defaultValue.substring(10);
            if (scriptCallbackId.startsWith("InAppBrowser")) {
                if (message == null || message.length() == 0) {
                    scriptResult = new PluginResult(PluginResult.Status.OK, new JSONArray());
                } else {
                    try {
                        scriptResult = new PluginResult(PluginResult.Status.OK, new JSONArray(message));
                    } catch (JSONException e) {
                        scriptResult = new PluginResult(PluginResult.Status.JSON_EXCEPTION, e.getMessage());
                    }
                }
                this.webView.sendPluginResult(scriptResult, scriptCallbackId);
                result.confirm("");
                return true;
            }
        } else {
            // Anything else with a gap: prefix should get this message
            LOG.w(LOG_TAG, "InAppBrowser does not support Cordova API calls: " + url + " " + defaultValue);
            result.cancel();
            return true;
        }
    }
    return false;
}
Also used : PluginResult(org.apache.cordova.PluginResult) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException)

Example 10 with PluginResult

use of org.apache.cordova.PluginResult in project jpHolo by teusink.

the class NetworkManager method sendUpdate.

/**
     * Create a new plugin result and send it back to JavaScript
     *
     * @param connection the network info to set as navigator.connection
     */
private void sendUpdate(String type) {
    if (connectionCallbackContext != null) {
        PluginResult result = new PluginResult(PluginResult.Status.OK, type);
        result.setKeepCallback(true);
        connectionCallbackContext.sendPluginResult(result);
    }
    webView.postMessage("networkconnection", type);
}
Also used : PluginResult(org.apache.cordova.PluginResult)

Aggregations

PluginResult (org.apache.cordova.PluginResult)45 JSONException (org.json.JSONException)19 JSONObject (org.json.JSONObject)15 IOException (java.io.IOException)6 JSONArray (org.json.JSONArray)6 Uri (android.net.Uri)4 FileNotFoundException (java.io.FileNotFoundException)4 AlertDialog (android.app.AlertDialog)3 DialogInterface (android.content.DialogInterface)3 Intent (android.content.Intent)3 TextView (android.widget.TextView)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 File (java.io.File)3 OutputStream (java.io.OutputStream)3 CallbackContext (org.apache.cordova.CallbackContext)3 CordovaInterface (org.apache.cordova.CordovaInterface)3 CordovaResourceApi (org.apache.cordova.CordovaResourceApi)3 NativeToJsMessageQueue (org.apache.cordova.NativeToJsMessageQueue)3 Test (org.junit.Test)3 SharedPreferences (android.content.SharedPreferences)2