Sunday, October 19, 2014

How to use Calendar Provider in Android to add, delete and read events

When I started writing a demo app for symptoms management, I thought it'd be cool to add the reminders directly into Android users' calendars instead of managing the notifications and clocks manually. I found out that Android Calendars can also be useful if one wants to manage events without actually creating and managing a database of events within an app.
Apart from reading Android Developer's documentation here, I also referred to a blog here that puts together all the information to get you started and running. However, bits and pieces of information are always missing and I decided to put together an example for people to start with.
You can find the example on Github here. Happy coding!

Thursday, September 4, 2014

Ubuntu 12.04 Freezing - After android source code download

A quick note:

If you were trying to build the Android source with the instructions present here, and after

repo sync

stage, when you reboot the machine and it doesn't get past the splash screen, you will need to reinstall the ubuntu-desktop again. As stupid as it sounds, I spent almost a day trying to tweak VirtualBox settings with no luck. So, go ahead and just do the following:

sudo apt-get install --reinstall 
Where, xX-desktop is the desktop environment you were running.

e.g.
sudo apt-get install --reinstall xubuntu-desktop
sudo apt-get install --reinstall ubuntu-desktop
sudo apt-get install --reinstall kubuntu-desktop

Thursday, November 21, 2013

Pass by Value and Pass by reference - In depth

Pass by value:

·         Variable Passed by value:
o   Value of the variable is passed as a parameter
o   The called function makes a separate copy of the passed parameter
o   Therefore, if the value is changed in the called function, it does not affect the original variable’s value
e.g.
               void f(int n) { n++;} 
 
               int main()
               {    
                       int x = 2;    
                       f(x);    
                       cout << x;  // x is still 2
               }
Here, although, function f increments the value of n, it does not affect the value of original variable x

·         Pointer passed by value:
o   Value of pointer variable is passed as a parameter
o   Called function makes another copy of pointer variable
o   Therefore, both pointers now ‘point’ to the same variable
o   New pointer can modify the value at the address pointed by the original pointer
o   But since, the pointer variables are still different, modifying the new pointer will not have any effect on the original pointer

void f(int *p) {
    *p = 5;
    p = NULL;
}
 
int main() {
    int x=2;
    int *q = &x;
    f(q);
    // here, x == 5, but q != NULL
}

Pass by reference (in the context of C++):

·         Conceptually, the actual parameter itself is passed
·         Therefore, every change made in the called function affects the originally passed parameter
·         How to remember –
o   Passed just like value
o   But received as an address with &
·         Useful when function is required to calculate multiple values

e.g.

void f(int &n) { n++;}
 
int main() {
    int x = 2;
    f(x);
    cout << x; 
}
 
//Here, the output of program will be 3.
 
/*Example swap function:*/
 
void swap( int &j, int &k ) {
    int tmp = j;
    j = k;
    j = tmp;
}


Pass by constant reference (in the context of C++):

·         Same as pass by reference above, but the pointer parameter is ‘received’ and interpreted by the called function as a constant pointer. And therefore, cannot modify the contents of the original parameter passed
·         Then why not just pass by value?
o   Because some parameters, e.g. a list might be too large to pass as a parameter and therefore it makes more sense to pass a pointer to it, but at the same time, you don’t want your function to “accidentally” modify the list
e.g.
void f(const IntList &L) {
 -- the code here cannot modify L or the compiler will complain --
}
·         The member functions like “f” above, that do not modify any data members should be declared “const”
e.g.

               void f(const IntList &L) {   L.Print(cout); }
 
        If Print were NOT defined as constant, the above line of code would cause a compile time error.
 
Because L is a const-reference parameter, it is the compiler's job to be sure that L is not modified by f (and that means that no data members of L are modified). The compiler doesn't know how the Print function is implemented; it only knows how it was declared, so if it is not declared const, it assumes the worst, and complains that function f modifies its const-reference parameter L.


Arrays as parameters:
·         In C++, arrays are ALWAYS passed as parameters, even though, they’re not declared as reference parameters in function definition.
·         This means that, any change that is made to the array in the called function, ends up modifying the original array
e.g.
void f(int A[]) {
    A[0] = 5;
}
 
int main() 
{
    int B[10];
    B[0] = 2;
    f(B);
    cout << B[0] << endl;  // the output is 5
} 



Arrays as parameters:

·         In C++, arrays are ALWAYS passed as parameters, even though, they’re not declared as reference parameters in function definition.
·         This means that, any change that is made to the array in the called function, ends up modifying the original array
e.g.
void f(int A[]) {
    A[0] = 5;
}
 
int main() {
    int B[10];
    B[0] = 2;
    f(B);
    cout << B[0] << endl;  // the output is 5
}
 



Tuesday, August 14, 2012

Curiosity Mars Rover

Scroll down to the section where Curiosity Rover functionality is explained!
This is just AWESOME

http://www.bbc.co.uk/news/science-environment-19262486

Sunday, August 5, 2012

Setting up a git repository

This is for my own reference, just in case I want to lookup the steps again:
1. Make sure that your linux machine has git installed. Create a project directory somewhere and do:

> git init

This will initialize a git repository. You might also want to setup some global parameters like username and default editor. The instructions are given here:
http://vimeo.com/34754947
(To save additional efforts to sync local machine and the repo you create on git, make sure this directory is empty. If you still want files to be in your local repo, you can use the normal git commands here)

2. After this is done, go to Github website, create an account if you don't have one already and follow the instructions to setup a new repo here:

Set Up Git

3. Generate and setup ssh key for the machine. Here are the instructions:

Setup SSH key

Note: If you're asked for the password at any point (when setting up git) this would be your paraphrase that you specified earlier

4. Then do this:

> git remote add origin git@github.com:myusername/myreponame.git 
> git push origin master

Sunday, March 18, 2012

Playing with Linux partitions

I always hated the obscure ways with which linux deals with certain things.
/dev for example. If you do 

ls /dev                                                                                                                                                          

This will list all the things that linux thinks is "devices". There's so much shit under it that you don't know which all of them are actually your hard disk partitions. Well, here's what I have learned
1. The disk partitions are always labeled such as sda, sdb, sda1, sdb1, hda, hdb... etc. So you can probably run  a command to find out about your partitions as below:

ls /dev/sd*                                                                                                                                                                                              

ls /dev/hd*                                                                                                                                                              

And this will list your partitions.

Now, the question remains... how do I find what lies under what partition? Here's a simple analogy:
You have a music CD, you want to find what files are there in the CD. How would you do it? You mount it on the CD player and browse it... right? Similarly, these partitions have to be mounted on an already existing directory in order to see their contents.
So, let's say you want to see what's under /dev/sda5, here's how you will do it.

1. Use and existing directory / create a new directory to mount this partition on. I will create a new directory below for the purpose of this discussion:

mkdir mount_point                                                                                                                                      

2. The above directory (aptly named as mount_point) will now be the directory on which the partition (/dev/sda5) will be mounted. You can mount the partition using following command

mount -v -t  ext3 /dev/sda5 mount_point                                                                                                                

Where, 
mount - is the linux command to mount the partition to a mount point (directory). Click on the mount ink to see the man page for mount and what those -v -t options mean
ext3 - is the filesystem with which the partition was formatted.
/dev/sda5 - is obviously the partition to be mounted
Finally, 
mount_point - is the directory on which the partition is being mounted.

Once the partition is mounted, you can simply

ls mount_point                                                                                                                                            

and see the contents of the /dev/sda5 partition.

To unmount, you can use the umount command as follows:

umount /dev/sda5                                                                                                                                        

This is the first of the many tips I am going to post as I learn linux myself. Comments, suggestions and corrections are most welcome.

Cheers!

Sunday, January 29, 2012

Excel Automation With Ruby

Recently, I was asked to develop a tool that kind of processes a data in ASCII file and generates a report in Microsoft Excel while using another excel file as a reference database (for lookup tables etc). I had worked with Apache POI libraries for excel and such before and didn't quite like the level of abstraction. So I decided to give ruby a shot, as it happens to be my second favorite programming language (and first favorite scripting language).
Writing to excel files is no big deal, but I thought it would be cool to be able to send sql like queries to excel sheet and get the result set in return. Apparently, there's a way to do it in Ruby and the database driver that lets us do that is ADO. This is an excellent blog that hosts a treasure of lessons about how to do cool stuff with Ruby. Pretty much everything you need can be found there and so, I am not going to re-post it here. However, here are a few very basic things to get you started and help you to avoid pitfalls:

1. Install Ruby interpreter for windows from here. Ruby windows installer comes with 'win32ole', so you don't need to download it somewhere else. At this point, I would suggest you to avoid the temptation of downloading 1.9x ruby release because there's no easy way to get the ruby debugger to work on this version yet ( as of 01/29/2011). I'll talk about this in a little more detail in the debugger installation section.

2. Install Dev-Kit from here (the same place as above). Dev-Kit installation is really simple, do not succumb to the temptation of not installing it. At some point, you're going to need the ruby debugger which needs native compilation and for which you need Dev-Kit.

3. Install gem called spreadsheet using the usual gem install command as follows:
gem install spreadheet

This is all you need as far as the spreadsheet part is concerned. Some useful code lines on how to use this gem is given on the blog is mentioned above.

4. Ruby Debugger: If you are using ruby 1.8x, ruby debugger can be installed using the same gem install command as above...

gem install ruby-debug

This gem has several dependencies (like ruby-debug-base, linecache, ruby_sources something etc) but the good news is,  once you issue this command, all the dependent gems will be automatically downloaded and installed.
Anyone who does not like to work with plain text editors and prefers IDEs instead, would obviously want to be able to run and debug scripts using the IDE. Now, this is where going with ruby1.9 is not a good idea (as yet). ruby-debug gem is NOT meant for ruby1.9x, they have a different name for that gem instead which is ruby-debug19 (and the prefix 19 goes with all the debug related gems for ruby1.9x). First of all, I have had problems with installing ruby-debug19 in itself but there's a workaround for that (installing pre-release versions of linecache etc. However, even after successfully installing it, I was unable to get it working with IDEs. The reason for that is, IDEs look for ruby-debug for debugger instead of ruby-debug19. I tried this with Eclipse DLTK as well as other editors such as Aptana's RadRails and also their eclipse plugin. After wasting a lot of time trying to make everything work smoothly, I decided to give up my pride and simply go with something that works. And I highly recommend doing so for a non expert person like me. You also need ruby-debug-ide gem to get the debugger working with the IDE.

5. What IDE/Editor to use?
At this point, you're all set. But before starting to write the application, one might wonder about what editor or IDE to use. I personally prefer Eclipse for several reasons. For one that it's free and I've used it a lot for development in java. For two, installing the plugin is no pain at all and the DLTK plugin has been around for a while, so it's reliable. And finally, there's a nice code completion assistance with this plugin. If these sound like good enough reasons to go with Eclipse DLTK, here are the installation instructions (you have to add a new update site under Help >> New software download). There's also Aptana's plugin for eclipse. Find it here. I used to like NetBeans, but apparently, Oracle yanked the ruby support from future releases... bummer!
Before using the DLTK for the first time, you will need to specify the ruby interpreter to the IDE and eclipse can search that for you. It should be under Project >> Properties >> ruby... something like that.
In rare situations, your eclipse or even the ruby interpreter won't find the gems. The fix is easy. Add an environmental variable called RUBYOPT  to windows and set its value to rubygems. Eclipse also has an option to add environmental variable and that is under Run >> Run Configurations... (and same for debug). I observed that, I did not have to define/add RUBYOPT, but someone else might..

6. Few tips and tricks and pitfalls:
- Make sure that the data format of columns is the same as the sql query is expecting. E.g. If you type in a number into a cell in excel, it will store it as a text (you also might get a comment-like warning saying that). If you're planning onto write queries that need numerical comparison, change the format of these cells to number.
- Make sure that the database excel sheet you're querying is not opened by any other application, this will keep your ruby script waiting for that excel sheet to be available without any warning (according to my observation)
- Ruby 1.8x does not handle special characters all that well because there's no encoding support in ruby 1.8x. The easiest option is to avoid special characters of course. But then, you also have an option to do the development on 1.8x and later upgrade your ruby to 1.9x. 
- I have also seen that in some cases, SQL query would return nil values for the cells whose format is number. I could not figure out why that happened, but the problem went away on its own. If something of this sort happens to you, try making a fresh excel sheet for database.

Hope this was helpful! Good luck :)