DiscoveryApppublic class DiscoveryApp extends MIDlet implements CommandListenerThe 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 | displayDisplay for this MIDlet. | private String | defaultInstallListUrlContains the default URL for the install list. | private TextBox | urlTextBoxContains the URL the user typed in. | private Form | progressFormDisplays the progress of the install. | private int | progressGaugeIndexGauge for progress form index. | private int | progressUrlIndexURL for progress form index. | private long | lastDisplayChangeKeeps track of when the display last changed, in milliseconds. | private javax.microedition.lcdui.List | installListBoxDisplays a list of suites to install to the user. | private Vector | installListContains a list of suites to install. | private Command | discoverCmdCommand object for URL screen to go and discover available suites. | private Command | installCmdCommand object for "Install" command in the suite list form . | private Command | backCmdCommand object for "Back" command in the suite list form. | private Command | saveCmdCommand object for URL screen to save the URL for suites. | private Command | endCmdCommand 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 void | commandAction(Command c, Displayable s)Respond to a command issued on any Screen.
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 void | destroyApp(boolean unconditional)Destroy cleans up.
| private void | discoverSuitesToInstall(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.
new Thread(new BackgroundInstallListGetter(this, url)).start();
| private void | displayException(java.lang.String title, java.lang.String message)Display an exception to the user, with a done command.
Alert a = new Alert(title, message, null, AlertType.ERROR);
a.setTimeout(Alert.FOREVER);
a.setCommandListener(this);
display.setCurrent(a);
| private Form | displayProgressForm(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.
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 void | displaySuccessMessage(java.lang.String successMessage)Alert the user that an action was successful.
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 void | getUrl()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 void | installSuite(int selectedSuite)Install a suite.
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 void | pauseApp()Pause; there are no resources that need to be released.
| private void | restoreSettings()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 void | saveURLSetting()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 void | startApp()Start.
| private void | updateProgressForm(java.lang.String url, int size, java.lang.String gaugeLabel)Update URL and gauge of the progress form.
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();
|
|