Monday, April 30, 2012

Use actual Android phone instead of Emulator

Every time when I had entered into mobile application development the biggest challenge was how to transfer the application from the emulator development environment to the actual mobile phone. In Windows Phone 7 too moving developed app to phone is little difficult as we need a developer unlocked phone or developer account. But I think it is far more easier than moving an iPhone application from development environment to actual iPhone for testing purpose.

But in Android things are very easy .Just follow the below steps

  • Connect your phone via USB
  • Make sure the USB Debugging is enabled in Settings -> Application Settings -> Development
  • Start the application from Eclipse. It will ask you which device to use. Select your phone.

That's it .Your first Android application is in your Android phone. Even if you disconnect the USB the app will be present in phone. I have my first HelloWorld application in my Samsung Galaxy Y even till now.

My environment is Win 7 32 bit and has the software in the phone CD installed. It worked without any issue. Heard that there may be some issues with drivers and all. So check the below detailed link, if you face any issue in debugging in Phone.
http://developer.android.com/guide/developing/device.html

Monday, April 23, 2012

Select top N rows in Android SQLite

I had heard a lot about SQLite which is embeddable and small makes it suitable for mobile devices. But never got a chance to work on SQLite until I started Android. Setting up the data base and tables are little lengthy .So may be blog about those details later. This post is to note down a difference in using normal SQL and SQLite to fetch the top n rows.
The scenario was simple. We are showing some notifications in the app which are periodically syncing with the database using WCF services. The Android app uses SQLite to store the notification locally so that users will never miss any notification. It requests the latest notifications by sending the id of last notification it has. Its simple to fetch the highest id of notification rows using the normal SQL .The normal SQL query will look as follows
--Don't look at the * in query.Its sample
Select Top 1 * 
From Notifications
Order By Id
Desc

But the top is not supported in SQLite. We need to use the other keyword limit. It just limits the number of rows fetched

public Cursor select(String condition,String orderBy,String limit,SQLiteDatabase db){
    String qry=String.format("select * from %s order by %s desc limit %s", TABLE_NAME,orderBy,limit);
    return db.rawQuery(qry,null);
}

Interested in SQLite ? There are some more limits to SQLite.For more details refer the below.
http://www.sqlite.org/limits.html

Monday, April 16, 2012

Android : Mime type of .APK and hosting in GoDaddy

Recently we (a group of developers in my current company) developed a small Android app which gives real time updates of a Cricket tournament currently going on in the company .I am not a big fan of cricket even I am not participating in the tournament. But to utilize a chance to learn Android and develop a seasoned app I jumped into this :-) .We were little new to Android deployment scenarios and after developing the first version we came to face the big problem in deployment. We need to get an account in google play market which has some fees in dollars !!! . For a developer in US who draws pay checks in dollar this may not matter .But as we are getting salary in INR, a small confusion started. Company is ready to pay but it may take some time .So decided to host in my personal hosting space which is purchased from godaddy.com .

Things were easy in terms of copying the apk vie FTP. But when we tested from Android mobile it showed an error. Not able to recognize the file because there is no mime type defined for .APK files in the server. It took 30mins in Godaddy interface to realize they don't have support for adding mine type. But soon we remembered the advantage of IIS 7.0 .It allows us to add mime type via the web config.

The mime type of Android application file extension ".APK "is "application/vnd.android.package-archive" .So it was a simpe task of adding the below config entry into the System.WebServer section.

  <system.webServer>
    <staticContent>
      <remove fileExtension=".apk" />
      <mimeMap fileExtension=".apk"
               mimeType="application/vnd.android.package-archive" />
    </staticContent>
     <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>

Yes.Now the app is ready from my hosting space.The mobile almost deals the app like how we install from the market. Do we really need the google play market ?Its true that it has a broad reach.

Monday, April 9, 2012

Artificial Intelligence v/s Developer Part 1 - What the hell is this?

I am not sure whether I am the right person to talk about AI because I passed that exam in my 2nd or 3rd attempt during the B.E days. The reason why I didn't give much attention to that is I never thought I will get a chance to write an Artificial Intelligence program in my life. Only thing I was concerned on those days was about whether the AI will contain if...else statement or something else. Our HOD who took that paper was always telling that it was not an if...else program.

From those days I was in search for only one thing related to AI. Whether AI use if...else or something else. Saw some programs which the creators claim that it has AI including famous iPhone Siri .But to me, it felt that they are just presenting the error codes with polished English words converted to voice. Also I never got a chance to see source code of any AI app.

Time passed away and now I feel that I may be writing an AI program or at least will get a chance to see somebody writing an AI program and the source perhaps. This is based on some meetings with high level management of my current company which may change at any time. From the delivery point of view it is better to hire an experienced AI programmer which reduces training cost just like how I was hired for WPF & Silverlight. Anyway these meetings together with an idea of having "Voice Based Information Retrieval" system made me to think on how to implement an AI program. Being in the position of technical lead really I felt ashamed to say "I don't know how to write an AI program to convert a voice input to corresponding query commands which learns itself and finds more command patterns."

I normally hate theories .But here there is no other way without some theories.

What is intelligence

In my words "it is the art of taking decisions based on experience and acting with a feedback mechanism". Now what is experience ? It is the collection of results of previous decisions. There is no rule that the experience must be one's personal. It can be from other's experience which means from books too. Only difference is the result is not straight away suitable for the other one. ie If the result of one decision may be +ve for one and may be -ve for another one.

Ok "Results". These are the actual factor determining or influencing the art of taking decision. If the experience is ours we should give more weight to it's result than the experience of others because it proved for us. Also if we see that the experience from one particular source is giving us consistent +ve results ,don't wait to give more weight for their results.


Simply saying we are building a knowledge base and it's true that the intelligence is directly proportional to the size of that knowledge base, speed retrieval and power of processing.

What is artificial intelligence

We know how a human is possessing intelligence. If a program has got the same capabilities it's intelligent. But as being humans and we don't want anything to be like us so we simply call it as artificial intelligence because we give it the capacity or ability to become intelligent.

Machine learning

Before we think about how machine learns, we should think about how a human learns? A fresh machine installed with an AI program is clearly compared to a child. So let's start with how a child learns? Before that if we analyze the child, it / he/ she has an installed capacity of memory which functions fast based on a cache of recently accessed information and stores information as linked to each other, a high / light speed processing unit called brain,good 5 types of sensors and various out put mechanism to act the decisions which are well connected to the processor and memory.

These things are the essential requirements for building intelligence.Ok lets start from the time the child is born.As long as the sensors work properly, it will feel hunger. The brain / processor searches the knowledge base.Oh Nothing !!! If no decisions can be taken on that input take the default decision and act. What is that ?  Its simple. Just "Cry" .Even this is the default action when a he/she grew up and finds no experience in her knowledge base :-) . For developers, this is what we call hard coding / default value.

I am not sure about the western community and the Yo-Yo community in India.But a normal communities  around the world enforce the right to get mother's milk. Coming to the learning point what child learnt from it's sensors? The feeling of hunger is gone away and it seems this is an good external experience source for me. Yes it started the learning process.

Next time when it feels hunger it will look at mother without crying. Mother may understand it, else it will cry and soon a communication will establish between mother and child through eyes. Yes it learned how to communicate.

I am not a good writer or a communicator .But I believe readers got the idea and can assume how the next steps will occur. Hey...we were all experienced the same. The child sees other people coming to visit and if they brought something interesting to it such as food or toys, it adds as external experience sources. "Something interesting" here means whatever it feels through it's sensors. As the best interesting things are coming from its mom & dad, it give more weight to their experience and results.

Learning from external sources

Just put for the sake of heading. We already covered the topic.

Self learning / learning using random attempts

The child / baby became kid. I think this is the right word to denote someone who just learned how to walk and talk. Someday he happened to be alone for some time in the kitchen. His eyes sensed something on the cooking table. Yes his brain starts processing the sensed data using the experience obtained. No luck. Its empty .This time not defaulting to the cry action as it is not directly causing any problems to him. But based on the quality of the program in his brain, he may or may not decide to expand his experience. If he decide to expand the experience,  he need to make use of the concept of random numbers. There are options that he can gain more knowledge by lifting or touching it using his another sensor named hand. Or he can move close to that and feel whether its hot or not by sensor named skin. In short by selecting a random number and using his sensors he creates entries and build a database of experience or lets call this time as knowledge .If hes too smart ie created with efficient program, he will gain knowledge on how to destroy that too:-).

I mentioned the quality of the program. But I think there is no separate programs as the program is simple as an execution engine of stored experience. But the speed of execution and storage capacity may vary based on the hardware. Later some point if somebody instruct him to observe everything he will start observing things.Isn't it?

Interesting thing here is how he store the data about the object he observed. If my knowledge about brain functioning is right he keeps the image of that object in high speed small sized cache memory and everything else in the slow speed big memory but linked to the cached image. This help him to recollect details easily on seeing that object at a later point of time.

Hybrid learning

No need to explain more. When his mom is back he asks her what is that thing? She answers "Oh this is the frying pan which I used to prepare your favorite omelet'. He puts the name 'frying pan' in his cache as this is the key to identify that object later.

Developer meets AI ie The algorithm

As a developer I don't want to stop a blog post without single line of code. Here is a quick algorithm I and one of my colleague derived last Thursday. I don't think this is a complete one and you can directly start coding. Because nobody wants to reveal their trade secret :-)

IEnumerable<Sense> senses=GetAllSenses();
Experience feasible = null; //This can be a collection I think.
foreach(Experience exp in AllExperiences)
{
         if(exp.MatchSenses(senses) && exp.HasAtleastOneSuccess()==true)
        {
                if(exp.BetterThan(feasible)) //BetterThan() must take care of whether its own exp or external weigth  of source etc...
                        feasible=exp;
        }
}
if (feasible!= null)
{
        feasible.Act();
        feasible.UpdateStatistics();// Includes changing the priority,weight of the source etc...
        if(feasilbe.Result==false)
        {
                 feasible=null;
        }
}
if(feasible ==null)
{
        for(;;) //Here threshold value can come.
       {
                feasible=CreateRandom(senses);
                AllExperience.Add(feasilbe)
                feasible.Act();
                feasible.UpdateStatictics();

                if(feasible.Result==true)
               {
                        break;
               }
       }
}

If you has seen one of my recent facebook status,you will feel that I believe "if somebody built a truly AI system it will never close itself" .Or it will learn how it can survive itself from the early operations of Alt+F4 & Close button click. This is what I believe happening exactly between human and it's so called creator God.

What next?

If I managed to get a laptop, you can expect the below posts soon .But anyway these posts will appear on this blog over time.

Artificial Intelligence v/s Developer Part 2 - Why somebody invented the concept of random number?
Artificial Intelligence v/s Developer Part 3 - Sensing & acting / IO devices and communication.
Artificial Intelligence v/s Developer Part 4 - Multi-core processors,MapReduce & Hadoop
Artificial Intelligence v/s Developer Part 5 - Are you ready to do some AI apps like this?

Thanks to the daily 1 hr long lunch and tea time we have outside the Kochi Infopark near to the bank of a small river where these types of ideas evolve. I will miss it soon as some rules on tea time is getting enforced.

Tuesday, April 3, 2012

What is LogCat in Android and how can I see logs?

As normal, immediately after starting Android development, I begun editing files directly and in the same speed, I got an error which refer to something called logcat? Error shown was as follows in the console window.


[2012-03-03 12:04:00 - HelloAndroid] Installing HelloAndroid.apk...
[2012-03-03 12:04:01 - HelloAndroid] Installation error: INSTALL_PARSE_FAILED_MANIFEST_MALFORMED
[2012-03-03 12:04:01 - HelloAndroid] Please check logcat output for more details.
[2012-03-03 12:04:01 - HelloAndroid] Launch canceled!
[2012-03-03 12:05:26 - HelloAndroid] ------------------------------

This says something happened while deploying the app to the emulator / device .But for details I need to check something called logcat ? Where is that LogCat located ?

It is another window related to Android development in Eclipse .You can show that as follows.

Widnow -> Show View -> LogCat (with the Android logo)

If you are not able to locate in the show view sub menu select

Window -> Show View -> Others -> Android -> LogCat

This windows is just an UI to see the logs. You can even read and write to the logs. It is little difficult to understand the LogCat messages at first. But will be familiar with no time. My issue was I edited the name of an activity which was mentioned as the Launcher in AndroidManifest.xml. So make sure while renaming the activity modify your manifest as well.