Search in sources :

Example 1 with Geofence

use of com.codename1.location.Geofence 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)

Example 2 with Geofence

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

the class StubLocationManager method removeGeoFencing.

@Override
public void removeGeoFencing(String id) {
    int index = -1;
    for (Geofence gf : geoFences) {
        if (gf.getId() != null && gf.getId().equals(id)) {
            index = geoFences.indexOf(gf);
            break;
        }
    }
    if (index >= 0) {
        geoFences.remove(index);
    }
    if (geoFences.isEmpty()) {
        if (geofenceTimer != null) {
            geofenceTimer.cancel();
            geofenceTimer = null;
            geofenceTask = null;
        }
    }
}
Also used : Geofence(com.codename1.location.Geofence)

Aggregations

Geofence (com.codename1.location.Geofence)2 GeofenceListener (com.codename1.location.GeofenceListener)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