S60 5th Edition includes UI support for a
touch screen as well as UIQ. If a user buys a Nokia 5800 XpressMusic
device with touch-screen support, a touch or stylus is used for
interaction. Java ME developers should take touch devices into
consideration when designing and planning the user interaction as more
and more touch-enabled devices appear in the market.
According to the MIDP specification, high-level LCDUI
widgets do not receive low-level touch events. LCDUI widgets continue
to receive the same events as before. This section, therefore, applies
mostly to canvas-based MIDlets.
1. Handling Stylus Events
Stylus events can be detected using the Canvas class, which provides developers with methods to handle pointer events:
Canvas.hasPointerEvents() indicates whether the device supports pointer press and release events (e.g., when the user touches the screen with a stylus).
Canvas.hasPointerMotionEvents() indicates whether the device supports pointer motion events (e.g., when the user drags the pointer).
Canvas.pointerDragged(int x, int y) is called when the pointer is dragged.
Canvas.pointerPressed(int x, int y) is called when the pointer is pressed.
Canvas.pointerReleased(int x, int y) is called when the pointer is released.
The pointer events delivery methods are only called while the Canvas is visible on the screen. CustomItem also supports pointer events and the way to query for supported input methods is the getInteraction-Modes() method.
The following snippet of code demonstrates how to handle stylus events:
public class StylusSupportExamplet extends Canvas {
// Members
private final TouchPoint pressed = new TouchPoint();
private final TouchPoint released = new TouchPoint();
private final TouchPoint dragged = new TouchPoint();
// Stylus support
// Indicate if pointer and pointer motion events are supported
public boolean supportsStylus() {
return this.hasPointerEvents() && this.hasPointerMotionEvents();
}
protected void pointerPressed(int x, int y) {
pressed.x = x;
pressed.y = y;
// TODO: handle event accordingly
}
protected void pointerReleased(int x, int y) {
released.x = x;
released.y = y;
// TODO: handle event accordingly
}
protected void pointerDragged(int x, int y) {
dragged.x = x;
dragged.y = y;
// TODO: handle event accordingly
}
}
// Encapsulation of [x,y] point on the screen
class TouchPoint {
int x, y;
public String toString(){
return "[" + x + "," + y + "]";
}
}
To ensure that a user can use your application with a
touch screen, you first need to add code to handle stylus events to the
relevant Canvas-based screens.
2. Replacing Directional Keys
There are other issues that we need to consider when
targeting a variety of Symbian smartphones and how your application must
support the input mechanism used on that phone. These issues are not
all enumerated, but we refer to a few to highlight how to plan to handle
various input mechanisms.
A phone designed for touch-screen interaction might
not have directional keys. You might design the greatest space invaders
game but would a user enjoy playing it using the stylus? You need to
think about such cases.
It is possible that your application is simply not suitable for this class of devices.
If
there are no directional keys, perhaps there is another set of
designated keys on the device? For example, the Sony Ericsson W960i has a
jog dial as the standard mechanism for scrolling up and down and
selecting items.
You
might want to add your own implementation of virtual keys that will be
rendered at the sides of the screen to allow the user to interact via
touch.
You may want to consider Nokia's simple solution for MIDlets that use a Canvas and are not originally made for touch devices. The Nokia-MIDlet-On-Screen-Keypad proprietary JAD attribute enables a virtual keypad that generates key events for the canvas-based MIDlet (see Figure 1). The possible values for Nokia-MIDletOn-Screen-Keypad are "no", "gameactions" and "navigationkeys". Regardless of the value, pointer events still work normally in the Canvas area.
3. Implementing Device Conventions
If your application supports the touch screen, it
should also adhere to the usage conventions of the device. For example,
touching the sides of the screen may be equivalent to normal usage of
the directional keys; your application should follow this convention and
handle touches at the sides of the screen as pressing on directional
keys. Additionally, your application must not block the conventional
mechanism from operating. If your game extends the LCDUI GameCanvas
then it is your responsibility to ensure the key-press events are not
blocked because of the handling of stylus events. Ensure that your
stylus-event-handling methods invoke the same method of the base class
to allow it to handle the stylus events and translate them to key
events, if applicable. Pressing at the side of the Sony Ericsson W960i
screen triggers a key event with a directional code (left or right). If
your application extends the GameCanvas and does not delegate the stylus events to the same GameCanvas, the key event is not dispatched by the implementation.
protected void pointerPressed(int x, int y) {
pressed.x = x;
pressed.y = y;
// TODO: handle event accordingly
// Call super.pointerPressed(x, y) otherwise, depending on the UI
// platform, keyPressed(int code) might not be invoked
super.pointerPressed(x, y);
}
protected void keyPressed(int code){
// TODO: handle key-event
}
4. Interacting with Graphical Elements
You should also consider the size and location of the
graphical elements with which the user interacts. Users don't always
use a stylus – they may use their fingers. If you render a button which
the user will touch, the button has to be of sufficient width and height
to allow the user to press it comfortably. Also, the gap between
adjacent interaction elements should be sufficient to ensure that the
user cannot accidentally press more than one element at a time.
5. Opportunities of Platform Input Mechanisms
Platform input mechanisms also provide a developer
with good opportunities. If there is touch-screen support, you can add
swipe interaction to your application. If you are using high-level LCDUI
widgets, you have free access to all the native text-entry mechanisms,
such as predictive text, handwriting recognition, and a virtual
on-screen keyboard. Such input mechanisms remain transparent to your
code, which receives the usual high-level events, without any additional
cost.
The single common Java ME platform on a
large number of Symbian smartphones mitigates the problem of Java ME key
code fragmentation. The same Java ME key codes are used in the majority
of Symbian smartphones, even from different phone manufacturers.