Example usage for org.apache.cordova PluginResult PluginResult

List of usage examples for org.apache.cordova PluginResult PluginResult

Introduction

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

Prototype

public PluginResult(Status status, List<PluginResult> multipartMessages) 

Source Link

Usage

From source file:com.commontime.cordova.audio.AudioHandler.java

License:Apache License

/**
 * 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.
 */// w  w w  .ja  v a2 s  .co m
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("startRecordingAudioWithCompression")) {
        String target = args.getString(1);
        String fileUriStr;
        try {
            Uri targetUri = resourceApi.remapUri(Uri.parse(target));
            fileUriStr = targetUri.toString();
        } catch (IllegalArgumentException e) {
            fileUriStr = target;
        }

        // set defaults
        Integer sampleRate = 44100;
        Integer channels = 1;

        JSONObject options = args.getJSONObject(2);

        try {
            channels = options.getInt("NumberOfChannels");
            sampleRate = options.getInt("SampleRate");
        } catch (JSONException e) {
            channels = 1;
            sampleRate = 44100;
        }
        // for use within resumeRecord, these values must be consistent when resume record is called.
        this.audioChannels = channels;
        this.audioSampleRate = sampleRate;

        this.startRecordingAudioWithCompression(args.getString(0), FileHelper.stripFileProtocol(fileUriStr),
                channels, sampleRate);
    } else if (action.equals("pauseRecordingAudio")) {
        this.pauseRecordingAudio(args.getString(0));
    } else if (action.equals("resumeRecordingAudio")) {
        String target = args.getString(1);
        String fileUriStr;
        try {
            Uri targetUri = resourceApi.remapUri(Uri.parse(target));
            fileUriStr = targetUri.toString();
        } catch (IllegalArgumentException e) {
            fileUriStr = target;
        }

        this.resumeRecordingAudio(args.getString(0), FileHelper.stripFileProtocol(fileUriStr),
                this.audioChannels, this.audioSampleRate);
    } 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;
    }

    //REM mods
    else if (action.equals("getRecordDbLevel")) {
        float f = this.getAudioRecordDbLevel(args.getString(0));
        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));
        getOrCreatePlayer(id, src);
    } else if (action.equals("release")) {
        boolean b = this.release(args.getString(0));
        callbackContext.sendPluginResult(new PluginResult(status, b));
        return true;
    } else if (action.equals("messageChannel")) {
        messageChannel = callbackContext;
        return true;
    } else { // Unrecognized action.
        return false;
    }

    callbackContext.sendPluginResult(new PluginResult(status, result));

    return true;
}

From source file:com.commontime.cordova.audio.AudioHandler.java

License:Apache License

void sendEventMessage(String action, JSONObject actionData) {
    JSONObject message = new JSONObject();
    try {/*from   ww w  . j av a2 s  .  c  o  m*/
        message.put("action", action);
        if (actionData != null) {
            message.put(action, actionData);
        }
    } catch (JSONException e) {
        Log.e(TAG, "Failed to create event message", e);
    }

    PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, message);
    pluginResult.setKeepCallback(true);
    if (messageChannel != null) {
        messageChannel.sendPluginResult(pluginResult);
    }
}

From source file:com.commontime.plugin.LocationManager.java

License:Apache License

private void createMonitorCallbacks(final CallbackContext callbackContext) {

    //Monitor callbacks
    iBeaconManager.setMonitorNotifier(new MonitorNotifier() {
        @Override/*from  ww w  .j av  a2 s  . c  o m*/
        public void didEnterRegion(Region region) {
            debugLog("didEnterRegion INSIDE for " + region.getUniqueId());
            dispatchMonitorState("didEnterRegion", MonitorNotifier.INSIDE, region, callbackContext);
        }

        @Override
        public void didExitRegion(Region region) {
            debugLog("didExitRegion OUTSIDE for " + region.getUniqueId());
            dispatchMonitorState("didExitRegion", MonitorNotifier.OUTSIDE, region, callbackContext);
        }

        @Override
        public void didDetermineStateForRegion(int state, Region region) {
            debugLog("didDetermineStateForRegion '" + nameOfRegionState(state) + "' for region: "
                    + region.getUniqueId());
            dispatchMonitorState("didDetermineStateForRegion", state, region, callbackContext);
        }

        // Send state to JS callback until told to stop
        private void dispatchMonitorState(final String eventType, final int state, final Region region,
                final CallbackContext callbackContext) {

            threadPoolExecutor.execute(new Runnable() {
                public void run() {
                    try {
                        JSONObject data = new JSONObject();
                        data.put("eventType", eventType);
                        data.put("region", mapOfRegion(region));

                        if (eventType.equals("didDetermineStateForRegion")) {
                            String stateName = nameOfRegionState(state);
                            data.put("state", stateName);
                        }
                        //send and keep reference to callback 
                        PluginResult result = new PluginResult(PluginResult.Status.OK, data);
                        result.setKeepCallback(true);
                        callbackContext.sendPluginResult(result);

                    } catch (Exception e) {
                        Log.e(TAG, "'monitoringDidFailForRegion' exception " + e.getCause());
                        beaconServiceNotifier.monitoringDidFailForRegion(region, e);

                    }
                }
            });
        }
    });

}

From source file:com.commontime.plugin.LocationManager.java

License:Apache License

private void createRangingCallbacks(final CallbackContext callbackContext) {

    iBeaconManager.setRangeNotifier(new RangeNotifier() {
        @Override/*from   ww w.  jav  a2s  .  c  o m*/
        public void didRangeBeaconsInRegion(final Collection<Beacon> iBeacons, final Region region) {

            threadPoolExecutor.execute(new Runnable() {
                public void run() {

                    try {
                        JSONObject data = new JSONObject();
                        JSONArray beaconData = new JSONArray();
                        for (Beacon beacon : iBeacons) {
                            beaconData.put(mapOfBeacon(beacon));
                        }
                        data.put("eventType", "didRangeBeaconsInRegion");
                        data.put("region", mapOfRegion(region));
                        data.put("beacons", beaconData);

                        debugLog("didRangeBeacons: " + data.toString());

                        //send and keep reference to callback 
                        PluginResult result = new PluginResult(PluginResult.Status.OK, data);
                        result.setKeepCallback(true);
                        callbackContext.sendPluginResult(result);

                    } catch (Exception e) {
                        Log.e(TAG, "'rangingBeaconsDidFailForRegion' exception " + e.getCause());
                        beaconServiceNotifier.rangingBeaconsDidFailForRegion(region, e);
                    }
                }
            });
        }

    });

}

From source file:com.commontime.plugin.LocationManager.java

License:Apache License

private void createManagerCallbacks(final CallbackContext callbackContext) {
    beaconServiceNotifier = new IBeaconServiceNotifier() {

        @Override/*from   ww  w.j av  a 2  s  . c  o m*/
        public void rangingBeaconsDidFailForRegion(final Region region, final Exception exception) {
            threadPoolExecutor.execute(new Runnable() {
                public void run() {

                    sendFailEvent("rangingBeaconsDidFailForRegion", region, exception, callbackContext);
                }
            });
        }

        @Override
        public void monitoringDidFailForRegion(final Region region, final Exception exception) {
            threadPoolExecutor.execute(new Runnable() {
                public void run() {

                    sendFailEvent("monitoringDidFailForRegionWithError", region, exception, callbackContext);
                }
            });
        }

        @Override
        public void didStartMonitoringForRegion(final Region region) {
            threadPoolExecutor.execute(new Runnable() {
                public void run() {

                    try {
                        JSONObject data = new JSONObject();
                        data.put("eventType", "didStartMonitoringForRegion");
                        data.put("region", mapOfRegion(region));

                        debugLog("didStartMonitoringForRegion: " + data.toString());

                        //send and keep reference to callback 
                        PluginResult result = new PluginResult(PluginResult.Status.OK, data);
                        result.setKeepCallback(true);
                        callbackContext.sendPluginResult(result);

                    } catch (Exception e) {
                        Log.e(TAG, "'startMonitoringForRegion' exception " + e.getCause());
                        monitoringDidFailForRegion(region, e);
                    }
                }
            });
        }

        @Override
        public void didChangeAuthorizationStatus(final String status) {
            threadPoolExecutor.execute(new Runnable() {
                public void run() {

                    try {
                        JSONObject data = new JSONObject();
                        data.put("eventType", "didChangeAuthorizationStatus");
                        data.put("authorizationStatus", status);
                        debugLog("didChangeAuthorizationStatus: " + data.toString());

                        //send and keep reference to callback 
                        PluginResult result = new PluginResult(PluginResult.Status.OK, data);
                        result.setKeepCallback(true);
                        callbackContext.sendPluginResult(result);

                    } catch (Exception e) {
                        callbackContext.error("didChangeAuthorizationStatus error: " + e.getMessage());
                    }
                }
            });
        }

        private void sendFailEvent(String eventType, Region region, Exception exception,
                final CallbackContext callbackContext) {
            try {
                JSONObject data = new JSONObject();
                data.put("eventType", eventType);//not perfect mapping, but it's very unlikely to happen here
                data.put("region", mapOfRegion(region));
                data.put("error", exception.getMessage());

                PluginResult result = new PluginResult(PluginResult.Status.OK, data);
                result.setKeepCallback(true);
                callbackContext.sendPluginResult(result);
            } catch (Exception e) {
                //still failing, so kill all further event dispatch
                Log.e(TAG, eventType + " error " + e.getMessage());
                callbackContext.error(eventType + " error " + e.getMessage());
            }
        }
    };
}

From source file:com.commontime.plugin.LocationManager.java

License:Apache License

private void isBluetoothEnabled(CallbackContext callbackContext) {

    _handleCallSafely(callbackContext, new ILocationManagerCommand() {

        @Override//from www . jav  a2  s. c  o m
        public PluginResult run() {
            try {
                //Check the Bluetooth service is running
                boolean available = bluetoothAdapter != null && bluetoothAdapter.isEnabled();
                return new PluginResult(PluginResult.Status.OK, available);

            } catch (Exception e) {
                debugWarn("'isBluetoothEnabled' exception " + e.getMessage());
                return new PluginResult(PluginResult.Status.ERROR, e.getMessage());
            }
        }
    });
}

From source file:com.commontime.plugin.LocationManager.java

License:Apache License

private void appendToDeviceLog(final String message, CallbackContext callbackContext) {
    _handleCallSafely(callbackContext, new ILocationManagerCommand() {

        @Override//from  w w w.  j  ava  2 s  . c  o m
        public PluginResult run() {

            if (message != null && !message.isEmpty()) {
                debugLog("[DOM] " + message);
                return new PluginResult(PluginResult.Status.OK, message);
            } else {
                return new PluginResult(PluginResult.Status.ERROR, "Log message not provided");
            }
        }
    });
}

From source file:com.commontime.plugin.LocationManager.java

License:Apache License

private void getAuthorizationStatus(CallbackContext callbackContext) {
    _handleCallSafely(callbackContext, new ILocationManagerCommand() {

        @Override/*  w w  w .ja  v  a2s.  co m*/
        public PluginResult run() {

            try {

                //Check app has the necessary permissions
                if (!hasBlueToothPermission()) {
                    return new PluginResult(PluginResult.Status.ERROR,
                            "Application does not BLUETOOTH or BLUETOOTH_ADMIN permissions");
                }

                //Check the Bluetooth service is running
                String authStatus = iBeaconManager.checkAvailability() ? "AuthorizationStatusAuthorized"
                        : "AuthorizationStatusDenied";
                JSONObject result = new JSONObject();
                result.put("authorizationStatus", authStatus);
                return new PluginResult(PluginResult.Status.OK, result);

            } catch (BleNotAvailableException e) {
                //if device does not support iBeacons and error is thrown
                debugLog("'getAuthorizationStatus' Device not supported: " + e.getMessage());
                return new PluginResult(PluginResult.Status.ERROR, e.getMessage());
            } catch (Exception e) {
                debugWarn("'getAuthorizationStatus' exception " + e.getMessage());
                return new PluginResult(PluginResult.Status.ERROR, e.getMessage());
            }

        }
    });
}

From source file:com.commontime.plugin.LocationManager.java

License:Apache License

private void getMonitoredRegions(CallbackContext callbackContext) {

    _handleCallSafely(callbackContext, new ILocationManagerCommand() {

        @Override/*from w ww  .ja v  a  2  s .c  o  m*/
        public PluginResult run() {
            try {
                Collection<Region> regions = iBeaconManager.getMonitoredRegions();
                JSONArray regionArray = new JSONArray();
                for (Region region : regions) {
                    regionArray.put(mapOfRegion(region));
                }

                return new PluginResult(PluginResult.Status.OK, regionArray);
            } catch (JSONException e) {
                debugWarn("'getMonitoredRegions' exception: " + e.getMessage());
                return new PluginResult(PluginResult.Status.ERROR, e.getMessage());
            }
        }
    });

}

From source file:com.commontime.plugin.LocationManager.java

License:Apache License

private void getRangedRegions(CallbackContext callbackContext) {

    _handleCallSafely(callbackContext, new ILocationManagerCommand() {

        @Override/*ww w  .  j  a  va2 s  . c om*/
        public PluginResult run() {
            try {
                Collection<Region> regions = iBeaconManager.getRangedRegions();
                JSONArray regionArray = new JSONArray();
                for (Region region : regions) {
                    regionArray.put(mapOfRegion(region));
                }

                return new PluginResult(PluginResult.Status.OK, regionArray);
            } catch (JSONException e) {
                debugWarn("'getRangedRegions' exception: " + e.getMessage());
                return new PluginResult(PluginResult.Status.ERROR, e.getMessage());
            }
        }
    });
}