001: /*
002: *
003: * Copyright (c) 2007, Sun Microsystems, Inc.
004: *
005: * All rights reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * * Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: * * Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in the
015: * documentation and/or other materials provided with the distribution.
016: * * Neither the name of Sun Microsystems nor the names of its contributors
017: * may be used to endorse or promote products derived from this software
018: * without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
021: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
022: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
023: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
024: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
027: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
028: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
029: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031: */
032: package example;
033:
034: import javax.microedition.lcdui.*;
035: import javax.microedition.midlet.*;
036:
037: /**
038: * An example MIDlet shows the values of the system properties.
039: * Refer to the startApp, pauseApp, and destroyApp
040: * methods so see how it handles each requested transition.
041: */
042: public class PropExample extends MIDlet implements CommandListener {
043: private Display display;
044: private Form props;
045: private StringBuffer propbuf;
046: private Command exitCommand = new Command("Exit", Command.EXIT, 1);
047: private boolean firstTime;
048:
049: /*
050: * Construct a new PropExample.
051: */
052: public PropExample() {
053: display = Display.getDisplay(this );
054: firstTime = true;
055: props = new Form("System Properties");
056: }
057:
058: /**
059: * Show the value of the properties
060: */
061: public void startApp() {
062: Runtime runtime = Runtime.getRuntime();
063: runtime.gc();
064:
065: long free = runtime.freeMemory();
066:
067: if (firstTime) {
068: long total = runtime.totalMemory();
069:
070: propbuf = new StringBuffer(50);
071:
072: props.append("Free Memory = " + free + "\n");
073: props.append("Total Memory = " + total + "\n");
074:
075: props.append(showProp("microedition.configuration"));
076: props.append(showProp("microedition.profiles"));
077:
078: props.append(showProp("microedition.platform"));
079: props.append(showProp("microedition.locale"));
080: props.append(showProp("microedition.encoding"));
081:
082: props.addCommand(exitCommand);
083: props.setCommandListener(this );
084: display.setCurrent(props);
085: firstTime = false;
086: } else {
087: props.set(0, new StringItem("", "Free Memory = " + free
088: + "\n"));
089: }
090:
091: display.setCurrent(props);
092: }
093:
094: public void commandAction(Command c, Displayable s) {
095: if (c == exitCommand) {
096: destroyApp(false);
097: notifyDestroyed();
098: }
099: }
100:
101: /**
102: * Show a property.
103: */
104: String showProp(String prop) {
105: String value = System.getProperty(prop);
106: propbuf.setLength(0);
107: propbuf.append(prop);
108: propbuf.append(" = ");
109:
110: if (value == null) {
111: propbuf.append("<undefined>");
112: } else {
113: propbuf.append("\"");
114: propbuf.append(value);
115: propbuf.append("\"");
116: }
117:
118: propbuf.append("\n");
119:
120: return propbuf.toString();
121: }
122:
123: /**
124: * Time to pause, free any space we don't need right now.
125: */
126: public void pauseApp() {
127: }
128:
129: /**
130: * Destroy must cleanup everything.
131: */
132: public void destroyApp(boolean unconditional) {
133: }
134: }
|