Tuesday, April 19, 2016

Event bus in android

Before learning how to implement event bus on Android, lets try to understand what's event bus and a scenario where it can be helpful.

There are two similar concepts that we need to understand here
Observer pattern
This is a pattern of development in which your class or primary object (known as the Observable) notifies other interested classes or objects (known as Observers) with relevant information (events). Your class will actually have a list of interested class that it needs to inform.
pub-sub pattern
Objective of this pattern is exactly same as the above pattern BUT, the focus here is on broadcasting the event. It is up to the Observer to receive the information
One of the most important advantage of this anonymity is it is easy to "Decouple" the Observable and the Observers. In this case Observable is the Publisher and Observers are the Subscribers. Hence the name pub-sub.
To achieve this decoupling, we will be using a middleman who handles the communication between pub and sub. Square's Otto and Green Robot’s EventBus

Lets think of a scenario, where event bus can be useful. On a login activity, when a user clicks on login button, most of the time a call will be made to REST server to check the user's authentication. During this background activity, if you are using a third party HTTP client like retrofit, we seldom use AsyncTask class. Retrofit would make all the calls in background in an "async" way. Depending on the result from the REST server we have to take further actions. when all these are happening in background, obviously we need to show user a dialogue that says that we are processing the request. In this situation we can have a "subscriber" who would receive an "event" after the REST call is a success/failure.

I will be using Green Robot’s EventBus here, as it is very simple and has few additional feature than Otto. https://github.com/greenrobot/EventBus/blob/master/COMPARISON.md can be checked for more information.
You can implement Green Robot’s EventBus in 3 easy steps.

  1. Create a POJO with all the information that you need to send form a "publisher" to "subscriber". This is the event that will be broadcasted

     public class MessageEvent {   
        public final String message;   
        public MessageEvent(String message) {   
          this.message = message;   

  2. Define Subscribers. This is as simple as adding a annotation for the method. Add the annotation @Subscribe for a method which will accept the event defined in the above step as a parameter.

     public class MessageEvent {   
        public final String message;   
        public MessageEvent(String message) {   
          this.message = message;   

  3. Post the event

     public class MessageEvent {   
        public final String message;   
        public MessageEvent(String message) {   
          this.message = message;   

Also, do not forget to register/unregister the subscribers on activity start and stop
 public class MessageEvent {   
    public final String message;   
    public MessageEvent(String message) {   
      this.message = message;   

Friday, February 19, 2016

Using your Singtel router as an Access Point

I bought a Singtel router (Aztech FG7003GRV AC) for the sole purpose of using it to extend my wifi. After trying few combination of settings, I was able to rig up the router to work as an access point.
Here are the steps I followed to make the router work
  1. I had a router that is currently serving internet and its DHCP is set from to
  2. IP of my existing router is
  3. Change the IP address of  Singtel router to anything that is not in the range of the IP set by your main router. I set it to 
  4. Disable DHCP on Singtel router, so that the IP addresses are assigned by the main router.
  5. Connect LAN from your main router to one of the 4 LAN ports of your Singtel router. Do not connect to WAN port.
  6. This will set your Singtel router to work in bridge mode automatically.
  7. Try to access wifi from both the routers.
  8. Also try to access the router's settings page of your main router ( as well as your Singtel router (

Monday, January 04, 2016

Use "ButterKnife" to make your life easier

Easiest way to bind an Android view to the class file is to use "ButterKnife". No more long findViewById()
Using "Butterknife" is as simple as the name suggests :)
  • On your app's build.gradle, add "compile 'com.jakewharton:butterknife:7.0.1"
  • To bind  a view element in the class, simply use 
    • @Bind(R.id.btnSubmit) Button btnSubmit; 
  • After setContentView(R.layout.blah_blah_activity); 
    • ButterKnife.bind(this);
For more information https://github.com/JakeWharton/butterknife

Friday, November 20, 2015

"could not find inappbrowser plugin" when using ngCordova and Ionic

You will probably get the error when you have installed Cordova v5.4.0 . To solve the issue downgrade your Cordova to v5.3.3 . Also you can follow https://github.com/nraboy/ng-cordova-oauth/issues/57 to get more updates on the issue.

How to downgrade Cordova can be found in my blog http://rakesh1988.blogspot.sg/2015/11/downgrading-cordova-installing-older.html


Thursday, November 19, 2015

Downgrading Cordova / Installing older Cordova version

Have you run into a situation where you upgraded Cordova and your application broke due to a third party plugin???
Here are the steps to install previous version of Cordova
  • cordova -v  would tell you the current version of cordova installed.
  • npm info cordova  would tell you the available versions of cordova in npm.
  • Suppose you want to install  v 5.3.3 then just follow the below commands
    • npm uninstall -g cordova  This will uninstall the current cordova verison
    • npm install -g cordova@5.3.3 This will install the version 5.3.3
  • Then you need to update your application to point to new cordova
    • cordova platform update android
    • cordova platform update ios
  • You can also update ionic as below
    • ionic lib update

Adding Font Awesome to your Ionic project

To add Font Awesome (https://fortawesome.github.io/Font-Awesome/) to your ionic project here are the steps that I followed

  1. node.js installed (v 5.1.0)
  2. Ionic (v 1.7.10)
  3. npm (v 3.3.12)
  4. bower (v 1.6.5)
  5. install gulp locally using, “npm install gulp”
To install Font Awesome, navigate to your Ionic’s parent directory and use the below bower command
  • bower install fontawesome -s
Above command will place all the files relates to Font Awesome in www/lib (because this is defined in .bowerrc file) directory and update bower.json file with the details of this dependency.

Next, we need to update @fa-font-path variable in www/lib/font-awesome/scss/_variables.scss

$fa-font-path:        "../lib/font-awesome/fonts" !default;

Next, we need to update Ionic’s SCSS file to import Font Awesome. Add below into scss/ionic.app.scss

@import "www/lib/font-awesome/scss/font-awesome"; 

Next, run gulp sass task. This will merge ionic’s CSS and Font Awesome’s CSS. Then, change the index.html to point <link href="css/ionic.app.css" rel="stylesheet"> and remove/comment Ionic’s stylesheet <link href="lib/ionic/css/ionic.css" rel="stylesheet">

Test Font Awesome
<i class="fa fa-google"></i>

Thursday, July 09, 2015

Checking the hash value and average execution time for a SQL id in Oracle

set lines 155 col execs for 999,999,999 col avg_etime for 999,999.999 col avg_lio for 999,999,999.9 col begin_interval_time for a30 col node for 99999 break on plan_hash_value on startup_time skip 1 select ss.snap_id, ss.instance_number node, begin_interval_time, sql_id, plan_hash_value, nvl(executions_delta,0) execs, (elapsed_time_delta/decode(nvl(executions_delta,0),0,1,executions_delta))/1000000 avg_etime, (buffer_gets_delta/decode(nvl(buffer_gets_delta,0),0,1,executions_delta)) avg_lio from DBA_HIST_SQLSTAT S, DBA_HIST_SNAPSHOT SS where sql_id = 'XXXXXXXXXXX' and ss.snap_id = S.snap_id and ss.instance_number = S.instance_number and executions_delta > 0 order by 1, 2, 3 /

Wednesday, July 03, 2013

Lock task bar in windows mobile

I had to do a kiosk mode application in windows mobile for which locking task bar was very much mandatory, as I had to block users from accessing mobile settings.
Here is a small piece of code that I had to use to achieve the same.

#region enable or disable taskbar swipe
public static bool enableTaskbar(bool bEnable)
    bool bRet = false;
    IntPtr hTaskbar = FindWindow("HHTaskBar", String.Empty);
    if (hTaskbar != IntPtr.Zero)
        bRet = EnableWindow(hTaskbar, bEnable);
    return bRet;

[DllImport("coredll.dll", SetLastError = true)]
private static extern IntPtr FindWindow(string lpClass, string lpTitle);

[DllImportAttribute("coredll.dll", SetLastError = true)]
private static extern bool EnableWindow(IntPtr hWnd, bool bEnable);

There is a catch in this code. Every time mobile is locked and unlocked, task bar can be used. So write this piece of code in paint event of the form as below.

private void frmMain_Paint(object sender, PaintEventArgs e)