My little software warehouse

Random thoughts about my experience as moonlight software developer.

Intellij, Robolectric and Android



I have been curious about intellij since droicon uk 2011, where I noticed that some of the speakers were using IntelliJ instead of eclipse.

A post from Richard Hyndman, who is a developer advocate from Google, was the final straw that pushed me to download IntelliJ and give it a try.

The first thing an eclipse user will notice in intellij is how slick and fast compares to the behemoth which is eclipse. On the other hand, an you can feel a bit disoriented at first.


Setting up a new android project using IntelliJ is pretty straightforward. You can follow the nice tutorial from jetbrains website http://wiki.jetbrains.net/intellij/Android.
For this reason, I will not write about how to set up an Android project in intellij here.


What I am going to write about is how to setup an android project with intellij and integrate robolectric with it.

I really like robolectric. It makes unit testing for android a lot faster as it would be on the emulator. What’s more, you can use mockito with it.
Mockito is a mocking framework that makes a lot easier to mock your java objects.
Mockito does not work with dalvik jvm, but robolectric makes your tests run on your pc’s jvm.


Despite robolectric provides instruction on how to setup an intellij project manually, I have tried that process a lot of times, carefully checking if I was missing a step.
However, I did not manage to get it working. The guys from Pivotal provide also an intellij starter project, which you can configure to point a git repository https://github.com/pivotal/AndroidIntelliJStarter


However, I was not either satisfied with this solution and, inspired by this post , I tried to setup a maven solution and to import it in intellij afterwards.

This provides another great benefit, which is the fact that I can import the same project in my linux netbook without bothering for relative paths in eclipse projects.

So, here are the steps I had to follow in order to get it working:

- Install the android sdk. This is trivial, I assume you are comfortable with that
- Let the ANDROID_HOME environment variable point the path where you uncompressed the android sdk
- You need to have at least one version of the sdk installed, so if you haven’t yet, you should launch the sdk manager that is the “android” executable on ANDROID_HOME/tools/android. Install also the extras, such as the compatibility library, admob stuff and all the libraries you find at the bottom


- install the maven android sdk deployer you can find at https://github.com/mosabua/maven-android-sdk-deployer and then install it.

I read somewhere that you can get android jar without relying on it, but in any case, if you need google maps or other private apis like that you still need to use this tool.
 I use the latest (almost) version which is 4.0.3, so I run mvn install -P 4.0.3


- Install maven android plugin. The easiest way is to use android archetypes from here:
https://github.com/akquinet/android-archetypes


Finally, you are now ready to generate your first maven android project:





mvn archetype:generate \
  -DarchetypeArtifactId=android-quickstart \
  -DarchetypeGroupId=de.akquinet.android.archetypes \
  -DarchetypeVersion=1.0.8 \
  -DgroupId=your.company \
  -DartifactId=my-android-application



You could find an issue.  Recently the tree of the android sdk folders has been slightly changed. Maven android plugin expects them to be in the updated version (I don’t remember what the differences where). However, if maven complains it cannot find android related stuff, and your sdk has been installed for some time, a good option would be to remove the sdk and reinstall it from scratch (at least the part you download with the sdk manager).



At this point, you have a working maven project, which you could already open with intellij. What is still missing is the robolectric dependencies.
Nothing more easy, all you have to do is to add




right below android dependencies. You should also create the test folder, under src/test/your/package/

Now, a problem I had was that I had to change the pointer of the repository in order to have robolectric 1.2 working.









At this point, you should be able to open the pom.xml with intellij.


Now, to have robolectric working from intellij, you still need to make a couple of changes:
- the order of include of jars does not respect the one you specified. What’s more, you will likely to get the STUB! error because android jar is included before junit and robolectric.
All you need to do is to move the android jar below junit and robolectric

- you need to manually specify the javadocs for android. You do this by highlighting android jar, pressing the little edit in the lower left corner.




and pressing the plus sign. Then you choose “Attach files or directories”, point the path related to your android docs,
which is /docs/references under your ANDROID_HOME. Finally, you declare that it’s related to javadoc.




Now you are _almost_ ready to start. You made all this mess because you want to have robolectric working with intellij.

You need to create, right under src/ folder, the following path:
src/test/java/ YOUR_PACKAGE 


where you are going to include your unit tests, and you are done!


If you liked this post, please consider following me @fedepaol .







Comments

Federico Paolinelli
I added here https://dl.dropbox.com/u/3092639/pom.xml a working pom.xml I am currently using. I just tried to mvn clean install from a clean checkout and it looks to be finding robolectric. I hope you are missing something which you can correct using this example. Ping me if you can't
Christopher Perry
I added the sonatype repository stuff, and it's not finding robolectric. Not sure why.
Christopher Perry
This comment has been removed by the author.

Tcp Connections in Android

Or how to keep your app connected and stop worrying. 

Disclaimer:
This is not another place where you can find java code snippet to open a socket and read and write to some server.
If you need that, go back to your favorite search engine and look for another result. But be careful: while looking for “inspiration” I found a lot of examples where the connection was held inside the UI thread. Please, don’t do that. At least use an asynctask.


Short version of this post (spolier warning):

  • If you need to interact with a tcp server, use a singleton object that contains the socket 
  • You can access it from all the activities of your app
  • If you want to be connected only while in foreground, have a counter that gets incremented on onStart() and gets decremented on onStop() of any client activity. Connect when the counter is > 0, disconnect when the counter is < 0




Background:
As a side project, I am developing a simple turn based multiplayer game, with a tcp server as backend (python twisted).

In order to make my life easier, I wanted the interaction with the server to work as follows:


  • if the player gets disconnected while playing, it can’t reconnect and he is out of the current game
  • if the app goes background for any reason, it will disconnect from the server

The way the app interacts with the server is not the subject of the current post (hint: a thread for sending, one for receiving AND google protobuf for serialization), but it will probably be the subject for some code on github and another blogpost.

What I am writing about is how to share a tcp connection between several android activities.


Using a service to share the socket between android activities:

  • if you think about something that needs to survive between activities transition, a service is the first thing you think about
  • the code to interact with the server was self contained, so in every activity I had to bind to the service, wait for the bind to be done, call the service that called my code
  • because of the constraint I imposed to make the tcp server easier, I had to stop the service and restart it whenever my app went background
  • I was risking to keep the service around, consuming resources with no reason
So, use a service ONLY if you really need to keep the connection even if your app is no longer visible.
If you need to do periodical tasks, or if you need to perform one shot operations, please consider using intent services. 
Be kind and let your users know that you are draining their battery, using a notification icon when the service is alive and your app gets backgrounded.

Using a singleton to share the tcp socket:
  • A singleton object resides in your process, and so once started it’s accessible from all the activities
  • You can even run one or more threads inside of it, and they will be running no matter which activity is visible
  • Not using a service, you authorize the os to suspend your application (or even to kill it) when it runs out of resources.
  • There is no need to have the service layer to bind from the activity, you can access directly your singleton object
So, this is the BEST WAY to share a tcp connection between activities.

But what if you want to connect only when the application is foregrounded?

How to check when your application is active:
Have a global counter (a good place might be the singleton itself) that gets incremented every time an onStart() is called from one of your activities, and decremented when onStop() gets called. 

If that counter is > 0, that means the (at least) one activity is active and then the socket must be connected.
On the other hand, if the counter gets to 0, no activity is visible and so you need to disconnect.

If you are afraid that the counter might go to 0 only because an activity is starting another one and so its onStop() gets called before onStart of the next one, please note that the official android documentation assures us that when starting one activity from another,  onStop gets called AFTER onStart() of the started activity http://developer.android.com/guide/topics/fundamentals/activities.html.

With that in mind, we are sure that the only time the counter will get to 0 is when the ALL the activities are stopped.


If you liked this post, please consider following me on twitter @fedepaol .






PostmanLib - Android and Rest Web Services

Edit 2013-03-10: I’ve performed a substancial refactoring of postmanlib, and the details can be found in this new post

With the development of MyDearSanta app, I needed to fetch and put some data to a rest service developed using django python framework with a simple set of json based rest apis.

The development of the interaction between android and the rest web service was a bit messy because I was in hurry to release the app before Christmas (giving it’s xmas related nature), but while developing it I thought it could have been a good start for my first android library. And so it was.

Yesterday I finally managed to write a first use case of my PostmanLib (rings twice) android library. I must confess it tooks nearly a couple of hours just to get a nice name, but I really like this one.

You can check the library HERE

It’s barely inspired by Virgil Dobjanschi talk at google io 2010, and its relies on the common pattern is to make the one-shot background work inside intent services, let them update the model with the results of their work and then notify the interface when the model it’s changed, so that it can refresh itself.

It does not provide a way to serialize your data into json nor how to store it into a content provider. There are a plenty of examples out there if you look for json, android, rest and other keyword like those and what’s more, they really depends on the interface your server is offering.

What was missing was a common and clean way to interact with the service without using the “asynctask inside an activity” approach, which I don’t really like because of the well known issues of configuration changes.

All you need to do with my lib is to implement the abstract methods of a Command class, such as the execute method that will be called inside the intentservice which is the method that performs the real work, and some method to serialize / deserialize the command inside an intent.
The library comes along with a singleton helper class that can be used in activities to fire the commands and get notified with their results.

The steps are:

  • the activity gets a reference to the helper
  • a command is sent to the intent service (serialized in an intent)
  • the intent service executes the command and updates the model (sqlite, shared prefs, content provider, whatever)
  • if the activity was registered as event listener, gets notified of the result and can check again the model (or notify the user about an error)
  • the activity can ask the helper about any in progress services (it can be useful in onresume stage to notify the user)

You will still have to implement the real interaction between Android and the web service


 It’s far from be completed, but I am really enjoying how it is growing, and I hope it might be of some help for somebody.

If you are curious about that, just checkout it from github here Android rest service interaction

MyDearSanta

This time, while PastTimer has not been released yet, I started an experiment with an old friend. We wanted not just to release another standalone app, but interact in some way with a backend.

The result is www.mydearsanta.info , a web based Christmas list, which comes together with and Android app https://market.android.com/details?id=com.smokingbytes.santas and an Iphone app (currently under the approval process) .

The backend was written using the Django framework, and the apps interact with it using a json based rest api.

A New App - Android Kitchen Timer

Pastimer is my 4th app. It’s a simple (but nice) Android Kitchen Timer based on barcode scanning.

I learned some things during my journey of android development, but this time I wanted to focus on user interface and ease of use.

As always, it starts as a simple idea, the you start putting some code in it, it’s “almost” working, and the difference between almost working and finished, is something like two months or more. I let it alone for more than two months.

However, now it almost ready to be released, and it will very soon.

The idea behind it is very simple.

You point the barcode, the app looks in its internal database, and if it does know the time associated, starts a timer. If it does not recognize the barcode, it prompts the user asking to put the data.

Sounds easy, doesn’t it? You can give a look at a couple of screenshots here Android Kitchen Timer

What I Have Been Working on.. Android Kitchen Timer

Apart from some rest, and reading some good books (like excellent Kent Beck’s Test Driven development by example, and “growing object-oriented software guided by tests”), I have been working on a new app in the last month or so.

A week end locked at home because my gf was ill, was a good excuse to start a deep programming session. The app I am about to release (well, to be honest it still lacks all the nice ui parts, it just look like a bunch of grey buttons thrown on the screen) solves (or at least tries to simplify) a problem I have every time I need to cook pasta.

Basically, if you need to cook some pasta (but this applies to every other boxed product), you need to put it in the boiling water and then look for the boiling time on the box, and set the timer with minutes and seconds on your phone.

With my app you simply point the camera to the barcode of the box, and it automatically start a countdown for the right amount of time. Of course some data entry is needed, and if the barcode is not in the internal storage, the user will be asked to fill the name of the pasta and the correct boiling time.

So, it’s a simplified timer for android, which should make easier and faster the countdown setting process.

I really hope to release it soon, if you are interested and you want to be notified when I will release it, just throw me an email at fedepaol gmail.com

Behind Activities: Android, Services, Background Operations and Threads

This time I want to share something I learned about working in background on android.

One of the biggest advantages of android over ios (or, at least, some ios version ago) is that your app is not sandboxed and can continue to live in background.

To perform background (not visible) work, all you need to know is how to use android services.

A service is a piece of an application that is allowed to be active even if the application is not in foreground anymore. The most commonly used example for describing a service is the music player, which need to continue to play music when your app is hidden by something else.

First of all, I want to remark a concept I had some problems with: a service does not have any relationship with threads! The service shares the main (ui) thread with the rest of the components of the app. This means that, if your app is in foreground AND you have a service active AND your service performs some time consuming operations, it will affect the responsivness of the gui (and you don’t want to disappoint your user).
So, even if you are now able to perform some background work, if you expect it to be heavy, please move it inside a thread (or an asynctask) hosted by the service.

Another advantage of bringing a service on board is that your application will gain priority over the other apps. Android prefers to kill processes that do not have active services. On the other hand, this means that you should use services carefully if you don’t want to drain your user’s battery. If you don’t need a service anymore, please kill it and restart it later (as pointed out from mark murphy in this old but interesting article: http://www.androidguys.com/2009/09/09/diamonds-are-forever-services-are-not/)

Said that, I am now going to list some typical service usage patterns

  • Listen and react to events from outside which need to be handled even if the application is not active (and maybe when the device is idle), such as location variation, accelerometer, incoming calls and so on
  • Tasks that need to be performed periodically, like polling a mail server every 15 minutes
  • Continuous tasks (like playing a song, or sampling the location with a very narrow interval) that need to be performed in background


I would say the first two are similar, and the third needs to be handled in a different way.


One shot tasks
If you need one shot tasks (like downloading your email), you could:

  1. start a service with an intent
  2. return START_NOT_STICKY in its onCommand callback
  3. start an AsyncTask to perform the heavy operations

    OR

you can use an IntentService. 

IntentServices are useful as much as they are little advertised.
If your service is an intentservice, it will be automatically hosted in its own thread. You can send it as many actions to be performed you want bundled in intents. They will be queued and handled one at a time.
You don’t even have to bother if you need to shut the service down. It will automatically shut down when you return from the onHandleIntent call. Much simple, isn’t it?

Here are some simple scenarios for using intent services:

  • Background operation triggered from user (such as a button): Just pack the command in an intent, and launch the service with the intent.
  • React to some broadcast event (such as some system broadcast event): Register a broadcast receiver in your manifest and then launch the intent service from its onReceive. You can’t handle long running operations in the broadcast receiver, there is a strict time limit in broadcast handling.
  • Perform periodic tasks: Use an alarm manager and make it launch your intent service. I also remind you that using setInexactRepeating in this case will help your app to save more resources.


Long running tasks

If you need to need to perform long, continuous background operations, you have no options, and you need a classic service, returning START_STICKY in its onCommand() method.
Again, remember that in this case you need to perform time consuming operations inside a thread or an asynctask, because the service shares the same thread with the gui.






The next time I will try to write down some more practical stuff on how to implement activities and services communication.


If I wasn’t clear enough, if you broke your phone trying to implement this advices, or if you think I’d better shoot in my foot than writing this ugly stuff in a very poor engRish, drop me an email fedepaol gmail.com .
If you liked this post, you can consider following me on twitter @fedepaol .


UPDATE: I am currently (23/01/2012) working on a library to be used to interact with a service using an intent service. In case you may be interested, you can find it here PostmanLib - Android webservice interaction


First 18 Days of Pegdroid

It’s already been 18 days, and it’s time to make some considerations.

For those who don’t know what pegdroid is, it’s a simple peg solitaire for android. I released it 18 days ago, it’s free and it’s basically ad-supported (but I don’t get much from that).

I integrated google analytics in my app, so I can get some help from the statistics I get every day. In the meanwhile, the market dashboard got updated and I also can see which kind of device are the most popular.

At the moment, it was downloaded 1022 times (android developer console), and something like 7000 boards were played. Don’t know why, but it looks like it’s quite popular in minneapolis :-) .

First, there was a huge (or so) number of accesses and downloads the first day (like 350 downloads), and then the number of downloads dropped considerably, as well as the number of accesses, that went from a considerable 1800, to an average of 300 accesses per day. I tried to tweet some news about the game, but I didn’t experience any noticeable effect.

I then released the first update, because some users said it would have been better to have the score expressed in terms of remaing pegs, than in terms of pegs already eaten.

With the second update I implemented the undo of the last move, which was a widely requested feature, and I fixed a sporadic crash some anonymous guy reported me. With this update (and not with the previous update) I had a spike in the download and access number, as you can see in the picture.



I am really curious on what drives the download number, at the moment I can only say that updates have a positive effect. The next move is to integrate some kind of social gaming platform (I am still evaluating)..

In any case, if you haven’t downloaded pegdroid yet, here you can find the link to the market:

Comments

Pharaviel
Ti lascio un piccolo commento per ringraziarti del fantastico lavoro che hai fatto con GeoTagger per android, sono un fotoamatore e apprezzo molto di poter usare le funzioni di iphoto senza dover comprare un costosissimo gps per la mia fotocamera!
DarĂ² volentieri uno sguardo a pegdroid visto che ogni tanto giochicchio con il telefono.

Ok basta sproloqui, grazie ancora!

PegDroid - Android Simple Game

I just released a new game. It’s (yet another) version of the popular solitaire game. I thought its name was “Chinese checkers”, but it looks like it’s most common name is “Peg”. Anyway, it didn’t take (too) much to develop it, I really enjoyed learning something about the powerful andengine framework, but I am still proceeding at a slow pace since I can work on android only after my daytime job.

I am releasing it with ads, but in any case I think I will update the sources on github very soon. Now it’s quite late and I’m afraid to make some mistake.

Good points: the framework and the assets. I think my favourite designer (yes, still my gf) has done a good job in drawing all the assets. Very far from what I would have been done alone.

Another thing I tried to include in this app is google analytics. I can see which are the most played boards, and who downloads my app. I must say I am pretty curious.

Next time I promise to post some technical stuff, for example how I handled andengine stuff in a more structured way in my new app.

I will end including the demo video of PegDroid down here: