Thursday, July 28, 2011

Android Aync task

I am working on some android applications and thought I'd share how Async task works with a simple example.

Android applications are designed to be responsive. When unresponsive the android OS uses the tough love approach and offloads the app i.e the ANR(App not responding exception). When we do network calls off the mainthread these will block since network connections are relatively slow . To avoid this problem we will need to offload this network call to another worker thread.
This can be achieved using the AsyncTask

Methods we care about in AsyncTask
*onPreExecute()-
Runs on UI Thread before handing over to worker thread

*doInBackground()
Workhorse runs in background
*publishProgress()
inform UI about progrèss called from doInBackground()
*onProgressUpdate()
runs on UI thread and updates progressBar
*onPostExecute
runs in UI thread once worker thread is done

ProgressActivity has a button which will update a progress bar when hit.


package com.foo;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Toast;

public class ProgressBarActivity extends Activity implements OnClickListener {
private ProgressBar progressBar;
private Button start;

public class BackgroundAsyncTask extends AsyncTask {

int myProgress;

@Override
protected void onPostExecute(Void result) {
Toast.makeText(ProgressBarActivity.this, "onPostExecute all done back in UI",
Toast.LENGTH_LONG).show();
}

@Override
protected void onPreExecute() {
Toast.makeText(ProgressBarActivity.this,
"preexecute -before back ground processing starts",
Toast.LENGTH_LONG).show();
myProgress = 0;
}

@Override
protected Void doInBackground(Void... params) {

while (myProgress < 100) {
myProgress++;
publishProgress(myProgress);
SystemClock.sleep(20);
}
return null;
}

@Override
protected void onProgressUpdate(Integer... values) {

progressBar.setProgress(values[0]);// called by the publishProgress
// method to update progress bar
}

}

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

start = (Button) findViewById(R.id.startprogress);
progressBar = (ProgressBar) findViewById(R.id.progressbar_Horizontal);
progressBar.setProgress(0);

start.setOnClickListener(this);

}

@Override
public void onClick(View v) {
new BackgroundAsyncTask().execute();

}
}

Friday, March 25, 2011

Zuccini and Potato soup

I have become a big soup freak . Its been one of the fads thats infected me for the last year
This is a really easy recipe for all you folks who arent fussed about sitting by the stove all day and comes out pretty darn nice
Take a couple of potatoes cube em
Zuccini *2 remove skin and cut coarsely
onions *1 slize
garlic *2 slice

Procedure
1)roast onions and garlic
2)put zucinni onions potatoes and garlic in some vegetable stock
3)cook till tender
4)then out comes the trustee hand blender
5)blend away add some pepper and salt
6)bring to boil
7)fry some sage with butter and add on top

serve with crusty bread

How to detect language of a document

Recently I wrote a program to detect the human language of a given text . I was asked to do this task in 24 hours .After a lil googling I finally found this paper for N-Gram-Based Text Categorization by these two guys
William B. Cavnar and John M. Trenkle from Michigan AnnHarbour
Defn Worth a read...

Firstly what is a ngram ?
An Ngram is an n character slice of a string(From the paper verbatim)
so for APPLE you will have ngrams _,A,P,L,E then _A,AP,PL,LE,_AP,PLE etc

The basic algorithim if you dont have the patience to read this paper is

1)Create a ngram based profile for a document i.e this is basically finding the frequency of occurances of all the NGrams in your language document
2)Sort this ngram based profile with the highest frequency on top this would tell you the most occuring ngrams.
3)Now if you were to find the language of origin of a document then you will need to find its profile and then sort it by highest frequency
4)Now find a minimum distance between these documents i.e if the document is like the language this should be very small so the frequency of occurance of the words/syllables in the document and language would be similar .