Category Archives: Mobile

5 Things I Learned from the AQuery Source


This is a tribute to Paul Irish’s excellent video 10 Things I Learned from the jQuery Source. I think it is an great video and a good lesson for every developer – get to know the libraries you work with, and if they are open sources, get familiar with the source code. I already posted in this blog about Backbone.js and specifically on Backbone.js Views events. I could not accomplish that w/o getting my hands dirty in BB source code. Also, as Paul points out, this is a great way to learn new tricks. These open source projects are developed by some of the brilliant minds of the industry and there is a lot to learn from these guys.

So w/o further ado, let’s get down to business.

AQuery – you misspelled it

AQuery is (as taken from the AQuery Website): “inspired by jQuery, a javascript framework for web site, hence the name “Query”.” The difference is that AQuery is a library intended for Andoid developers and as such is written in Java.

I first came a cross this framework a couple of days ago when a colleague pointed it up to me. It immediately caught my attention and my team’s Android developer took it on a test drive the day after. We saw immediate results in performance boost and our code became much shorter and simpler. We only just started using it for image downloading and it is amazing. Till now, we implemented a whole “Download manager” component that became more and more complicated over time. With just a few lines of code we managed to make this component redundant and enjoy AQuery doing all the “heavy lifting”.

Inspired by Paul Irish’s video and approach, I started looking at the source code of AQuery, and I saw that not only they did an amazing library, the code is just equally great (thinking of it, it kinda make sense that a great library will be well written). This is how this blog post came to life. Hope you’ll enjoy reading it as much as I enjoyed writing it. I also hope that some of the things will give you new ideas about Android and Java programming.

1. Constants in Interfaces

This is an awesome way to avoid putting constants that you need to share across several classes in one class. I saw this in the AbstractAQuery class that implements the Constants interface. At first I thought: “What an odd way to name an interface”. Then it occurred to me that in Java, interfaces can contain constants, so why not use them this way instead of putting the constants in one of the classes and accessing them from all other classes?

It is important to note that I saw that the Constants Interfaces is considered bad practice (at least by this Stack Overflow post).

2. Generics and Object Creation

To extend AQuery, you need to write the following code:

class MyQuery extends AbstractAQuery<MyQuery> {…}
This, in turn, creates a new MyQuery object each time you use one of the “selector” function such as id. The reason this can be accomplished is the fact that you pass MyQuery as the generic param for the implementation. This is a nice way to implement extend ability.

3. Make it Green – Recycle

This is a very important practice that I already know from the adapters. When using adapters, one of the important parameters in the getView method is the convertView. This enables us to reuse an existing view (that is out of the scope of the screen) instead of creating a new one. The AQuery team also did an optimization that enables you to reuse the same AQuery object in an adapter.

4. Common Listener

Again, a nice trick they use to avoid creating multiple classes. The Common object implements a variety of listeners. This enables them to use the same invoke method for all the listeners. Also, it enables them to use that class as a file comparator, runnable and many other things.

5. Use of Weak References for Handlers

In the AJAX requests, the handlers are saved in a weak reference. This way, if the request is returned after the activity was destroyed, the activity will not leak. This is a very important practice to avoid leaking activities.


Mobile Web Debugging

In my current job, I became part of the FED (Front End Developers) community.
In Android there are great debugging tools, and the tools for FED development are improving more and more. I got to know WebKit’s Web Inspector (in Safari and Chrome) and learnt how to use them.
But when running the same web app on mobile devices, I encountered a wall… Suddenly I was compeled to use old fashion debugging techniques like print to log and alerts. Of course some devices don’t natively support even that and you need to implement your own print to log mechanism (fun fun fun).
But help came in the last few days in the name of Weinre. This is a great Web debugging tool that lets you remotely debug your mobile web application with most of the functionality of Web Inspector. Of course, since the “debugger” on the Web App side is actualy a JS script, you can’t put breakpoints and stuff like that, but it eases the working process tremendously.
So enjoy and good work Weinre guys…

Android Web Fragmentation

Everybody talks about Android fragmentation problem. Meaning, each device manufacture creates its own flavor of Android OS. The problems starts when they customize heavily leaving the developers struggling with changes in Application look and feel, and worse, in behavior. At WeFi I saw that a lot, when each device’s WiFi driver reacted differently though it had the same API for the developer. There is also fragmentation in the browsers arena, but I naively thought that choosing the browser as the platform will reduce fragmentation head ache. I couldn’t been more wrong! Not only there is difference in behavior between browsers in different Android devices running, what should be, the same OS version, there is a difference between the behavior between the WebView and the browser on the same device. I encountered performance variations, or to be exact, the WebView is much slower than the browser. This is due to different cache and memory handling (can’t take the credit for finding that out). Here is the link to the guy that posted a very interesting answer regarding performance boost to the WebView. Enjoy and happy fragmentation.

Android Soft Keyboard and WebViews

When adding input text area to an Android WebView, you don’t get the behavior you expect. The webview jumps up and down when the input text area is focused since the Soft Keyboard is opened. Also, you JavaScript code does not get notified when the keyboard is hidden (only when it is shown). I tried listening to window resize event, but with no luck. Looking for a solution to my problem I encountered this cool post. Simple and elegant. 10x Michael.