Tag Archive: Mobile Programming


Introducing Android SDK

Smart mobiles are gaining wider acceptance with each passing day. The market acceptance is resulting in increased demand for easier as well as flexible programming platforms, which in turn has resulted in new players entering the mobile SDK arena. The latest entrant is Google with its Android platform and corresponding SDK, which will be the focus of this discussion. The first section will be about whys and wherefores of Android. In the second section, the focus will be on the steps required in developing a basic Android application. The last section will focus on developing an application based on the steps described in the second section. That sets the agenda for this discussion.

Android – the Whys and Wherefores:

By definition “Android is a software stack for mobile devices that includes an operating system, middleware and key applications”. In other words, Android not only provides a runtime environment but also an SDK to develop applications aimed at the runtime environment. The Android stack consists of the following layers:

1. Kernel

2. Android Runtime

3. Libraries

4. Application Framework

5. Applications

The layers enumerated above are in bottom-up fashion i.e. Kernel forming the bottom most layer and Applications forming the topmost layer. Even though, Android is termed as a software stack, it, more or less exhibits characteristics of an Operating System that will be evident from details of the layers.

1. Kernel

It forms the foundation of Android. Kernel does all the low-level interactions at the hardware level. Kernel achieves this via the device drivers that are present at this layer. Android makes use of Linux kernel 2.6. The core services provided by the kernel include security, memory management, process management and network stack and driver model. In short, the kernel acts as a layer that encapsulates the interaction between other layers and hardware.

2. Android Runtime:

Runtime contains basic libraries that are required to run an application. These libraries provide most of the functionalities provided by Java libraries. Android applications target Dalvik virtual machine (VM), which essentially is the runtime.  Each application runs in its own process, each with its own instance of Dalvik VM. The runtime depends on the kernel for functionalities such as system-level threading and low-level memory management.

3. Libraries:

The various components and sub-systems of Android are included in the stack as a set of C/C++ libraries. The library includes System library, Media libraries, Surface Manager Library, OpenGL library etc. The functionalities of these libraries are provided to the developer through the Application Framework Layer.

4. Application Framework:

This layer exposes the functionalities provided by the Libraries layer to the developer. The Libraries layer contains C/C++ API. The Application Framework layer wraps those API and exposes it to the developer as Java API. The Application Framework consists of components such as Activity Manager, Window Manager, Content Providers, View Systems etc. Applications are built upon the services provided by these components.

5. Applications:

This is topmost layer of the Android stack. This layer contains pre-built applications such as email client, SMS program, maps, browser etc. The custom applications deployed by the user sit in this layer.

That completes the bird-eye view of the Android. In the next section, the steps to develop an Android application will be enumerated. From here on, Android would mean the complete stack and Android SDK would mean the tools and libraries required for developing applications for Android.

Developing an Android Application – Step-by-Step:

An Android application can be developed either by using Eclipse plug-in or by using tools provided alongwith the SDK. In this discussion, the second way will be used. The advantage of using the second step is that one will not be tied up with a particular IDE. Following are the steps for developing application without using Eclipse:

1. Generate Project Directories and Stubs

2. Develop UI logic

3. Compile and Deploy the Project

For all the steps except second, SDK provide tools. Here are the details.

1. Generate Project Directories and Stubs:

First step is to generate the directory structure and the source code stubs. To achieve this end, Android SDK provides a tool named activityCreator.py. It is a Python script. The script also creates an ant build file that can later be used to compile the application. The option that one needs to keep in mind is the out option. Using the out option, the name of the project directory can be specified. For example, to create a HelloWorld project with ‘HelloWorld’ as the directory and org.me.android.hello.HelloWorld as the class, the command will be

activityCreator.py –out HelloWorld com.android.hello.HelloWorld

2. Develop UI logic:

Next step is to develop the UI of the application. The activityCreator.py script created the stubs. To develop the UI, the generated source-code stub needs to be edited. It can be found in the src folder of the generated application folder. For example, the stub generated for the HelloWorld can be found inside HelloWorld\src folder. The auto-generated class will be as follows

public class HelloWorld extends Activity {

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle icicle) {

super.onCreate(icicle);

setContentView(R.layout.main);

}

}

The UI in Android is composed of hierarchies of Views. Each component is a view. Some of the views act as containers. One of the most commonly used views is TextView. It provides the functionality of a label. To instantiate an object of the TextView class, an instance of the Android Context is required.

Context is a handle to the system. It provides access to the services such as accessing database, resolving resource bundles to their locales etc. Activity class is a sub-class of Context class. Any class that is generated by the script inherits Activity. Hence, instance of Context can be passed by passing the instance of the generated class using this keyword. For example, to instantiate an object of TextView class named textView, the statement will be

TextView textView = new TextView(this);

Next step is to display a text value using the TextView. To do so, setText method of TextView class can be used. For example, the following statement will display “Hello World”

textView.setText(“Hello World”);

Last step in displaying the text label is to add instance of TextView to the onscreen display. setContentView() method of Activity class can be used to connect or add instance of TextView class. For example, to add textView to the onscreen display, the statement will be

setContentView(textView);

Next, let us see how to compile the project and deploy it.

3. Compile and Deploy the Project:

To compile the project, the only tool needed is ant. In the folder containing the
build.xml file, one has to just give the following command

ant

The ant tool will take the build.xml file and generate the class files. Then it will
package them into an .apk file. It is Android asset package file. The next step is to
deploy the .apk file. To do so, adb tool can be used. The install option of the adb
tool tells the tool to install the package into the simulator. The parameter that
needs to be passed to the tool is the path to the package.

That completes this section. Next, a small application will be developed based on the steps detailed in this section.

Android SDK – In Real World:

The application to be developed will have following features:

1. A widget, which will be a button

2. An event-handler for the button

3. A dialog box that will be shown when the button is clicked

The application, though simple, will be the base for future discussions. So lets get started. First step is to create the application structure. The name of the application will be AndroidNotepad. Using the createActivity.py the structure can be created. Once the structure is created, the AndroidNotepad.java will contain the following code

package org.me;

import android.app.Activity;

import android.os.Bundle;

public class AndroidNotepad extends Activity {

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle icicle) {

super.onCreate(icicle);

setContentView(button);

}

}

Next, a button needs to be added. That can be done as shown in the code

package org.me;

import android.app.Activity;

import android.os.Bundle;

import android.widget.Button;

public class AndroidNotepad extends AndroidNotepad {

/** Called when the activity is first created. */

Button button;

@Override

public void onCreate(Bundle icicle) {

super.onCreate(icicle);

button =new Button(this);

setContentView(button);

}

}

The next step is to capture the click event of the button. To do so, the OnClickListener interface needs to be implemented. The interface can be found in View package. To implement the OnClickListener interface, one needs to override the onClick() method. The following code implements the OnClickListener for the button

package org.me;

import android.app.Activity;

import android.os.Bundle;

import android.widget.Button;

import android.view.*;

public class AndroidNotepad extends Activity implements View.OnClickListener {

/** Called when the activity is first created. */

Button button;

@Override

public void onCreate(Bundle icicle) {

super.onCreate(icicle);

button =new Button(this);

setContentView(button);

}

public void onClick(View arg0) {

}

}

To attach the event handler to the button, the setOnClickListener() method of Button can be used.

package org.me;

import android.app.Activity;

import android.os.Bundle;

import android.widget.Button;

import android.view.*;

public class AndroidNotepad extends Activity implements View.OnClickListener {

/** Called when the activity is first created. */

Button button;

@Override

public void onCreate(Bundle icicle) {

super.onCreate(icicle);

button =new Button(this);

button.setOnClickListener(this);

setContentView(button);

}

public void onClick(View arg0) {

}

}

Lastly, the code to display the dialog box when the button is clicked is as under

package org.me;

import android.app.Activity;

import android.os.Bundle;

import android.widget.Button;

import android.view.*;

public class AndroidNotepad extends Activity implements View.OnClickListener {

/** Called when the activity is first created. */

Button button;

@Override

public void onCreate(Bundle icicle) {

super.onCreate(icicle);

button =new Button(this);

button.append(“Click”);

button.setOnClickListener(this);

setContentView(button);

}

public void onClick(View arg0) {

showAlert(“Button”, 0, “Button Clicked”, “OK”, true);

}

}

To show the dialog box showAlert method is used. More about the showAlert() method in the future discussions. That brings us to the end of this discussion. This discussion has just touched the tip of the iceberg that is Android. The next part of this discussion will be about using XML based definitions for the UI. Till then…

Smart mobiles are gaining wider acceptance with each passing day. The market acceptance is resulting in increased demand for easier as well as flexible programming platforms, which in turn has resulted in new players entering the mobile SDK arena. The latest entrant is Google with its Android platform and corresponding SDK, which will be the focus of this discussion. The first section will be about whys and wherefores of Android. In the second section, the focus will be on the steps required in developing a basic Android application. The last section will focus on developing an application based on the steps described in the second section. That sets the agenda for this discussion.

Android – the Whys and Wherefores:

By definition “Android is a software stack for mobile devices that includes an operating system, middleware and key applications”. In other words, Android not only provides a runtime environment but also an SDK to develop applications aimed at the runtime environment. The Android stack consists of the following layers:

1. Kernel

2. Android Runtime

3. Libraries

4. Application Framework

5. Applications

The layers enumerated above are in bottom-up fashion i.e. Kernel forming the bottom most layer and Applications forming the topmost layer. Even though, Android is termed as a software stack, it, more or less exhibits characteristics of an Operating System that will be evident from details of the layers.

1. Kernel

It forms the foundation of Android. Kernel does all the low-level interactions at the hardware level. Kernel achieves this via the device drivers that are present at this layer. Android makes use of Linux kernel 2.6. The core services provided by the kernel include security, memory management, process management and network stack and driver model. In short, the kernel acts as a layer that encapsulates the interaction between other layers and hardware.

2. Android Runtime:

Runtime contains basic libraries that are required to run an application. These libraries provide most of the functionalities provided by Java libraries. Android applications target Dalvik virtual machine (VM), which essentially is the runtime.  Each application runs in its own process, each with its own instance of Dalvik VM. The runtime depends on the kernel for functionalities such as system-level threading and low-level memory management.

3. Libraries:

The various components and sub-systems of Android are included in the stack as a set of C/C++ libraries. The library includes System library, Media libraries, Surface Manager Library, OpenGL library etc. The functionalities of these libraries are provided to the developer through the Application Framework Layer.

4. Application Framework:

This layer exposes the functionalities provided by the Libraries layer to the developer. The Libraries layer contains C/C++ API. The Application Framework layer wraps those API and exposes it to the developer as Java API. The Application Framework consists of components such as Activity Manager, Window Manager, Content Providers, View Systems etc. Applications are built upon the services provided by these components.

5. Applications:

This is topmost layer of the Android stack. This layer contains pre-built applications such as email client, SMS program, maps, browser etc. The custom applications deployed by the user sit in this layer.

That completes the bird-eye view of the Android. In the next section, the steps to develop an Android application will be enumerated. From here on, Android would mean the complete stack and Android SDK would mean the tools and libraries required for developing applications for Android.

Developing an Android Application – Step-by-Step:

An Android application can be developed either by using Eclipse plug-in or by using tools provided alongwith the SDK. In this discussion, the second way will be used. The advantage of using the second step is that one will not be tied up with a particular IDE. Following are the steps for developing application without using Eclipse:

1. Generate Project Directories and Stubs

2. Develop UI logic

3. Compile and Deploy the Project

For all the steps except second, SDK provide tools. Here are the details.

1. Generate Project Directories and Stubs:

First step is to generate the directory structure and the source code stubs. To achieve this end, Android SDK provides a tool named activityCreator.py. It is a Python script. The script also creates an ant build file that can later be used to compile the application. The option that one needs to keep in mind is the out option. Using the out option, the name of the project directory can be specified. For example, to create a HelloWorld project with ‘HelloWorld’ as the directory and org.me.android.hello.HelloWorld as the class, the command will be

activityCreator.py –out HelloWorld com.android.hello.HelloWorld

2. Develop UI logic:

Next step is to develop the UI of the application. The activityCreator.py script created the stubs. To develop the UI, the generated source-code stub needs to be edited. It can be found in the src folder of the generated application folder. For example, the stub generated for the HelloWorld can be found inside HelloWorld\src folder. The auto-generated class will be as follows

public class HelloWorld extends Activity {

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle icicle) {

super.onCreate(icicle);

setContentView(R.layout.main);

}

}

The UI in Android is composed of hierarchies of Views. Each component is a view. Some of the views act as containers. One of the most commonly used views is TextView. It provides the functionality of a label. To instantiate an object of the TextView class, an instance of the Android Context is required.

Context is a handle to the system. It provides access to the services such as accessing database, resolving resource bundles to their locales etc. Activity class is a sub-class of Context class. Any class that is generated by the script inherits Activity. Hence, instance of Context can be passed by passing the instance of the generated class using this keyword. For example, to instantiate an object of TextView class named textView, the statement will be

TextView textView = new TextView(this);

Next step is to display a text value using the TextView. To do so, setText method of TextView class can be used. For example, the following statement will display “Hello World”

textView.setText(“Hello World”);

Last step in displaying the text label is to add instance of TextView to the onscreen display. setContentView() method of Activity class can be used to connect or add instance of TextView class. For example, to add textView to the onscreen display, the statement will be

setContentView(textView);

Next, let us see how to compile the project and deploy it.

3. Compile and Deploy the Project:

To compile the project, the only tool needed is ant. In the folder containing the
build.xml file, one has to just give the following command

ant

The ant tool will take the build.xml file and generate the class files. Then it will
package them into an .apk file. It is Android asset package file. The next step is to
deploy the .apk file. To do so, adb tool can be used. The install option of the adb
tool tells the tool to install the package into the simulator. The parameter that
needs to be passed to the tool is the path to the package.

That completes this section. Next, a small application will be developed based on the steps detailed in this section.

Android SDK – In Real World:

The application to be developed will have following features:

1. A widget, which will be a button

2. An event-handler for the button

3. A dialog box that will be shown when the button is clicked

The application, though simple, will be the base for future discussions. So lets get started. First step is to create the application structure. The name of the application will be AndroidNotepad. Using the createActivity.py the structure can be created. Once the structure is created, the AndroidNotepad.java will contain the following code

package org.me;

import android.app.Activity;

import android.os.Bundle;

public class AndroidNotepad extends Activity {

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle icicle) {

super.onCreate(icicle);

setContentView(button);

}

}

Next, a button needs to be added. That can be done as shown in the code

package org.me;

import android.app.Activity;

import android.os.Bundle;

import android.widget.Button;

public class AndroidNotepad extends AndroidNotepad {

/** Called when the activity is first created. */

Button button;

@Override

public void onCreate(Bundle icicle) {

super.onCreate(icicle);

button =new Button(this);

setContentView(button);

}

}

The next step is to capture the click event of the button. To do so, the OnClickListener interface needs to be implemented. The interface can be found in View package. To implement the OnClickListener interface, one needs to override the onClick() method. The following code implements the OnClickListener for the button

package org.me;

import android.app.Activity;

import android.os.Bundle;

import android.widget.Button;

import android.view.*;

public class AndroidNotepad extends Activity implements View.OnClickListener {

/** Called when the activity is first created. */

Button button;

@Override

public void onCreate(Bundle icicle) {

super.onCreate(icicle);

button =new Button(this);

setContentView(button);

}

public void onClick(View arg0) {

}

}

To attach the event handler to the button, the setOnClickListener() method of Button can be used.

package org.me;

import android.app.Activity;

import android.os.Bundle;

import android.widget.Button;

import android.view.*;

public class AndroidNotepad extends Activity implements View.OnClickListener {

/** Called when the activity is first created. */

Button button;

@Override

public void onCreate(Bundle icicle) {

super.onCreate(icicle);

button =new Button(this);

button.setOnClickListener(this);

setContentView(button);

}

public void onClick(View arg0) {

}

}

Lastly, the code to display the dialog box when the button is clicked is as under

package org.me;

import android.app.Activity;

import android.os.Bundle;

import android.widget.Button;

import android.view.*;

public class AndroidNotepad extends Activity implements View.OnClickListener {

/** Called when the activity is first created. */

Button button;

@Override

public void onCreate(Bundle icicle) {

super.onCreate(icicle);

button =new Button(this);

button.append(“Click”);

button.setOnClickListener(this);

setContentView(button);

}

public void onClick(View arg0) {

showAlert(“Button”, 0, “Button Clicked”, “OK”, true);

}

}

To show the dialog box showAlert method is used. More about the showAlert() method in the future discussions. That brings us to the end of this discussion. This discussion has just touched the tip of the iceberg that is Android. The next part of this discussion will be about using XML based definitions for the UI. Till then…

In the last post, the topic of discussion was about basic UI controls that PyS60 provides. These controls are useful when the solution to be developed is simple in terms of interaction. However, if a scenario presents itself where interaction becomes complex, then the basic controls would not suffice. For such situations, advanced controls need to be used that can abstract out the complexities of the interaction to the user as well as provide simple and consistent interface for the developer to work with. PyS60 has many such controls that a developer can use. In this discussion, the focus will be on three of the most commonly used controls – selection list, multi-selection list and text. The first section would focus on the whys and wherefores of these controls. In the last section, the application developed in the previous part will be enhanced using the controls being discussed in this section. That is the agenda for this discussion.

Lists and Text Controls – Whys and Wherefores

There are times when providing a list of choices is a better option than asking the user to enter data in an entry box. In such circumstances, providing a list of choices is a better option. To provide lists, PyS60 has two UI controls

1. Selection List

2. Multi-selection List

The former is a good choice when only one item needs to be chosen whereas when several items need to be chosen then the later is the control of choice. Here are the details.

1. Selection List:

As the name suggests, it displays a list to the user from which he or she can choose. The selection list provided by PyS60 also contains a search field that helps the user to narrow down the choices. It is displayed to the user using a function called selection_list(). It comes under the category of dialogs wrapped in functions. It takes two arguments:

a. choices

b. search_field

The latter is an optional argument.

a. choices:

It is a list of Unicode strings containing the options to be displayed to the user.

b. search_field:

It accepts a ‘0’ or ‘1’ as the value. The value decides whether the search field will be  shown or not. If the value is ‘1’, the search field is shown. The default value is ‘0’ i.e. the search field is off by default. If enabled, the search field is shown after the first key press occurs.

The value returned by the selection_list() function is the index of the selected item. For example, to show a list with ‘egg’, ‘spam’ and ‘butter’ to choose from without any search field, the statements will be

list = [u”egg”, u”spam”, u”butter”]

selection_list(list)

2. Multi-selection List:

There are cases where a single selection is not a solution. This is where multi-selection list comes handy. Like selection list, multi-selection list is also a function that wraps up a dialog and displays the dialog when it is executed. The function that brings up a Multi-selection List is multi_selection_list(). It accepts the following arguments

a. choices

b. style

c. search_field

The first and third are similar to the selection_list() arguments. The second argument is unique to multi_selection_list(). Here are the details

a. choices:

Just as in the case of selection_list, the value accepted by this argument is a list of Unicode strings.

b. style:

This is optional. The value of this parameter decides how the list will be displayed. There are two valid values for the style parameter:

i. checkbox – It is the default value. If ‘checkbox’ is given as the value of the
style, the list presented to the user will be a list containing a
checkbox against each list item. Empty checkboxes indicate
selectable item.

ii. checkmark – It is the other acceptable value for the style argument. If
‘checkmark’ is used as the value, then the list presented to
the user doesn’t have a visual clue as to which item to be
selected. However, once selected, a checkmark will appear
against that item.

c. search_field:

This argument does the same thing that search_field does for selection_list()
function. If value of 1 is passed, the search field is displayed and if value of 0 is
given, the search field is not displayed.

The value returned by the function is tuple containing selected values. If no values are selected, then an empty tuple is returned. For example, the following statements display a list from which multiple items can be selected having the checkmark style, having a search field and the returned values are displayed.

list = [u”egg”, u”spam”, u”butter”]

result = multi_selection_list(list, ‘checkmark’, 1)

print result

Next, let us move onto the control that provides the text editor functionalities.

Text:

It provides text editor with almost all the text formatting functionalities. The control is provided by Text type. Unlike selection or multi-selection list, text is a type. The functionalities provided by text type are categorized under the following:

a. Attributes

b. Methods

Since the text is a type the functionalities are exposed either as a attribute or a method. Here are the details

a. Attributes

Following are the most commonly used attributes of Text:

i. color:

It defines the color of the text. The valid values are all the colors supported by
graphic models in graphics module.

ii. font:

This attribute determines the font family of the text. It can be set using the
supported Unicode name.

iii. style:

It affects the style of the text. The valid values for this attribute are defined as
flags. The valid flags are provided by appuifw module. Some of the commonly
used flags are

  • STYLE_BOLD – Makes the text bold
  • STYLE_UNDERLINE – Underlines a displayed text
  • STYLE_ITALIC – Makes the text italic

For example, to use a Text type with LatinPlain12  as font’ value with bold text, the statements will be

t = appuifw.Text()

t.font = u”LatinPlain12″ # sets font to Latin Plain 12

t.style = appuifw.STYLE_BOLD

b. Methods

Text type provides various methods to perform common operations on the text
held by the editor. The most common methods are:

i. add() : It accepts Unicode string as argument. This method appends a text to
the existing text. It can also be used to insert the
passed argument at current cursor position.

ii. set() : Just like add() method, tt accepts Unicode string as argument. It sets
the passed Unicode string as argument. It replaces any text that
may be present in the editor.

iii. delete() : It accepts two arguments – position and length and deletes length of
characters in the editor starting from the position passed as
argument.

For example, to set a string having value as “This is a text” as the text in the editor, the statement is

t.set(u”This is a text”)

This brings us to the end of this section. In the next section, the guessing game application that was developed in last part will be enhanced using single selection list.

PyS60 in Real World:

In the last discussion, a guessing game was developed. The code was as follows

from appuifw import *

continue_guess=true

while continue_guess:

guess_no=random()

user_guess=query(u”Enter your guess”, ‘number’)

if user_guess is Null:

note(u”You have opted out”)

continue_guess=false

elif user_guess<guess_no:

note(u”Your guess is lesser than the goal”)

elif user_guess>guess_no:

note(u”Your guess is higher than the goal”)

else:

note(u”Congrats for excellent guess”)

user_choice=query(u”Enter Y to continue or N to quit”)

if user_choice is Null or user_choice==’N’:

continue_guess=false

else:

continue_guess=true

Now, let us change the display part for displaying the choices. Instead of taking user’s input using a query dialog, let us show a selection list from which the user can select a value. The following code

user_guess=query(u”Enter your guess”, ‘number’)

needs to be changed to

list = [guess-100, guess, guess*200, u”Quit”]

user_guess = selection_list(list)

first, a list is created using the guess no. and its combination. Then the list is given as an argument to selection_list() method. The returned index, which is the value selected by the user, is then stored in the user_guess variable.

Next, let us change how the result is processed. To do so, the following code

if user_guess is Null:

note(u”You have opted out”)

continue_guess=false

elif user_guess<guess_no:

note(u”Your guess is lesser than the goal”)

elif user_guess>guess_no:

note(u”Your guess is higher than the goal”)

else:

note(u”Congrats for excellent guess”)

to the following

if  guess_no < list [user_guess]:

note(u”Your guess is lesser than the goal”)

elif guess_no < list [user_guess]:

note(u”Your guess is higher than the goal”)

elif guess_no == list [user_guess]:

note(u”Congrats for excellent guess”)

else:

note(“Bye”)

continue_guess = false

The index returned by the selection_list() is used by the if condition to get the value selected by the user, from the list. If the index corresponds to the value lesser or greater than the goal value, then a note is displayed telling the user that he/she is either short of the goal or overshot the goal. Otherwise, user is congratulated. If user selected the option to quit, the continue_guess is set to false, thus ending ‘the game’. The complete code is as follows

from appuifw import *

continue_guess=true

while continue_guess:

guess_no=random()

list = [guess-100, guess, guess*200, u”Quit”]

user_guess = selection_list(list)

if  guess_no < list [user_guess]:

note(u”Your guess is lesser than the goal”)

elif guess_no < list [user_guess]:

note(u”Your guess is higher than the goal”)

elif guess_no == list [user_guess]:

note(u”Congrats for excellent guess”)

else:

note(“Bye”)

continue_guess = false

That completes our application. This discussion focused on the UI controls. From the next discussion, the focus will be moving towards the third party libraries in PyS60. However, the next discussion will focus primarily on graphics module. Till then…

Smart phones are becoming a common sight nowadays. Above all, the ‘smartness’ of the smart phones come from the fact that they can be programmed. In other words, one can develop custom applications for these mobiles and use them in the mobiles. Almost all the languages, from C to Java and Perl to Ruby, provide API to access and create services for the smart phones. Among them, nothing, at present, can beat the flexibility and ease-of-development that Python provides. Symbian is the most common OS used by smart phones. Symbian port of Python is known as pyS60. in this discussion I will be focusing on the basics of pyS60. The first section will focus on the topic ‘what is pyS60’. In the second section, I will detail the steps in developing and deploying a pyS60 application. In the last section, a real world application will be developed. That sets the outline for this discussion.

What is PyS60:

PyS60, also known as Python for S60 version, is a port of Python, released by Nokia for Symbian OS and targeted at N60 series of mobiles. The libraries provided by PyS60 can be divided into broad categories:

1. Built-in libraries

2. Dynamically loadable libraries

Since PyS60 essentially, is an extension to the standard Python, so, it is known as Python for S60 extension. Hence the categories can also be called as Built-in extensions and Dynamically loadable extensions. Here are the details

1. Built-in libraries or extensions:

It is one of the two native C++ extensions available in PyS60. These provide non-proprietary API to Symbian 60’s (S60) platform. The built-in extensions again divided into two modules:

i. e32:

This module provides access to those services that are not available in standard Python libraries. Essentially, this module is built into the Python interpreter to provide access to the Platform services of S60. These services include timer service (Ao_timer class), system information (sysinfo module and its classes), listing of the drives (drive_list method) etc.

ii. appuifw:

Using this module developer can access the API related to UI elements of S60 platform. The main UI elements include Text, Listbox, Canvas etc. Using the elements GUI can be constructed either as part of a window or as the part of screen itself. If the elements or controls are the part of the window then window becomes the container.

2. Dynamically loadable libraries or extension:

These extensions, as the name suggests, are loaded dynamically i.e. only when

needed as opposed to the Built-in extensions which are loaded when import statement is encountered. The reason for such a behavior is that the Dynamically Loadable extensions provide access to proprietary S60 API. There are 16 dynamically loadable extensions. Most commonly used among them are

i. graphics:

It provides access to the graphics manipulation capabilities of S60 platform including loading, saving, resizing of images.

ii. messaging:

This module provides access to the messaging capabilities of the S60. It

includes SMS and MMS services.

iii. inbox:

Using the inbox module one can access the inbox of the mobile on which

application is being executed. The information one can access includes the

message, time of message, the address of the sender etc.

iv. camera:

When one wants to access the camera, one can use this module. It grants access to all the functionalities of the camera that include image modes, flash modes, maximum zoom available etc.

v. audio:

It provides access to the audio capabilities of the device. The functionalities

include playback of different formats of audio files, recording of voice, text to

speech etc.

vi. calendar:

To use the calendar functionalities of the device, one can make use of this

module.

vii. contacts:

Using contacts module, one can access the contact list in a device. The

contacts are provided in the form of dictionary.

That completes the services provide by PyS60. Next, let us see the steps in

accessing the different services of the mobile using Python.

Accessing Services – Step By Step:

The ease of programming using Python reduces the steps required to access the different services. However, there are two steps that is always required. They are:

1. Instantiating the object of the Service

2. Setting the required properties

3. Calling the methods to access required functionality

These steps are generic to not only S60 platform but also to any mobile platform, including embedded Linux, which is another mobile platform. I will use sound module as an example for all the steps.

1. Instantiating the object of Service:

Most of the time this is the first step in accessing the service. This step is essential when working with built-in extensions. However, for most of the dynamically loadable extensions, objects are not required as their functionalities can be directly accessed. The reason is that the methods of the dynamically loadable modules are static. For example, to create a text field (the name of text field component is query) the statements will be

input= query(u”Enter Text”,”Text”)

On the other hand, to use sound module to load a file, one has to just call the open method of Sound class. The open method is a static, so object of Sound class is not required. Following statement opens a .wav file named test.wav.

Sound.open(“test.wav”)

2. Setting the required attributes:

Next step is to set up the attributes of the object instantiated. If the service can be accessed through static methods, then attributes can also be set or accessed through static mutators (setters) or accessors (getters). As with all the classes of Python, the attributes can be set at the time of instantiating the object. For example, selection_list can be used to show a list of items to the user, from which he or she can tell his or her choice. The option to show a search field can be set at instantiation. The following statements display a list of fonts available on the device and let the user select one of the available fonts.

li =available_fonts()

sl=selection_list(li,1)

However, to set the volume of Sound object one has to just call the set_volume() method, which is again a static method. The following statement sets the volume to 10.

Sound.set_volume(10)

3. Calling the methods to access required functionality:

The last step is to call the required methods on the objects to access the services. If the object is a UI component then the ‘service’ will either be displaying some value to the user or get some value from the user. If it were a device-based service such as Sound, then accessing the functionality would mean requesting the platform to either play or stop the audio file. For example, the query component returns the value entered by the user after initialization. Therefore, the instantiation returns the value entered by the user. The returned value can be displayed using note object. The following statements accepts a string from the user and display it to the user

data=entry(u”Enter your message:”, “text”)

note(u”Your message was: “+data, “info”)

If one needs to access the play service provided by Sound module, he or she has to just call the play() method of Sound class. The following statement just does that

Sound.play()

That completes the steps to work with PyS60. One point to keep in mind is that the string being passed to the methods and constructors need to be Unicode i.e. the string should be prefixed with “u” otherwise the string may not be displayed correctly. Next, I am going to develop a small application that would accept path to a .wav file and then play it.

PyS60 Programming – In Real World:

The application to be developed will have the following functionalities

1. Input box – To accept the path for file to be played

2. Playing the selected File – To play the file entered by the user

Here we go. First comes the imports. Since both UI and Sound are required, so appuifw and audio modules need to be imported

from appuifw import *

import audio

Next we need to show the entry box to the user and get the path entered by him or her. To display the text box, we can use query component. The first argument is the prompt to be shown and the second argument is the type of query. The type can be ‘text’, ’date’, ’time’, ’float’ etc. Here the type will be text as we need textual data i.e. string. The value returned by query component will be stored in a variable named data. Since, the value returned by

from appuifw import *

import audio

data=query(u’Enter the path of the file to be played’, ‘text’)

Now we need to load the file. For that we need to call the load method of sound. As discussed in the previous section every method in sound module is static. We will also check whether user clicked cancel. If cancel has been clicked, then data will contain null.

from appuifw import *

import audio

data=query(u’Enter the path of the file to be played’, ‘text’)

if data is not Null:

Sound.load(data)

else:

note(u’Give the path name’, ‘info’)

Next let us tell Symbian to play it.

from appuifw import *

import audio

data=query(u’Enter the path of the file to be played’, ‘text’)

if data is not Null:

Sound.load(data)

Sound.play()

else:

note(u’Give the path name’, ‘info’)

That completes our application. Though it is small, yet it introduces several important concepts of PyS60. In the coming discussions, I will go into the depth of each module. Till then…

Follow

Get every new post delivered to your Inbox.