Category Archives: API

ConcurrentModificationException when using unmodifiableCollection

Learning something new everyday.

One of the best ways to avoid data manipulation problems in concurrent environments is to use read only data sets, one of the best way way to create a read only collection in Java is to use unmodifiableCollection method, however this method is not as innocent as you may think.

Behind the scenes unmodifiable collections are just a stub that redirect all methods to the original collections, except for methods that actually change the collection like add etc, in which case it throws an exception.

Test yourself, what happens when trying to iterate over an unmodifiable collection, when in a different thread new elements are added to the original collection… ConcurrentModificationException .

The best way to avoid it is to actually create a copy, e.g. new LinkedList(…)

To sum it up:

  1. Use unmodifiable collections whenever you want to make sure that in an external usage of the collection nothing will be modified, for example use it as a return value for your methods.
  2. Use different mechanisms in case of a multi-threaded environment, for example creating a real copy.

Leave a comment

Filed under API, Uncategorized

Instagram API wrapper for Android


Instagram(IG) API wrapper for Android for handling an Instagram app lifecycle. Split into 2 sub-projects:

  • InstagramAuth Authentication library, based uppon Facebook for Android library.
  • restig A wrapper arround IG rest API calls. Based upon restFB.


Currently short on documentation, will add more as time permits.



 * Authorize a user in Instagram. Will show up the auth dialog and will
 * allow the user to enter his/her credentials.

* <strong> Pay attention</strong> Use the listener afterwards to save the
 * authentication token.
 * @param listener
 * - A dialog listener that can react to various authentication
 * events. Cannot be null.
 * @param permissions
 * - Use various permissions. Can be empty but not null.
 * @param activity
 * - An activity that will be the parent of the dialog box.
 public void authorize(final DialogListener listener,
 final Activity activity, final String... permissions) {
 InstagramModule module = new InstagramModule();
 final InstagramAuthDialog dialog = new InstagramAuthDialog(activity,
 listener, mAppId, permissions);

public class InstagramModule implements
 DialogListener {
 public void onComplete(final Bundle values) {
 String accessToken = values.getString("access_token");

 public void onError(final DialogError error) {
 // Log error

 public void onCancel() {
 // User canceled.
 authorize(new InstagramModule(), activity, "likes", "comments");


InstagramClient userClient = new DefaultInstagramClient(appId, mAccessToken);
final String endPoint = format("media/%s/comments",
final InstagramCollection comments = client
.fetchCollection(endPoint, Comment.class);

You can download the source from github

Leave a comment

Filed under API

Using atom feed to read mails in Gmail

I wanted to read my emails from Gmail using an atom feed. Gmail supports it, for the entire procedure you are welcomed to read however one thing that is missing from the explanation is the final part, i.e. how do I fetch using the access code that I just received.

At the beginning I tried to append ?access_token=YOUR_ACCESS_TOKEN to my oauth address ( in our case), it didn’t work, I always got 401 access denied.

Guess what, Gmail only supports  the Authorization: Bearer <access_token> header  so your request should be something like

GET /m8/feeds/contacts/default/full HTTP/1.1
Authorization: Bearer YOUR_ACCESS_TOKEN

Hope it will save you some frustration.


Filed under API

Make your app social

Using a neat little feature you can make your app social using 5 lines of code.

The idea is to launch an intent of type ACTION_SEND which will pop up an Android dialog box for sharing. Every activity that binds itself to this activity will be displayed. Usually activities that have sharing capabilities (like Linkedin, Twitter and Facebook) will show up in that list.

Share using SEND_TO

Use the following code:

Intent intent=new Intent(android.content.Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_SUBJECT, "Subject");

intent.putExtra(Intent.EXTRA_TEXT, "Description");
activity.startActivity(Intent.createChooser(intent, "Dialog caption"));

Ain’t that simple ?

In case you want specific activities to handle your request then make sure to change the type, usually most social apps will listen to text/plain.

Leave a comment

Filed under API

Freemium and paid app from same source code

Many times you want to create two or more applications from the same source code, in my case I wanted to create an app that has both a freemium version (with advertisement) and a paid one.

Development Process

  1. Usually I’ll start by creating a regular Android project and code my app just like any other app.
    Using a single project for development at least in the beginning eases the entire process.
  2. When I’m done with the full app, I’ll add my advertisement code:
     * If it is a light version show ads.
    private void handleLightVersion() {
    	String isLight = getString(R.string.is_light);
    	if (isLight.equalsIgnoreCase("true")) {
    		// Look up the AdView as a resource and load a request.
    		AdView adView = (AdView)this.findViewById(;
    		AdRequest request = new AdRequest();

    Lines 5 and 6 do the magic, test to see if a specific string exists and get its value, if the value is true then show ads, otherwise don’t.

  3. Convert the project into a library project (see SDK).
  4. Add new regular project and add the library project as a reference (see SDK). Pay attention the name of the app package should be different than the one used in the library project (you can add ‘light’ suffix to the name).
  5. Under the android manifest make sure to add all the activities/services etc that you intend to use (usually all of them). Make sure to reference them with the correct package name (the one used in the library project).
  6. Under the strings.xml file add the string that indicates whether it is a light  version or not. In our case it is.
    <string name="is_light">true</string>
  7. Android will automatically know to merge the various resources together, now that is neat.
  8. Create another project this time for the one without advertisement, follow  steps 4-7, but this time change is_light to false.
    <string name="is_light">false</string>


Leave a comment

Filed under API


The android platform has many UI design patterns, among them you can find:

  • Action Bar
  • Dashboard
  • Dynamic list
  • etc
The biggest problem with these patterns is the fact that someone needs to implement them. Well… someone did the work for you. Please welcome the GreenDroid
An open source library developed by Cyril Mottier that helps developers to use UI design patterns. You can download an example app from the Market to see its capabilities.


Filed under API