Monday 1 October, 2012

An Android design flaw and how ADB saved the day

    My little cousins (5 years and 8 years old) love to play games on my Nexus One - Angry Birds is their latest favorite. However, they have their mid-term examinations going on, and as per their mother's request, I attempted to restrict their game time. Unfortunately, even the little 5 year old is tech savvy enough to unlock the simple slide-to-unlock screen. So, I had to resort to the pattern unlock, which is where the problems started.

    Usually, they bring the mobile to me to unlock, I unlock it if it's their break time from studying, and all goes well. Today, however, the little one today decided it was time to become a rebel and try to break the security. She kept on trying different patterns, even persisting through the 'Wait 30 seconds' message, while I was busy elsewhere. Finally, my Android had had enough, and said 'Too many pattern attempts', and asked for my Google login and password.

    Now again, I should have been able to give my username and password, set a new pattern and go on my way. However, there were two things combined to make things not so easy: my mobile had both GPRS data and WiFi turned off, and I had enable two-factor auth for my account. Because of the latter, my actual Gmail password did not work in the unlock page, and because of the former, even disabling two-factor auth in my Google account from my laptop made no change. I also tried a few other "solutions" given online such as:
  • trying the password 'null' (without quotes)
  • calling the mobile from some other number, accessing the notifications area from the incoming call screen, hoping you have some message or mail notifications, finding a clever way to go to the Settings screen from there (this one failed right at the beginning - the notifications area couldn't be opened from the incoming (or ongoing) call screen)
  • pressing the menu key repeatedly while the OS is booting up
    None of these were of any help, and I had at this stage started cursing Android for this poor design and even started thinking maybe an iPhone wasn't such a bad idea after all (blasphemy!).

    Finally, my incessant searching led to a forum post that linked to this post which started me on the right path. However, the stuff about factory reset at the end of it was pretty discouraging, and the method mentioned seemed pretty ugly too. I kept searching further, which led me to this page. Combining the info on these two posts, I was able to get my phone unlocked and breathe a sigh of accomplishment! These were my steps:
  1. I wasn't sure I had USB debugging enabled, but anyway tried connecting the mobile to my laptop. 
  2. I then went to the tools folder under my android-sdk-windows (I already had the Android SDK installed) directory in the Windows command line
  3. I tried the 'adb devices' command. Thankfully, it listed one device as currently attached. That meant USB debugging was enabled and adb could talk to my device
  4. Next, I opened the adb shell with 'adb -d shell', and the # prompt popped up
  5. Then, I opened the settings.db in sqlite as 'sqlite3 data/data/com.android.providers.settings/databases/settings.db'
  6. By this time, I'd come to know that the mobile_data setting that controls whether the device connects to GPRS data services used to be in the 'system' database table in old Android versions, but has moved to the 'secure' table now. I didn't know which table had it in my phone, so I first tried 'select value from system where name="mobile_data";'. This printed out my input itself, which I guessed to mean no such row in the table (bad design in SQLite here?)
  7. Now, I tried 'select value from secure where name="mobile_data";'. This time, it printed out 0, which meant mobile_data was in this table and was set to 0, thus disabling GPRS access. 
  8. I set the above value to 1 with 'update secure set value=1 where name="mobile_data";'
  9. I then finished it off with '.exit', then 'exit', then 'adb reboot'.
    When the device rebooted now, it had a GPRS data connection on and working. Phew. Now I tried my original Google password (since I had disabled 2-factor auth temporarily), the device checked it with the Google servers and deigned to allow me in. Yay!

    So, despite my complaints about Android and the iPhone fantasies, it's Android's hackability and openness that allowed me to find a solution much less onerous than a hard reset of the device. That, however, in no way excuses Android's poor design in this area; off the top of my head, one solution would be to special case the Internet access from this lock screen and enable data and WiFi when the username-password check fails once - there are probably much better solutions the fellas at Google could come up with, if only they paid a little attention to this. Also, there doesn't seem to be any option to increase the number of pattern attempts before Android pops this annoying page up - with there being 389112 distinct patterns, I'd gladly allow 100 attempts before assuming someone's trying to hack my mobile.

    My moral from this story has been to save the application-specific password of my mobile somewhere safe, and to always have USB debugging enabled in the mobile. What's yours?

Friday 7 May, 2010

Online compiler/interpreter/IDE


I had been using www.codepad.org whenever I wanted to run a piece of code and didn't have the required compiler/interpreter in the system I was using. But the main disadvantage with this was that you cannot read input from the user while using this site - scanf(), cin, <STDIN>, etc. (in C, C++, Perl respectively) are promptly ignored. This was often an irritation since I'd have to modify my program to not take an input before testing there, which might not be an easy process (especially given complex data structures).

Today, I came across http://ideone.com, which is very similar to the above, except that it allows providing input to the program! Yay! You type (or paste) the program in a text area as in codepad, choose your language from the list, and then click on "click here to paste input (stdin) or additional note". Whatever you type here becomes the input to your program. That's a neat solution to the problem of providing input to remotely-evaluated programs.
And it supports "more than 40 languages", including Assembler, Common Lisp, and even Whitespace (of course the boring old C, C++, Java, Perl, Python, etc. are there too). It has other goodies like syntax highlighting for all these languages, 'private' pasting for times when you don't want others to see the code, etc. The site is quite very well done.

While we're in the topic, let me also add two more online code evaluation tools (compilers/interpreters):

* Lord of the REPLs: This is a Read-Eval-Print-Loop from Google, where each line you enter is evaluated immediately. Despite the fancy "Lord" name and the Google brand, I haven't found this very useful mainly because: it doesn't preserve function definitions or variables' values after you evaluate them, unlike other REPLs. This makes it hard to run anything more than trivial "hello world" programs. Also, I found it quite buggy at times.

* "Compile & Execute": This is somewhat similar to ideone, allowing you to enter your own input and having a choice of a similar number of languages. However, it doesn't have a 'private' pasting option, and has syntax highlighting for only a few languages. This leads to weird highlighting at times: if you choose to paste Perl code, the Highlight choice remains in cpp (which is the default), so your Perl code gets highlighted as C++ code! Sloppy design like this makes me think this site might not be very well designed.

These two are the only generic code evaluation tools I could find. If you find that none of these do it for you, try googling for something specific to your language. For example, for Python, searching for 'python online (IDE OR compiler OR interpreter)' gives a lot of good results.

If you know some other online code evaluation tool, or have experience with any of the above, do share it in the comments. Thanks!


Thursday 1 April, 2010

Songs list from files list

I had a list of mp3 files like:

ANBE_VAA_ARUGILE.MP3
ANBU_THAYAE.MP3
ANDANVAN_THEANAKTRU.MP3
ANNANENNA.MP3

and wanted the list of song names from this… What shall we do?

* Open Cygwin (Unix emulator within Windows), navigate to that folder, do:
      ls | cut -d. -f1 |  sed –e 's/_/ /g'
   list the files in this directory, cut and extract the 1st field with delimiter as . character, then substitute space character in place of _ character globally. In other words, get the actual filename removing the .mp3 extension, then replace underscores with spaces.

* Save the above output to a file
     Actually this too should be a part of the above step, like:
      ls | cut -d. -f1 |  sed –e 's/_/ /g' > ./songnames.txt
     The file songnames.txt now has:

ANBE VAA ARUGILE
ANBU THAYAE
ANDANVAN THEANAKTRU
ANNANENNA

* Open the file in Vim editor, and select all the text by pressing ggVG. Then, press : to enter command mode, and Vim will show :’<,’> and wait for your input. Type this:
    !perl –lpe "@words = map { ucfirst(lc($_)) } split; $_ = join(\" \", @words)"
and press enter, and voila, the list has become:

Anbe Vaa Arugile
Anbu Thayae
Andanvan Theanaktru
Annanenna

Beautiful, isn't it?
Thank God, Unix exists! :)