Sms Firewall !!!

Tired of prank SMSes. Need a simple way to block them, and keep your inbox clean? Don't worry. You got it!

SMS Firewall is a simple and cute (hope you like it!) Android application to block and quarantine unwanted SMS from reaching your inbox. Unwanted are those who are neither in your contacts list nor in the allowed list, which is provided by the application. Phone numbers or sender names such as your bank or mobile service provider can be added to the allowed list. You got an option to either notify you of the blocked SMS or ignore it, in which case you will have check the quarantine vault by yourself. Besides, you have a 'Just Monitor' mode, which is primarily used for debugging purposes or when you don't want to block SMS from unknown sender(s) (for a while). When this mode is switched on, SMS from unknown senders will reach the inbox and also a copy of it is saved in the quarantine vault.

Here are a few screen shots to get you attracted...

Main Screen
Settings
Messages
Allowed List
You can download the latest version of the application from the Android market. Give it a try and let me know how you like it (rate it!). I hope it proves to be of use to you in keeping your inbox clean.

To Ritche !!!

Dennis Ritchie, whom we all know as the creator of the C programming language passed away on Oct 12, 2011. We have lost one of the brilliant minds of mankind. I owe him this post for he has been one of the greatest inspirations in my life and the very reason that I am into programming.

Dennis Ritchie (1941-2011)
The first time I saw Ritchie's picture, I felt him like Jesus. I would say that the divinity that his face reflected pulled into the world of programming. It was during my college days that I lay hands on his book - The C Programming Language. It is one of the most influential computer books I have read. I had read it at least a couple of times and referred it a thousand times. I felt it like a lecture by Ritchie. And so I had marked a lot of pages with comments and questions as if they are addressed directly to Ritchie himself. I had the book with me almost all the time. I even hide myself in the last row during other lectures to read the book. I felt excited reading the book. The book has a special place on my book shelf.

C is the among the first programming languages that I learnt, and it has its influence on me until this day. In the same way, it still has its influence in the computer industry. And his work on Unix, it proposed and implemented the principles of a modern operating system and also opened the gates of open source programming. Kudos to his contributions to the field of computer science.

I believe he will continue to live among us and be remembered all the time through his work and creations.

May his soul rest in peace.

See Also: Rob Pike's wall post on Google+.

Seinfeld Calendar Update !!!

We have released an update for Seinfeld Calendar with a bunch of some exciting features and defect fixes. I hope they are exciting for you too.
  • NOTES: You can add notes or additional information for a task on a month basis. Again, the notes are maintained month wise and not date wise. It is just to keep it simple, since adding it date wise would require a special selection mechanism for marking a date as completed.
  • REMINDERS: When you create a new task or edit an existing task, you will now be able to add reminders for a task, or disable them too if you added one already. As you might have guessed it, the reminder pops up a notification at the specified time reminding you to do the task. It could be jogging in the morning or taking your pills at night. The tool is now improved to not only keep track of your task status, but also helps you to do it everyday by reminding you. So you will not miss doing it!
  • HOME SCREEN WIDGET: We have added the facility to drop home screen widgets on your home screen per task. The home screen will show you the current date and you can click the date to mark it done. If you click the task name, it will take you to the specific task\current month in the application.
So with the above features, doing the task and tracking it has become super easy. Despite all the handy features, it is all in your hands to do your task. It is just not about marking it done.

However, we did a bloody mistake. We lost the upload\upgrade key for the original Seinfeld Calendar, which you guys would have already installed on your phones. So this update had to be released as a separate application, which sadly means you can not directly upgrade your application from the Android Market. Sorry folks, very foolish of us to lose the key. We renamed the old one as Ex-Seinfeld Calendar, which will remain as such for the rest of its days.

You can find the new application in its latest version here or visit the market on your Android phone and search for Seinfeld Calendar.

Thanks to the users who emailed us their comments sharing their opinion and requesting improvements such as the above.

Android meets .NET !!!

It is always fun for me to program in C# (besides C++). If so, how would I feel if I was able to program for Android in C#? You may be wondering what in the world I am talking about. Android development environment is all Java and open source stuff. How could this Microsoft thing fit onto it?

Well, it seems that some clever guys had huddled up and ported Mono for Android, developed .NET libraries for the Android SDK, and also supplemented it with a 'Mono for Android' project template in Visual Studio; and called it mono for android. Thus we end up writing C# code happily for Android.

After a bunch of installations, fire up your Visual Studio (2010) and you should be seeing a new project template 'Mono for Android' under C#.


Create a new 'Mono for Android' project, which by default comes with an activity - C# code.

Modified the orginal code to try starting a new activity from the default one...

The project layout for most of the part is similar to the native Android project. The layout resources files are named *.axml, and the strings.xml is really clean with named string constants with the ugly behind the scenes of ids and stuff in the Resource.designer.cs. The way to code is straight forward and needs no special explanation. A good to see thing for me was .NET coding style.

A similar set of addins and plugins have been developed for MonoDevelop too; although I did not try that.

It is really a wondrous thing to know, program and relish such an attempt. However, there are sad curves in the story:-
  • Mono for Android is available only for Windows (and Mac). It is not available for Linux.
  • The installation of the application from Visual Studio and application response for the few clicks is pig slow. It is not Visual Studio's fault. It seems Mono takes quite a bit of time for JITting. After each part of the code is JITed, the application response for that part is fairly ok, but not as good as Android's native\custom VM Dalvik.
  • The trial version does not allow deploying on the device. You can play around and deploy only on the emulator. If you want to use it on the device, it costs. Yes, it costs somewhere between $400-2500, depending on the licesing and support. Pretty expensive!
Try it out (the trial one or the full version if you are rich), at least for fun!

Quiz: Beauty of Numbers

Sriram asked:

Imagine there is a queue of people for getting a ticket for a movie or somehing. Where should be standing in the queue to last until the manager or some guy keeps removing people at odd indices. For instance, if the queue has 5 people given a token A to E, first we remove the first set of odd numbered positions in the queue, so A, C and E are gone. Now B and D remain. Again we remove the odd numbered positions. This time B alone is gone, and D is the winner. So in a queue like that, what is the lucky position you should hold so that you survive the wave of removals?

I could give you the solution right here in front of your eyes. But that would not give you time to think to solve it on your own. So solve it and\or read the solution here.

To Hold or Not to Hold – A story on Thread references !!!

void SomeMethod(int x, double y) {
  // some code
  ....
  new Thread(ThreadFunc).Start();
}

What do you think about the code above?

Some may say nothing seems to be wrong. Some may say there is not enough information to comment. A few may say that it is awful to spin off a thread like that (the last line of the method), and that there is a probability for the thread to be garbage collected at an unexpected point of execution. That is something interesting to discuss about.

Before presenting my thoughts and supporting facts, the short answer is NO. A thread spun off like that will not be garbage collected as we expect, although one should be morally insane to write such code. Alright, let us discuss.

The Thread class is like any other reference type in the BCL. When an instance of a reference type holds no more outstanding references, it is a candidate to be garbage collected. Even worse, it could become a candidate even while it is executing an instance method, while there are no more outstanding references to that instance. If such facts are considered, then the thread created at free will in the above code is bound to be collected anytime while executing the associated ThreadFunc. Let us try that with simple sample application.

static void Main(string[] args) 
{
   Console.WriteLine("The Beginning...."); 

   // ThreadFunc inlined as anonymous delegate
   new Thread(delegate() 
   {
      for (int i = 0; i < 1000; i++) 
      {       
         var obj = new Junk(i, i, i.ToString()); 
         Console.WriteLine("{0}, ", i); 
         Thread.Sleep(100); 

         if (i % 10 == 0) 
         {
            GC.Collect(); 
            GC.WaitForPendingFinalizers();
         }
       
      }    
   }).Start(); 

   GC.Collect(); 
   GC.WaitForPendingFinalizers(); 

   Console.WriteLine("At the end!");
}

In the sample application above, I have forced garbage collections and also waited for pending finalizers at two important points of execution - 1) at the end of main thread 2) in the course of execution of the thread delegate. I have made sure that there is enough chance of GC kicking in by creating some Junk objects too. Those are some of the important triggers for GC to kick in.

If you run the application, you will see that the application does not quit until the loop runs to completion; although the main thread runs to completion - prints "At the end!". Now, let us not enter an argument that the thread is a foreground thread and application shall not quit until all foreground threads have finished executing. Yes, if the thread in the above code was a background thread, the application would have quit before the loop completed. But then we would have created a reference to set it as a background thread, and we would have to stop the discussion there since we are talking about threads created\started without holding a reference. Besides, the context of the problem is not application exit but application running.

Alright, let me rephrase it in a way that is relevant to our context - If you run the application, you will see that the thread function runs to completion successfully (loops 1000 times, creates 1000 objects, waits for 100ms each iteration of the loop, triggers garbage collection/waits for pending finalizers during execution). If thread had been garbage collected, it would not have run a 1000 iterations successfully. So does that mean the CLR has a soft corner for Thread types? Seems so.

If you drill down the Thread type using Reflector, you will see that it neither implements IDisposable nor a finalizer. How could an object not implement cleanup mechanism, escape the almighty garbage collector, and still not cause any havoc? That is weird! It gives the impression that we might be leaking the underlying native thread resource. Obviously, the CLR folks are not that careless or we would not be running applications today written in managed code. My common sense told me that there is some trick played behind the scenes such that a reference to every thread is somehow maintained and the clean up is thus taken care of.

So Ananth and I rolled up our sleeves to ponder for the evidence inside the runtime using SSCLI. It is tough to project a few snippets of code or so from SSCLI to show you "Here, this is the evidence". However, I can share what we saw. When a thread is created, a reference to the thread object is added to a static list maintained by the framework. Thus a reference is established whether the user code holds it or not. When a thread is started, there is a bit of framework code executed, which then turns over control to our thread delegate. When our thread delegate finishes execution (normally or abnormally), it returns to the caller inside the framework code, which takes care of removing the reference from the static list. The framework code then does some cleanup which involves closing the thread handle and such. Only then, does a thread object become a candidate for garbage collection, which finally has nothing specific to cleanup. I think the CLR folks are smart enough (obviously) to do it this way. Because 1) threads are really special resources whose behavior is a bit different than other native resources 2) Thread type is a just wrapper for over the original native\OS thread.

We have seen the evidence. Now, let us discuss something about morale. When you take a look at the code with thread spun off at free will, don't you wink twice? Doesn't it raise a lot of questions about correctness, safety etc. Obviously, it is not a good practice. Just because there is something in the framework to take care of, it does not unleash us to do such things. Agreed that SSCLI does not lie but is it completely safe for our application code to rely on some very intrinstic detail of the framework code? I don't think so.

The intent of the code like SomeMethod seems to fire (off a thread to do some processing) and forget, since it is not interested in holding a reference to the thread object. And it is very well possible that SomeMethod could be called few or many times, and we would be creating new threads just to fire and forget. Haven't we heard that threads are expensive resources? Why did people come up with idea of thread pool? Thread pool is the apt choice for such fire and forget or processing that do not require thread affinity.

Next time, you see such code, if you have the authority to change it, correct it. If not, speak to the developer who wrote the code. Start with a soft and warm conversation and explain him the morale or show him this post (a little marketing!). Make sure that the conversation does not become aggressive (getting the developer defensive). Even after such warm conversations, the developer does not have the brains to take your side, shoot him! Just kidding.

Well, that is something I wanted to share. It is now your turn to comment and/or correct. Please share your thoughts if you have found or know of any other evidences about thread references and related. I am sure it would be an interesting and worthy discussion.

Crazy Brackets - [](){}();

What does this cryptic bracket sequence mean? What programming language is it? Is it valid syntax? If there is even a weak chance of this syntax being valid, then what does it mean?

Alright, alright, alright.....It is C++. That would calm most people; with all their love (pun) for C++. Specifically, it is C++0x. Amongst many other features that we have been waiting for, C++0x gives us the power of lambdas.

The formal definition of a lambda in C++0x is as follows:-

[capture_mode] (parameters) mutable throw() -> return_type {
   body
} 

So a lambda may capture one or more variables in scope by value or by reference, or it may capture none. Specifying return_type is not necessary if the type can be inferred or is void.

For instance, a std::for_each's functor based code could be inlined with a lambda as follows:-

std::for_each(v.begin(), v.end(), [](int x) {
   cout << x << std::endl;
}); 

A lambda definition could be assigned to a variable and then used or invoked later.

auto lessthan = [](int left, int right) {
 return left < right;
}; 

In the above code, lessthan represents a function that takes two int parameters, and returns a bool. And it can be invoked as lessthan(2, 3), which returns true. The cute thing about a lambda is that it can invoked directly right after its definition. The following code defines a lambda (which takes two ints and returns a bool) and invokes it right away.

[](int left, int right) {
   return left < right;
} (2, 3);

Coming back to our initial question...............you should have guessed it by now. The bracket sequence - [](){}(); - is nothing but a definition followed by a call (right away) to a lambda taking no arguments and returning nothing.

To end with a quote, C++ code is like calligraphy. In other words, it is beautiful to those who understand it, while cryptic to others.

P.S: Pardon me if calligraphy is inappropriate in this context.

Wetting my feet in Android - Seinfeld Calendar

A couple of my colleagues and I huddled up to learn a bit of Android. I think I told you about that a short while back. We developed a very simple application - The Seinfeld Calendar.

Seinfeld calendar, or otherwise called the habit calendar, is Seinfeld's productivity secret. The secret of achieving your goal is practising something, whatever your goal is, everyday and make it a habit. And mark it in your calendar each day you practice, and make sure you do not break the chain. Our application helps you keep track of your everyday tasks.

Our application does not jog or meditate or quit smoking for you. You have to do your tasks. Our application provides the facility to create tasks for the habits you wish to pursue, which in certain cases like smoking means quitting. And then you mark each day in the calendar if you pursued your task, else you don't mark and break the chain. And no cheating!

The application is in very nascent stage and does not provide you (fancy) statistics and all. I mean if you are really serious about pursuing something to make it your habit, then you will use our application. If not, you will just be waiting for funky features and newer versions of the application. Just kidding!

All fellow android users, download the application, give it a try and let me know how good or bad the application is. And also let me know your wishlist of features in the application.

Anonymous Classes vs Delegates !!!

I am not a java programmer. By that, I do not mean I am against Java. As a programmer by profession and passion, I try to learn things along the way. That includes a little of bit of Java. I should say that my proper encounter, so to say, with Java is a simple application that I am trying out with Android. There might be some hard core differences and/or limitations in the Android version of Java. But I am almost certain that I am using only primary level features of Java.

In android, there is this OnClickListener interface, which is used as a callback interface for a button click. So, it is used something like this:-

// Create an anonymous implementation of OnClickListener
private OnClickListener mCorkyListener = new OnClickListener() {
    public void onClick(View v) {
        // do something when the button is clicked
    }
};

protected void onCreate(Bundle bundle) {
    ...

    Button button = (Button)findViewById(R.id.someButton);
    button.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
        // Click handler action code...
        }
    });
    ...
}

OnClickListener, which is an interface with a single method onClick, represents a type for the button click event. The highlighted portion of the code that registers an event handler for the button click action is called an Anonymous Class definition. That is some really some clever syntax; although it seems a wrong tool for our purpose here. Actually the click event requires only a method to call when the button is clicked. Nothing more. So why do we need an interface here?

I know of a better way in C#. Back there, it is called a delegate. In simple words, a delegate is an object-oriented pointer to a function, and it could point to any public\private instance\static function of any class. So a delegate is a good fit for our situation here. If the highlighted portion of the code (event registration) were to be written in C#:-

button.setOnClickListener(delegate(View v) {
    // Click handler action code....
});

I have gone one step further and used an anonymous delegate, which is even more succinct. Sometimes, less syntactic noise is a good feeling for a programmer. I am not doing a language war here. I am just trying to vote for delegates in Java. I am not sure if they are already there in one of the latest versions.

But there is a C# fanatic inside of me, which compels me to show the world how better and good-looking (see pascal casing) C# code actually is.

protected void OnCreate(Bundle bundle)
{
    var button = FindViewById<Button>(R.Id.SomeButton);
     button.Click += delegate(View v) {
        // Click handler code.
    };
}

Beauty lies in the eyes of the beholder!

Nevertheless, anonymous class is definitely a wonderful and powerful syntax, but does not look good in the example above.

Quiz - Where am I?

The question is, in C++, how do detect if an object is allocated on the stack or heap.

On Windows, the stack address is in the range of 0x80000000. If the address of the variable is in this range, then you could say that the object is allocated on the stack; else it is allocated on the heap. This technique of detecting is not preferrable since it may not work on other operating systems (such as linux), and deals with the platform specific information making it a non-portable solution.

Let us try to use standard C++ stuff to solve the problem. Ok, we want to know where an object is allocated. In C++, new operator is responsible for allocating an object.

It was very thoughtful of Stroustoup to keep the object allocation and initialization\construction separate. new operator is responsible only for allocation. The compiler is responsible for woving up the code for allocation and calling the constructor. It was also very thoughtful of being able to specify the location where the object needs to be allocated, which of course does not necessarily require overloading the new operator.

C++ allows taking control of object allocation by overloading the new operator. By taking control, you would be able to detect where an object is allocated, and also keep a count of the objects allocated on the stack\heap. The following code snippet illustrates the same:-

//
// SomeClass.h
//

#include <iostream>
#include <stdlib.h>
#include <deque>
#include <algorithm>

using namespace std;

class SomeClass;
typedef std::deque<SomeClass*>      SomeClassDB;
typedef SomeClassDB::iterator    SomeClassDBIter;
typedef SomeClassDB::const_iterator SomeClassDBConstIter;

class SomeClass
{
private: static SomeClassDB heapObjects;
private: static SomeClassDB stackObjects;

private: double value;
private: bool isOnHeap;

public: SomeClass(double d) : value(d), isOnHeap(SomeClass::IsOnHeap(this))
        {
           if (!IsOnHeap())
           {
              SomeClass::stackObjects.push_back(this);
           }

           PrintLocationInfo();
        }

public: ~SomeClass()
        {
           SomeClassDBIter iter = std::find(SomeClass::heapObjects.begin(),
                    SomeClass::heapObjects.end(),
                    this);

           if (iter != SomeClass::heapObjects.end())
           {
              SomeClass::heapObjects.erase(iter);
           }
        }

public: double Value() const
        {
           return this->value;
        }

public: bool IsOnHeap() const
        {
           return this->isOnHeap;
        }

public: bool IsOnStack() const
        {
           return !IsOnHeap();
        }

public: std::string Location() const
        {
           return IsOnHeap() ? "Heap" : "Stack";
        }

public: void PrintLocationInfo() const
        {
           std::cout << Value() << " is on " << Location().c_str() << std::endl;
        }

private: static bool IsOnHeap(SomeClass* scPtr)
         {
            SomeClassDBConstIter iter = std::find(SomeClass::heapObjects.begin(), SomeClass::heapObjects.end(), scPtr);
            return iter != SomeClass::heapObjects.end();
         }

private: static bool IsOnStack(SomeClass* scPtr)
         {
            return !IsOnHeap(scPtr);
         }

public: static void* operator new(size_t size)
        {
           SomeClass* scPtr = (SomeClass*)malloc(size);
           SomeClass::heapObjects.push_back(scPtr);
           return scPtr;
        }

public: static void operator delete(void* ptr)
        {
           free(ptr);
        }

public: static size_t HeapCount()
        {
           return SomeClass::heapObjects.size();
        }

public: static size_t StackCount()
        {
           return SomeClass::stackObjects.size();
        }

public: static void PrintObjectCount()
        {
           std::cout << "OnHeap: " << HeapCount() << " .... OnStack: " << StackCount() << std::endl;
        }
};

//
// SomeClass.cpp
//

#include "SomeClass.h"

SomeClassDB SomeClass::heapObjects;
SomeClassDB SomeClass::stackObjects;

int main (int argc, char *argv[])
{
   SomeClass sc(0.123);
   SomeClass* scPtr = new SomeClass(1.234);

   SomeClass::PrintObjectCount();

   {
      SomeClass sc1(2.345);
      SomeClass::PrintObjectCount();
   }

   delete scPtr;

   SomeClass::PrintObjectCount();

   return 0;
}

You should be aware of implementing the custom delete if you have provided a custom new operator. It is logical because only a custom implementation that allocated memory for the object could possibly know how to deallocate. The above technique of overloading new\delete is portable and safe since it is standard C++. As always, writing standard C++ is fun.

But why would one care to know where an object is allocated or keep a count of objects. I don't think it would something you would require for production purposes. It could be for development\debugging purposes; maybe you want to detect memory leaks or a general distribution of objects. You could take control of the allocation by overloading new for a particular class or for all classes by declaring a global new operator.