Saturday, 28 May 2011

Android vs WP7

After a short stint in android development, I thought of experimenting with a different mobile platform. I didn't have too many options. iPhone development was ruled out since i did not have a mac and wasn't quite comfortable with Objective C. I chose WP7, hoping to catch up with C# coding which I haven't been doing for a looong time. And moreover the latest buzz about the next version of windows phone, Mango added up to the excitement to see what it offers to the developers. I made up my mind and setup the environment  and started off with a simple BMI calculator application for WP7 and android. Here is the summary of stuff that I discovered last week.

                   Android                      Windows Phone 7
The multiple screen sizes are handled by android itself. The developer is concerned with following the best practices to support this. Though only two screen sizes are possible, you need to code differently for them. Even conditional statemenst in code and different xaml files might be required to cater different screen sizes.
The template editor is still basic. Throws up exceptions when background of an element is set to an xml. Needs a lot of improvement in this area. Has a very decent template editor - more similar to what we've seen and used to design the template for VB and C# windows applications. It is very dynamic and responsive to changes in xaml file and vice versa.
Though you'll find some surprises its easy to draw analogy from web development. Silverlight is used for development. Requires too much of code to achieve even the simplest functionality.
Takes a good amount of time to setup the eclipse plugin and android SDK. But once setup development and testing in emulator is much faster than WP7 Setting up the environment takes more than three hours and the tools are pretty slow if you are developing and testing it in the emulator frequently.
Costs only $25 to register as an android developer and start publishing. To publish an app in the marketplace, you need to pay an annual subscription fee of $99

The source code for android and windows phone are available in github.

Friday, 6 May 2011

Ruby - Gems - Manage dependencies

Had a chance to work on a Ruby on Rails project recently. It is almost impossible to have a ruby application without using any gems. Gem is a packeged ruby application or library which can be used in other applications (equivalent to a jar in a java project). You can specify the gem dependencies for your application in the Gemfile. The bundler takes care of resolving the gem dependencies of the application for you. It makes sure that the same version of gems are used in all the machines/environments wherever you develop/deploy the application.

Still, you could run into the problem of nested dependencies not being resolved, as I did. My application was dependent on a gem (say Gem-A), which was dependent on another gem (say Gem-B). You'll naturally expect the bundler to resolve this and install Gem-B for your app. But the bundler failed to resolve this in my case.

Let's take a small peek into how gems are built. Gems are ruby projects in themselves. In case of  a gem, you have another place (apart from the Gemfile) where its gem dependencies should be specified. Enter the gemspec file. The gemspec file contains the specification of the gem, like the name, version, authors. A sample gemspec file would look like this:

Gem::Specification.new do |s|
  s.name = 'my_gem'
  s.version = '1.0.0'
  s.authors = ["Me", "Myself"]
.
.
.
  s.add_runtime_dependency 'activesupport', '~> 3.0'
  s.add_development_dependency 'rails', '~> 3.0'
  s.add_development_dependency 'rspec', '~> 2.0'
  s.add_development_dependency 'rake'
  s.add_development_dependency 'sqlite3'
end

If any application is dependent on 'my_gem', the bundler will look at the runtime dependencies from the my_gem.gemspec file and it'll install the 'activesupport' gem as well, since it is a nested dependency.

Coming back to the problem in hand, the root cause was in the way Gem-A has specified its dependencies. It has added Gem-B as a dependency in its Gemfile, but hasn't specified it as a runtime dependency in its gemspec file and hence the bundler cannot identify Gem-B as a dependency. To resolve this, I had to add Gem-B as a dependency to my application as well.

It is a good practice to specify your runtime and development dependencies in gemspec and include it in your Gemfile, while developing gems. Your Gemfile will look something like:

source 'http://rubygems.org'
gemspec
gem 'ruby-debug', :platform => :ruby_18
gem 'ruby-debug19', :platform => :ruby_19

This will save a lot of time for the developers who use your gem.

Monday, 11 April 2011

Model form - Get it working

Started playing around with python and django last week. There is a lot of stuff that django provides us to build a basic web app. Despite its very good documentation, I found it hard to get the model form working. Model forms are useful when you have a basic model with some fields defined and you need a form to either add or edit the model. Writing views for such models is redundant, since both of them are going to contain the same information anyway and its prone to human error. Model form comes in handy in such situations.

Lets get started with a basic model that holds an item, its price and the date when it was added:

from django.db import models

class Transaction(models.Model):
item = models.CharField(max_length=200)
price = models.IntegerField()
date_added = models.DateTimeField('date added')

A model form for the above would look like:

from django import forms

class TransactionForm(forms.ModelForm):
        class Meta:
                model = Transaction
        date_added = forms.DateField(initial=datetime.date.today)

To display the model form in a view, we need to have a html file that'll be used to render the model form that is passed as a parameter to that. Using django's shortcuts we can render the form using the code below:

def index(request):
        return render_to_response('managExpenses/transaction_form.html', {'form':TransactionForm}, context_instance=RequestContext(request))

If you are using django's generic views, then the code will look like:

def index(request):
        return create_object(request, form_class=TransactionForm)

In the above code, the create_object method will look for the transaction_form.html placed in the templates folder and use it to render the form.

The transaction_form.html looks something like:

<form action="/some/url/" method="post">
{% csrf_token %}
{{form}}
<input type="submit" value="Submit"/>
</form>

The model form is replaces the {{form}} in the above html. The csrf_token is used by the CSRF (Cross Site Request Forgery) protection and needs to be included in every form that posts to your internal url. 

You can now go ahead and modify the urls.py to assign a url pattern to the above view and see it working.

Friday, 1 April 2011

Tamil in android

Displaying tamil characters in android can be easily achieved by using TSCII (Tamil Standard Code for Information Interchange) fonts. TSCII is an extended ASCII encoding standard. Unlike ASCII which uses 7 bits for encoding, TSCII makes use of all the 8 bits in a byte. The first 128 bytes remains the same as that of ASCII.  The tamil vowels and consonants along with the common prefixes, suffixes and other special characters fill up the rest of the 128 bytes.


You can place the TSCII font in the assets folder of your android application and set it to any view where you need to display tamil text.

Typeface tf = Typeface.createFromAsset(context.getAssets(), "fonts/tscFont.ttf");
TextView textView = new TextView(context);
textView.setTypeface(tf);


If you have the text in unicode format, there are online tools available that can help you convert the tamil text from unicode to TSCII format and vice versa.