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

LocationProviderImpl

public abstract class LocationProviderImpl extends LocationProvider
This class is an implementation of the LocationProvider class defined by the JSR-179 specification.

Fields Summary
private static Vector
proximityListeners
protected LocationListener
locationListener
protected Criteria
criteria
private LocationThread
locationThread
private StateThread
stateThread
private int
locationQueries
private boolean
resetRequested
private static final String
SEPARATOR
private static LocationProviderImpl[]
providers
Constructors Summary
Methods Summary
booleancompareCriterias(Criteria c1, Criteria c2)

        if (!c1.isAllowedToCost() && c2.isAllowedToCost()) {
            return false;
        }
        if (c1.isSpeedAndCourseRequired() && !c2.isSpeedAndCourseRequired()) {
            return false;
        }
        if (c1.isAltitudeRequired() && !c2.isAltitudeRequired()) {
            return false;
        }
        if (c1.isAddressInfoRequired() && !c2.isAddressInfoRequired()) {
            return false;
        }
        if (c1.getHorizontalAccuracy() != Criteria.NO_REQUIREMENT &&
            c1.getHorizontalAccuracy() < c2.getHorizontalAccuracy()) {
            return false;
        }
        if (c1.getVerticalAccuracy() != Criteria.NO_REQUIREMENT &&
            c1.getVerticalAccuracy() < c2.getVerticalAccuracy()) {
            return false;
        }
        if (c1.getPreferredResponseTime() != Criteria.NO_REQUIREMENT &&
            c1.getPreferredResponseTime() < c2.getPreferredResponseTime()) {
            return false;
        }
        if (c1.getPreferredPowerConsumption() != Criteria.NO_REQUIREMENT
            && c1.getPreferredPowerConsumption() <
            c2.getPreferredPowerConsumption()) {
            return false;
        }
        return true;
    
public abstract intgetDefaultInterval()

public abstract intgetDefaultMaxAge()

public abstract intgetDefaultTimeout()

public static com.sun.j2me.location.LocationProviderImplgetInstanceImpl(Criteria criteria)

        LocationProviderImpl[] found = new LocationProviderImpl[2];
        int state;
        if (criteria == null) {
            criteria = new Criteria();
        }
        boolean allOutOfService = true;
        LocationProviderImpl[] providers = getProviders();
        // loop over all providers and set the ones that match the criteria
        // in their proper state, to give the one available preference over
        // the unavailable one
        for (int i = 0; i < providers.length; i++) {
            LocationProviderImpl provider = (LocationProviderImpl)providers[i];
            state = provider.getState();
            if ((state == AVAILABLE) || (state == TEMPORARILY_UNAVAILABLE)) {
                allOutOfService = false;
                if (provider.matchesCriteria(criteria)) {
                    found[state - 1] = provider;
                }
            }
        }
        if (allOutOfService) {
            throw new LocationException("All providers are out of service");
        }
        // first try to get the available one
        if (found[AVAILABLE - 1] != null) {
            return found[AVAILABLE - 1];
        }
        if (found[TEMPORARILY_UNAVAILABLE - 1] != null) {
            return found[TEMPORARILY_UNAVAILABLE - 1];
        }
        return null;
    
public static synchronized LocationgetLastKnownLocation()

        return PlatformLocationProvider.getLastKnownLocation();
    
abstract LocationImplgetLastLocation()

public LocationgetLocation(int timeout)

        Util.checkForPermission(Permissions.LOCATION, false);
        if (timeout == 0 || timeout < -1) {
            throw new IllegalArgumentException("Illegal timeout value");
        }
        if (timeout == -1) {
            timeout = getDefaultTimeout();
        }
        LocationImpl location = getLocationImpl(timeout);
        return location;
    
protected LocationImplgetLocationImpl(int timeout)

        long startTime = System.currentTimeMillis();
        long endTime = startTime + timeout * 1000;
        LocationImpl newLocation = null;
        if (getState() == OUT_OF_SERVICE) {
            throw new LocationException("Provider is out of service");
        }
        try {
            locationQueries++;
            while (!resetRequested && System.currentTimeMillis() < endTime) {
                if (getState() == AVAILABLE) {
                    newLocation = updateLocation(endTime - 
                                                    System.currentTimeMillis());
                    if (resetRequested) {
                        break;
                    }
                    if (newLocation != null) {
                        return newLocation;
                    }
                } else {
                    Thread.sleep(getStateInterval() * 1000);
                }
                long delay = Math.min(getResponseTime() * 1000,
                                      endTime - System.currentTimeMillis());
                if (delay <= 0) {
                    break;
                }
                while (!resetRequested && delay > 0) {
                    Thread.sleep(100);
                    delay -= 100;
                }
            }
            if (!resetRequested) {
                if (getState() == TEMPORARILY_UNAVAILABLE) {
                    throw new LocationException("Provider is temporarily unavailable");
                }
                // try one last time
                newLocation = updateLocation(getResponseTime() * 1000);
                if (!resetRequested) {
                    if (newLocation != null) {
                        return newLocation;
                    } 

                    throw new LocationException("Could not acquire location");
                }
            }
        } finally {
            locationQueries--;
        }
        throw new InterruptedException("Location query was interrupted");
    
public LocationListenergetLocationListener()

	return locationListener;
    
static com.sun.j2me.location.LocationProviderImpl[]getProviders()

	if (providers == null) {
	    Vector vectProviders = new Vector();
            String listProviders = PlatformLocationProvider.
                                        getListOfLocationProviders();
            if (listProviders != null &&
                (listProviders = listProviders.trim()) != "") {
                /* parsing the list of providers */
                while (listProviders.length() > 0) {
                    int posSpace = listProviders.indexOf(SEPARATOR);
                    String newProviderName;
                    if (posSpace == -1) { // last provider name
                        newProviderName = listProviders;
                        listProviders = "";
                    } else { // not last name
                        newProviderName = listProviders.substring(0, posSpace);
                        listProviders = listProviders.substring(posSpace + 1);
                    }
                    try {
                        LocationProviderImpl providerInstance = new 
                            PlatformLocationProvider(newProviderName);
                        vectProviders.addElement(providerInstance);
                    } catch (IllegalAccessException e) {
                        if (Logging.TRACE_ENABLED) {
                            Logging.trace(e, "Illegal access to provider");
                        }
                    }
                }
            }

            providers = new LocationProviderImpl[vectProviders.size()];
            vectProviders.copyInto(providers);
        }
        return providers;
    
public abstract intgetResponseTime()

public abstract intgetStateInterval()

public booleanmatchesCriteria(Criteria criteria)


    // JAVADOC COMMENT ELIDED
        
	return compareCriterias(criteria, this.criteria);
    
public voidreset()

        if (locationQueries > 0) {
            resetRequested = true;
            int attemptCount = Integer
                .parseInt(Configuration
                          .getProperty("com.sun.j2me.location.ResetTimeout"))
                * 10;
            while (locationQueries > 0 && attemptCount-- > 0) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                }
            }
            resetRequested = false;
        }
    
public voidsetLocationListener(LocationListener listener, int interval, int timeout, int maxAge)

        Util.checkForPermission(Permissions.LOCATION, true);
        if (interval < -1 ||
            (interval != -1 && (timeout > interval || maxAge > interval ||
                                timeout < 1 && timeout != -1 || 
                                maxAge < 1 && maxAge != -1))) {
            if (listener != null) {
                throw new IllegalArgumentException("Timeout value is invalid");
            }
        }
        // stop the current locationThread and stateThread
        if (locationThread != null) {
            locationThread.terminate();
            try { // wait for thread to die
                locationThread.join();
            } catch (InterruptedException e) { // do nothing
                if (Logging.TRACE_ENABLED) {
                    Logging.trace(e, "Wrong thread exception.");
                }
            }
            locationThread = 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;
        }
        if (listener == null) {
            locationListener = null;
            return;
        }
        if (interval == -1) {
            interval = getDefaultInterval();
            maxAge = getDefaultMaxAge();
            timeout = getDefaultTimeout();
        }
        if (maxAge == -1) {
            maxAge = getDefaultMaxAge();
        }
        if (timeout == -1) {
            timeout = getDefaultTimeout();
        }
        this.locationListener = listener;
        // Start the location thread when interval > 0
        if (interval > 0) {
            locationThread = new LocationThread(this, listener, interval,
                                                timeout, maxAge);
            locationThread.start();
        }
        // Start the state update thread
        stateThread = new StateThread(this, listener);
        stateThread.start();
    
protected abstract LocationImplupdateLocation(long timeout)