FileDocCategorySizeDatePackage
ProximityNotifier.javaAPI DocphoneME MR2 API (J2ME)11749Wed May 02 18:00:40 BST 2007com.sun.j2me.location

ProximityNotifier

public class ProximityNotifier extends Object
Asynchronous thread for periodic location event handling.

Fields Summary
static ProximityNotifier
Instance
Instance of the proximity notifier. If the application does not use proximity notification, this class will not be instantiated.
Vector
proximityListeners
Array of registered listeners.
ProximityThread
proximityThread
Thread for proximity notifications.
StateMonitorThread
stateThread
Current thread performing monitoring state updating.
LocationProviderImpl
proximityProvider
Dedicated provider for delivering proximity data.
Constructors Summary
private ProximityNotifier()
Constructor.

    
Methods Summary
public voidaddProximityListener(ProximityListener listener, Coordinates coordinates, float proximityRadius)

        synchronized (proximityListeners) {
	    proximityListeners.addElement(
		new ProximityListenerDecorator(listener, coordinates,
					       proximityRadius));
	}
	synchronized (this) {
	    if (proximityProvider == null) {
		try {
		    proximityProvider =
			LocationProviderImpl.getInstanceImpl(null);
		} catch (LocationException e) {
		}
	    }
	    if (proximityThread == null) {
		proximityThread = new ProximityThread();
		proximityThread.start();
	    }
	    if (stateThread == null) {
		stateThread = new StateMonitorThread();
		stateThread.start();
	    }
	}
    
voidfireMonitoringStateChanged(boolean isMonitoringActive)
Dispatches a monitor stat changed event.

param
isMonitoringActive is true if monitoring is enabled

        // prevents concurent modification in which the event code modifies
        // the vector by invoking remove/add during event execution
        ProximityListenerDecorator[] listeners;
        synchronized (proximityListeners) {
            listeners =
		new ProximityListenerDecorator[proximityListeners.size()];
            proximityListeners.copyInto(listeners);
        }
        for (int i = 0; i < listeners.length; i++) {
            listeners[i].monitoringStateChanged(isMonitoringActive);
        }
    
voidfireProximityEvent(Location location)
Dispatches a proximity event.

param
location location that triggered the proximity event

        // prevents concurent modification in which the event code modifies
        // the vector by invoking remove/add during event execution
	ProximityListenerDecorator[] listeners;
        synchronized (proximityListeners) {
            listeners =
		new ProximityListenerDecorator[proximityListeners.size()];
            proximityListeners.copyInto(listeners);
        }
        for (int i = 0; i < listeners.length; i++) {
            listeners[i].proximityEvent(location);
        }
    
public static com.sun.j2me.location.ProximityNotifiergetInstance()
Gets a handle to proximity notifier.

return
current proximity notifier handle


                    
        
        if (Instance == null) {
            Instance = new ProximityNotifier();
        }
        return Instance;
    
java.util.VectorgetListeners()
Returns a vector of proximity listeners.

return
the vector of registered proximity listeners

        return proximityListeners;
    
LocationgetLocation()
Retrieves location from the dedicated provider.

return
location to be used for proximity detection.

	Location location = null;
	try {
	    if (proximityProvider == null) {
		proximityProvider =
		    LocationProviderImpl.getInstanceImpl(null);
	    }
	    location = proximityProvider.getLocationImpl(-1);
	} catch (LocationException e) {
	} catch (InterruptedException e) {
	}
	return location;
    
booleangetMonitoringState()
Checks if the monitoring is active.

return
true if monitoring is active, false otherwise

	return proximityProvider != null &&
	    proximityProvider.getState() == LocationProvider.AVAILABLE;
    
intgetProximityInterval()
The recommended time interval for proximity update events.

return
the default interval of the dedicated provider

	if (proximityProvider != null) {
	    return proximityProvider.getDefaultInterval();
	}
	return 10;
    
intgetStateInterval()
The recommended time interval for querying monitoring state.

return
time interval in seconds

	if (proximityProvider != null) {
	    return proximityProvider.getStateInterval();
	}
	return 10;
    
public voidremoveProximityListener(ProximityListener listener)

        ProximityListenerDecorator[] listeners;
        synchronized (proximityListeners) {
            listeners =
		new ProximityListenerDecorator[proximityListeners.size()];
            proximityListeners.copyInto(listeners);
        }
	for (int i = 0; i < listeners.length; i++) {
	    if (listeners[i].listener == listener) {
		synchronized (proximityListeners) {
		    proximityListeners.removeElement(listeners[i]);
		}
	    }
	}
	// check if it was the last listener
	if (proximityListeners.isEmpty()) {
	    synchronized (this) {
		if (proximityThread != null) {
		    proximityThread.terminate();
		    if (Thread.currentThread() != proximityThread) {
			try { // wait for thread to die
			    proximityThread.join();
			} catch (InterruptedException e) { // do nothing
			    if (Logging.TRACE_ENABLED) {
				Logging.trace(e, "Wrong thread exception.");
			    }
			}
		    }
		    proximityThread = null;
		}
		if (stateThread != null) {
		    stateThread.terminate();
		    try { // wait for thread to die
			stateThread.join();
		    } catch (InterruptedException e) { // do nothing
			if (Logging.TRACE_ENABLED) {
			    Logging.trace(e, "Wrong thread exception.");
			}
		    }
		    stateThread = null;
		}
		// dedicated provider is no longer needed
		proximityProvider = null;
	    }
	}