Tools

Namespaces

Variants

Views

Actions

Search

Contents

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.

* Add to your WRT project 2 images of the same size, one representing the empty battery, the other one representing the empty one. Sample images used in this article are the following:

+

* Add to your WRT project 2 images of the same size, one representing the empty battery, the other one representing the full one. Sample images used in this article are the following:

−

[[Image:Wrt_battery_component_empty.png]]

+

[[File:Wrt battery component empty.png]]

−

[[Image:Wrt_battery_component_full.png]]

+

[[File:Wrt battery component full.png]]

* '''Initialize a Battery instance''', passing these arguments:

* '''Initialize a Battery instance''', passing these arguments:

** the empty battery image path

** the empty battery image path

Line 80:

Line 104:

}

}

</code>

</code>

−

The '''Battery constructor''' actually stores the battery width and height, passed as arguments, and then call the '''init() method''', that creates the component DOM structure, storing some HTML element references in instance variables.

+

The Battery constructor' actually stores the battery width and height, passed as arguments, and then call the {{Icode|init()}} method', that creates the component DOM structure, storing some HTML element references in instance variables.

'''WRT 1.0 and 1.1 SystemInfo Service APIs must be handled with different code''', so we define a method that, after appending the Battery DOM element to a specific parent element, calls the initialization method specific to the WRT version:

+

WRT 1.0 and 1.1 SystemInfo Service APIs must be handled with different code, so we define a method that, after appending the Battery DOM element to a specific parent element, calls the initialization method specific to the WRT version:

<code javascript>

<code javascript>

Battery.prototype.appendTo = function(parentElement)

Battery.prototype.appendTo = function(parentElement)

Line 132:

Line 156:

}

}

</code>

</code>

−

In the above code, we define 2 '''custom handlers''' for the ''onchagerconnected'' and ''onchargelevel events''. Since these functions are not Battery instance methods, they need to grab a reference to the single Battery instance, in order to properly work. To do this, let's first define an array that will hold all the Battery objects (assuming there could be more than one).

+

In the above code, we define 2 custom handlers for the {{Icode|onchagerconnected}} and {{Icode|onchargelevel}} events. Since these functions are not Battery instance methods, they need to grab a reference to the single Battery instance, in order to properly work. To do this, let's first define an array that will hold all the Battery objects (assuming there could be more than one).

What these 2 handlers do is to loop the Battery instances, and call the '''setChargerConnected()''' and '''updateBattery()''' methods, that will be implemented later, and that will manage charger status and battery level, respectively.

+

What these 2 handlers do is to loop the Battery instances, and call the {{Icode|setChargerConnected(}} and {{Icode|updateBattery()}} methods, that will be implemented later, and that will manage charger status and battery level, respectively.

===WRT 1.1 SystemInfo Service initialization===

===WRT 1.1 SystemInfo Service initialization===

−

The '''WRT 1.1 SystemInfo Service API''' allows widgets to access and modify system information on a device. The '''API is integrated into WRT through the device object'''.

+

The '''WRT 1.1 SystemInfo Service API''' allows widgets to access and modify system information on a device. The API is integrated into WRT through the device object'.

<code javascript>

<code javascript>

Battery.prototype.startService11 = function()

Battery.prototype.startService11 = function()

Line 208:

Line 232:

</code>

</code>

The above code uses the '''GetInfo()''' method to retrieve current '''status of battery level''' (in asynchronous mode) and '''charging status''' (in synchronous mode). Also, the '''GetNotification()''' method is used to be notified of changes of these 2 properties.

The above code uses the '''GetInfo()''' method to retrieve current '''status of battery level''' (in asynchronous mode) and '''charging status''' (in synchronous mode). Also, the '''GetNotification()''' method is used to be notified of changes of these 2 properties.

−

The async '''GetInfo()''' and both the '''GetNotification()''' methods need a callback handler: ''batteryCallback()'' for the Battery level information, and ''chargerCallback()'' for the charger-related information. Being asynchronous callback handlers, they have to be defined as described on Forum Nokia Library:

+

The async '''GetInfo()''' and both the '''GetNotification()''' methods need a callback handler: ''batteryCallback()'' for the Battery level information, and ''chargerCallback()'' for the charger-related information. Being asynchronous callback handlers, they have to be defined as described on Nokia Developer Library:

The Battery constructor' actually stores the battery width and height, passed as arguments, and then call the init() method', that creates the component DOM structure, storing some HTML element references in instance variables.

WRT 1.0 and 1.1 SystemInfo Service APIs must be handled with different code, so we define a method that, after appending the Battery DOM element to a specific parent element, calls the initialization method specific to the WRT version:

In the above code, we define 2 custom handlers for the onchagerconnected and onchargelevel events. Since these functions are not Battery instance methods, they need to grab a reference to the single Battery instance, in order to properly work. To do this, let's first define an array that will hold all the Battery objects (assuming there could be more than one).

What these 2 handlers do is to loop the Battery instances, and call the setChargerConnected( and updateBattery() methods, that will be implemented later, and that will manage charger status and battery level, respectively.

The above code uses the GetInfo() method to retrieve current status of battery level (in asynchronous mode) and charging status (in synchronous mode). Also, the GetNotification() method is used to be notified of changes of these 2 properties.
The async GetInfo() and both the GetNotification() methods need a callback handler: batteryCallback() for the Battery level information, and chargerCallback() for the charger-related information. Being asynchronous callback handlers, they have to be defined as described on Nokia Developer Library:

Both these handlers check if there is no error, and then call the same functions called by WRT 1.0 service handling methods: setChargerConnected() and updateBattery().
In case of errors, the component simply does nothing: this is intended to not interfere with the Widget main behavior, so avoiding intrusive alerts or popup messages.

Error management: this component manages errors simply skipping them. This was done to not interfere with the Widget main behavior. A possible alternative management could be to hide the component if something goes wrong, or to show an according image to the user, notifying him of the unavailability of the battery level.

WRT 1.0 support: the WRT 1.0 code actually works also on WRT 1.1 runtimes, so it would have been enough to write code only for WRT 1.0. However, the purpose of this article is to show how to manage different runtime versions in JavaScript code, and to implement both versions of the component.