Monday, December 12, 2011

Real World Example : Simple Publish/Subscribe Pattern with Reactive Extensions

Shown below is a requirement.

Let me explain a little about what I want to achieve. I have an entity called “Frequent” which implements INotifyPropertyChanged interface. So I can subscribe to the change notification and then upon firing the event, I simply increment a counter. Now, my requirement is that the PropertyChanged can fire way too quickly as simulated in the for-loop. So in UI applications, it doesn’t make sense for me to fire property changed events that frequently as in a real world intensive WPF application, this can become your biggest bottleneck (among several others things).

So my requirement is that, within 1 second, I would like to receive only 1 change notification per property, if it has changed indeed. The simple implementation for this would be to do something like pushing the properties that have changed into a queue and process the queue once a second and then raise change notification event. This can be done very easily but then it can be a little tedious job for such a requirement to restrict an event from firing more than X times a second.

So I was looking at the Reactive Extensions (Rx Framework) and it occurred to me that it should support Observer pattern out of the box. So I started playing with it but hit a road block immediately. There are numerous resources that shows how to generate observables from Timers, Time Intervals, Enumerables, etc but what I want to is to take advantage of extension methods supported by Rx Framework on IObserver such as Throttle(), Buffer(), Window(), etc on a simple Pub-Sub system. May be I did not look hard enough but I could not find a simple example. So I thought it would be helpful for rest of the people like me if I made a blog post.

In my implementation, the Frequent object is by itself a Publisher and a Subscriber. In Rx terms – IObservable and IObserver. Instead of implementing these interfaces, I want to have some observable of strings where I will publish a property that has changed and the subscriber on the observable would receive it. So for that purpose of simple message passing between Observers and Observables, you can use a Subject<T>. There are other variants like ReplaySubject<T>, BehaviorSubject<T>, AsyncSubject<T>, etc. but that is out of scope of this post. I publish on Subject<T> and then the subscribers who subscribed earlier to it would be notified of these messages.

Shown below is a the Frequent class implementation.

Take a minute or two to go through the simple class, it is self explanatory. All I am doing is that when a property changes, instead of firing it immediately, I simply notify my throttler about the property that has changed. The throttler’s responsibility is to determine when to notify the subscribers about this change. Shown below is the implementation.

You can read my comments that I wrote out of frustrations. All I do in this simple façade is to wrap an Observable –> Subject<string> and buffer the messages received for 1/2 second and then get unique messages from the buffer and publish it to the subscribers. This is all done in the constructor. The other methods are simply my take on making things simple for the consumer of this class.

Rx is complex and powerful but in my opinion it has a very steep learning curve. But it surely did save a good few days of implementation for my team. Again, I am very new to Rx, so if you have some better ways to do it, then please let me know.

Friday, December 09, 2011

Hannspad Hannspree SN10T2 – Rooting, Android Market and Some Crazy stuff

Recently the Android Tablet bug bit me which led me to buy Hannspad Hannspree SN10T2 from AliExpress.com. The order was made on the day of Thanksgiving and I received the tablet within two weeks. The whole goal was to get a cheap tablet on which I can try installing custom ROM, may be try and develop some applications. But turns out it is not that simple. See the Slatedriod.com forums has custom ROM but for SN10T1 model but not SN10T2 model – which runs on two different processors. So all the cool stuff I read about that people do with SN10T1 is not applicable for SN10T2 – example being booting the tablet in recovery mode. There are not hard volume buttons that lets me boot into recovery console. I tried all different combinations instead of missing Volume + button but the recovery key sequence remains unknown.

Connecting the Hannspad Tablet to Windows 7 x64

Hooking up the USB cable to the Windows 7 machine doesn’t really install the drivers. You can still be able to copy files around once you enable the USB File Transfer on the tablet. If you are new like me, just drag the notifications bar on top of the tablet and the instructions then are clear enough for you to know what to do. So first time you hook up the tablet, you get the message saying OMAP-3/4 driver is not found. Don’t even fight it, there is a really simple way to install the driver for the tablet.

  1. Download Super One Click from Shortfuse.org. The direct link to what I am using is here. It is version 2.3.1.
  2. Extract the zip file to some place you have your portable applications. I put it in temp but now I have a problem deleting the folder because driver was installed from this location??
  3. Launch SuperOneClick.exe and then click on Advanced Tab.
  4. Click on Check Driver.
  5. Then say OK for installation of the driver.
  6. Accept Windows warning about unsigned driver and move on.

What this driver lets you do is to let ADB (Android Debug Bridge or visualize like a remote shell access for your tablet unix system) identify that this device is hooked up and running.

Rooting the SN10T2 (may work for other tablets too!)

If you search for information on how to root the SN10T2, the first thing you see is the article by Sir Shagsalot (Winking smile). It has precisely what you need to do in order to enable Super User access for the programs running on your Android Tablet. Since the information is not so eye-soothing, I will try to replicate the information. But be aware

  1. The information here is from the article I mentioned above plus my experience struggling with the Tablet.
  2. I am not an Android Expert – I am just a software developer who writes software for a living mostly on Windows Platform.
  3. Don’t blame me for any physical or mental or financial pain that may cause as a result of following this information.
  4. Rooting implies the warranty is void. You cannot bitch about your tablet to your vendor anymore. Not my problem again Smile

Option 1 : Using the same SuperOneClick

If you followed the previous steps, you can use SuperOneClick to Root your device.

Option 2 : Using z4Root 

  1. Download the z4root.apk and place it in your SD Card on the tablet.
  2. On the tablet, go to Menu->Settings->Application Settings and check the option for Unknown Sources.
  3. Now go to Home->Applications->ES File Explorer.
  4. Navigate to the z4root.apk wherever you copied it.
  5. Install the APK file.
  6. Then open the z4root application.
  7. You will see two options –> Temporary Root or Permanent Root. If your application is already rooted, you will see the option to Unroot.
  8. I selected Permanent Root.
  9. Then wait until you see “Rebooting..”. If the tablet doesn’t reboot in say 10 minutes, I would not bother to wait. Just restart the tablet manually. It is fine!

The Android SDK Tools

Basically, rooting gives you Super user access and lets you access the shell from ADB console. The super one click application distributes the ADB console application if you poke inside the installation folder.

Anyway for you to officially get the ADB application, you can download the Android SDK from here. 

  1. Download the Android SDK.
  2. Download the Java Development Kit, if you don’t have it already. You need JDK not JRE.
  3. Install JDK. Install Android SDK.
  4. Now in the Android folder (C:\Program Files(x86)\Android\android_sdk\), launch the SDK Manager application. See image below.
  5. You need to check “Android SDK Tools” and then click “Install Packages”.
  6. Once done, you will find a folder called “platform-tools”.

image

Some fun with ADB.

  • Open command prompt and assuming platform-tools is in your PATH.

    (Of course, the table is connected to the machine, USB Debugging enabled on the tablet).

  • Listing all the devices – adb devices
  • Opening the Unix Shell for your tablet – adb shell
  • Copying files from your PC to the device
    • For SD Card, you don’t need to mount as it is already mounted for “Read-Write”. Just run the command -
      • adb push c:\myfiles\superdocument.pdf /sdcard/
    • For copying to /system/app folder, which you may have to do for manual installation of Google Apps and Market
      • Go to the shell of tablet using “adb shell”. Now inside the shell,
      • Mount the /system using the command
        • mount -o remount,rw /dev/block/mmcblk0p5 /system
      • Copy the files you want to push to /system/app, using
        • push c:\files\superapp.apk /system/app/
      • Unmount the /system using the command
        • mount -o remount,ro /dev/block/mmcblk0p5 /system
      • Make sure you unmount once you are done.
  • Reboot the tablet from Windows – adb reboot
  • Copying files from your tablet to the PC – adb pull \sdcard\super.pdf c:\temp\goodbook.pdf

Installing the Android Market on the SN10T2

You can follow the original instructions from top to bottom that Sir ShagsALot wrote here. I will just use the ADB to install the Android Market application.

  • Root your tablet either using SuperOneClick or Z4Root as described previously. Reboot your tablet.
  • Download the required Google Apps distribution from the link mentioned in the article.
  • Extract them to some place on your machine.
  • Make sure ADB is in your PATH. Open the Command Prompt and navigate to the folder where you extracted the googleapps.rar archive.
  • You should see something like shown below.

image

  • Navigate the command prompt into the “app” folder.
  • Using the ADB shell, first mount the /system on your tablet. Then run the following commands to push APK files to /system/root/
    • c:\temp\Working Google Apps\app> adb push GoogleServicesFramework.apk /system/app
    • c:\temp\Working Google Apps\app> adb push OneTimeInitializer.apk /system/app
    • c:\temp\Working Google Apps\app> adb push SetupWizard.apk /system/app
    • c:\temp\Working Google Apps\app> adb push com.android.vending.apk /system/app/Vending.apk
    • Notice the Application for Market is renamed to Vending.apk (don’t think you need to do this, but everyone else uses Vending.apk) Smile
    • Shown below is the command prompt snapshot for your reference

image

  • From the above you should have understand by now that I am only interested in making the Android Market work.
  • Don’t forget to un-mount the /system and also reboot the device as soon as you are done copying.
  • Once the tablet boots, you will be seeing a welcome screen.
  • Just go through the welcome screen and add your google account.
  • Now go to Market and make sure it runs.

Some issues that I had

  • ES File Explorer –> Menu –> Settings –> Root Options. Check the “Root Explorer” failed with message "sorry, test failed. This feature cannot run on your phone” message. So what is the alternative ? Use ADB, it might sound intimidating but it is just a simple Shell prompt. If you are messing around your tablet, most likely you are comfortable with the command prompt.
  • There is a great tool called “File Expert”. It is one tool that impressed me a lot! It is similar to ES File Explorer but much better looking and much easier and intuitive to use. You can upload files from your PC without having to connect your tablet.
    • Install File Expert. You can get it from the App Center that ships with the SN10T2 as well.
    • Launch File Expert. Go to “Share my contents”.
    • Just touch on the “Share via Web” option. Viola!
    • Now you should see a brief message explaining how you can access your tablet SD Card from the Web Browser.
  • Uploading multiple files from File Expert Web UI. You cannot upload more than one file when you are in the root of the SD Card. But create one folder and then you would be able to upload multiple files simultaneously.
  • MOUNT Options in File Expert does not work and can make you believe that it worked. So what is the alternative?? Use ADB.
  • If you manually move some files to /system/app in File Expert of ES File Manager, you may believe it was successfully copied, but they aren’t really done. You don’t see an error that /system has to be mounted.
  • If you are messing with different versions of Google Apps, the one in the link by Sir ShagsALot is the only one I found to be working. If you mess up, then remove the APK files from the /system/app manually.
  • I copied bad APK files and was stuck with Emergency dial screen. So what I realized later was that SetupWizard was badly renamed. So I removed the APK files and copied them keeping the name intact.
  • Again, if you are stuck with wizard screen and reboot doesn’t help, use ADB to delete the APK file.
  • Android Market app is installed but launches and dies immediately. You need to make sure GoogleServicesFramework.apk is properly installed/copied into /system/app folder. When in doubt, repeat the process after taking some break.
  • Android Market App launches but cannot download application. The application page seems to be stuck at “Starting download…”. This only happened when I installed incompatible version of Android App Market.

So these are all the things that I struggled with this week and I hope anyone like me who is new to Android Rooting may find this information helpful. By the way, don’t forget to check out SlateDroid. They are awesome!