Example usage for org.apache.cordova PluginResult setKeepCallback

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

Introduction

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

Prototype

public void setKeepCallback(boolean b) 

Source Link

Usage

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

License:Apache License

private void createMonitorCallbacks(final CallbackContext callbackContext) {

    //Monitor callbacks
    iBeaconManager.setMonitorNotifier(new MonitorNotifier() {
        @Override// w  w w  .  j a v  a  2 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//  www. ja v a  2  s.  com
        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  w w w  .ja va  2  s.  c  om
        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 enableBluetooth(CallbackContext callbackContext) {

    _handleCallSafely(callbackContext, new ILocationManagerCommand() {

        @Override//from   w w  w  .j  av a  2 s .  c o  m
        public PluginResult run() {
            try {
                bluetoothAdapter.enable();
                PluginResult result = new PluginResult(PluginResult.Status.OK);
                result.setKeepCallback(true);
                return result;
            } catch (Exception e) {
                Log.e(TAG, "'enableBluetooth' service error: " + e.getCause());
                return new PluginResult(PluginResult.Status.ERROR, e.getMessage());
            }
        }
    });
}

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

License:Apache License

private void disableBluetooth(CallbackContext callbackContext) {

    _handleCallSafely(callbackContext, new ILocationManagerCommand() {

        @Override/*from  w  w  w . j  a v  a2  s . c o m*/
        public PluginResult run() {
            try {
                bluetoothAdapter.disable();
                PluginResult result = new PluginResult(PluginResult.Status.OK);
                result.setKeepCallback(true);
                return result;
            } catch (Exception e) {
                Log.e(TAG, "'disableBluetooth' service error: " + e.getCause());
                return new PluginResult(PluginResult.Status.ERROR, e.getMessage());
            }
        }
    });
}

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

License:Apache License

private void startMonitoringForRegion(final JSONObject arguments, final CallbackContext callbackContext) {

    _handleCallSafely(callbackContext, new ILocationManagerCommand() {

        @Override/* w  w w.j a  v a  2 s . co  m*/
        public PluginResult run() {

            Region region = null;
            try {
                region = parseRegion(arguments);
                iBeaconManager.startMonitoringBeaconsInRegion(region);

                PluginResult result = new PluginResult(PluginResult.Status.OK);
                result.setKeepCallback(true);
                beaconServiceNotifier.didStartMonitoringForRegion(region);
                return result;

            } catch (RemoteException e) {
                Log.e(TAG, "'startMonitoringForRegion' service error: " + e.getCause());
                beaconServiceNotifier.monitoringDidFailForRegion(region, e);
                return new PluginResult(PluginResult.Status.ERROR, e.getMessage());
            } catch (Exception e) {
                Log.e(TAG, "'startMonitoringForRegion' exception " + e.getCause());
                beaconServiceNotifier.monitoringDidFailForRegion(region, e);
                return new PluginResult(PluginResult.Status.ERROR, e.getMessage());
            }

        }

    });
}

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

License:Apache License

private void stopMonitoringForRegion(final JSONObject arguments, final CallbackContext callbackContext) {

    _handleCallSafely(callbackContext, new ILocationManagerCommand() {

        @Override//from  w w w. j  av a  2  s. c  om
        public PluginResult run() {

            try {
                Region region = parseRegion(arguments);
                iBeaconManager.stopMonitoringBeaconsInRegion(region);

                PluginResult result = new PluginResult(PluginResult.Status.OK);
                result.setKeepCallback(true);
                return result;

            } catch (RemoteException e) {
                Log.e(TAG, "'stopMonitoringForRegion' service error: " + e.getCause());
                return new PluginResult(PluginResult.Status.ERROR, e.getMessage());
            } catch (Exception e) {
                Log.e(TAG, "'stopMonitoringForRegion' exception " + e.getCause());
                return new PluginResult(PluginResult.Status.ERROR, e.getMessage());
            }

        }
    });

}

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

License:Apache License

private void startRangingBeaconsInRegion(final JSONObject arguments, final CallbackContext callbackContext) {

    _handleCallSafely(callbackContext, new ILocationManagerCommand() {

        @Override/*from   www.  j a  v  a2  s  . co m*/
        public PluginResult run() {

            try {
                Region region = parseRegion(arguments);
                iBeaconManager.startRangingBeaconsInRegion(region);

                PluginResult result = new PluginResult(PluginResult.Status.OK);
                result.setKeepCallback(true);
                return result;

            } catch (RemoteException e) {
                Log.e(TAG, "'startRangingBeaconsInRegion' service error: " + e.getCause());
                return new PluginResult(PluginResult.Status.ERROR, e.getMessage());
            } catch (Exception e) {
                Log.e(TAG, "'startRangingBeaconsInRegion' exception " + e.getCause());
                return new PluginResult(PluginResult.Status.ERROR, e.getMessage());
            }
        }
    });
}

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

License:Apache License

private void stopRangingBeaconsInRegion(final JSONObject arguments, CallbackContext callbackContext) {
    _handleCallSafely(callbackContext, new ILocationManagerCommand() {

        @Override// ww w.ja v a 2  s  . c o  m
        public PluginResult run() {

            try {
                Region region = parseRegion(arguments);
                iBeaconManager.stopRangingBeaconsInRegion(region);

                PluginResult result = new PluginResult(PluginResult.Status.OK);
                result.setKeepCallback(true);
                return result;

            } catch (RemoteException e) {
                Log.e(TAG, "'stopRangingBeaconsInRegion' service error: " + e.getCause());
                return new PluginResult(PluginResult.Status.ERROR, e.getMessage());
            } catch (Exception e) {
                Log.e(TAG, "'stopRangingBeaconsInRegion' exception " + e.getCause());
                return new PluginResult(PluginResult.Status.ERROR, e.getMessage());
            }
        }
    });

}

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

License:Apache License

private void requestStateForRegion(final JSONObject arguments, CallbackContext callbackContext) {

    _handleCallSafely(callbackContext, new ILocationManagerCommand() {
        @Override/*from  w ww. j  ava  2  s  .c  o m*/
        public PluginResult run() {

            //not supported on Android
            PluginResult result = new PluginResult(PluginResult.Status.ERROR,
                    "Manual request for monitoring update is not supported on Android");
            result.setKeepCallback(true);
            return result;

        }
    });
}