Archive for the ‘javascript’ Category

Putting It All Together: SpellDial php class and Android Application – Part Two

Sunday, July 10th, 2011

I’m back, and I’ve got a little time on my hands. Let’s continue on how to use the spelldial php class now in an android phonegap application.

Now, let’s look at the actual php code that will make the spelldial call based on what your android spelldial app will send:

You can save this code snippet in a file named droidspell.php:

include "spelldial.php";
$spell = new Spelldial();
$result = $spell->get_info($_POST['spelldial'],$_POST['calltype']);
if($result->error_code !== '0')
{
die("error");
}
echo $result->content[0]->info[0]->uri;

This is a typical use of the spelldial class I made (this uses post variables spelldial and calltype, ie. tel or skype to determine what to return as output to the android application).

Now for the html code snippet:

<script src="jquery-1.4.2.min.js">jquery-1.4.2.min.js</script>
<form id="spellform" name="spellform" method="post" action="http://yoursite/droidspell.php">
<table width="300" border="0" cellspacing="0" cellpadding="0">
<tr>
<td height="27" colspan="2">SpellDial Name</td>
</tr>
<tr>
<td height="31" colspan="2"><label>
<input type="text" name="spelldial" id="spelldial" />
</label></td>
</tr>
<tr>
<td height="42" colspan="2" valign="top"><label>
<select name="calltype" id="calltype">
<option value="TEL">Telephone</option>
<option value="SKYPE">Skype</option>
</select>
</label></td>
</tr>
<tr>
<td colspan="2"><label>
<input type="submit" name="dial" id="dial" value="Call me using SpellDial" />
</label></td>
</tr>
</table>
</form>
<script>
$("#spellform").submit(function(event) {
event.preventDefault();
var $form = $( this ), spelldial = $form.find( 'input[name="spelldial"]' ).val(), calltype = $form.find( 'select[name="calltype"]' ).val(),
url = $form.attr( 'action' );
$.post( url, { 'spelldial': spelldial, 'calltype': calltype },
function( data ) {
if(data == 'error' || data == '')
{
alert('Spelldial API returned an error: '+data);
} else {
location.href = data;
}
}
);
});
</script>

As you may see, this is typical html form/jquery event code to get the result of the droidspell.php call and then determine if an error occurred and if not, go ahead to the android dialer to make a call.

You’ll see some stuff like jquery-1.4.2.min.js in the code above. Where is it placed? It is placed where the index.html is placed in the assets/www folder:

If you want to use the phonegap wrapper functions place this snippet in the <head> portion of your index.html file:

<script type="text/javascript" charset="utf-8" src="phonegap.0.9.6.js"></script>

You may rename phonegap.0.9.6.js to phonegap.js if you wish.

This is how a finished phonegap application looks like in the emulator (yeah yeah, ugliness noted, I made this quickly!):

Once you see this application running in your emulator, you might want to package this application into an apk. Simply right click on your project in the project window and select Android Tools->Export Unsigned Application Package – once you follow the instructions, this will create an unsigned apk file for you automatically.

You can also create signed apk files for distribution into the Market. For further reference, please see http://developer.android.com/guide/publishing/app-signing.html for details.

And there you have it, we have now made an android application in phonegap using the spelldial php class, tested it in an emulator, and published it as an apk file.

Putting It All Together: SpellDial php class and Android Application – Part One

Thursday, June 23rd, 2011

Ok, I’ve got a little time on my hand before I start with a wordpress project I am doing (creating wordpress plugins are fun :) – yipee ).

Let’s try to make a simple Android application using the phonegap cross-development framework calling a php script that uses my spelldial class to return a spelldial uri. Part one will describe steps on how to make your very first native webapplication in android (simply a hello world android web application).

I assume that you know how to install eclipse, the android sdk and the phonegap framework on your system (I’ll cover this in small detail, to help you get started).

There are many ways to program in android, ios, symbian, blackberry or winmo phones. There are cross-development frameworks which give you an abstract way to program in these mobile devices without dealing with the underlying phone hardware.

Two different technologies come to mind (these are the most popular that I’ve seen):
1. Appcelerator Titanium – this cross development tool allows web developers to quickly go into the mobile application department using what they know. HTML5, CSS, and a host of web programming languages such as php and ruby are supported. What is unique about this SDK is that these technologies are then compiled into their native counterparts – the end result is a native application. Impressive, actually. Supports both iOS and Android environments.
2. Phonegap takes a different approach. This is closest to what you call as a web application encapsulated in the mobile phone’s browser class. For example, what this means, in Android, this framework wraps around the WebView Java class and makes certain native phone functionalities available (like accessing your contacts for example) through javascript. For flash/flex developers, this is much like the ExternalInterface class that bridges between javascript and the flash application.

Which is more appropriate? If you’re a web developer and would like to make your application as close as possible to a native application, your best bet would be Appcelerator.

Why am I using phonegap in this case? If you just like to quickly develop applications and do not care if they look different from native applications, use phonegap. By the way, because of how phonegap works, it is available to more mobile environments than Appcelerator. Symbian, WinMo, Blackberry, iOS and Android mobile environments are supported by phonegap. No matter what the environment, your application would look the same because it is just practically your html application enclosed in the native phone’s browser class.

Also, phonegap is supported by the Dreamweaver CS 5.5 release, which means if you can afford it, mobile development will be quite simple to do (without using Eclipse as your environment to develop phonegap applications).

Now, lets begin. Let’s create a new project in eclipse by selecting File->New->Other

We now include our details for the application (Since Froyo or Eclair, the android version before 2.2, runs in 86% of android phones, I choose the lowest common denominator, Eclair), below:

We will have to add the phonegap jar file to the libs folder (which you will have to create) and also create the /assets/www folder where you will place the html files for your application. In this www folder, you will have also to place the phonegap.js file in there. This js file contains the functions that you will call to access native mobile phone functions such as accessing your camera, contacts, etc.

In order for Eclipse not to spurt out an error message (having a java class not available for use), we have to add phonegap.jar to the Build Path (Right click on the libs folder, select Build Path->Configure Build Path):

We will now edit the spelldial.java source code which was created using the New Project Wizard to look like this:

Let’s look at it line by line:

import ...
import com.phonegap.*;

The code above imports the phonegap class for use in our application.

public class spelldial extends DroidGap

Our spelldial class (which was created by the Wizard), extends upon the DroidGap class, which is in fact an extension of the WebView class.

This line below is what runs the web application (sort of like the main() function in your c program):

super.loadUrl("file://android_asset/www/index.html");

You can change the url to whatever you have placed in the assets/www folder as your starting point. index.html can simply be text saying “hello world” at this point.

One more thing, since phonegap uses certain native phone functions which require explicit permissions for the app to run, we’ll have to edit our AndroidManifest.xml to allow our app to use these:

These tags are placed below the android:versionName section:

<supports-screens
android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:resizeable="true"
android:anyDensity="true"
/>
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

And there you have it, some basic steps on how to set up the phonegap framework in eclipse. Part 2 will be the actual js code and php code that this web application uses to call a person using the spelldial api php class hosted on a server.

To end this, let’s take a look at phonegap support in Dreamweaver CS 5.5:

Spelldial Class

Monday, June 20th, 2011

Hi guys,

Another short post. I went to a small group hosted by techtalks (my former boss was one of the co-organizers) in IT Park. I met a lot of interesting people there, and there were quite a number of foreigners too (having their startups, some in the process of making a startup, etc)!

In short, this was a great experience because I for one dream to make a startup of my own too (poor programmer, no funds to do it – but the stuff I found there was educational to say the least).

Anyways, I saw a team of young guys (from the looks of it, new graduates, or even maybe in college), who have a startup company named Spelldial. Well, I maybe an old fart, but I when I see enthusiasm, especially from youth, of course, as heck, I get impressed.

What is SpellDial? From their website, it says:

SpellDial is a technology that allows you to dial names instead of numbers. This is applicable only for those who have setup a spelldial account. Once you have checked the availability of your username you can register it to point to a specific number. So instead of dialing a number, you dial their username.

They have a webapp that you can point your android or iphone to, but nevertheless, when you point that url to your desktop browser, it doesn’t work (obvious reasons).

I then took a quick peek at their api, and made a php wrapper class for it in about an hour (so that you won’t need to think about the details of calling the api). It’s as simple as doing these lines of code:


include "spelldial.php";
$spell = new Spelldial();
$result = $spell->get_info("us.example");
print_r($result);
?>

I know, the api yet is apparently in their very early stages (some of their api functions return nothing), but considering they are all young guys, it’s a great start for them ;)

Get my spelldial class here. I might in the future make an actionscript class (for flash users) and maybe even a java class for this one, but no promises.

Here’s the spelldial class in action (it’s a quick hack, so I’m sorry it’s ugly) :)