Android: record screen cast like a breeze

Record screen video can be super easy. First, put below lines in your .bash_profile file. Then type the command screenrecord demo_video_name. When you are done, press Ctrl + C to stop recording. Easy, huh!

Please note: Android 4.4+ (KitKat or newer) is required.


Posted in android | Tagged | Leave a comment

Android: capture screenshot with one line of adb command

Just one-liner to capture android screenshot:

Posted in android | Leave a comment

Tutorial: Create android gradle project with dagger

It is tricky to set up multi-module projects with android & gradle, if you are new to it.

It is even trickier to make it working with Dagger. A bit background knowledge: Dagger is an open-source dependency injection framework designed for Android, by Square Inc. Other open-source option is RoboGuice.

Here is the tutorial, which you can use it to quickly kick off your projects. I’d recommend to run it with latest version of Android Studio or gradle or gradle wrapper. I haven’t tested it with other situations yet. Hope you enjoy it!

Important Notes:
1. I learned these pieces of knowledge from multiple places, including Dagger codebase. Please let me know if I miss a link to your shared tips.

2. I don’t use dagger at my everyday job. Therefore, please kindly let me know if any issue or improvement or pull requests. Enjoy!

3. I may add more inlined comments later. Comment here, if you have questions.

Posted in android | Tagged , , | 12 Comments

Gradle & Dagger: Add dagger code generation to android library and app

Edit: A newer and simpler method, is published in this blog post. Recommend you use that method.

Before gradle better support annotation processors, you need a workaround to make: 1). dagger code generation working with android project; 2). Make really reusable dagger Modules if you have multiple APK projects.

There is a solution here Issue #319. However, it doesn’t address requirement #2.

So here is my solution. First, the library build.gradle:

Then for the APK project, the setup is pretty much clone of a solution in Issue #319. Here it goes:

Posted in Uncategorized | 2 Comments

Technology Touched Over the Weekend

Haven’t dived into new technologies for a while. Did several interesting things:

  1. OAuth 2.0 Provider Implementation – Implement both authorization provider and resource provider on Tornado, using oauth2lib.
  2. Redis – Used to store/retrieve OAuth codes and tokens.
  3. Social login – Refactored social logins so that it uses event bus to communicate the states
  4. Basic skeleton of Android API client and service
  5. Retrofit – An awesome RESTful client.
Posted in Uncategorized | Leave a comment

Speed up static resources with AWS CloudFront CDN

Nowadays CDN is easy to use. Even a startup or personal website shall consider use CDN to speed up static resources, such as photos, javascript/css files. Here are simple steps to use AWS CloudFront CDN and enable it in Tornado.

Enable AWS CDN with Custom Origin option

  1. Open CloudFront in AWS Management Console. If you don’t have AWS account, create it at
  2. Click ‘Create Distribution‘.
  3. Select the option ‘Download‘. Click Continue.
  4. Set ‘Origin Domain Name‘ to your web server domain.
  5. Set ‘Forward Query Strings‘ option to ‘Yes‘. This is because tornado can generate a hash code for each static resource, for best cache performance.
  6. Set other options accordingly. Click ‘Create Distribution‘. Your CDN distribution is ready to use, once its state becomes Enabled.
  7. Record the Domain Name field. We will use it soon. It normally looks like

Enable it easily with python tornado

  1. First, apparently we don’t want to use CDN in dev mode. To easily switch between dev and production mode, customize your base RequestHandler like below

We override the static_url method here. Custom the is_cdn_enabled method. Let it depends on the production mode. Then it will automatically employ CDN in production deployment. Enjoy coding!

Posted in Internet, python, Server | Leave a comment

INSTALL_REFERRER broadcast helps to track android installation referrer

Many businesses ask you how you hear about them, when you first visit them. Because the data is important for growing their businesses.

OK, you have an android app. Aren’t you interested in tracking how the users find you? For example, via facebook posts, tweets, forum threads, or partners’ blog posts?

How to track it? The answer: use the INSTALL_REFERRER broadcast.

How does it work? You pass encoded data to Google Play app, via a special link query parameter (i.e. referrer). Once the users click the link to Google Play store, and then installed your app, Google Play sends your app a broadcast, passing back the referrer data.

You can put in many information into the referrer query string, such as ad campaign name, channel names, affiliate names. You may put in other data too. Use your imagination. Of course, do not put in sensitive data like your salary amount. ;-)

For details, visit Campaign Measurement – Android SDK.

Posted in android | Leave a comment

How to display FPS (frame per second) number in Android

Well, check out this blog:

Window Backgrounds & UI Speed

Posted in Uncategorized | Leave a comment

Deploy OAuth provider with mod_wsgi

Recently I deploy an OAuth provider with apache/mod_wsgi. Everything works fine on django dev server locally, but is broken onto apache server. After analyzing the logs, it shows that the HTTP header ‘Authroization’ is dropped by default.

To pass thru OAuth parameters to mod_wsgi applications, you need to use the WSGIPassAuthorization directive. An example is like below, at the same level of ServerName directive:

Bear in mind that the ‘Authorization’ header will be passed thru and renamed to ‘HTTP_AUTHORIZATION’. You need to do such modification if you use python-oauth2 like me. I plan to submit a patch, in order to contribute and learn about git as well, LOL.


  1. WSGIPassAuthorization Directive
  2. mod_wsgi: Access Control Mechanisms
  3. My patch for python-oauth2
Posted in Server | Tagged , , , | Leave a comment

Many profilers, and I need at least one

Performance improvement is an ongoing task, always. So a profiler is a good friend. For me, the very basic tool is timing and print functions, lol. Yes, they are. In one of language tool, it helps to identify the parsing phase takes 60~80% of the time. The granulatiry is coarse (not down to function level), but very useful. Later I introduced multithreading for parsing. It’s very useful to run one parsing thread on each CPU core.

This week, I tried 2 C++ profiler. ‘Very Sleepy’ and VS profiler. I like ‘Very Sleepy’. It helps to identify a hash table implementation in a runtime module. It is mostly from key collision, and we got significant performance improvement after tuning the codes.

For Java, last year I tried eclipse TPTP and yourkit. TPTP sucks. Yes, you wasted hours and lose patience before a running finished. Normally it takes 30+ seconds for each running. Maybe TPTP is not good at profiling a recursive-descedent parser. Then I tried yourkit and quickly identified which parser rule was causing the problem. After the fix, the time dropped from 1200seconds to ~4 seconds in doing 22 nested if-else-if-else statements. Pretty exciting profiling experience.

4 years ago we profiled a C# web app. I forgot which profiler we used. Finally we improved the performance by trade-off in UX/speed + more offline computing.

Haven’t got experience with profiler in other languages. But so many profilers, I need at least one. :)

Posted in Engineering | Leave a comment

Another Way of Javascript Method Overloading

In John Resig post, he presented a subtle method to support method overloading. It’s very useful when you have method overloading and each method behaves differently.

However, it adds overhead by wrap up existing methods inside a new function. For performance consideration, we check the length of arguments to execute different logics. It’s a more efficient and simple way to do the overloading. Examples:

Posted in javascript, Web | Tagged | Leave a comment

Mercurial: How to ignore everything in a folder?

I have been looking for this for a while. It’s pretty simple. Let’s say you have project A, and want to ignore 2 folders: bin and output.

1. In your project folder, edit the file ‘.hgignore’, or create it if non-existing.
2. Add the following lines:

That’s it. You are all set.

Posted in Engineering | Leave a comment

Implement Unsigned Right Shift (>>>) if the Operator is Unsupported

If you are Java/Javascript/AS3 developer, you know what the >>> operator does.

It is called unsigned right shift, or logical right shift. Check the definition in wikipedia.

However, C# and C++ don’t have such operator for signed integers, though it is available for unsigned integers. Here is one of my implementation. Hope it helps if you looking for such code:

Posted in Engineering | Leave a comment

Get the real client IP address, when you use a reverse proxy

Let’s say you are running ASP.NET, PHP, JSP, or Python web. If the code is visited via reverse proxy, then the client IP address is the proxy IP with regular detecting routue. To fix the issue, let’s first take a look at 3 HTTP headers added by proxies:

The IP address(s) of the client. If there are multiple proxies, you see multiple IP addresses, separated by comma. For example:

X-Forwarded-For: client1, proxy1, proxy2

The original host requested by the client in the Host HTTP request header.

The hostname of the proxy server.


The Solution:

It’s pretty straightforward. The flow is:

  1. Check if there is X-Forwarded-For in http headers.
  2. If yes, get the string, and split it by comma. Then get the first piece.
  3. If no, fall back on regular way of reading IP address.

That’s it.

Posted in Server, Web | Tagged , | Leave a comment

Base64 Encoding in Android 2.1 or Earlier

Base64 is common used. Now Google added a utility class android.util.Base64. Unfortunately it is only available in Android 2.2 and later.

So, you need just a little bit work to use it with Android 2.1 or ealier. Yes, just copy and past the source of Base64 into your project. Make sure to preserve the Apache 2.0 license section, in order to conform to the license. The source code is provided at the end of this post, for your reference.

My disclaimer: I am not responsible for any result in doing the practice described hereby. Read the license if you need.

Now let’s see example usage:

The output is:

Encoded: dGhpcyBpcyBhIG1lc3NhZ2UgYnkgZnJhbmsu

Decoded: this is a message by frank.

Let’s try decode the message in python:

You will get the same input message htere. Now you are ready to send your data to anywhere, like app engine, you know!

The android.util.Base64 source

Posted in android, java, python | Tagged , , | 3 Comments