Android snippet: Hide Android soft keyboard

If you want to hide the Android soft keyboard, eg. when a button is clicked and you want to present full screen to user, use the following short snippet :


SQL Performance finetune – How do you validate if an SQL is efficient

I am hit with a database performance recently and had a hard struggle to analyse and validate the efficiency of SQL.

Few things learnt from this exercise.

(1) What are the meanings of different values in SQL profiler’s trace ?

SQL Trace file field explanation

Field Description
TextData the content of SQL statement executed
Note: you may find entries with NULL TextData, those entries are likely of EventClass: 15-Audit:Logout, this is normal.
StartTime/EndTime the server time when this query starts / end
Duration the actual elapse time to run this query. This value is measured in micro seconds (1s = 1mil micro secs), so need to devide by 1,000,000 to get the duration in seconds.
Duration = EndTime – StartTime
CPU the amount of time this statement consumes CPU resources. This value is measured in mili seconds, so need to devide by 1,000 to get the duration in seconds.
The higher the worse.
Read discussion point #3 below for more description on Elapse time vs. CPU time .
Reads the number of logical reads incurred by the query. The higher the worse. 

Excessive “Reads” may indicate a few things:
a. tables in the query are not indexed properly (Solution: add index)
b. index statistics not updated (Solution: update statistics)
c. there are criteria in join conditions or “where” clause that are too broad.
(Solution: revalidate the join condition & where condition.
>> Change to dynamic query if need to.
>> Change from subquery to join if need to.
>> Use EXISTS instead of join, if need to

Writes the number of physical writes incurred by the query. The higher the worse.
EventClass 15=Audit:Logout 

10=RPC:Completed(Occurs when a remote procedure call has completed)

>> Elapsed Time = CPU Time + Waits

Waits can include reading from disk, waiting on locks, waiting on latches, and waiting on the client to fetch rows.

>> “Elapse time” might not be a good yardstick, use CPU time instead as a measurement


(2) How do you measure if an SQL statement is efficient ?

>> turn on IO and Time Statistic in SQL Studio (just for query purpose) and execute your SQL query -> then the statistics under tab “Message”

Source: Brent Ozar


(3) Why CPU time is more than Elapse time ? eg. CPU Time 3256ms, Duration 1792ms

If you see this, it means that your DB server has multi CPUs and your SQL statement actually consumes more CPU resources than it looks.

On the look, the statement may seems to take only ~1.8seconds to complete. However, it actually consumes approximately 1628ms (~1.6s) of CPU time on each CPU (assuming your server runs on dual-CPU processor)

Android code snippet: Press Back button to exit activity

I am sure you have seen a number of Android apps allowing user to press Back button to exit the app.


This is particularly useful when you don’t need to implement a separate button for the purpose of closing the app, yet you can implement all the necessary clean up actions upon app exit.

Here is the short snippet for this back-press-to-exit behavior:

Draw frame for a photo in GIMP

GIMP is an image editor and to my surprise, it doesn’t provide a very straight forwards way to draw simple shapes like rectangle, circle, straight line, etc..

I need to add a frame around my photo and there is no easy way to just draw a simple rectangle for that purpose. Searching around the Internet finally gives me some clue, this is how to achieve it :

GIMP -> Filters -> Render -> Gfig -> choose the Rectangle/Circle shape for my frame



ADB with Genymotion – “adb server is out of date. killing…”

I installed Genymotion to use for my Android development.

After Genymotion is installed, every time I try to run adb, I am hit with this message :

Turn out this is due to the fact that there are 2 versions of adb now on my machine, 1 from android sdk, and 1 from Genymotion.

So to stop this, the simple solution is set Genymotion to use Android SDK’s adb instead of its own.

Goto Genymotion -> Settings -> ADB -> Use custom Android SDK tools -> then point to [/Users//android-sdks] (I am on Mac)

Voila !

Tricks in Microsoft Outlook

  1. Search emails from a sender: use label “from”, eg. “from:Mary” search for all emails sent from Mary
  2. Search unread emails: keyword “read:no”
  3. Search attachments using extension : keyword ext:[extension], eg. ext:docx, ext:(docx OR pdf), ext:(docx NOT pdf)
  4. Search any email with attachment: keyword hasattachment:yes
  5. Search by date range: keyword received:[date] eg.received:yesterday, received:today, received:=’2015/1/5′

Install custom ROM KitKat on my Nexus S

Disclaimer: This guide is taken from multiple sources and for my own use
I do not write this guide.

Step by step for Kitkat ROM installation: article from AndroidGeeks
Reproduce here :
1. Download the custom ROM build. I use AOSP 4.4.3 this, taken from xda-developers

2. Download Gapps for this ROM from here.

3. Connect your Nexus S with your PC, then move both zip files on your device’s internal storage.
– disconnect the device from computer when all files are copied successfully.

4. Power off the device now.
5. Boot it in CWM Recovery Mode now. Here is another guide showing how to do so. I ended up using the 3rd method (via adb) .

6. First you need to create a full NANDROID Backup of your current ROM by selecting ‘backup and restore’ option.
– save the file on the Nexus S internal storage, then continue to the next step.

7. Now, apply the factory reset:
– select ‘wipe data/factory reset’. Confirm the process.

8. Install the custom ROM zip by selecting ‘install zip from sdcard’, then ‘choose zip from sdcard’.
– find the AOSP Android 4.4 custom ROM and confirm its installation.
=> issue encountered in the 1st run :
According to this StackOverflow thread:

In short, if you want to flash KitKat Android 4.4, you’ll have to upgrade to a version of CWM greater than v6.0.4.5. Or, use an alternate bootloader.

Mine was CWM v6.0.3.1 => need to upgrade to the latest v6.0.4.3
Download from ROM-manager, latest version at the time of writing : v6.0.4.3
Once downloaded this CWM image, copy it over to my sdk/platform-tools/ folder, and then execute this :

fastboot flash recovery clockworkmod.img

where clockworkmod.img is the filename of your image
My first attempt failed with some error in [Invalid parameters], I just re-download the image and it worked fine
Once flashing completes, from CWM, select **Recovery**, your phone should enter CWM v6.0.4.3 now.

Btw, make sure your device is connected by firing : fastboot devices
If your device is not visible to fastboot it is probably due to a few common reasons:
a. phone is not in fastboot mode -> need to reboot into fastboot mode -> for Nexus S: To enter into Bootloader/Fastboot : (while turned off) Press and hold Volume Up + Power
b. missing driver -> for Nexus S, install driver following these steps:
i. Download Google USB Driver, latest one here, or direct link
ii. Update the driver by : Device Manager -> right click and select “Update Drives Software”
2014-10-29 17_03_27-Clipboard
Browse my computer for driver software -> Let me pick from a list of device drivers on my computer -> Android Phone -> Have Disk, then browse for file android_winusb.inf (in SKD folder) -> Android ADB
Windows will prompt some warning, just proceed.

9. After the ROM is flashed, repeat step 8 actions and flash the Gapps zip too.
Note: 1st attempt fails due to insufficient space (image)
Download the micro GApps version here, or direct link

10. Return to the main custom Recovery screen when both files are flashed successfully.
11. Reboot the phone.
– note that the first boot process takes several minutes to complete, which is why you shouldn’t press any buttons until all files are loaded.

12. Enter your Google Account details when the home screen appears.

That’s it, now you need to take control over your device and start testing all the new features.