📄 graphicalinstaller.java
字号:
* Respond to a command issued on any Screen. * * @param c command activiated by the user * @param s the Displayable the command was on. */ public void commandAction(Command c, Displayable s) { 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 == nextCmd) { // the user has entered a username and password resumeInstallWithPassword(); } else if (c == okCmd) { resumeInstallAfterWarning(); } else if (c == continueCmd) { startJarDownload(); } else if (c == saveCmd) { saveURLSetting(); } else if (c == keepRMSCmd) { setKeepRMSAnswer(true); } else if (c == removeRMSCmd) { setKeepRMSAnswer(false); } else if (c == stopCmd) { if (installer != null) { /* * BackgroundInstaller may be displaying * the "Finishing" message * * also we need to prevent the BackgroundInstaller from * re-displaying the list before the cancelled message is * displayed */ synchronized (this) { if (installer.stopInstalling()) { displayCancelledMessage(cancelledMessage); } } } else { // goto back to the manager midlet notifyDestroyed(); } } else if (c == cancelCmd) { displayCancelledMessage(cancelledMessage); cancelBackgroundInstall(); } else if (c == endCmd || c == Alert.DISMISS_COMMAND) { // goto back to the manager midlet notifyDestroyed(); } } /** * Get the settings the Manager saved for the user. */ private void restoreSettings() { ByteArrayInputStream bas; DataInputStream dis; byte[] data; RecordStore settings = null; try { settings = RecordStore.openRecordStore(SETTINGS_STORE, false); data = settings.getRecord(1); if (data != null) { bas = new ByteArrayInputStream(data); dis = new DataInputStream(bas); defaultInstallListUrl = dis.readUTF(); } } catch (RecordStoreException e) { // ignore } catch (IOException e) { // ignore } finally { if (settings != null) { try { settings.closeRecordStore(); } catch (RecordStoreException e) { // ignore } } } } /** * Initialize the settings database if it doesn't exist. This may create * two entries. The first will be for the download url, the second will * be for storing the storagename of the currently selected midlet */ static void initSettings() { try { RecordStore settings = RecordStore. openRecordStore(SETTINGS_STORE, true); if (settings.getNumRecords() == 0) { // space for a URL settings.addRecord(null, 0, 0); // space for current MIDlet Suite name settings.addRecord(null, 0, 0); } settings.closeRecordStore(); } catch (Exception e) {} } /** * Save the settings the user entered. * * @param url the url to save * @param curMidlet the storagename of the currently selected midlet * @return the Exception that may have been thrown, or null */ static Exception saveSettings(String url, String curMidlet) { Exception ret = null; try { String temp; ByteArrayOutputStream bas; DataOutputStream dos; byte[] data; RecordStore settings; bas = new ByteArrayOutputStream(); dos = new DataOutputStream(bas); settings = RecordStore.openRecordStore(SETTINGS_STORE, false); if (url != null) { dos.writeUTF(url); data = bas.toByteArray(); settings.setRecord(URL_RECORD_ID, data, 0, data.length); } if (curMidlet != null) { bas.reset(); dos.writeUTF(curMidlet); data = bas.toByteArray(); settings.setRecord(SELECTED_MIDLET_RECORD_ID, data, 0, data.length); } settings.closeRecordStore(); dos.close(); } catch (Exception e) { ret = e; } return ret; } /** * Save the URL setting the user entered in to the urlTextBox. */ private void saveURLSetting() { String temp; Exception ex; temp = urlTextBox.getString(); ex = saveSettings(temp, null); if (ex != null) { displayException(Resource.getString("Exception"), ex.toString()); return; } defaultInstallListUrl = temp; displaySuccessMessage(Resource.getString("Saved!")); } /** * Update a suite. * * @param storageName storage name of the suite to update */ private void updateSuite(String storageName) { MIDletSuite midletSuite = installer.getMIDletSuite(storageName); MIDletInfo midletInfo; String name; if (midletSuite.getNumberOfMIDlets() == 1) { midletInfo = new MIDletInfo(midletSuite.getProperty("MIDlet-1")); name = midletInfo.name; } else { name = midletSuite.getProperty(Installer.SUITE_NAME_PROP); } cancelledMessage = Resource.getString("Update cancelled."); finishingMessage = Resource.getString("Finishing update."); installSuiteCommon("Updating", name, midletSuite.getDownloadUrl(), name + Resource.getString(" was successfully updated"), true); } /** * 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. */ private void discoverSuitesToInstall(String url) { new Thread(new BackgroundInstallListGetter(this, url)).start(); } /** * Install a suite. * * @param selectedSuite index into the installList */ private void installSuite(int selectedSuite) { SuiteDownloadInfo suite; suite = (SuiteDownloadInfo)installList.elementAt(selectedSuite); cancelledMessage = Resource.getString("Installation cancelled."); finishingMessage = Resource.getString("Finishing installation."); installSuiteCommon("Installing", suite.label, suite.url, suite.label + Resource.getString(" was successfully installed"), false); } /** * Common helper method to install or update a suite. * * @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 successMessage message to display to user upon success * @param updateFlag if true the current suite is being updated */ private void installSuiteCommon(String action, String name, String url, String successMessage, boolean updateFlag) { try { createProgressForm(action, name, url, 0, "Connecting..."); backgroundInstaller = new BackgroundInstaller(this, url, name, successMessage, updateFlag); new Thread(backgroundInstaller).start(); } catch (Exception ex) { StringBuffer sb = new StringBuffer(); sb.append(name); sb.append("\n"); sb.append(Resource.getString("Error")); sb.append(": "); sb.append(ex.toString()); displayException(Resource.getString("Cannot access: "), sb.toString()); } } /** * Create and display the progress form to the user with the stop action. * * @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 */ private void createProgressForm(String action, String name, String url, int size, String gaugeLabel) { Form installForm; // display the JAR progress form installForm = displayProgressForm(action, name, url, size, gaugeLabel); installForm.addCommand(stopCmd); installForm.setCommandListener(this); } /** * 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 */ private Form displayProgressForm(String action, String name, String url, int size, String gaugeLabel) { Gauge progressGauge; StringItem urlItem; progressForm = new Form(null); progressForm.setTitle(Resource.getString(action) + " " + name); if (size <= 0) { progressGauge = new Gauge(Resource.getString(gaugeLabel), false, Gauge.INDEFINITE, Gauge.CONTINUOUS_RUNNING); } else { progressGauge = new Gauge(Resource.getString(gaugeLabel), false, size, 0); } progressGaugeIndex = progressForm.append(progressGauge); if (url == null) { urlItem = new StringItem("", ""); } else { urlItem = new StringItem(Resource.getString("Website") + ": ", url); } progressUrlIndex = progressForm.append(urlItem); display.setCurrent(progressForm); lastDisplayChange = System.currentTimeMillis(); return progressForm; } /** Cancel an install (if there is one) waiting for user input. */ private void cancelBackgroundInstall() { if (backgroundInstaller != null) { backgroundInstaller.continueInstall = false; synchronized (backgroundInstaller) { backgroundInstaller.notify(); } } } /** * Ask the user for the URL. */ private void getUrl() { try { if (urlTextBox == null) { urlTextBox = new TextBox(Resource.getString( "Enter a website to Install From:"), 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("Exception"), ex.toString()); } } /** * Update the status form. * * @param status current status of the install. * @param state current state of the install. */ private void updateStatus(int status, InstallState state) { if (status == Installer.DOWNLOADING_JAD) { updateProgressForm("", 0, "Downloading the application description file..."); return; } if (status == Installer.DOWNLOADING_JAR) { updateProgressForm(state.getJarUrl(), state.getJarSize(), "Downloading the application file..."); return; } if (status == Installer.DOWNLOADED_1K_OF_JAR && state.getJarSize() > 0) { Gauge progressGauge = (Gauge)progressForm.get(progressGaugeIndex); progressGauge.setValue(progressGauge.getValue() + 1); return; } if (status == Installer.VERIFYING_SUITE) { updateProgressForm(null, 0, "Verifying the application..."); return; } if (status == Installer.STORING_SUITE) { displaySuccessMessage(finishingMessage); return; } } /** * 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 */ private void updateProgressForm(String url, int size, String gaugeLabel) { Gauge oldProgressGauge; Gauge progressGauge; StringItem urlItem; // We need to prevent "flashing" on fast development platforms. while (System.currentTimeMillis() - lastDisplayChange < ALERT_TIMEOUT); if (size <= 0) { progressGauge = new Gauge(Resource.getString(gaugeLabel), false, Gauge.INDEFINITE, Gauge.CONTINUOUS_RUNNING); } else { progressGauge = new Gauge(Resource.getString(gaugeLabel), false, size, 0); } oldProgressGauge = (Gauge)progressForm.get(progressGaugeIndex);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -