Protected calls from Commands

On Asha software platform 1.0, protected methods that are executed from Commands, i.e. called within a CommandListener implementation, need to be wrapped in a Thread. This is a significant change compared to the Series 40 platform that allows protected methods to be called directly from the commandAction() method, without necessarily initiating a Thread.

As an example, we demonstrate below what changes are needed, when a location retrieval operation takes place as the result of a Command selection.

In the sample code below, the location retrieval, which is a protected call, is performed directly within the commandAction() method.

public class ProtectedCallS40
            extends MIDlet
            implements CommandListener {

    private Display display;
    private Form mainScreen;
    private Command execute;
    private Command exitCmd;

    protected void startApp() throws MIDletStateChangeException {
        if (display == null) {
            // The initialization screen with the Commands
            display = Display.getDisplay(this);
            execute = new Command("Execute", Command.OK, 1);
            exitCmd = new Command("Exit", Command.EXIT, 1);
            mainScreen = new Form("Protected Call");
            mainScreen.append("Select the Command to execute a protected method call\n");
            display.setCurrent(mainScreen);
            mainScreen.addCommand(execute);
            mainScreen.addCommand(exitCmd);
            mainScreen.setCommandListener(this);
        }
    }

    public void commandAction(Command cmd, Displayable display) {
        if (cmd == execute) {
            try {
                // Specify the retrieval method to Online/Cell-ID
                int[] methods = {(Location.MTA_ASSISTED | Location.MTE_CELLID | Location.MTE_SHORTRANGE | Location.MTY_NETWORKBASED)};
                // Retrieving the location is a protected method call
                LocationProvider provider = LocationUtil.getLocationProvider(methods, null);
                Location location = provider.getLocation(50000);
                QualifiedCoordinates coordinates = location.getQualifiedCoordinates();
                mainScreen.append("Latitude:" + coordinates.getLatitude() + "\n");
                mainScreen.append("Longitude:" + coordinates.getLongitude() + "\n");
            }
            catch (Exception exception) {
                mainScreen.append("Exception: " + exception.getMessage() + "\n");
            }
        }
        if (cmd == exitCmd) {
            notifyDestroyed();
        }
    }

    protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
        // TODO Auto-generated method stub
    }

    protected void pauseApp() {
        // TODO Auto-generated method stub
    }
}

This operation is allowed on Series 40, but throws a SecurityException on Asha software platform 1.0 with the following Exception message: “Blocking call performed in the event thread”.

In order to perform the same operation on Asha software platform 1.0, we need to initiate a Thread, as soon as the execute Command is selected. The CommandAction() method contains only the starting of the Thread. The location retrieval is performed within the Thread’s body, i.e. the run() method, as follows:

public class ProtectedCallAsha
            extends MIDlet
            implements CommandListener, Runnable {

    private Display display;
    private Form mainScreen;
    private Command execute;
    private Command exitCmd;
    Thread thread;

    protected void startApp() throws MIDletStateChangeException {
        //The initialization screen with the Commands
        if (display == null) {
            display = Display.getDisplay(this);
            execute = new Command("Execute", Command.OK, 1);
            exitCmd = new Command("Exit", Command.EXIT, 1);
            mainScreen = new Form("Protected Call");
            mainScreen.append("Select the Command to execute a protected method call\n");
            display.setCurrent(mainScreen);
            mainScreen.addCommand(execute);
            mainScreen.addCommand(exitCmd);
            mainScreen.setCommandListener(this);
        }
    }

    public void commandAction(Command cmd, Displayable display) {
        // Direct call to the protected method from commandAction is not allowed on Asha software platform 1.0
        if (cmd == execute) {
            thread = new Thread(this);
            thread.start();
        }
        if (cmd == exitCmd) {
            notifyDestroyed();
        }
    }

    public void run() {
        //The protected method call is executed within a Thread
        try {
            // Specify the retrieval method to Online/Cell-ID
            int[] methods = {(Location.MTA_ASSISTED | Location.MTE_CELLID | Location.MTE_SHORTRANGE | Location.MTY_NETWORKBASED)};
            // The location retrieval is a protected method call
            LocationProvider provider = LocationUtil.getLocationProvider(methods, null);
            Location location = provider.getLocation(50000);
            QualifiedCoordinates coordinates = location.getQualifiedCoordinates();
            mainScreen.append("Latitude:" + coordinates.getLatitude() + "\n");
            mainScreen.append("Longitude:" + coordinates.getLongitude() + "\n");
        }
        catch (Exception exception) {
            mainScreen.append("Exception: " + exception.getMessage() + "\n");
        }
    }

    protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
        // TODO Auto-generated method stub
    }

    protected void pauseApp() {
        // TODO Auto-generated method stub
    }
}