The LocationFinder class makes the Location API optional so that devices that do not support the Location API can run the MIDlet.
The getFinder method checks if the Location API is supported and creates a LocationFinder object.
public static LocationFinder getFinder(Listener listener) { if (listener == null) { throw new NullPointerException("listener not defined"); } LocationFinder finder = null; try { // this will throw an exception if JSR-179 is missing Class.forName("javax.microedition.location.Location"); if (finder == null && supportsGPS()) { Class c = Class.forName( "com.nokia.example.attractions.location.GpsLocationFinder"); finder = (LocationFinder) (c.newInstance()); try { finder.init(listener); } catch (InitializationException e) { finder = null; } } if (finder == null && supportsCellId()) { Class c = Class.forName( "com.nokia.example.attractions.location.CellIdLocationFinder"); finder = (LocationFinder) (c.newInstance()); try { finder.init(listener); } catch (InitializationException e) { finder = null; } } } catch (Exception e) { finder = null; } return finder; } private static final String[] S40_GPS_DEVICES = {"2710", "6350", "6750", "3710", "6700", "6260" }; private static boolean supportsGPS() { String platform = System.getProperty("microedition.platform"); if (platform != null) { for (int i = 0; i < S40_GPS_DEVICES.length; i++) { if (platform.indexOf(S40_GPS_DEVICES[i]) > -1) { return true; } } } return Main.isS60Phone(); } private static boolean supportsCellId() { try { Class.forName("com.nokia.mid.location.LocationUtil"); return true; } catch (Exception e) { return false; } }
The GpsLocationFinder class provides a location provider that uses GPS to determine the location.
The criteria for the location provider are specified in the class constructor.
GpsLocationFinder() { criteria = new Criteria(); criteria.setCostAllowed(true); criteria.setPreferredPowerConsumption(Criteria.NO_REQUIREMENT); criteria.setSpeedAndCourseRequired(false); criteria.setAltitudeRequired(false); criteria.setAddressInfoRequired(false); }
The getLocationProvider method returns an instance of the location provider.
protected final LocationProvider getLocationProvider() throws LocationException { return LocationProvider.getInstance(criteria); }
The CellIdLocationFinder class provides a location provider that uses cell ID positioning to determine the location.
The getLocationProvider method returns an instance of the location provider.
protected final LocationProvider getLocationProvider() throws LocationException, SecurityException { // Prompt first for network connection // to prevent bugs in Cell ID service. HttpConnection conn = null; try { conn = (HttpConnection) Connector.open(TEST_URL); } catch (SecurityException e) { throw e; } catch (IOException e) { } finally { if (conn != null) { try { conn.close(); } catch (IOException e) { } } } int[] methods = {(Location.MTA_ASSISTED | Location.MTE_CELLID | Location.MTE_SHORTRANGE | Location.MTY_NETWORKBASED)}; return LocationUtil.getLocationProvider(methods, null); }