/** * Copyright (c) 2012 Nokia Corporation. All rights reserved. * Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation. * Oracle and Java are trademarks or registered trademarks of Oracle and/or its * affiliates. Other product and company names mentioned herein may be trademarks * or trade names of their respective owners. * See LICENSE.TXT for license information. */ package com.nokia.example.contenthandler.texthandler; import java.io.InputStream; import javax.microedition.content.Invocation; import javax.microedition.io.HttpConnection; import javax.microedition.lcdui.Alert; import javax.microedition.lcdui.AlertType; import javax.microedition.lcdui.Command; import javax.microedition.lcdui.CommandListener; import javax.microedition.lcdui.Displayable; import javax.microedition.lcdui.Form; import javax.microedition.lcdui.Item; import javax.microedition.lcdui.StringItem; /** * Class display the text invocation. It uses a form to display the invocation * text. Basically, it makes HTTP Connection with the invocation URL and get the * content. * * @author gothadiy * */ public class TextViewer extends Form implements CommandListener { // Intial title of the text display form private static final String DEFAULT_TITLE = "Text Viewer"; // Message saying.. No content to display private static final String NO_CONTENT = "no content to display"; // Instance of TextHandler used to display form private TextHandler textHandler = null; // Instance of invocation. Get value from the constructor private Invocation invoc = null; Command detailCommand = new Command("Details", Command.ITEM, 1); // Go back to the choice list window Command backCommand = new Command("Back", Command.BACK, 2); /** * Constructor initialize gives instances of TextHandler and Invocation and * initalize the text contents. * * @param _invoc Text Invocation instance. * @param _textHandler Instance of TextHandler */ TextViewer(Invocation _invoc, TextHandler _textHandler) { super(TextViewer.DEFAULT_TITLE); invoc = _invoc; textHandler = _textHandler; this.addCommand(detailCommand); this.addCommand(backCommand); setCommandListener(this); displayContent(invoc); } /** * It validates the invoc object and URL. It creates the HTTP connection and * get the text content. Append it to the form and display. * * @param invoc Text Invocation instance. */ private void displayContent(Invocation invoc) { String url; if ((invoc == null)) { this.append(TextViewer.NO_CONTENT); return; } if ((url = invoc.getURL()) == null) { this.append(TextViewer.NO_CONTENT); return; } // Set file name as a form title String title = url.substring(url.lastIndexOf('/') + 1); this.setTitle(TextViewer.DEFAULT_TITLE + " - " + title); HttpConnection conn = null; InputStream input = null; StringBuffer txtContentBuff; try { //Open connection using invocation conn = (HttpConnection) invoc.open(false); conn.setRequestMethod(HttpConnection.GET); // If not successful, Display response code with response message. if (conn.getResponseCode() != HttpConnection.HTTP_OK) { this.append(conn.getResponseMessage() + " (" + conn.getResponseCode() + ")"); return; } // 2,5 training 1 bf + meeting 3,5 CH updating // Create StringBuffer with the size of response length txtContentBuff = new StringBuffer((int) conn.getLength()); // Max size supported is char 4096; int maxLen = 4096; input = conn.openInputStream(); // Read char and store it to the StringBuffer int ch; while ((ch = input.read()) != -1) { txtContentBuff.append((char) ch); if (txtContentBuff.length() >= maxLen) { break; } } // Process the buffer and append text to the form this.append(new StringItem(null, txtContentBuff.toString(), Item.PLAIN)); } catch (Throwable th) { txtContentBuff = null; //th.printStackTrace(); } finally { try { if (input != null) { input.close(); } if (conn != null) { conn.close(); } } catch (Exception e) { } } } public void commandAction(Command command, Displayable disp) { if (command == detailCommand) { try { String details = "Invocation Type: " + invoc.findType() + "\n"; details += "Action: " + invoc.getAction() + "\n"; details += "ID: " + invoc.getID() + "\n"; details += "Invoking App Name: " + invoc.getInvokingAppName() + "\n"; details += "Invoking Authority: " + invoc.getInvokingAuthority() + "\n"; details += "Invoking ID: " + invoc.getInvokingID() + "\n"; Alert alert = new Alert("Details", details, null, AlertType.INFO); alert.setTimeout(Alert.FOREVER); textHandler.display.setCurrent(alert); } catch (Exception e) { //e.printStackTrace(); } } else if (command == backCommand) { textHandler.doFinish(Invocation.OK); textHandler.display.setCurrent(textHandler.choices); } } }