Posts for the ‘Code’ Category


Android SDK, Timers and GUI Update

Tuesday, February 10th, 2009

Here is some piece-meal code for folks looking for information about how to implement a sort-of-a-timer and GUI update in Android based applications.

Some initial definitions;

1
2
3
private Handler handlerTimer = new Handler();

private static final int UPDATE_STUFF_ON_DIALOG = 999; //this is a true arbitrary number.

Init the timer at some point. Perhaps on “onCreate”, or where ever you need it.

1
2
handlerTimer.removeCallbacks(taskUpdateStuffOnDialog );
handlerTimer.postDelayed(taskUpdateStuffOnDialog , 100); //set a 100 milisecond delay for the initial post

The Runnable thread that will possibly do some lengthy operation;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
private Runnable taskUpdateStuffOnDialog = new Runnable() {
       public void run() {     

            /*
               Do some lengthy stuff here !!!
            */

   
            // handling be in the dialog
            // don't mess with GUI from within a thread
            Message msg = new Message();
            msg.what = UPDATE_STUFF_ON_DIALOG;
            handlerEvent.sendMessage(msg); 

            //Do this again in 30 seconds          
            handlerTimer.postDelayed(this, 30000);
    }
};

taskUpdateStuffOnDialog task will post the message to the main dialog’s event handler, where we will finally do some GUI stuff.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
private Handler handlerEvent = new Handler() {

    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
        case UPDATE_STUFF_ON_DIALOG: {

             // have a go at your GUI stuff here. enable/disable stuff, flash things, change text...

        }
            break;         
        default: {
            super.handleMessage(msg);
        }
            break;         
        }
    }
};

That’s it.



Building, Installing, Configuring The Android Platform and Android SDK on Ubuntu 9.10 64-bit x86

Wednesday, November 11th, 2009

Android SDK LogoUbuntu Logo

This post is about my battle of building, installing and configuring the Android Platform and the Android SDK on Ubuntu 9.10 64-bit x86. Insult to the injury, all that is happening in a VirtualBox VM on a Mac running Snow Leopard.

Please remember, this is NOT intended to be a step by step instruction manual. RTFM ;)

Follow the regular installation instructions and possibly throw in the following in the mix, when and if you have to.

You will need the Android Source to build (doh!). Here.

Obstacle #1. No Java 1.5.0 in Ubuntu 9.10. Gotta have it, otherwise Android “make” barely bothers to launch.

http://java.sun.com/javase/downloads/index_jdk5.jsp is a starting point. I grabbed the rpm build.

Assuming you have the “rpm” (otherwise apt-get install rpm), install the downloaded package. –force-debian –nodeps flags may be required.

Now you have Java 1.5.0 in place, you need to setup your environment properly.

1
sudo update-alternatives --install /usr/bin/java java /usr/java/jdk1.5.0_22/bin/java 50
1
sudo update-alternatives --install /usr/bin/javac javac /usr/java/jdk1.5.0_22/bin/javac 50

** change your jdk1.5.0 path if necessary

Getting close.

The “apt-get install” in the documentation http://source.android.com/download includes “sun-java5-jdk“, so take it out and run the command as follows:

1
sudo apt-get install git-core gnupg flex bison gperf build-essential zip curl zlib1g-dev gcc-multilib g++-multilib libc6-dev-i386 lib32ncurses5-dev ia32-libs x11proto-core-dev libx11-dev lib32readline5-dev lib32z-dev

Try the “make” now.

Next is installing the Android SDK on Ubuntu 9.10. Grab it at http://developer.android.com . Unzip, move it around etc.

Starting with Android SDK r3, you need to pick and choose what you want to download and install. “./android” under the “tools” directory does that for you. However, every single time I tried this on my Macs, I got the “You might want to force download through HTTP in the settings.” error. So be it, go the the options, simply check the “Force https://… sources to be fetched using http://…” option and download happily ever after. This was a no-go in my Ubuntu 9.10. The trick to properly enable and utilize that checkbox is to run:

1
export GDK_NATIVE_WINDOWS=true

Now, you should be able to download the SDK without the https:// mallarky.

After all this, hopefully, you should be Android’ing in your Ubuntu box in no time ;)

Good luck.



Crockford on JavaScript — Act III: Function the Ultimate

Wednesday, February 24th, 2010

Act III: Function the Ultimate. We’re going to be talking about functions tonight. Functions are the very best part of JavaScript. It’s where most of the power is, it’s where the beauty is. Like everything else in JavaScript, they’re not quite right, but you can work around that, and there’s a lot of good stuff here.

Keep watching for the rest….



Must Call This Function!

Tuesday, June 30th, 2009

I was going through some code refactoring today, and needed a certain set of classes calling a particular function during their initialization.

I needed the function to be absolutely (i mean absolutely) implemented by any deriving classes of the base object.

I needed the function called automatically, so I won’t have to remember calling it anytime I derive something new out of the base class.

So, here is how I accomplished this lovely must-call-this-function pattern.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
interface ISample
{
    public function makeSureThatThisFunctionIsCalled();
}

abstract class ASample extends TheParentClass implements ISample
{
    public function __construct ($someParams)
    {
        parent::__construct($someParams);

        $this->makeSureThatThisFunctionIsCalled();
    }
}

class Sample extends ASample
{
    public function __construct($someParams)
    {
        parent::__construct($someParams);
    }

    public function makeSureThatThisFunctionIsCalled()
    {
        // do whatever you need to do here....
    }
}

class AnotherSample extends ASample
{
    public function __construct($someParams)
    {
        parent::__construct($someParams);
    }

    public function makeSureThatThisFunctionIsCalled()
    {
        // do whatever else you need to do here....
    }
}

The code should be self explanatory for the OOP savvy folks. Here is a very short description of what happens here.

Our actual class “Sample” extends the abstract class “ASample” which in turn implements the interface “ISample” (don’t worry about extending the TheParentClass). Implementing the “ISample” will require us to actually implement the makeSureThatThisFunctionIsCalled() function somewhere either in “ASample” or “Sample” class. We need class specific implementation of the function, so “Sample” and “AnotherSample” classes implement it.

Oh great, yay! We enforced our derived classes implement a function of the interface. Big deal!

How will we make sure that “makeSureThatThisFunctionIsCalled()” function will actually get called ?

In this sample, all the deriving class constructors call their parent::_construct which keeps bubbling up to TheParentClass so on and so forth…

So, we simply stick in “makeSureThatThisFunctionIsCalled()” in the immediate parent’s constructor and tada !!!

Our must-be-called function is surely getting called during the object’s initialization.

This may sound like a glorified initializer, but you never know. You may just need something like this.



NewsXperiment.com – Tech Stuff – Episode 1

Sunday, August 10th, 2008

newsXperiment logoAs stupid as it looks, and it “does NOT make any sense” at many angles, NewsXperiment bears a few interesting software technologies and paradigms.

NewsXperiment project consists of two parts: NewsXperiment Scrambler Engine (NSE), and Web frontend.

NewsXperiment Scrambler Engine runs offline and gathers, processes, scrambles and outputs a zip file that consists of scrambled news item pickles.

Once executed, NSE goes through its categorized feed repository and retrieves the feeds. Thanks to Mark Pilgrim’s excellent “feedparser” library.

Now that the feeds are read, the engine performs the following:

  • randomly picks a certain number of news items from each category as base feeds.
  • randomly associates a certain number of scrambler feeds to each base feed.

At this point, the engine has the initial data in place. There comes the scrambling…. However, before scrambling anything, all the entries picked to be scrambled need to be tagged, chunked, chinked. :)

  • Using NLTK, all the titles, and summaries read are tagged, chunked, chinked.(i love this part)
  • Accoding to the chunkie, chinckie data, each base feed item’s title and summary are scrambled with the set that was destined to be the scrambler for the base. Ofcourse, this does not always result in a well-constructed sentence.
  • At some point, the scrambling process is completed and time to generate the output file.
  • Output file is created out of each scrambled item, and consists of a list of titles, summaries and links back to the news items that are used to create them. This file is a pickle dump dictionary elements.
  • The output file is datestamped, and zipped. Zip file because, doh!, it’s compressed.  Plus, I couldn’t find a way around uploading the pickle content to Google AppEngine. Very likely a MIME type issue, but didn’t dig deep into that. A zipped pickle dump was all I needed, and I had it.

Very well, I have the zipped pickles, what do I do with them? If I cannot get them up to Google AppEngine’s data store, how possibly could I share ?

(more…)



StumbleUpon’ing Into Yahoo! Search via Yahoo! Pipes

Friday, March 6th, 2009

If you see “No Results” in the box above, it’s because WordPress messes up with the javascript embed.

You can try this on your own StumbleUpon.com account @ http://pipes.yahoo.com/yipes/yourstumbles

What is this ? How does this happen ?

Per Yahoo!;

Pipes is a powerful composition tool to aggregate, manipulate, and mashup content from around the web.

And it really is.

(more…)



Suicidal Pattern and Observers

Wednesday, June 17th, 2009

The other day, I came up with a sort of bizarre idea of exception handling in PHP. Not so interestingly it may be, you’ll find out as you read, this design pattern is called “Suicidal Pattern“.

The idea is around an object throwing itself as “throw self” (actually throw this) and it’s own handler catching and handling and announcing to its observers.

And here it goes:

(more…)