/** * 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 javax.microedition.content.ContentHandlerServer; import javax.microedition.content.Invocation; import javax.microedition.content.Registry; import javax.microedition.content.RequestListener; import javax.microedition.lcdui.Alert; import javax.microedition.lcdui.AlertType; import javax.microedition.lcdui.Display; import javax.microedition.lcdui.Form; import javax.microedition.midlet.MIDlet; import javax.microedition.midlet.MIDletStateChangeException; public class TextHandler extends MIDlet implements RequestListener { // Choice list title private static final String FAVOURITE_LINKS = "Favourite Links"; // content type constant private static final String CONTENT_TYPE = "text/plain"; // Current class name private static final String CLASS_NAME = "com.nokia.example.contenthandler.texthandler.TextHandler"; // Display instance Display display = null; // Current invocation, null if no Invocation. private Invocation invocation; // ContentHandlerServer from which to get requests. private ContentHandlerServer handler; // Access to Registry functions and responses. private Registry registry; // instance to display choice list ChoiceList choices; // Invocation URL String url; /** * Constructor creates Display instance and create choice list. */ public TextHandler() { display = Display.getDisplay(this); //Display the choice list choices = new ChoiceList(FAVOURITE_LINKS, this); display.setCurrent(choices); } protected void startApp() throws MIDletStateChangeException { register(); } protected void destroyApp(boolean arg0) { } protected void pauseApp() { } /** * Method displays Text Invocation. Current invocation handles Text Content * type "text/plain" * * @param invoc invocation with content type "text/plain" */ void showCurrentInvocation(Invocation invoc) { Form textViewerFrom = new TextViewer(invoc, this); display.setCurrent(textViewerFrom); } /** * Register this MIDlet to receive invocations about text/plain */ void register() { try { registry = Registry.getRegistry(TextHandler.CLASS_NAME); handler = registry.register(TextHandler.CLASS_NAME, new String[]{TextHandler.CONTENT_TYPE}, null, null, null, null, null); if (handler != null) { handler.setListener(this); } displayMessage(TextHandler.CONTENT_TYPE + " was successfully registered for this MIDlet!"); } catch (Exception che) { //che.printStackTrace(); displayMessage(TextHandler.CONTENT_TYPE + " registration failed!"); } } /** * It displays alert messages * * @param message Message to display * */ private void displayMessage(String message) { Alert alert = new Alert("Message", message, null, AlertType.INFO); display.setCurrent(alert); } /** * Handles incoming invocation request using the ContentHandlerServer * instance. If there is a current Invocation finish it so the next Contact * can be displayed. */ public void invocationRequestNotify(ContentHandlerServer server) { if (invocation != null) { handler.finish(invocation, Invocation.OK); } invocation = handler.getRequest(false); if (invocation != null) { showCurrentInvocation(invocation); } } /** * Finish the current invocation * * @param invocStatus */ void doFinish(int invocStatus) { if (invocStatus == Invocation.OK) { if (invocation != null) { invocation = null; } } } /** * Invoke the URL provided. * * @param url http URL */ void doInvoke(String url) { try { Invocation invoc = new Invocation(url); if (registry.invoke(invoc)) { destroyApp(true); notifyDestroyed(); } else { //displayMessage("Could not link to " + url); } } catch (Exception ex) { //displayMessage("Could not link to " + url); } } }