Lecture 2 Event driven programming Mobile and Handheld Applications1 Programming of Handheld and Mobile Devices Lecture 2: Event driven programming concepts.

Slides:



Advertisements
Similar presentations
Understanding an Apps Architecture ASFA Computer Science: Principles Fall 2013.
Advertisements

Programming Paradigms and languages
CS0004: Introduction to Programming Visual Studio 2010 and Controls.
Introduction to Systems Programming (CS 0449) PalmOS Events Handling Events OS–AppEventLoop() – Event Handlers.
CSI 3120, Exception handling, page 1 Exception and Event Handling Credits Robert W. Sebesta, Concepts of Programming Languages, 8 th ed., 2007 Dr. Nathalie.
3.3. G AME I NPUT Handling input within games. In lecture exploration of answers to frequently asked student questions.
Automating Tasks With Macros
6/13/20151 CS 160: Lecture 13 Professor John Canny Fall 2004.
CSE 466 – Spring 2000 – PalmOS - 1 Palm OS zInterface management ybasic user interface model yevent dispatch loop paradigm zSystem management yalarm and.
1 Exception and Event Handling (Based on:Concepts of Programming Languages, 8 th edition, by Robert W. Sebesta, 2007)
Programming Handheld and Mobile devices 1 Programming of Handheld and Mobile Devices Lecture 19 Microsoft’s Approach 1 –.NET Mobile Framework part 2 Rob.
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
Programming Handheld and Mobile devices 1 Programming of Handheld and Mobile Devices Lecture 12 Using the KToolbar Rob Pooley
Java: Chapter 1 Computer Systems Computer Programming II.
Study Guide For Test Chapter 5, 6,& 7 Test is Friday, May 15th.
Introduction to Matlab & Data Analysis
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
Prepared by Fareeha Lecturer DCS IIUI 1 Windows API.
Developing the Game User Interface (UI) Lesson 5.
Programming Handheld and Mobile devices 1 Programming of Handheld and Mobile Devices Lecture 12 lcdui Rob Pooley
Lecture 2 Object Oriented Programming Basics of Java Language MBY.
Programming Handheld and Mobile devices 1 Programming of Handheld and Mobile Devices Lecture 5 Compiling with resources Rob Pooley
Java Programming, Second Edition Chapter Five Input and Selection.
Programming of Mobile and Handheld Devices Lecture 7: Programming OXO for Palm OS Rob Pooley
CMPS 211 JavaScript Topic 1 JavaScript Syntax. 2Outline Goals and Objectives Goals and Objectives Chapter Headlines Chapter Headlines Introduction Introduction.
Programming Handheld and Mobile devices 1 Programming of Handheld and Mobile Devices Lecture 23 Summing up Rob Pooley
In the next step you will enter some data records into the table. This can be done easily using the ‘Data Browser’. The data browser can be accessed via.
Extending HTML CPSC 120 Principles of Computer Science April 9, 2012.
Programming Handheld and Mobile devices 1 Programming of Handheld and Mobile Devices Lecture 7 J2ME Rob Pooley
Chapter 5: More on the Selection Structure Programming with Microsoft Visual Basic 2005, Third Edition.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
1 Version /05/2004 © 2004 Robert Oshana Requirements Engineering Use cases.
Chapter 5 Implementing UML Specification (Part II) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Programming with Microsoft Visual Basic th Edition
1 Kyung Hee University Statecharts Spring Kyung Hee University Specifying Objects’ Behaviour  Interaction diagrams show message-passing behaviour.
Events (Alice In Action, Ch 6) Slides Credit: Joel Adams, Alice in Action CS 120 Lecture September 2012.
Grade Book Database Presentation Jeanne Winstead CINS 137.
The Software Development Process
1 CSCD 326 Data Structures I Software Design. 2 The Software Life Cycle 1. Specification 2. Design 3. Risk Analysis 4. Verification 5. Coding 6. Testing.
Software Engineering Principles Dr. José M. Reyes Álamo.
ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson Slides4-2.ppt Modification date: March 23, Procedures Essential ingredient of high level.
Object Oriented Programming.  Interface  Event Handling.
Lecture 4 OXO for PalmMobile and Handheld Applications1 Programming of Mobile and Handheld Devices Lecture 4: Programming OXO for Palm OS Rob Pooley
Class Builder Tutorial Presented By- Amit Singh & Sylendra Prasad.
Lecture 3 Programming on Palm OS Mobile and Handheld Applications1 Programming of Mobile and Handheld Devices Lecture 3: Programming for Palm OS Rob Pooley.
CSCI 383 Object-Oriented Programming & Design Lecture 20 Martin van Bommel.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Forms and Controls Part 3 dbg --- Naming conventions, Button, Event handlers, Label.
1 The Software Development Process ► Systems analysis ► Systems design ► Implementation ► Testing ► Documentation ► Evaluation ► Maintenance.
Perfecto Mobile Automation
Diagram Editor Use Case Analysis Assumption: simple stand-alone, single user application. Three basic kinds of interaction of the user with the diagram.
Chapter Five More on the Selection Structure Programming with Microsoft Visual Basic th Edition.
National Diploma Unit 4 Introduction to Software Development Procedures and Functions.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Component 1.6.
Event Loops and GUI Intro2CS – weeks
Exception and Event Handling
Introduction to Event-Driven Programming
Debugging Dwight Deugo
Miscellaneous Topics #6: Polygons GUI Components and Event Handlers
Important terms Black-box testing White-box testing Regression testing
Important terms Black-box testing White-box testing Regression testing
How to Run a Java Program
HAPPY NEW YEAR! Lesson 7: If-statements unplugged
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
Debugging Dwight Deugo
Exception and Event Handling
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
Presentation transcript:

Lecture 2 Event driven programming Mobile and Handheld Applications1 Programming of Handheld and Mobile Devices Lecture 2: Event driven programming concepts Rob Pooley

Lecture 2 Event driven programming Mobile and Handheld Applications2 What is event driven programming? All mobile device programming is essentially a version of event driven programming. Programs are built as state machines. Events (such as mouse clicks or interrupts from comms ports) cause event notices to be added to an event queue Event handlers maintain these, removing them and acting appropriately Handling one event may result in another being posted This sounds difficult, but is really rather simple.

Lecture 2 Event driven programming Mobile and Handheld Applications3 How it works Inspect event queues Select event Handle event

Lecture 2 Event driven programming Mobile and Handheld Applications4 More detail An event driven system has one or more event handling loops. For each loop there is one or more event queues. At the start of each cycle of each loop an event is removed from one of the event queues and its type decides what is done next.

Lecture 2 Event driven programming Mobile and Handheld Applications5 Variations on this theme In some systems the event is handled directly. In others, there is a mechanism known as a callback, which passes the event to a user defined method, to handle it. In the latter approach, the event loop is created behind the scenes and hidden from the programmer. We will look at both types of system. –Palm OS requires the programmer to write an explicit event handling loop, based on a predefined event queue. –J2ME, a Java system, hides the event queue and requires the programmer to write event handlers as callback methods matching a defined interface.

Lecture 2 Event driven programming Mobile and Handheld Applications6 Simple example The example is a very simple quiz with two categories of question and one question in each category. Initially the user is presented with a screen listing the categories – mathematics and history – in a form which will allow them to select one. This takes them to a new screen which asks the question and allows them to answer it. Their total score is updated accordingly and they return to the initial screen. We will also extend this example to show how we can store and retrieve information in each environment.

Lecture 2 Event driven programming Mobile and Handheld Applications7 Simple example - statechart Throughout this course we will be looking at two simple examples. The figure shows the simpler of these as a UML statechart. Statecharts are a very powerful and useful way of designing PDA applications. They show the key points reached as states and the paths between states are triggered by events.

Lecture 2 Event driven programming Mobile and Handheld Applications8 A general approach Note that each different screen to be displayed corresponds to a high level state in the UML statechart. When designing MID applications we can usually work out the high level logic, along with the sequence of screens to be used in this way.

Lecture 2 Event driven programming Mobile and Handheld Applications9 Second example Our second running example is also shown as a statechart in Figure opposite. It is also a game – this time with a more elaborate user interface, to allow us to explore drawing and other graphical facilities. We will also use a multi-player version to explore communication facilities in the various MID packages.

Lecture 2 Event driven programming Mobile and Handheld Applications10 Programs on Palm OS

Lecture 2 Event driven programming Mobile and Handheld Applications11 Top level structure PilotMain StartQuizApp QuizAppEventLoop EndQuizApp StartQuizApp QuizAppEventLoop EndQuizApp do getEvent try in turn SysHandleEvent MenuHandleEvent AppHandleEvent FrmDispatchEvent while not appStopEvent FrmGotoForm(MainForm)

Lecture 2 Event driven programming Mobile and Handheld Applications12 Typical PilotMain UInt32 PilotMain (UInt16 cmd, void *cmdPBP, UInt16 launchFlags) { UInt32 error = StartQuizApp(); if(error) return error; QuizAppEventLoop(); EndQuizApp(); return errNone; }

Lecture 2 Event driven programming Mobile and Handheld Applications13 Simple Start Application static UInt16 StartQuizApp(void) { UInt16 error = 0; FrmGotoForm(MainForm); return errNone; }

Lecture 2 Event driven programming Mobile and Handheld Applications14 Event loop static 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);

Lecture 2 Event driven programming Mobile and Handheld Applications15 Event loop for Palm OS The key is that this function runs as a loop. It takes events one at a time from the system event queue, using EvtGetEvent. The loop terminates when the next event to be handled is an appStopEvent. Within the loop there is a nested if statement, which looks for events of different types There is only one thread active at a time. There is only one event queue per thread. Events are handled in a first come first served order. Events can be posted by application code, by user actions (button pushes, menu selections etc) or by the operating system. Inspect event queue Select event Handle event Possibly post event

Lecture 2 Event driven programming Mobile and Handheld Applications16 QuizAppEventLoop First it calls SysHandleEvent, from the System manager bundle, which takes any system events, handles them and returns true. If the event is not a system one, it returns false. It may post a new event to the queue as part of handling the current one. If the result of SysHandleEvent is false, the second conditional branch is tried, calling MenuHandleEvent, from the Menu manager bundle, which handles the event if it comes from a Menu action and returns true. It may also post a new event to the queue. These are both standard functions within the Palm OS. You do not need to write them If both Sys- and MenuHandleEvent cannot handle the event, AppHandleEvent is called to determine whether the event is an application specific one. It will handle the event and return true if the event is specific to this application. This is where a lot of the application’s unique functionality is linked to the forms defined in the Resource Editor. You write this one yourself. It may post a new event to the queue. If none of these accepts the event and handles it, we pass it to the Palm OS function FrmDispatchEvent. This assumes that any event left is a request to switch to a new form. It will invoke the appropriate event handler function for the current. This is described in more detail in the next section.

Lecture 2 Event driven programming Mobile and Handheld Applications17 EndQuizApp static void EndQuizApp(void) { FrmCloseAllForms( ); }

Lecture 2 Event driven programming Mobile and Handheld Applications18 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.

Lecture 2 Event driven programming Mobile and Handheld Applications19 Typical AppHandleEvent static 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; }

Lecture 2 Event driven programming Mobile and Handheld Applications20 FrmDispatchEvent - pseudocode Boolean 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 system’s FrmHandleEvent.

Lecture 2 Event driven programming Mobile and Handheld Applications21 Example of an event handler Boolean WrongFormHandleEvent(EventPtr event) { Boolean handled = false; FormPtr form; switch (event->eType) { case frmOpenEvent: form = FrmGetActiveForm(); FrmDrawForm(form); // here's where you'd add a call to FrmSetFocus 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; }