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.http;
033:
034: import java.io.DataInputStream;
035: import java.io.IOException;
036: import java.io.InputStream;
037: import java.io.OutputStream;
038:
039: import java.lang.String;
040:
041: import java.util.Date;
042: import java.util.Vector;
043:
044: import javax.microedition.io.*;
045: import javax.microedition.lcdui.*;
046: import javax.microedition.midlet.*;
047:
048: /**
049: * An example MIDlet to fetch a page using an HttpConnection.
050: * Refer to the startApp, pauseApp, and destroyApp
051: * methods so see how it handles each requested transition.
052: */
053: public class PushExample extends MIDlet implements CommandListener {
054: // Wait for 2sec
055: static final int DefaultTimeout = 2000;
056:
057: /** user interface command for indicating Exit request. */
058: Command exitCommand = new Command("Exit", Command.EXIT, 2);
059:
060: /** user interface component containing a list of URLs */
061: List urlList;
062:
063: /** array of current URLs */
064: Vector urls;
065:
066: /** user interface alert component. */
067: Alert alert;
068: Image newsHoundImage;
069: boolean imageLoaded;
070:
071: /** current display. */
072: Display display;
073:
074: /** current requested url. */
075: String url;
076:
077: /** initialize the MIDlet with the current display object. */
078: public PushExample() {
079: display = Display.getDisplay(this );
080: }
081:
082: /**
083: * Start creates the thread to do the timing.
084: * It should return immediately to keep the dispatcher
085: * from hanging.
086: */
087: public void startApp() {
088: try {
089: newsHoundImage = Image
090: .createImage("/example/http/images/newshound.png");
091: imageLoaded = true;
092: } catch (java.io.IOException ex) {
093: System.err.println("Image is not loaded :" + imageLoaded);
094: }
095:
096: alert = new Alert("News Hound", "", newsHoundImage,
097: AlertType.INFO);
098:
099: alert.setTimeout(DefaultTimeout);
100: setupList();
101:
102: /* Bytes read from the URL update connection. */
103: int count;
104:
105: /* Check for inbound async connection for sample Finger port. */
106: String[] connections = PushRegistry.listConnections(true);
107:
108: /* HttpView was started to handle inbound request. */
109: String pushProperty = getAppProperty("MIDlet-Push-1");
110:
111: if ((connections != null) && (connections.length > 0)) {
112: String newurl = "Pushed URL Placeholder";
113:
114: /* Test basic get registry information interfaces. */
115: try {
116: String midlet = PushRegistry.getMIDlet(connections[0]);
117: String filter = PushRegistry.getFilter(connections[0]);
118: } catch (Exception e) {
119: e.printStackTrace();
120: }
121:
122: /* Check for socket or datagram connection. */
123: if (connections[0].startsWith("socket://")) {
124: try {
125: /* Simple test assumes a server socket connection. */
126: ServerSocketConnection scn = (ServerSocketConnection) Connector
127: .open(connections[0]);
128: SocketConnection sc = (SocketConnection) scn
129: .acceptAndOpen();
130:
131: /* Read one line of text as a new URL to add to the list. */
132: DataInputStream dis = sc.openDataInputStream();
133: byte[] buf = new byte[256];
134: int endofline = 0;
135: count = dis.read(buf);
136:
137: for (int i = 0; i < count; i++) {
138: if (buf[i] == '\n') {
139: endofline = i;
140:
141: break;
142: }
143: }
144:
145: newurl = new String(buf, 0, endofline);
146:
147: dis.close();
148:
149: sc.close();
150: scn.close();
151: } catch (IOException e) {
152: System.err
153: .println("******* io exception in push example");
154: e.printStackTrace();
155: }
156: } else {
157: System.err.println("Unknown connection type");
158: }
159:
160: urlList.append(newurl, null);
161: urls.addElement(newurl);
162: } else {
163: connections = PushRegistry.listConnections(false);
164: }
165:
166: display.setCurrent(alert, urlList);
167: }
168:
169: /**
170: * Pause signals the thread to stop by clearing the thread field.
171: * If stopped before done with the iterations it will
172: * be restarted from scratch later.
173: */
174: public void pauseApp() {
175: }
176:
177: /**
178: * Destroy must cleanup everything. The thread is signaled
179: * to stop and no result is produced.
180: * @param unconditional true if a forced shutdown was requested
181: */
182: public void destroyApp(boolean unconditional) {
183: }
184:
185: /**
186: * Check the attributes in the descriptor that identify
187: * url's and titles and initialize the lists of urls
188: * and urlList.
189: * <P>
190: * The attributes are named "ViewTitle-n" and "ViewURL-n".
191: * The value "n" must start at "1" and increment by 1.
192: */
193: void setupList() {
194: urls = new Vector();
195: urlList = new List("News Headlines", List.IMPLICIT);
196: urlList.setFitPolicy(Choice.TEXT_WRAP_OFF);
197: urlList.addCommand(exitCommand);
198: urlList.setCommandListener(this );
199:
200: for (int n = 1; n < 100; n++) {
201: String nthURL = "ViewURL-" + n;
202: String url = getAppProperty(nthURL);
203:
204: if ((url == null) || (url.length() == 0)) {
205: break;
206: }
207:
208: String nthTitle = "ViewTitle-" + n;
209: String title = getAppProperty(nthTitle);
210:
211: if ((title == null) || (title.length() == 0)) {
212: title = url;
213: }
214:
215: urls.addElement(url);
216: urlList.append(title, null);
217: }
218:
219: urlList.append("Next InComing News: ", null);
220: }
221:
222: /**
223: * Respond to commands, including exit
224: * @param c user interface command requested
225: * @param s screen object initiating the request
226: */
227: public void commandAction(Command c, Displayable s) {
228: try {
229: if (c == exitCommand) {
230: destroyApp(false);
231: notifyDestroyed();
232: }
233: } catch (Exception ex) {
234: ex.printStackTrace();
235: }
236: }
237: }
|