example.imageviewer
Class ImageViewer

java.lang.Object
  extended by javax.microedition.midlet.MIDlet
      extended by example.imageviewer.ImageViewer
All Implemented Interfaces:
javax.microedition.content.RequestListener, javax.microedition.content.ResponseListener, javax.microedition.lcdui.CommandListener

public class ImageViewer
extends javax.microedition.midlet.MIDlet
implements javax.microedition.lcdui.CommandListener, javax.microedition.content.RequestListener, javax.microedition.content.ResponseListener

Simple Image Viewer for PNG images. This application uses the CHAPI API both to invoke a content handler via a URL and to handle an Invocation request. The application implements the CHAPI request listener to respond to requests. The listener constructs an Image from the URL and displays it. A "Back" command is made available to finish the request. A response listener is used to handle the response as an informational alert to the user. The application provides a simple user interface to enter a URL and a "Go" command to invoke the URL. A "Save" command is used to invoke a ScreenSaver application.


Field Summary
(package private) static java.lang.String[] ACCESS_ALLOWED
          The list of applications allowed to access.
(package private)  javax.microedition.lcdui.Command backCommand
          The Back Command to dismiss the viewer.
(package private) static java.lang.String CALLER_CLASSNAME
          The application class that will be calling Registry functions.
(package private) static java.lang.String CH_CLASSNAME
          The content handlers class that implements the image viewer.
(package private) static java.lang.String CHID
          The Content Handler ID.
(package private)  javax.microedition.lcdui.Display display
          The Display for the viewer.
(package private)  javax.microedition.lcdui.Form form
          The Form to display the image.
(package private)  javax.microedition.lcdui.Command goCommand
          The Go Command to invoke the link.
(package private)  javax.microedition.content.ContentHandlerServer handler
          ContentHandlerServer from which to get requests.
(package private)  javax.microedition.lcdui.ImageItem imageItem
          The ImageItem displaying the image.
(package private)  javax.microedition.content.Invocation invoc
          Current invocation, null if no current Invocation.
(package private) static java.lang.String PNG_SUFFIX
          The suffix supported.
(package private) static java.lang.String PNG_TYPE
          The type for PNG.
(package private)  javax.microedition.content.Registry registry
          Access to Registry functions and responses.
(package private)  javax.microedition.lcdui.Command saveCommand
          The Save Command to invoke the screen saver.
(package private)  javax.microedition.lcdui.TextField urlField
          The TextField to input a URL.
 
Constructor Summary
ImageViewer()
          Initialize the viewer user interface and listeners for requests and responses.
 
Method Summary
 void commandAction(javax.microedition.lcdui.Command c, javax.microedition.lcdui.Displayable s)
          Handle command on the Form.
 void destroyApp(boolean force)
          Cleanup and destroy the application.
(package private)  void displayImage(javax.microedition.content.Invocation invoc)
          Fetch the Image and display.
(package private)  void doInvoke(java.lang.String url)
          Invoke the URL provided.
(package private)  void doSave()
          Saving the image to a ScreenSaver application.
(package private)  boolean finish(int status)
          Finish the current invocation if any.
 void invocationRequestNotify(javax.microedition.content.ContentHandlerServer h)
          Process a new Invocation request.
 void invocationResponseNotify(javax.microedition.content.Registry r)
          Process a response to a previous request.
 void pauseApp()
          Pause the application; no additional action needed.
(package private)  void register()
          Dynamic registration of content handler.
(package private)  void showImage(javax.microedition.lcdui.Image image)
          Show an image.
(package private)  void showURL()
          Switch to show the URL input field.
 void startApp()
          Start the application; no additional action needed.
 
Methods inherited from class javax.microedition.midlet.MIDlet
checkPermission, getAppProperty, notifyDestroyed, notifyPaused, platformRequest, resumeRequest
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PNG_TYPE

static final java.lang.String PNG_TYPE
The type for PNG.

See Also:
Constant Field Values

PNG_SUFFIX

static final java.lang.String PNG_SUFFIX
The suffix supported.

See Also:
Constant Field Values

CHID

static final java.lang.String CHID
The Content Handler ID.

See Also:
Constant Field Values

ACCESS_ALLOWED

static final java.lang.String[] ACCESS_ALLOWED
The list of applications allowed to access.


CH_CLASSNAME

static final java.lang.String CH_CLASSNAME
The content handlers class that implements the image viewer.

See Also:
Constant Field Values

CALLER_CLASSNAME

static final java.lang.String CALLER_CLASSNAME
The application class that will be calling Registry functions.

See Also:
Constant Field Values

invoc

javax.microedition.content.Invocation invoc
Current invocation, null if no current Invocation.


handler

javax.microedition.content.ContentHandlerServer handler
ContentHandlerServer from which to get requests.


registry

javax.microedition.content.Registry registry
Access to Registry functions and responses.


display

javax.microedition.lcdui.Display display
The Display for the viewer.


form

javax.microedition.lcdui.Form form
The Form to display the image.


imageItem

javax.microedition.lcdui.ImageItem imageItem
The ImageItem displaying the image.


urlField

javax.microedition.lcdui.TextField urlField
The TextField to input a URL.


backCommand

javax.microedition.lcdui.Command backCommand
The Back Command to dismiss the viewer.


goCommand

javax.microedition.lcdui.Command goCommand
The Go Command to invoke the link.


saveCommand

javax.microedition.lcdui.Command saveCommand
The Save Command to invoke the screen saver.

Constructor Detail

ImageViewer

public ImageViewer()
Initialize the viewer user interface and listeners for requests and responses.

Method Detail

showURL

void showURL()
Switch to show the URL input field. Enable the "Go" command.


showImage

void showImage(javax.microedition.lcdui.Image image)
Show an image. Enable the "Go" command.

Parameters:
image - the Image to display

startApp

public void startApp()
Start the application; no additional action needed.

Specified by:
startApp in class javax.microedition.midlet.MIDlet

pauseApp

public void pauseApp()
Pause the application; no additional action needed.

Specified by:
pauseApp in class javax.microedition.midlet.MIDlet

destroyApp

public void destroyApp(boolean force)
Cleanup and destroy the application.

Specified by:
destroyApp in class javax.microedition.midlet.MIDlet
Parameters:
force - true to force the exit (always exit)

invocationRequestNotify

public void invocationRequestNotify(javax.microedition.content.ContentHandlerServer h)
Process a new Invocation request. In this example, the new request may interrupt the display of a current request. If so, the current request is "finished" so the new request can be displayed. To avoid application thrashing do not exit, even if requested, until the new request is finished.

Specified by:
invocationRequestNotify in interface javax.microedition.content.RequestListener
Parameters:
h - the ContentHandlerServer with the new request

invocationResponseNotify

public void invocationResponseNotify(javax.microedition.content.Registry r)
Process a response to a previous request. Put up the prompt for URL again.

Specified by:
invocationResponseNotify in interface javax.microedition.content.ResponseListener
Parameters:
r - the Registry with the response

commandAction

public void commandAction(javax.microedition.lcdui.Command c,
                          javax.microedition.lcdui.Displayable s)
Handle command on the Form. On user command "Back", finish the Invocation request. On user command "Go", invoke the supplied URL.

Specified by:
commandAction in interface javax.microedition.lcdui.CommandListener
Parameters:
c - the Command
s - the Displayable the command occurred on

doInvoke

void doInvoke(java.lang.String url)
Invoke the URL provided.

Parameters:
url - the URL of an image

finish

boolean finish(int status)
Finish the current invocation if any. If the application is ask to exit then the cleanup in destroyApp is done and the application manager notified of the exit.

Parameters:
status - the status to pass to finish
Returns:
true if the application should exit

displayImage

void displayImage(javax.microedition.content.Invocation invoc)
Fetch the Image and display.

Parameters:
invoc - an Invocation with the URL and contents

register

void register()
Dynamic registration of content handler. This example shows the registration of a MIDlet as a handler with the application ID "com.sun.example.imageviewer", for the type "image/png", suffix ".png", and access allowed to the "com.sun.example".


doSave

void doSave()
Saving the image to a ScreenSaver application. Suppose there is a ScreenSaver application that is used to control the idle image. This Image viewer application should be able to invoke the screen saver so the current image can be set as the idle screen. An additional command is added to invoke the screensaver. Invoking the screen saver MUST not be called from the UI thread because it may block while I/O completes.