Search in sources :

Example 61 with PluginResult

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

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 {
    CordovaResourceApi resourceApi = webView.getResourceApi();
    PluginResult.Status status = PluginResult.Status.OK;
    String result = "";
    if (action.equals("startRecordingAudio")) {
        String target = args.getString(1);
        String fileUriStr;
        try {
            Uri targetUri = resourceApi.remapUri(Uri.parse(target));
            fileUriStr = targetUri.toString();
        } catch (IllegalArgumentException e) {
            fileUriStr = target;
        }
        this.startRecordingAudio(args.getString(0), FileHelper.stripFileProtocol(fileUriStr));
    } else if (action.equals("stopRecordingAudio")) {
        this.stopRecordingAudio(args.getString(0));
    } else if (action.equals("startPlayingAudio")) {
        String target = args.getString(1);
        String fileUriStr;
        try {
            Uri targetUri = resourceApi.remapUri(Uri.parse(target));
            fileUriStr = targetUri.toString();
        } catch (IllegalArgumentException e) {
            fileUriStr = target;
        }
        this.startPlayingAudio(args.getString(0), FileHelper.stripFileProtocol(fileUriStr));
    } 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.PluginResult) CordovaResourceApi(org.apache.cordova.CordovaResourceApi) Uri(android.net.Uri)

Example 62 with PluginResult

use of org.apache.cordova.PluginResult in project cordova-androidwear by tgardner.

the class AndroidWearPlugin method keepCallback.

private void keepCallback(final CallbackContext callbackContext, JSONObject message) {
    PluginResult r = new PluginResult(PluginResult.Status.OK, message);
    r.setKeepCallback(true);
    callbackContext.sendPluginResult(r);
}
Also used : PluginResult(org.apache.cordova.PluginResult)

Example 63 with PluginResult

use of org.apache.cordova.PluginResult in project phonegap-plugin-push by phonegap.

the class PushPlugin method execute.

@Override
public boolean execute(final String action, final JSONArray data, final CallbackContext callbackContext) {
    Log.v(LOG_TAG, "execute: action=" + action);
    gWebView = this.webView;
    if (INITIALIZE.equals(action)) {
        cordova.getThreadPool().execute(new Runnable() {

            public void run() {
                pushContext = callbackContext;
                JSONObject jo = null;
                Log.v(LOG_TAG, "execute: data=" + data.toString());
                SharedPreferences sharedPref = getApplicationContext().getSharedPreferences(COM_ADOBE_PHONEGAP_PUSH, Context.MODE_PRIVATE);
                String token = null;
                String senderID = null;
                try {
                    jo = data.getJSONObject(0).getJSONObject(ANDROID);
                    // If no NotificationChannels exist create the default one
                    createDefaultNotificationChannelIfNeeded(jo);
                    Log.v(LOG_TAG, "execute: jo=" + jo.toString());
                    senderID = getStringResourceByName(GCM_DEFAULT_SENDER_ID);
                    Log.v(LOG_TAG, "execute: senderID=" + senderID);
                    try {
                        token = FirebaseInstanceId.getInstance().getToken();
                    } catch (IllegalStateException e) {
                        Log.e(LOG_TAG, "Exception raised while getting Firebase token " + e.getMessage());
                    }
                    if (token == null) {
                        try {
                            token = FirebaseInstanceId.getInstance().getToken(senderID, FCM);
                        } catch (IllegalStateException e) {
                            Log.e(LOG_TAG, "Exception raised while getting Firebase token " + e.getMessage());
                        }
                    }
                    if (!"".equals(token)) {
                        JSONObject json = new JSONObject().put(REGISTRATION_ID, token);
                        json.put(REGISTRATION_TYPE, FCM);
                        Log.v(LOG_TAG, "onRegistered: " + json.toString());
                        JSONArray topics = jo.optJSONArray(TOPICS);
                        subscribeToTopics(topics, registration_id);
                        PushPlugin.sendEvent(json);
                    } else {
                        callbackContext.error("Empty registration ID received from FCM");
                        return;
                    }
                } catch (JSONException e) {
                    Log.e(LOG_TAG, "execute: Got JSON Exception " + e.getMessage());
                    callbackContext.error(e.getMessage());
                } catch (IOException e) {
                    Log.e(LOG_TAG, "execute: Got IO Exception " + e.getMessage());
                    callbackContext.error(e.getMessage());
                } catch (Resources.NotFoundException e) {
                    Log.e(LOG_TAG, "execute: Got Resources NotFoundException " + e.getMessage());
                    callbackContext.error(e.getMessage());
                }
                if (jo != null) {
                    SharedPreferences.Editor editor = sharedPref.edit();
                    try {
                        editor.putString(ICON, jo.getString(ICON));
                    } catch (JSONException e) {
                        Log.d(LOG_TAG, "no icon option");
                    }
                    try {
                        editor.putString(ICON_COLOR, jo.getString(ICON_COLOR));
                    } catch (JSONException e) {
                        Log.d(LOG_TAG, "no iconColor option");
                    }
                    boolean clearBadge = jo.optBoolean(CLEAR_BADGE, false);
                    if (clearBadge) {
                        setApplicationIconBadgeNumber(getApplicationContext(), 0);
                    }
                    editor.putBoolean(SOUND, jo.optBoolean(SOUND, true));
                    editor.putBoolean(VIBRATE, jo.optBoolean(VIBRATE, true));
                    editor.putBoolean(CLEAR_BADGE, clearBadge);
                    editor.putBoolean(CLEAR_NOTIFICATIONS, jo.optBoolean(CLEAR_NOTIFICATIONS, true));
                    editor.putBoolean(FORCE_SHOW, jo.optBoolean(FORCE_SHOW, false));
                    editor.putString(SENDER_ID, senderID);
                    editor.putString(MESSAGE_KEY, jo.optString(MESSAGE_KEY));
                    editor.putString(TITLE_KEY, jo.optString(TITLE_KEY));
                    editor.commit();
                }
                if (!gCachedExtras.isEmpty()) {
                    Log.v(LOG_TAG, "sending cached extras");
                    synchronized (gCachedExtras) {
                        Iterator<Bundle> gCachedExtrasIterator = gCachedExtras.iterator();
                        while (gCachedExtrasIterator.hasNext()) {
                            sendExtras(gCachedExtrasIterator.next());
                        }
                    }
                    gCachedExtras.clear();
                }
            }
        });
    } else if (UNREGISTER.equals(action)) {
        cordova.getThreadPool().execute(new Runnable() {

            public void run() {
                try {
                    SharedPreferences sharedPref = getApplicationContext().getSharedPreferences(COM_ADOBE_PHONEGAP_PUSH, Context.MODE_PRIVATE);
                    JSONArray topics = data.optJSONArray(0);
                    if (topics != null && !"".equals(registration_id)) {
                        unsubscribeFromTopics(topics, registration_id);
                    } else {
                        FirebaseInstanceId.getInstance().deleteInstanceId();
                        Log.v(LOG_TAG, "UNREGISTER");
                        // Remove shared prefs
                        SharedPreferences.Editor editor = sharedPref.edit();
                        editor.remove(SOUND);
                        editor.remove(VIBRATE);
                        editor.remove(CLEAR_BADGE);
                        editor.remove(CLEAR_NOTIFICATIONS);
                        editor.remove(FORCE_SHOW);
                        editor.remove(SENDER_ID);
                        editor.commit();
                    }
                    callbackContext.success();
                } catch (IOException e) {
                    Log.e(LOG_TAG, "execute: Got JSON Exception " + e.getMessage());
                    callbackContext.error(e.getMessage());
                }
            }
        });
    } else if (FINISH.equals(action)) {
        callbackContext.success();
    } else if (HAS_PERMISSION.equals(action)) {
        cordova.getThreadPool().execute(new Runnable() {

            public void run() {
                JSONObject jo = new JSONObject();
                try {
                    Log.d(LOG_TAG, "has permission: " + NotificationManagerCompat.from(getApplicationContext()).areNotificationsEnabled());
                    jo.put("isEnabled", NotificationManagerCompat.from(getApplicationContext()).areNotificationsEnabled());
                    PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, jo);
                    pluginResult.setKeepCallback(true);
                    callbackContext.sendPluginResult(pluginResult);
                } catch (UnknownError e) {
                    callbackContext.error(e.getMessage());
                } catch (JSONException e) {
                    callbackContext.error(e.getMessage());
                }
            }
        });
    } else if (SET_APPLICATION_ICON_BADGE_NUMBER.equals(action)) {
        cordova.getThreadPool().execute(new Runnable() {

            public void run() {
                Log.v(LOG_TAG, "setApplicationIconBadgeNumber: data=" + data.toString());
                try {
                    setApplicationIconBadgeNumber(getApplicationContext(), data.getJSONObject(0).getInt(BADGE));
                } catch (JSONException e) {
                    callbackContext.error(e.getMessage());
                }
                callbackContext.success();
            }
        });
    } else if (GET_APPLICATION_ICON_BADGE_NUMBER.equals(action)) {
        cordova.getThreadPool().execute(new Runnable() {

            public void run() {
                Log.v(LOG_TAG, "getApplicationIconBadgeNumber");
                callbackContext.success(getApplicationIconBadgeNumber(getApplicationContext()));
            }
        });
    } else if (CLEAR_ALL_NOTIFICATIONS.equals(action)) {
        cordova.getThreadPool().execute(new Runnable() {

            public void run() {
                Log.v(LOG_TAG, "clearAllNotifications");
                clearAllNotifications();
                callbackContext.success();
            }
        });
    } else if (SUBSCRIBE.equals(action)) {
        // Subscribing for a topic
        cordova.getThreadPool().execute(new Runnable() {

            public void run() {
                try {
                    String topic = data.getString(0);
                    subscribeToTopic(topic, registration_id);
                    callbackContext.success();
                } catch (JSONException e) {
                    callbackContext.error(e.getMessage());
                }
            }
        });
    } else if (UNSUBSCRIBE.equals(action)) {
        // un-subscribing for a topic
        cordova.getThreadPool().execute(new Runnable() {

            public void run() {
                try {
                    String topic = data.getString(0);
                    unsubscribeFromTopic(topic, registration_id);
                    callbackContext.success();
                } catch (JSONException e) {
                    callbackContext.error(e.getMessage());
                }
            }
        });
    } else if (CREATE_CHANNEL.equals(action)) {
        // un-subscribing for a topic
        cordova.getThreadPool().execute(new Runnable() {

            public void run() {
                try {
                    // call create channel
                    createChannel(data.getJSONObject(0));
                    callbackContext.success();
                } catch (JSONException e) {
                    callbackContext.error(e.getMessage());
                }
            }
        });
    } else if (DELETE_CHANNEL.equals(action)) {
        // un-subscribing for a topic
        cordova.getThreadPool().execute(new Runnable() {

            public void run() {
                try {
                    String channelId = data.getString(0);
                    deleteChannel(channelId);
                    callbackContext.success();
                } catch (JSONException e) {
                    callbackContext.error(e.getMessage());
                }
            }
        });
    } else if (LIST_CHANNELS.equals(action)) {
        // un-subscribing for a topic
        cordova.getThreadPool().execute(new Runnable() {

            public void run() {
                try {
                    callbackContext.success(listChannels());
                } catch (JSONException e) {
                    callbackContext.error(e.getMessage());
                }
            }
        });
    } else if (CLEAR_NOTIFICATION.equals(action)) {
        // clearing a single notification
        cordova.getThreadPool().execute(new Runnable() {

            public void run() {
                try {
                    Log.v(LOG_TAG, "clearNotification");
                    int id = data.getInt(0);
                    clearNotification(id);
                    callbackContext.success();
                } catch (JSONException e) {
                    callbackContext.error(e.getMessage());
                }
            }
        });
    } else {
        Log.e(LOG_TAG, "Invalid action : " + action);
        callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.INVALID_ACTION));
        return false;
    }
    return true;
}
Also used : PluginResult(org.apache.cordova.PluginResult) SharedPreferences(android.content.SharedPreferences) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) IOException(java.io.IOException) JSONObject(org.json.JSONObject) Iterator(java.util.Iterator)

Example 64 with PluginResult

use of org.apache.cordova.PluginResult in project phonegap-plugin-push by phonegap.

the class PushPlugin method sendEvent.

public static void sendEvent(JSONObject _json) {
    PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, _json);
    pluginResult.setKeepCallback(true);
    if (pushContext != null) {
        pushContext.sendPluginResult(pluginResult);
    }
}
Also used : PluginResult(org.apache.cordova.PluginResult)

Example 65 with PluginResult

use of org.apache.cordova.PluginResult in project barcodescanner-sdk-cordova by Scandit.

the class SubViewPickerController method returnFrameBufferIfWanted.

private void returnFrameBufferIfWanted(byte[] bytes, int width, int height) {
    if (mShouldPassBarcodeFrame) {
        String base64Data = SampleBufferConverter.base64StringFromFrame(bytes, width, height);
        JSONArray args = Marshal.createEventArgs(ScanditSDK.DID_PROCESS_FRAME, ResultRelay.jsonForDidProcessFrame(base64Data));
        PluginResult result = Marshal.createOkResult(args);
        mCallbackContext.sendPluginResult(result);
    }
}
Also used : PluginResult(org.apache.cordova.PluginResult) JSONArray(org.json.JSONArray)

Aggregations

PluginResult (org.apache.cordova.PluginResult)68 JSONException (org.json.JSONException)23 JSONObject (org.json.JSONObject)21 JSONArray (org.json.JSONArray)14 IOException (java.io.IOException)8 NativeToJsMessageQueue (org.apache.cordova.NativeToJsMessageQueue)6 Test (org.junit.Test)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 ArrayList (java.util.ArrayList)3 CallbackContext (org.apache.cordova.CallbackContext)3 CordovaResourceApi (org.apache.cordova.CordovaResourceApi)3 SharedPreferences (android.content.SharedPreferences)2