Search in sources :

Example 1 with GeofenceListener

use of com.codename1.location.GeofenceListener in project CodenameOne by codenameone.

the class IOSImplementation method onGeofenceExit.

public static void onGeofenceExit(final String id) {
    if (lm != null) {
        final GeofenceListener ls = lm.getGeofenceListener(id);
        if (ls != null) {
            Display.getInstance().callSerially(new Runnable() {

                @Override
                public void run() {
                    ls.onExit(id);
                }
            });
        }
        lm.clearExpiredGeofences();
    }
}
Also used : GeofenceListener(com.codename1.location.GeofenceListener)

Example 2 with GeofenceListener

use of com.codename1.location.GeofenceListener in project CodenameOne by codenameone.

the class IOSImplementation method onGeofenceEnter.

public static void onGeofenceEnter(final String id) {
    if (lm != null) {
        final GeofenceListener ls = lm.getGeofenceListener(id);
        if (ls != null) {
            Display.getInstance().callSerially(new Runnable() {

                @Override
                public void run() {
                    ls.onEntered(id);
                }
            });
        }
        lm.clearExpiredGeofences();
    }
}
Also used : GeofenceListener(com.codename1.location.GeofenceListener)

Example 3 with GeofenceListener

use of com.codename1.location.GeofenceListener in project CodenameOne by codenameone.

the class StubLocationManager method addGeoFencing.

@Override
public void addGeoFencing(final Class GeofenceListenerClass, Geofence gf) {
    if (gf.getId() != null) {
        String id = gf.getId();
        int index = -1;
        for (Geofence f : geoFences) {
            if (id.equals(f.getId())) {
                index = geoFences.indexOf(f);
                break;
            }
        }
        if (index >= 0) {
            geoFences.remove(index);
        }
        if (gf.getRadius() < 0) {
            throw new IllegalArgumentException("Attempt to add geofence with negative radius");
        }
        if (gf.getRadius() < 100) {
            Log.p("Adding Geofence with a radius of " + gf.getRadius() + " metres.  On an actual device, the effective radius will vary.  Typical Android and iOS devices have a minimum geofence radius of approximately 100m");
        }
        long expires = gf.getExpiration();
        geoFences.add(gf);
        if (geofenceTimer == null) {
            geofenceTimer = new java.util.Timer();
            geofenceTask = new TimerTask() {

                public void run() {
                    Display.getInstance().callSerially(new Runnable() {

                        public void run() {
                            Location loc;
                            try {
                                loc = getCurrentLocation();
                                if (JavaSEPort.locSimulation == null) {
                                    loc.setLongitude(loc.getLongitude() + 0.001);
                                    loc.setLatitude(loc.getLatitude() + +0.001);
                                } else {
                                    loc.setLongitude(JavaSEPort.locSimulation.getLongitude());
                                    loc.setLatitude(JavaSEPort.locSimulation.getLatitude());
                                }
                                // Do exits first
                                for (final Geofence f : geoFences) {
                                    if (!isInRegion(loc, f) && insideFences.contains(f.getId())) {
                                        insideFences.remove(f.getId());
                                        try {
                                            final GeofenceListener l = (GeofenceListener) GeofenceListenerClass.newInstance();
                                            new Thread() {

                                                public void run() {
                                                    // In a separate thread to simulate that
                                                    // this might not happen on EDT
                                                    l.onExit(f.getId());
                                                }
                                            }.start();
                                        } catch (Throwable t) {
                                            Log.e(t);
                                        }
                                    }
                                }
                                // Do entrances next
                                for (final Geofence f : geoFences) {
                                    if (isInRegion(loc, f) && !insideFences.contains(f.getId())) {
                                        insideFences.add(f.getId());
                                        try {
                                            final GeofenceListener l = (GeofenceListener) GeofenceListenerClass.newInstance();
                                            new Thread() {

                                                public void run() {
                                                    // In a separate thread to simulate that
                                                    // this might not happen on EDT
                                                    l.onEntered(f.getId());
                                                }
                                            }.start();
                                        } catch (Throwable t) {
                                            Log.e(t);
                                        }
                                    }
                                }
                            } catch (IOException ex) {
                                Logger.getLogger(StubLocationManager.class.getName()).log(Level.SEVERE, null, ex);
                            }
                        }
                    });
                }
            };
            geofenceTimer.schedule(geofenceTask, new Date(System.currentTimeMillis() + 10000L), 10000L);
        }
    } else {
        Log.p("Attempt to add Geofence with null ID", Log.WARNING);
    }
}
Also used : GeofenceListener(com.codename1.location.GeofenceListener) Timer(java.util.Timer) IOException(java.io.IOException) Date(java.util.Date) TimerTask(java.util.TimerTask) Geofence(com.codename1.location.Geofence) Location(com.codename1.location.Location)

Aggregations

GeofenceListener (com.codename1.location.GeofenceListener)3 Geofence (com.codename1.location.Geofence)1 Location (com.codename1.location.Location)1 IOException (java.io.IOException)1 Date (java.util.Date)1 Timer (java.util.Timer)1 TimerTask (java.util.TimerTask)1