December 03, 2009

Handling calls in Windows Mobile

Last two weekends, I ventured into different kind of project. Away from web application frameworks, Java technology.. I entered into Windows mobile application programming.I decided to play with my HTC HD. My first intention was to play around the TouchFlo interface, but my mind caught on the idea of handling calls on mobile. I decided to make an application that will ignore calls from unwanted callers. Or simply hang up calls that I don't want to receive! Here is how it was built.

To start with I created a Visual C#, Smart Device project in Visual Studio. After playing around, I found that I need TAPI API in order to handle the phone calls. For my project, I used OpenNETCF.Telephony library. You will have to download the library source and build it. Also remember to add the dll into our project's Reference.

Well, I wouldn't explain the UI or other functionalities in this article. Let discuss about handling the call.

Monitoring and Getting the call
In order to handle calls, you will have to use the TAPI library. The first thing you will need to do is initialize the Telephony class provided by the OpenNETCF.Telephony library. Our requirement is to monitor and handle the incoming calls and get its information. Upon receiving any incoming call, my callback function is called where I have the necessary logic to hang up. The code below shows how to Initialize Telephony class, get the incoming calls and register the callback function.
Telephony tapi = new Telephony();
tapi.Initialize();
CellularLine cl = tapi.CellularLine(MediaMode.InteractiveVoice, CallPrivilege.Monitor | CallPrivilege.Owner);
cl.NewCall += new Line.NewCallHandler(callAction);
The Telephony class is initialized using the Initialize method. The CellularLine method gets the voice calls with monitor and owner privileges and finally, the NewCallHandler register callAction method as the callback function.

Handling the call
The callback function will have to code to handle all the incoming calls. During my venture, I tried different call manipulations. The most simple activity was to reject all the calls that I received. The call rejection was done using the API provided by the library itself. Below is my simple call rejection method:
void callAction(Call call) {            
call.Hangup();
}
Well this may not be the case always. My intention was to reject calls from specific numbers. So, I put some conditions into the callAction methods and spiced it up. The phone numbers are read from a simple text file in my mobile phone and loaded into an array when the application starts. When I receive each call, the numbers are checked and actions taken.

Getting call information
I found different ways to get call information. I could use the .Net Compact Framework's SystemState properties or use TAPI library's CallInformation class. In my application I used the SystemState properties as below:
if (checkNumber.CompareTo(SystemState.PhoneIncomingCallerNumber.Trim()) == 0)  {
call.Hangup();
}
The PhoneIncomingCallerNumber return empty string when there is no incoming call. When you get an incoming call, the variable gives you the caller's number.

Trouble and Conclusion
I initially planned to release the complete project code along with the article. But, I decided to release it later due to few troubles I have with the application. Even though the application handles the call, I did notice some glitches. My trouble is I am yet to solve those bugs. I observed some strange behaviour and have not got any response to my discussions at the codeplex or stackoverflow websites.

November 06, 2009

Google releases Closure!


When Gmail was released I was very enthusiastic in knowing how they built the application around the Ajax technology. Since Gmail, Google have released many successful web application to the public and now they have decided to release the core javascript library that they used to build these application.

Introducing the Closure library and javascript tools built by Google to build applications that scale! They are open sourcing these tools to the web development community. The toolkit consists of :

Closure Complier - A javascript compiler that is used to optimize the code. The compiler is said to remove dead code and minimize the code. It also checks the syntax, variable references and warn about javascript pitfalls.

Closure Library - Is a broad, modular and cross browser javascript library. Developers can make use of the wide range of UI widgets and controls, server communication, effects, data structures etc etc..

And finally, the Closure Templates - are web templates that are precomplied to efficient javascript. Interesting part of templates is that they are implemented for both javascript and java! Meaning you can use the same template on client and sever side.

But, I really wonder why they named the library "Closure" as it very related to programming language terminologies. I also wonder why they released it into Google Labs if they have been using it. According to Google the Closure started out as 20% projects and all google applications currently use it. Open sourcing such a tool is a great boon to web developers!

November 05, 2009

Dynamic loading of ComboBox using ExtJS

Ext JS provide us with a very flexible ComboBox widget that can be loaded locally or remotely (dynamic loading). You can also load the combo box from the server in response to an event like changing selection of another combo box. In this article I am going to walk you through an example of dynamically loading a ComboBox in a form.

For example, we have simple form to add details of country. The fields are country name, population and population type. I will keep the first two fields as normal text fields and population type as a combo box. Upon loading of the form, we need to retrieve the population type and load it into the ComboBox.

The first step would be to create a data store (Ext.data.Store) to hold our dynamically loaded data. Lets assume I have a server side method at URL /testapp/poptype.json that fetches the data from some data repository. So, here is our data store:
var ds = new Ext.data.Store({
proxy: new Ext.data.HttpProxy({url: '/testapp/poptype.json',method:'GET'}),
reader: new Ext.data.JsonReader({
root: 'rows',
fields: [ {name: 'myId'},{name: 'displayText'}] 
})
});
Please note that to load the data into the data store, I will have to call the load method. Next, we need to bind this data store to our form. The binding is done through the store attribute when we configure the drop down in the form. Here is our form:
var addCountryForm = new  Ext.form.FormPanel({
id:"addCountryForm",
title:"Add Country Form",
height:300,
closable:true,
style: {margin:5},
frame:false,
items:  [{xtype:'textfield',name:'cname',fieldLabel:'Country Name' },
{xtype:'numberfield',name:'pop',fieldLabel: 'Population'},
new Ext.form.ComboBox({                        
fieldLabel: 'Population Type',
hiddenName:'popType',
store: ds,
valueField:'myId',
displayField:'displayText',
triggerAction: 'all',
emptyText:'Select',
selectOnFocus:true,
editable: false                        
})

],
buttons: [{
text: 'Save'
},{
text: 'Cancel'
}]
});
Now, lets have a look at a sample JSON output:
{
"rows":[
{"myId": "PT1" , "displayText": "Small"},
{"myId": "PT2" , "displayText": "Medium"},
{"myId": "PT3" , "displayText": "Large"}
]
}
Hope this article helped you out. Happy coding!

Read other ExtJS related articles!

June 16, 2009

Change ComboBox's default option using jQuery

I have started heavy use of jQuery in my current project.Today's task was very interesting. I am currently building a data filtering component for my application where form and data display table are loaded dynamically.In this article, I will explain how I did it!

I have a complex form with lots of elements.. In a way, all kinds of element!! User select values from drop down (ComboBox) that are dynamically loaded using Ajax. Upon selecting a value, the next drop down or form element values change or are set. I require to set default values to these dropdowns. Lets take a simple drop down:


<select id="centerTypeDropDown" name="centerType">
<option value="ASC" selected="selected">ASC</option>
<option value="NSC">NSC</option>
<option value="USC">USC</option>
<option value="IKT">IKT</option>
<option value="UCC">UCC</option>
</select>
The default selected value is ASC. In my application, these values are loaded using Ajax and the default values may change when user select other form elements or drop down. So, in short, new elements get added into this drop down and the selected value changes. For our example, let's say we need to set the default selection to USC instead of ASC.

Using jQuery I can change the default selection to the required option. Here is how to do it:

$("select#centerTypeDropDown option[selected]").
removeAttr("selected");
$("select#centerTypeDropDown option[value='USC']").
attr("selected", "selected");
That's it! For jQuery beginners, selector string is the magic string. selector returns a object or collection of objects. Using selectors, I can select any DOM object and then use appropriate methods to manipulate its properties. In this case, I need to change the selected attribute to another option. The first line for code is used to select the current selected value in the drop down and then by using the removeAttr method, I remove the attribute from the option tag. The second line of code is used to set the new selected value. I select the same object again then use attr method to set the appropriate attribute.

The above code works fine when we know exactly what our new default value is. What if we get the values and default value from the server dynamically? For example my JSON returned from the server contains new or additional options and default value. Note that, in above code we specify which option to be selected in the sector string through option[value='USC']. In our case, we cannot hard code the selector string, instead we create it dynamically.

var selectorSrting = 
"select#centerTypeDropDown option[value='"+ defaultValueObtainedFromServer + "']";
$(selectorSrting).attr("selected","selected");
And that's our final code! Let me know if there are better solutions to this problem. And intersted readers, watch out for the next article on dynamically loading ComboBox using jQuery.

June 07, 2009

Converting Double to String without E notation

Last week, I got a task of creating a data loader for one of application. The huge data was presented to me in the form of excel files and for sure there is no better way than using program to read the excel files and load the data to database. I jump started to build the loader application using the Apache POI library. The data types of table schema and data read from the excel didn't match!

There is the trouble: A numeric field in excel files was represented as string in database. Even though this number was not used for calculation, it seems for some reason, the database designer has kept it that way! Trouble started when the POI library's getNumericCellValue() method returned the numbers in the form of scientific notation. The number 12,345,600 would be returned as 123.456E5. But I need it in plain text 1245600 without scientific notation and grouping of numbers!

Solution to this problem lies in configuring NumberFormat or DecimalFormat class found in the package java.text. Here is solution code:


Double comSysNumber = cell.getNumericCellValue();
NumberFormat f = NumberFormat.getInstance();
f.setGroupingUsed(false);
String refinedNumber = f.format(comSysNumber);
Now the string variable refinedNumber is all set to be stored into my database :-). Hope this becomes useful to others.