For information about the design and functionality of the MIDlet, see section Design.
The classes needed for
using the MIDlet suite are in the texthandler
package.
The main class, TextHandler
, implements a start
screen that displays a list of the choices specified in the links.txt
file. The list is implemented as a ChoiceList
that has ChoiceBean
items. A ChoiceBean
instance has the attributes keyTitle
(title of
the choice) and choiceName
(URL of the choice).
The TextHandler
MIDlet is registered as a handler
of plain text files at the startup by calling a custom register method:
protected void startApp() throws MIDletStateChangeException { register(); }
The startApp
method creates a ContentHandlerServer
instance using the Registry
class. It also sets the RequestListener
for any
incoming Invocation
:
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!"); } }
After the application startup and the registration,
a ChoiceList
form is displayed. The user can add
URL links and execute them by selecting them on the list. When an
URL is selected on the list, the doInvoke
method
of the TextHandler
class is called. The doInvoke
method creates an Invocation
instance
from the URL of the item (taken as a parameter) and invokes the correct
handler for the content in the 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); } }
The TextHandler
class itself functions
as a RequestListener
, so it has to implement the invocationRequestNotify
method. It uses the ContentHandlerServer
for handling incoming invocation requests. In this example, the
current invocation is always finished before proceeding to the next
one:
public void invocationRequestNotify(ContentHandlerServer server) { if (invocation != null) { handler.finish(invocation, Invocation.OK); } invocation = handler.getRequest(false); if (invocation != null) { showCurrentInvocation(invocation); } }
When an invocation is completed., for example,
when the user exits a handler showing content, the doFinish
method is called (for example, in case of text content, from TextViewer
). It finalizes the invocation process:
void doFinish(int invocStatus) { if (invocStatus == Invocation.OK) { if (invocation != null) { invocation = null; } } }
When AudioHandler
is executed, it
registers itself as an audio content handler (similarly as TextHandler
registers itself as a text content handler):
try { registry = Registry.getRegistry(AudioHandler.CLASS_NAME); handler = registry.register(AudioHandler.CLASS_NAME, new String[]{AudioHandler.CONTENT_TYPE_AUDIO}, null, null, null, null, null); if (handler != null) { handler.setListener(this); } } catch (Exception che) { che.printStackTrace(); }
So, if the requested content is audio, AudioHandler
is invoked to handle it:
public void invocationRequestNotify(ContentHandlerServer server) { if (invocation != null) { handler.finish(invocation, Invocation.OK); } invocation = handler.getRequest(false); if (invocation != null) { handleAudio(invocation); } }
If the content is an image, TextHandler
invokes the native image content handler in the device (in the doInvoke
method).