FileDocCategorySizeDatePackage
DiscoveryApp.javaAPI DocphoneME MR2 API (J2ME)19902Wed May 02 18:00:02 BST 2007com.sun.midp.installer

DiscoveryApp

public class DiscoveryApp extends MIDlet implements CommandListener
The Graphical MIDlet suite Discovery Application.

Let the user install a suite from a list of suites obtained using an HTML URL given by the user. This list is derived by extracting the links with hrefs that are in quotes and end with ".jad" from the HTML page. An href in an extracted link is assumed to be an absolute URL for a MIDP application descriptor. The selected URL is then passed to graphical Installer.

Fields Summary
private Display
display
Display for this MIDlet.
private String
defaultInstallListUrl
Contains the default URL for the install list.
private TextBox
urlTextBox
Contains the URL the user typed in.
private Form
progressForm
Displays the progress of the install.
private int
progressGaugeIndex
Gauge for progress form index.
private int
progressUrlIndex
URL for progress form index.
private long
lastDisplayChange
Keeps track of when the display last changed, in milliseconds.
private javax.microedition.lcdui.List
installListBox
Displays a list of suites to install to the user.
private Vector
installList
Contains a list of suites to install.
private Command
discoverCmd
Command object for URL screen to go and discover available suites.
private Command
installCmd
Command object for "Install" command in the suite list form .
private Command
backCmd
Command object for "Back" command in the suite list form.
private Command
saveCmd
Command object for URL screen to save the URL for suites.
private Command
endCmd
Command object for "Back" command in the URL form.
Constructors Summary
public DiscoveryApp()
Create and initialize a new discovery application MIDlet. The saved URL is retrieved and the list of MIDlets are retrieved.


                             
      
        String storageName;

        display = Display.getDisplay(this);

        GraphicalInstaller.initSettings();
        restoreSettings();

        // get the URL of a list of suites to install
        getUrl();
    
Methods Summary
public voidcommandAction(Command c, Displayable s)
Respond to a command issued on any Screen.

param
c command activated by the user
param
s the Displayable the command was on.

        if (c == discoverCmd) {
            // user wants to discover the suites that can be installed
            discoverSuitesToInstall(urlTextBox.getString());
        } else if (s == installListBox &&
                  (c == List.SELECT_COMMAND || c == installCmd)) {
            installSuite(installListBox.getSelectedIndex());
        } else if (c == backCmd) {
            display.setCurrent(urlTextBox);
        } else if (c == saveCmd) {
            saveURLSetting();
        } else if (c == endCmd || c == Alert.DISMISS_COMMAND) {
            // goto back to the manager midlet
            notifyDestroyed();
        }
    
public voiddestroyApp(boolean unconditional)
Destroy cleans up.

param
unconditional is ignored; this object always destroys itself when requested.

    
private voiddiscoverSuitesToInstall(java.lang.String url)
Let the user select a suite to install. The suites that are listed are the links on a web page that end with .jad.

param
url where to get the list of suites to install.

        new Thread(new BackgroundInstallListGetter(this, url)).start();
    
private voiddisplayException(java.lang.String title, java.lang.String message)
Display an exception to the user, with a done command.

param
title exception form's title
param
message exception message

        Alert a = new Alert(title, message, null, AlertType.ERROR);

        a.setTimeout(Alert.FOREVER);
        a.setCommandListener(this);

        display.setCurrent(a);
    
private FormdisplayProgressForm(java.lang.String action, java.lang.String name, java.lang.String url, int size, java.lang.String gaugeLabel)
Display the connecting form to the user, let call set actions.

param
action action to put in the form's title
param
name name to in the form's title
param
url URL of a JAD
param
size 0 if unknown, else size of object to download in K bytes
param
gaugeLabel label for progress gauge
return
displayed form

        Gauge progressGauge;
        StringItem urlItem;

        progressForm = new Form(null);

        progressForm.setTitle(action + " " + name);

        if (size <= 0) {
            progressGauge = new Gauge(gaugeLabel,
                                      false, Gauge.INDEFINITE,
                                      Gauge.CONTINUOUS_RUNNING);
        } else {
            progressGauge = new Gauge(gaugeLabel,
                                      false, size, 0);
        }

        progressGaugeIndex = progressForm.append(progressGauge);

        if (url == null) {
            urlItem = new StringItem("", "");
        } else {
            urlItem =
                new StringItem(Resource.getString
                               (ResourceConstants.AMS_WEBSITE) + ": ", url);
        }

        progressUrlIndex = progressForm.append(urlItem);

        display.setCurrent(progressForm);
        lastDisplayChange = System.currentTimeMillis();

        return progressForm;
    
private voiddisplaySuccessMessage(java.lang.String successMessage)
Alert the user that an action was successful.

param
successMessage message to display to user

        Image icon;
        Alert successAlert;

        icon = GraphicalInstaller.getImageFromInternalStorage("_dukeok8");

        successAlert = new Alert(null, successMessage, icon, null);

        successAlert.setTimeout(GraphicalInstaller.ALERT_TIMEOUT);

        // We need to prevent "flashing" on fast development platforms.
        while (System.currentTimeMillis() - lastDisplayChange <
               GraphicalInstaller.ALERT_TIMEOUT);

        lastDisplayChange = System.currentTimeMillis();
        display.setCurrent(successAlert);
    
private voidgetUrl()
Ask the user for the URL.

        try {
            if (urlTextBox == null) {
                urlTextBox = new TextBox(Resource.getString
                                         (ResourceConstants.
                                          AMS_DISC_APP_WEBSITE_INSTALL),
                                         defaultInstallListUrl, 1024,
                                         TextField.ANY);
                urlTextBox.addCommand(endCmd);
                urlTextBox.addCommand(saveCmd);
                urlTextBox.addCommand(discoverCmd);
                urlTextBox.setCommandListener(this);
            }

            display.setCurrent(urlTextBox);
        } catch (Exception ex) {
            displayException(Resource.getString(ResourceConstants.EXCEPTION),
                             ex.toString());
        }
    
private voidinstallSuite(int selectedSuite)
Install a suite.

param
selectedSuite index into the installList

        MIDletStateHandler midletStateHandler =
            MIDletStateHandler.getMidletStateHandler();
        MIDletSuite midletSuite = midletStateHandler.getMIDletSuite();
        SuiteDownloadInfo suite;
        String displayName;

        suite = (SuiteDownloadInfo)installList.elementAt(selectedSuite);

        midletSuite.setTempProperty(null, "arg-0", "I");
        midletSuite.setTempProperty(null, "arg-1", suite.url);
        midletSuite.setTempProperty(null, "arg-2", suite.label);

        displayName =
            Resource.getString(ResourceConstants.INSTALL_APPLICATION);
        try {
            midletStateHandler.startMIDlet(
                "com.sun.midp.installer.GraphicalInstaller", displayName);
            /*
             * Give the create MIDlet notification 1 second to get to
             * AMS.
             */
            Thread.sleep(1000);
            notifyDestroyed();
        } catch (Exception ex) {
            StringBuffer sb = new StringBuffer();

            sb.append(displayName);
            sb.append("\n");
            sb.append(Resource.getString(ResourceConstants.ERROR));
            sb.append(": ");
            sb.append(ex.toString());

            Alert a = new Alert(Resource.getString
                                (ResourceConstants.AMS_CANNOT_START),
                                sb.toString(), null, AlertType.ERROR);
            a.setTimeout(Alert.FOREVER);
            display.setCurrent(a, urlTextBox);
        }
    
public voidpauseApp()
Pause; there are no resources that need to be released.

    
private voidrestoreSettings()
Get the settings the Manager saved for the user.

        ByteArrayInputStream bas;
        DataInputStream dis;
        byte[] data;
        RecordStore settings = null;

        try {
            settings = RecordStore.openRecordStore(
                       GraphicalInstaller.SETTINGS_STORE, false);

            data = settings.getRecord(1);
            if (data != null) {
                bas = new ByteArrayInputStream(data);
                dis = new DataInputStream(bas);
                defaultInstallListUrl = dis.readUTF();
            }

        } catch (RecordStoreException e) {
            if (Logging.REPORT_LEVEL <= Logging.WARNING) {
                Logging.report(Logging.WARNING, LogChannels.LC_AMS,
                               "restoreSettings threw a RecordStoreException");
            }
        } catch (IOException e) {
            if (Logging.REPORT_LEVEL <= Logging.WARNING) {
                Logging.report(Logging.WARNING, LogChannels.LC_AMS,
                               "restoreSettings threw an IOException");
            }
        } finally {
            if (settings != null) {
                try {
                    settings.closeRecordStore();
                } catch (RecordStoreException e) {
                    if (Logging.REPORT_LEVEL <= Logging.WARNING) {
                        Logging.report(Logging.WARNING, LogChannels.LC_AMS,
                        "closeRecordStore threw a RecordStoreException");
                    }
                }
            }
        }
    
private voidsaveURLSetting()
Save the URL setting the user entered in to the urlTextBox.

        String temp;
        Exception ex;

        temp = urlTextBox.getString();

        ex = GraphicalInstaller.saveSettings(temp, MIDletSuite.INTERNAL_SUITE_ID);
        if (ex != null) {
            displayException(Resource.getString
                             (ResourceConstants.EXCEPTION), ex.toString());
            return;
        }

        defaultInstallListUrl = temp;

        displaySuccessMessage(Resource.getString
                              (ResourceConstants.AMS_MGR_SAVED));
    
public voidstartApp()
Start.

    
private voidupdateProgressForm(java.lang.String url, int size, java.lang.String gaugeLabel)
Update URL and gauge of the progress form.

param
url new URL, null to remove, "" to not change
param
size 0 if unknown, else size of object to download in K bytes
param
gaugeLabel label for progress gauge

        Gauge oldProgressGauge;
        Gauge progressGauge;
        StringItem urlItem;

        // We need to prevent "flashing" on fast development platforms.
        while (System.currentTimeMillis() - lastDisplayChange <
               GraphicalInstaller.ALERT_TIMEOUT);

        if (size <= 0) {
            progressGauge = new Gauge(gaugeLabel,
                                      false, Gauge.INDEFINITE,
                                      Gauge.CONTINUOUS_RUNNING);
        } else {
            progressGauge = new Gauge(gaugeLabel,
                                      false, size, 0);
        }

        oldProgressGauge = (Gauge)progressForm.get(progressGaugeIndex);
        progressForm.set(progressGaugeIndex, progressGauge);

        // this ends the background thread of gauge.
        oldProgressGauge.setValue(Gauge.CONTINUOUS_IDLE);

        if (url == null) {
            urlItem = new StringItem("", "");
            progressForm.set(progressUrlIndex, urlItem);
        } else if (url.length() != 0) {
            urlItem =
                new StringItem(Resource.getString
                               (ResourceConstants.AMS_WEBSITE) + ": ", url);
            progressForm.set(progressUrlIndex, urlItem);
        }

        lastDisplayChange = System.currentTimeMillis();