Search in sources :

Example 1 with LocationException

use of javax.microedition.location.LocationException in project Samples-for-Java by blackberry.

the class TestScreen method fieldChanged.

public void fieldChanged(Field field, int context) {
    if (simpleProvider == null) {
        try {
            simpleProvider = new SimpleLocationProvider(getMode(modeField.getSelectedIndex()));
        } catch (final Exception le) {
            UiApplication.getUiApplication().invokeLater(new Runnable() {

                public void run() {
                    Dialog.alert(le.getMessage());
                }
            });
        }
        if (simpleProvider != null) {
            simpleProvider.setGeolocationTimeout(geoTimeoutField.getSelectedValue());
            simpleProvider.setGPSTimeout(gpsTimeoutField.getSelectedValue());
            simpleProvider.setRetryFactor(retryFactorField.getSelectedValue());
            simpleProvider.setTrackignInterval(trackingIntervalField.getSelectedValue());
            simpleProvider.setMaxRetryDelay(maxRetryDelayField.getSelectedValue());
        }
    }
    if (field == startTrackingField) {
        if (simpleProvider != null) {
            simpleProvider.addSimpleLocationListener(this, trackingIntervalField.getSelectedValue());
        }
    } else if (field == stopTrackingField) {
        if (simpleProvider != null) {
            simpleProvider.removeSimpleLocationListener();
        }
    } else if (field == restartField) {
        if (simpleProvider != null) {
            try {
                simpleProvider.restart();
            } catch (IllegalStateException ise) {
                UiApplication.getUiApplication().invokeLater(new Runnable() {

                    public void run() {
                        Dialog.alert("Tracking session must be in progress. Please select Start Tracking.");
                    }
                });
            }
        }
    } else if (field == modeField) {
        if (simpleProvider != null) {
            try {
                simpleProvider.setMode(getMode(modeField.getSelectedIndex()));
            } catch (final LocationException le) {
                UiApplication.getUiApplication().invokeLater(new Runnable() {

                    public void run() {
                        Dialog.alert(le.toString());
                    }
                });
            }
        }
    } else if (field == trackingIntervalField) {
        if (simpleProvider != null) {
            simpleProvider.setTrackignInterval(trackingIntervalField.getSelectedValue());
        }
    } else if (field == gpsTimeoutField) {
        if (simpleProvider != null) {
            simpleProvider.setGPSTimeout(gpsTimeoutField.getSelectedValue());
        }
    } else if (field == geoTimeoutField) {
        if (simpleProvider != null) {
            simpleProvider.setGPSTimeout(geoTimeoutField.getSelectedValue());
        }
    } else if (field == retryFactorField) {
        if (simpleProvider != null) {
            simpleProvider.setRetryFactor(retryFactorField.getSelectedValue());
        }
    } else if (field == maxRetryDelayField) {
        if (simpleProvider != null) {
            simpleProvider.setMaxRetryDelay(maxRetryDelayField.getSelectedValue());
        }
    } else if (field == getSingleLocationField) {
        new Thread() {

            public void run() {
                location = simpleProvider.getLocation(30);
                if (location != null && location.isValid()) {
                    locationField.setText(location.getQualifiedCoordinates().getLatitude() + ", " + location.getQualifiedCoordinates().getLongitude());
                    statusField.setText("Obtained single location.");
                } else {
                    statusField.setText("Failed to obtain location!");
                }
            }
        }.start();
    } else if (field == getLastLocationField) {
        if (simpleProvider != null) {
            location = simpleProvider.getLastKnownLocation();
            if (location != null && location.isValid()) {
                locationField.setText(location.getQualifiedCoordinates().getLatitude() + ", " + location.getQualifiedCoordinates().getLongitude());
                statusField.setText("Obtained last known location.");
            } else {
                statusField.setText("Last known location not available! Please acquire a single location or start a tracking session first.");
            }
        } else {
            statusField.setText("Last known location not available! Please acquire a single location or a tracking session first.");
        }
    } else if (field == satellitesField) {
        if (location != null) {
            final PopupScreen satPopup = new PopupScreen(new VerticalFieldManager()) {

                protected boolean keyDown(int keycode, int time) {
                    int key = Keypad.key(keycode);
                    if (key == Keypad.KEY_ESCAPE) {
                        Screen screen = getScreen();
                        UiApplication.getUiApplication().popScreen(screen);
                    }
                    return true;
                }
            };
            EditField satField = new EditField("", "");
            satPopup.add(satField);
            StringBuffer buffer = new StringBuffer();
            Enumeration satEnum = location.getSatelliteInfo();
            if (satEnum != null && satEnum.hasMoreElements()) {
                buffer.append("ID\tAzimuth\tElevation\tSignal\n");
                while (satEnum.hasMoreElements()) {
                    SatelliteInfo satInfo = (SatelliteInfo) satEnum.nextElement();
                    if (satInfo != null && satInfo.isValid()) {
                        buffer.append(satInfo.getId() + "\t" + satInfo.getAzimuth() + "\t" + satInfo.getElevation() + "\t" + satInfo.getSignalQuality() + "\n");
                    }
                }
                satField.setText(buffer.toString());
            } else {
                buffer.append("No satellite information available for current location fix");
                satField.setText(buffer.toString());
            }
            UiApplication.getUiApplication().invokeLater(new Runnable() {

                public void run() {
                    UiApplication.getUiApplication().pushScreen(satPopup);
                }
            });
        } else {
            UiApplication.getUiApplication().invokeLater(new Runnable() {

                public void run() {
                    Dialog.inform("No location fix available..");
                }
            });
        }
    }
}
Also used : EditField(net.rim.device.api.ui.component.EditField) Enumeration(java.util.Enumeration) Screen(net.rim.device.api.ui.Screen) PopupScreen(net.rim.device.api.ui.container.PopupScreen) MainScreen(net.rim.device.api.ui.container.MainScreen) LocationException(javax.microedition.location.LocationException) PopupScreen(net.rim.device.api.ui.container.PopupScreen) SimpleLocationProvider(rimx.location.simplelocation.SimpleLocationProvider) LocationException(javax.microedition.location.LocationException) SatelliteInfo(net.rim.device.api.gps.SatelliteInfo) VerticalFieldManager(net.rim.device.api.ui.container.VerticalFieldManager)

Aggregations

Enumeration (java.util.Enumeration)1 LocationException (javax.microedition.location.LocationException)1 SatelliteInfo (net.rim.device.api.gps.SatelliteInfo)1 Screen (net.rim.device.api.ui.Screen)1 EditField (net.rim.device.api.ui.component.EditField)1 MainScreen (net.rim.device.api.ui.container.MainScreen)1 PopupScreen (net.rim.device.api.ui.container.PopupScreen)1 VerticalFieldManager (net.rim.device.api.ui.container.VerticalFieldManager)1 SimpleLocationProvider (rimx.location.simplelocation.SimpleLocationProvider)1