Programming of Mobile and Handheld Devices

  • View
    40

  • Download
    3

Embed Size (px)

DESCRIPTION

Programming of Mobile and Handheld Devices. Lecture 3: Programming for Palm OS Rob Pooley rjp@macs.hw.ac.uk. Programming conventions. Each application has a PilotMain() function Palm OS applications are largely event-driven and so contain an event loop ; - PowerPoint PPT Presentation

Text of Programming of Mobile and Handheld Devices

  • Programming of Mobile and Handheld DevicesLecture 3: Programming for Palm OSRob Pooleyrjp@macs.hw.ac.uk

    Mobile and Handheld Applications

  • Programming conventionsEach application has a PilotMain() function Palm OS applications are largely event-driven and so contain an event loop; this event loop is only started in response to the normal launch code. Your application may perform work outside the event loop in response to other launch codes or define a different event loop for other codes.

    Mobile and Handheld Applications

  • Typical PilotMainUInt32 PilotMain (UInt16 cmd, void *cmdPBP, UInt16 launchFlags) { UInt32 error = StartQuizApp(); if(error) return error; QuizAppEventLoop(); EndQuizApp(); return errNone; }

    Mobile and Handheld Applications

  • Simple Start Applicationstatic UInt16 StartQuizApp(void) { UInt16 error = 0; FrmGotoForm(MainForm); return errNone;}

    Mobile and Handheld Applications

  • Event loopstatic void QuizAppEventLoop(void){ Err error; EventType event; do { EvtGetEvent(&event, evtWaitForever); if (! SysHandleEvent(&event)) if (! MenuHandleEvent(0, &event, &error)) if (! AppHandleEvent(&event)) FrmDispatchEvent(&event); } while (event.eType != appStopEvent);

    Mobile and Handheld Applications

  • EndQuizAppstatic void EndQuizApp(void) { FrmCloseAllForms( ); }

    Mobile and Handheld Applications

  • Event handlers and related functions An application should define and attach event handlers to any resources which require them. In this example there is an event handler for each form which is used. When AppHandleEvent is called it can detect events which request the loading of a new form and set the appropriate event handler for that form.An event handler is a function which is automatically called, by FrmDispatchEvent, when a particular form is active. In this way we set up some of the logic of our application. In our application there are several types of form, each created using the Resource Editor tool in the Developer Suite and each with its own buttons. We make the Id of each form distinct. It is not strictly necessary for resources of different types to have different Ids.

    Mobile and Handheld Applications

  • Typical AppHandleEventstatic Boolean AppHandleEvent(EventPtr event){ UInt16 formId; FormPtr form; if (event->eType == frmLoadEvent) { // Load the form resource. formId = event->data.frmLoad.formID; form = FrmInitForm(formId); ErrFatalDisplayIf(!form, "Can't initialize form"); FrmSetActiveForm(form); switch (formId) { case MainForm: FrmSetEventHandler(form, MainFormHandleEvent); break; case HistoryForm: FrmSetEventHandler(form, HistoryFormHandleEvent); break; case MathsForm: FrmSetEventHandler(form, MathsFormHandleEvent); break; case WrongForm: FrmSetEventHandler(form, WrongFormHandleEvent); break; case CorrectForm: FrmSetEventHandler(form, CorrectFormHandleEvent); break; default: ErrFatalDisplay("Invalid Form Load Event"); break; } return true; } else return false;}

    Mobile and Handheld Applications

  • FrmDispatchEvent - pseudocodeBoolean FrmDispatchEvent(EventType *event) { Boolean handled = result of calling Form's event handler; if (handled) return true; else return FrmHandleEvent(event); } FrmDispatchEvent is written to allow the application to interpret events such as button presses if they are meaningful to this application. This is done by calling an event handler which is installed within the current form. If that fails, it passes these on to the systems FrmHandleEvent.

    Mobile and Handheld Applications

  • Example of an event handlerBoolean WrongFormHandleEvent(EventPtr event){ Boolean handled = false; FormPtr form; switch (event->eType) { case frmOpenEvent:

    form = FrmGetActiveForm(); FrmDrawForm(form); handled = true; break;

    case ctlSelectEvent:

    switch (event->data.ctlSelect.controlID) { case TryAgainButton: FrmGotoForm(MainForm); handled = true; break;

    default: ErrFatalDisplay("Mysterious Wrong Form Button Event"); handled = false; break; }

    break;

    case frmCloseEvent: MainFormDeinit(FrmGetActiveForm()); handled = false; break;

    default: } return handled;}

    Mobile and Handheld Applications

  • More on conventionsMost Palm OS applications contain a user interface made up of forms, which are analogous to windows in a desktop application Only one form is normally visible at a timeThe Palm OS approach is to define the elements of the user interface separately and merge these with the application code when compiling and linking the final application

    Mobile and Handheld Applications

  • More conventionsAll applications should use the memory and data management facilities provided by the system. Applications employ operating system services by calling Palm OS functions. managers, are groups of functions that work together to implement a feature usually defined by a type of resource which they manage. Managers are available, for example, to generate sounds, send alarms, perform network communication, and beam information through an infrared port.

    Mobile and Handheld Applications

  • Compatibility with Palm OSIntegrate with the system software as follows:Handle sysAppLaunchCmdNormalLaunchHandle or ignore other application launch codes as appropriateIn the stop routine, tidy up and return memoryan application should first flush all active records, then close the application's database, and finally save those aspects of the current state needed for startup. Be sure your application uses the system preferences for numeric formats, date, time, and start day of week.Dont obscure shift indicators.

    Mobile and Handheld Applications

  • Writing Robust CodeCheck assumptions.Avoid continual polling.Avoid reading and writing to NULL (or low memory). Check result codes when allocating memory. Avoid making assumptions about the screen. Built-in applications can change.

    Mobile and Handheld Applications

  • Uniquely Identifying Your Palm OS ApplicationEach Palm OS application (in fact each Palm OS database) is uniquely identified by a combination of its name and a four-byte creator ID.Each database on the Palm Powered device has a type as well as a creator ID. The database type allows applications and the OS to distinguish among multiple databases with the same creator ID.Types and creator IDs are case-sensitive and are composed of four ASCII characters in the range 32-126 . Types and creator IDs consisting of all lowercase letters are reserved for use by PalmSource, so any type or creator ID that you choose must contain at least one uppercase letter, digit, or symbol

    Mobile and Handheld Applications

  • Header files and resources#include #include "AppDefs.h"PalmOS.h is the Palm OS API include file and is always needed. As a system library header it is enclosed in angle brackets when it is included.The other file here is one which you create and place into the src subdirectory (subfolder) of your project. It contains all the resource identifiers you will use to link to the resource definition file and defines a unique symbolic name for each of them. I have used the name AppDefs.h for this file, but you can choose your own name. As a programmer defined header file, it is enclosed in double quotes when it is included.

    Mobile and Handheld Applications

  • The files in a project

    Mobile and Handheld Applications

  • AppDefs.h#define MainForm 1000#define MainGotoHistoryFormButton 1000#define MainGotoMathsFormButton 1001#define MainLabel1 1002#define MainLabel2 1003#define MainOKButton 1004#define HistoryForm 1100#define HistoryCorrectButton 1100#define HistoryWrongButton 1101#define HistoryLabel 1102#define HistoryTextField 1103#define MathsForm 1200#define MathsCorrectButton 1200#define MathsWrongButton 1201#define MathsLabel 1202#define MathsTextField 1203#define WrongForm 1400#define TryAgainButton 1402#define CorrectForm 1300#define AcceptCorrectButton 1302

    Mobile and Handheld Applications

  • Excerpt from GuessALot.xrd

    "Welcome to Guess a Lot." 1000 2 2 156 156 TRUE TRUE TRUE 0 0 0 "Guess a Lot" 1000 93 97 58 12 TRUE TRUE "History" FALSE STD_FONT STANDARD_BUTTON_FRAME

    Mobile and Handheld Applications

  • Palm OS makefile ## -----------------------------------------------------------------------# Palm OS Generic Makefile for Eclipse v1.0.0# PalmOS4/68K## Fill in this file to specify your project and the source that you want# to build, and the settings involved in the build. The makefile-engine.mk# will then do the hard work of the makefile and dependency handling.## After starting a new project, please remember the following steps...#1. Add all sources and resources in SOURCES and RESOURCES#2. Review the other settings as needed.### -----------------------------------------------------------------------SHELL = /bin/sh## -----------------------------------------------------------------------# Set up the artifact name, which is the root name of your project's output# without the extension.## The PRC and/or static library name, the database name, and other file names # are based on the artifact name## -----------------------------------------------------------------------ARTIFACT_NAME =GuessaLotMoreEMPTY =SPACE =$(EMPTY) $(EMPTY)ESCAPED_ARTIFACT_NAME = $(subst $(SPACE),\ ,$(ARTIFACT_NAME))PRC_NAME = $(ESCAPED_ARTIFACT_NAME).prcLIB_NAME = $(ESC