Post on 19-May-2015
Series 40 Java
1
Touch and Type
Touch Gestures
Single Tap
Long Press
Long Press
Repeated
Drag
Drop
Flick
Non Touch and Type MIDlets
Standard LCDUI screens (Alert, Form, List, TextBox)
Canvas
• No pointerXXX methods
• Has ponterXXX methods ?
Inline/Native Aligned—TextField
import com.nokia.mid.ui.LCDUIUtil;
LCDUIUtil.setObjectTrait(
textField,“nokia.ui.s40.item.substate”,
new Boolean(true) );
Gesture API
“To use platform specific
defined touch gestures and
align with the platform look
and feel.”
Class Diagram
Usage – step 1
• Create a GestureInteractiveZone
// Create a GestureInteractiveZone for all Gesture Types
GestureInteractiveZone giz =
new GestureInteractiveZone( GestureInteractiveZone.GESTURE_ALL );
// set bounding rectangle of zone.
giz.setRectangle( x, y, width, height);
Usage – step 2
// Define a GestureListener
Class GestureCanvas extends Canvas implements GestureListener
{
protected void paint(Graphics g) { … }
public void gestureAction( Object container,
GestureInteractiveZone zone, GestureEvent event) {
...
}
}
Usage – step 3
• Register the GestureInteractiveZone with the GestureRegistrationManager
// Register for Gesture events.
Canvas canvas = new GestureCanvas();
GestureRegistrationManager.register( canvas, giz );
GestureRegistationManager.setListener( canvas, canvas );
Usage – gestureAction in more detail
public void gestureAction( Object container,
GestureInteractiveZone zone, GestureEvent event) {
switch( event.getType() ) {
case GestureInteractiveZone.GESTURE_TAP:
...;
break;
case GestureInteractiveZone.GESTURE_LONG_PRESS:
case GestureInteractiveZone.GESTURE_LONG_PRESS_REPEATED:
case GestureInteractiveZone.GESTURE_DRAG:
case GestureInteractiveZone.GESTURE_DROP:
case GestureInteractiveZone.GESTURE_FLICK:
}
}
GestureEvent details
Methods int getType()
int getStartX()
int getStartY()
int getDragDistanceX()
int getDragDistanceY()
float getFlickDirection() returns flick direction in radians
int getFlickSpeed()
int getFlickSpeedX()
int getFlickSpeedY()
Flick Direction
• getFlickDirection returns float which is the direction ( in radians )
Frame Animator API
―To assist in providing
decelerated animations in
scrolling UI components.”
Class Diagram
Usage – step 1
• Define a GestureListener as before and a FrameAnimatorListener
class FrameAnimatorCanvas extends Canvas implements FrameAnimatorListener
{
public void animate(FrameAnimator frameAnimator,
short x, short y,
short delta, short deltaX, short deltaY,
boolean lastFrame) {
. . .
}
}
Usage – step 2
• Register your FrameAnimatorListener
frameAnimator = new FrameAnimator();
frameAnimator.register(refX, refY, maxFps, maxPps, this);
More on maxFps and maxPps
• maxFps is % of maximum supported frames per second.
• maxPps is % of maximum supported pixels per second.
• MIDlet gets default values though System properties.
System.getProperty( “com.nokia.mid.ui.frameanimator.fps” );
System.getProperty( “com.nokia.mid.ui.frameanimator.pps” );
Usage – step 3
case GestureInteractiveZone.GESTURE_FLICK:
frameAnimator.kineticScroll(
event.getFlickSpeed(),
FrameAnimator.FRAME_ANIMATOR_FREE_ANGLE,
FrameAnimator.FRAME_ANIMATOR_FRICTION_MEDIUM,
event.getFlickDirection()
);
More Information
Visit Forum Nokia Website to Download SDK | http://forum.nokia.com
Visit Forum Nokia Developer Library | http://library.forum.nokia.com
New extensions to
Location API
LocationUtil
• The com.nokia.mid.location.LocationUtil class provides an extra mechanism for
creating a LocationProvider.
• This is an extension to the JSR-179 Location API, which provides the
LocationProvider class.
class LocationUtils {
static LocationProvider LocationUtil.getLocationProvider(
int[] preferedMethods, String parameters );
}
• The parameters is for additional platform parameters, for now use null.
LocationUtil
• The preferedMethods can be a combination of the following definitions of the
JSR-179 Location class:
MTA_ASSISTED
MTA_UNASSISTED
MTE_CELLID
MTE_SATELLTITE
MTE_SHORTRANGE
MTY_NETWORKBASED
MTY_TERMINALBASED
Cell-ID based location
• A MIDlet can obtain approximate location by using the Cell-ID based method.
• This method will send to a network based server the cell-id that the phone is
connected to. This server will return the known location of that cell-id.
• This enabled phones without built in GPS to obtain an approximate Location
without requiring a separate Bluetooth GPS device.
• Note using Cell-ID based location, the coordinates can vary from a few meters to
hundreds of kilometers, depending on the local network deployment and on how
much information the backend database has been able to collect for that area.
Cell-ID example code:
• Example code:
int[] methods = {(Location.MTA_ASSISTED |
Location.MTE_CELLID |
Location.MTY_NETWORKBASED)};
LocationProvider lp = LocationUtil.getLocationProvider(methods,
null);
Location loc = lp.getLocation(-1);
Coordinates coords = loc.getQualifiedCoordinates();
double lat = coords.getLatitude();
double lng = coords.getLongitude();
Location Based
Services Platform API
The Basics
• The Location Based Services Platform is a non resident API.
• Use it to include Maps and other location based services in your MIDlets.
• Download LBSP.zip from FN [ http://forum.nokia.com/maps ]
• This Zip files contains:
• The API Javadocs
• An Example MIDlet
• LBSP.jar
• The LBSP.jar implements the API using existing platform API.
• This file needs to be included in your MIDlet projects that use this API.
Package overview
• The API is divided into four packages.
Package Description
com.nokia.maps.comon Common classes needed by other packages including
Address, ApplicationContext, GeoBoundingBox and
GeoCoordinate.
com.nokia.maps.map Map package contains classes needed to show and
interact with map content and place objects on map.
com.nokia.maps.routing Routing package contains classes to calculate route and
access route data. PositioningManager provides an easy
to get current location information based on GPS device
or device cell information.
com.nokia.maps.search Search package contains SearchManager for geocoding
and reverse geocoding.
The minimal LBSP App
• Displaying interactive map to the user is simple as creating MapCanvas instance
and setting it as a current displayable:
import com.nokia.maps.map.MapCanvas;
/**
* Minimal MIDP application to show map content to the user.
*/
public class MapMIDlet extends MIDlet {
/**
* This method is called when the application is started and sets
* MapCanvas as a current Displayable.
*/
protected void startApp() throws MIDletStateChangeException {
Display display = Display.getDisplay(this);
MapCanvas mapCanvas = new MapCanvas( display );
display.setCurrent(mapCanvas);
}
}
The minimal LBSP App (screen shots)
The minimal LBSP App (screen shots)
Map
Map Markers
• A Marker can be added to a map with the following code:
double lat = 51.3;
double lng = 0.8;
int size = 10;
String s = “You are here!”;
GeoCoordinate coord = new GeoCoordinate(lat, lng, 0 );
MapFactory mapFactory = mapCanvas.getMapFactory();
MapStandardMarker marker =
mapFactory.createStandardMarker(coord, size, s);
mapCanvas.getMapDisplay().addMapObject( marker );
Map Marker (screen shot)
Other Map Markers
• The MapFactory class defines other types of Map Markers:
MapMarker createMapMarker( GeoCoordinate coordinate, Image image );
MapCircle createMapCircle( double radius, GeoCoordinate centre );
MapRectangle createMapRectangle( GeoBoundingBox boundingBox );
MapPolygon createMapPolygon( GeoCoordinate[] coordinates );
MapPolyline createMapOptPolyline( GeoCoordinate[] coordinates );
Search Manager
• Places can be searched for with the SearchManager.
• You can also look up locations near a specified GeoCoordinate.
// Get instance of SearchManager
SearchManager searchManager = SearchManager.getInstance();
searchManager.setListener(this);
// Look up locations based on a search string
searchManager.geocode( “Germany,Berlin", null );
// Or, get locations at specified GeoCoordinate
GeoCoordinate coord = new GeoCoordinate(51.3, 0.8, 0 );
searchManager.reverseGeocode( coord );
Routing
• RouteManager provides access to Ovi routing service.
• Calculating route requires at least two waypoints and wanted modes.
• Modes are used to control route calculation features.
// Create the waypoints
GeoCoordinate start = new GeoCoordinate(51.3, 0.8, 0);
GeoCoordinate end = new GeoCoordinate(55.0, 0.0, 0);
WaypointParameterList waypoints = new WaypointParameterList();
waypoints.addCoordinate(start);
waypoints.addCoordinate(end);
RouteManager rm = RouteManager.getInstance();
rm.setListener(this);
// Use default routing mode
RoutingMode[] modes = { new RoutingMode() };
rm.calculateRoute(waypoints, modes );
Route Listener
public void calculateRouteRequestFinished(RouteManager rm) {
Route[] routes = rm.getRoutes();
Maneuver m = route[0].getFirstManeuver();
while( m != null ) {
String s = m.getInstruction();
…
m = m.getNextManeuver();
}
}
public void routeRequestError(RouteManager rm, Throwable error) {
// handle error condition
}
public void routeResponseCleared(RouteManager rm) {
// route cleared
}
Routing Mode
• A RoutingMode consists of:
• A routing type.
• A list of transport modes.
• A list of routing options.
public RoutingMode(int routingType,
int[] transportModes,
java.lang.String[] routingOptions);
Routing Types
• The Routing Type must be one of the types defined in RoutingType.
• These are static ints.
RoutingType.DIRECT_DRIVE
RoutingType.ECONOMIC
RoutingType.FASTEST
RoutingType.FASTEST_NOW
RoutingType.SCENIC
RoutingType.SHORTEST
Transport Modes
• The following transport Modes are available.
TransportMode.BICYCLE
TransportMode.CAR
TransportMode.PEDESTRIAN
TransportMode.PUBLICTRANSPORT
TransportMode.TRUCK
Routing Options
• These are string constants defines in RoutingOptions.
NOTE: HOVLANE = High occupancy vehicle lane.
RoutingOptions.AVOID_BOATFERRY
RoutingOptions.AVOID_DIRTROAD
RoutingOptions.AVOID_MOTORWAY
RoutingOptions.AVOID_PARK
RoutingOptions.AVOID_PUBLICTRANSPORT
RoutingOptions.AVOID_RAILFERRY
RoutingOptions.AVOID_STAIRS
RoutingOptions.AVOID_TOLLROAD
RoutingOptions.AVOID_TUNNEL
RoutingOptions.PREFER_HOVLANE
Custom Display
• So far the example used a MapCanvas which performs all the Map rendering.
• The LBSP API also allows you to use the MapDisplay directly, to provide a
greater control on how the Maps are rendered to the canvas.
MapFactory mapFactory = MapFactory.createMapFactory(
MapDisplayType.TILE_MAP,
MapResolutionEnum.MAP_RESOLUTION_128_x_128,
getWidth(), getHeight() );
MapDisplay map = mapFactory.createMapDisplay();
• Display Type can be either: TILE_MAP or VECTOR_MAP
• Resolution can be either: 128 x 128 or 256 x 256
Using MapDisplay
• You can then render the map on your own Canvas with renderMap(Graphics)
protected void paint(Graphics g) {
map.renderMap(g);
}
• Alternatively, you can render the map to an Image instead and then use that
Image wherever you like.
Image image = Image.createImage( width, height );
Graphics g = image.getGraphics();
map.renderMap(g);
Custom Display Screen shot
On-Line Resources: forum.nokia.com
http://forum.nokia.com/Develop/Java
http://forum.nokia.com/Develop/Web
On-Line Resources: forum.nokia.com
http://forum.nokia.com/Maps
Thank you!