Android

Mobiconf 2015

Mobiconf 2015

I’ve been invited as speaker at Mobiconf 2015 in Krakow. It was a really nice event and the place was a real cinema, quite an intimidating stage :)

I want to thank to the organizers for such a wonderful event and the excellent treatment received. If they are interested on inviting me again, I will for sure repeat next year.

Here is a list of the conferences I’ve attended and a small wrap-up. I’ll add the links to presentations once they are online.

Kevin Goldsmith – Apportioning Monoliths

Kevin is dev lead on Spotify. He was working on Microsoft for many years and he compared they way of working in the 90’s in MS with their current methods on Spotify. Basically, the first was a huge falldown process with a 3 years production phase. In Spotify they have now a 2 week release cycle and they want to move it down to 1 week
He also explained how they moved from a team-per-layer structure to a team-per-functionality structure and how this allowed this to avoid inter-layer locks. This speech really got me, as I worked in a company performing translations for Microsoft in that time and I lived part of the issues he mentioned.

Sergi Martínez – Android Data Binding

That was mine. Android introduced data binding in I/O 2015 and is still in beta, but it could be a huge difference in the way we create UIs. There are still some things missing, like the two-way binding, which can be achieved at the moment with a lot of tricks. Data binding also allows using the MVVM pattern, as we use View Models to feed the data into views. I’ll write an article about that.

Diego Gómez Olivera – Booking Now Architectural Overview: Clean code for Android

Diego is an Android engineer at Booking Now and a GDE. He explained how they implemented the Clean Architecture pattern in their application. The communication of the different components is based on a Bus architecture to send commands and receive actions. They use tiny bus, as it was the most efficient in their benchmarks over GreenBus and Otto.

Svetlana Isakova – Kotlin on Android

Svetlana is part of team that created the Kotlin language in JetBrains. This speech was an introduction to the characteristics of the language and why they think it is a good language with a lower development effort needed. The second part was a brief introduction to the language itself introducing the changes and new elements of the version 2.0. Really nice good-looking language, but still not intended for production.

Denzil Ferreira – AWARE: mobile context instrumentation framework

Denzil is an investigator at the Oulu university in Sweden. He and his team work on the AWARE framework, an open-source library for Android responsible for getting information about the user context, not only from device sensors but from other data sources. The framework is really scalable via plug-ins and the backend allows to easily retrieve information and change what data is collected on the fly. It also simplifies a lot the use of sensors and, specially, collecting and sampling data. I’ve specifically ask for car sensors and he told me that plug-ins are not available yet, but he was tinkering with it (because he has a VW car) and they are easily integrable via plugins. More: http://www.awareframework.com/

Cesar Valiente – New permissions model on a “Nice” way

Cesar works at Wunderlist (now Microsoft) as Android dev and is a GDE. He introduced us in the new permissions feature in Android Marshmallow. He told us about the “official” way of using them, and how easy it works, but also discussed about how difficult is to integrate in an efficient way into a complex application. He shown us the full workflow and a proposal to integrate it into a project based on Clean Architecture

Savvas Dalkitsis – Lightweight Android lifecycle annotations

Savvas is an android dev at Shazam. We were introduced to a library Shazam published that allows to use Aspect Oriented Programming to Android UI. They moved from calling them aspects to traits. Basically, it allows to add an remove features from view objects (View, Activity, Fragment) with just annotations. Quite amazing library, for me the best example was adding the annotion @FABButton as annotation, and hop, the activity has a FAB button configured via annotation parameters. The library allows us to easily create new annotations that add new functionalities. Worth to check.

Marcin Kasz – Your beacons have been hacked! Why security Matters

Marcin from Kontakt.io discussed about the importance of security on a beacon implementation. We reviewed the most common types of attacks and some tricks to avoid them. My favorite was piggybanking, where a competitor uses your beacons to show you geolocated prices of products that are cheaper in their store.

James Nocentini – How to develop an offline-first native Android app

James works on the CoachBase project for Android. The speech was an explanation on how their system works. The great advantage of CouchDb for Android is that synchronization between server and client is made automatically in the background and can also be done between clients in P2P mode. We were introduced to several synchronization strategies and he made a nice demo of a website accessible by everybody showing a picture of a city and a text to introduce the name and how this picture was changed in every mobile through sync. I’m not sure if it’s valid for all cases, but could be interesting for a project like the Christmas parade.

Frederik Schweiger – Hello again, Android Wear!

Frederik is from the GDG Düsseldorf and a real enthusiast in Android Wear. He made a review of Wear basics and updated to the current state of the art. He also shown us some examples of apps he made like a remote door opener for his car. The good thing about this speech was that being the last one, a lot of people already left the convention and we had more an open discussion that a round of questions. Some interesting answers to my questions: despite having wifi, new wear doesn’t allow direct connection, is just for cloud notifications. Google fit is accessible from the watch, but only gets info obtained from the watch, not from the cloud and permission must be given from the phone before, and yes, the watch can fully access sensors.

It’s the Arts 2! – Dealing with bitmaps

In the previous article (Having fun with canvas) we started playing around with the canvas using some Bitmap objects. Unfortunately, playing with Bitmaps it’s no so easy. They tend to be huge and need a lot of memory in order to be stored.

There are a lot of articles talking about how bitmaps are stored in memory, but, tl;dr, so I’m just going to set some basic points about it:

  • Bitmaps are not stored in the same memory space as objects
  • Bitmaps are stored raw. It means that if you have a 100×100 pixels using 24 bits per pixel it will need 100x100x24 bytes = 240,000 bytes (around 240Kb).
  • We need to get rid of bitmaps as soon as possible, at the same moment we stop using them
  • Remember, Bitmaps loaded from disk are immutable so we can’t draw on them (but we can use them to draw on another canvas)

Let’s see how to fix these issues one by one.

Loading smaller bitmaps

In the previous article, we saw that you can load a Bitmap from disk (specifically a Resource) like this:

The problem here is that maybe the bitmap is too large for the final size in screen, so we are wasting precious memory that is not needed. For example, if our bitmap is 1000×1000 but it’s going to be shown in a view that is just 100×100, we are loading 24,000,000 bytes when we only need 240,000. Wow! 24Mb of image loaded to be drawn as a 240Kb image. Remember, image is precious.

He knows about memory

There is a second implementation of the method decodeResource that allows us to set a BitmapFactory.Options object in order to tweak how the bitmap is loaded.

We pay special attention to the parameter inJustDecodeBounds. This parameter will make BitmapFactory to not load the bitmap, but just load the out parameters of the Options object, giving us the width and height of the image.

We also have a parameter named inSampleSize, that forces BitmapFactory to load the image using this parameter as a reducing scale factor.

Now we have the elements that we need to load smaller bitmaps. Recap:

  1. Determine the minimum size of the bitmap that we need
  2. Setup a BitmapFactory.Options object with the inJustDecodeBounds set as true
  3. Decode the bitmap
  4. Get width and height from the resulting options object
  5. Calculate the appropiate scaling factor
  6. Set the factor to the inSampleSize property and inJustDecodeBound as false
  7. Load the bitmap

Here you have a function that does the work:

And the method calculateInSampleSize

This method has been taken from Android documentation. You can find this code and MUCH more information about handling memory on http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

Mutable bitmaps, immutable bitmaps and getting rid of them

As said before, bitmaps loaded via BitmapFactory. If you need to make them mutable, you have two options:

  • Set the property inMutable from BitmapFactory.Options to true before loading
  • Copy the bitmap using the method copy from Bitmap class with the parameter mutable as true.

Making bitmaps mutable needs more memory, so use it wisely, only when you need to draw ON the bitmap. Sometimes is better, if you are going to paint it on a canvas later, to paint your bitmap on the canvas, then paint over. It depends on what you want to achieve. Remember…

Oh! who cares…

Last but not least, we can mark bitmaps to be cleaned from memory before the GC destroys the bitmap object. Just call the recycle() method in the bitmap object and the memory used to hold the pixels will be clean. The object will still exist, but any try to use it will give you an error.

We will see examples on how to use it in future articles, but for the moment, get used to the concept.

Just to summarize. Memory is important. Bitmaps are huge. Optimize them by loading smaller bitmaps and freeing them asap.

Stay cool and reduce your bitmaps memory footprint

It’s the arts! The Workshop

It’s the arts! The Workshop

Last week I had the pleasure to make a 4 hours workshop titled “It’s the Arts! Playing around with the Android Canvas” organized by GDG Barcelona.

The workshop was based on 3 topics, each one with an introduction to explain concepts and related classes and methods plus a set of exercises to test and practice the topic.

After the workshop we enjoyed a nice BBQ with all assistants!

The event was performed at La Fábrica,  a new space to share and hack in Barcelona (Sagrera).

You can find the slides on Slideshare and the exercises on Github.

You can also find pictures of the event in the G+ event page.

As usual, a lot of topics were left out of the workshop, as Android canvas is a huge graphics framework, but I’m happy I was able to introduce a lot of concepts, some of them really hard if you have never worked with graphics before. I think that making exercises as a progressive git project will help people to review these exercises later on at their own pace.

As this workshop required a lot of preparation (170 slides!), I will probably repeat it in the future, so stay alert!

Thanks again to the people from GDG and La Fábrica, and of course, to all the people who assisted to the workshop!

See you in the next event!

 

It’s the Arts! Having fun with a canvas

It’s the Arts! Having fun with a canvas

Making your own views in Android is a lot of fun, and using the Canvas to make them more appealing is even more fun. Graphical effects, animations, and a lot of other things can be achieved extending a simple view and modifying its behavior during the canvas drawing phase.

Starting at this task is quite hard. Information seems to be written for people who already know what they are doing. Then, when you start to see concepts like matrixes, PorterDuff, and so on, you are forced to check if you missed documentation explaining it all .

In this articles I will start reviewing the basic steps to start working with the canvas and drawings.