Category Archives: Android

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.

ADT Graphical Layout tool and SCM systems

Well, most of you android developers encountered this problem. The graphical layout designer in the ADT (Android Developer Tool – the eclipse plugin) creates every element in the XML as one line. You might ask, why is it a problem? well, in most cases, it is not. But there is one annoying thing that makes this feature/bug or whatever you want to call this a real time consumer. Unless you are a solo Android developer that saves his code in a local or remote storage as is, you probably use some kind of SCM (Source Control Management) system (e.g. git, svn, cvs, tfs, clearcase and the list goes on and on). Most (if not all) of these systems and their diff tools (also most of the diff tools I encountered) use line comparison. They tell you if you added/removed/edit a line. In case you edit a line, it marks the parts of the line you edited. This is all nice when you edit the text code yourself. But in the GL (Graphical Layout) you don’t. And then, if you changed one property of an element using the GL, you will see it in the SCM as an edited line. The thing is, now you need to go over the whole element (that sometimes has several properties) and see which property exactly you changes. And let’s say, for example, that you changed two properties. One is at the beginning of the line and the other at the end. How easy it would be to miss that second property? Basically, what the GL does is to eliminate many of the advantages of SCMS.

In the past, I usually chose between two options:

  1. Not use the GL (not always possible since I don’t remember all the properties by heart).
  2. After using the GL, go over the file and add line breaks manually.
I wanted to use the auto formatting tool of eclipse, but it always looked bad. Here it how it looked before:
And After:
After a bit of digging in the eclipse preferences by my colleague he found the solution: going to preferences -> XML -> XML Files -> Editor and checking the “Split multiple attributes each on a new line” and after pressing Ctrl+Shift+F voila:
No more hard work setting the text to align as i want it.

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.