Fork us on GitHub

Material Icons, Background Music, Geofencing & Gradle

Bolstering the applications look, functionality and native access
Post Image

Material Icons, Background Music, Geofencing & Gradle

We became infatuated with icon fonts a while back and used them quite a bit, recently we added the FontImage class that made them really easy to use.
However, up until now you had to download a font. Look up the value and enter it in. This was OK but not ideal in terms of syntax/availability especially for simpler apps.

Google's material design includes a really cool mobile friendly icon font and using that for all our builtin features seems like a no-brainer. This reduces the total download size while making the fidelity of the UI much higher. As a result of this features like pull-to-refresh, infinite progress, share button etc. will implicitly look different and use the icon font.
You can also use one of the huge list of Google's material design fonts with just one line of code!

To do that just go to the Material Design Icon Catalog and type the icon name in the search field (or just scroll thru the images). E.g. say I want a thumb up button I can just apply it to a button like this:

FontImage.setMaterialIcon(getStarted, FontImage.MATERIAL_THUMB_UP);

The cool part about that code is that you don't have to define a special font, color or anything!
The API will implicitly extract the button styles and use that to set the right icon into place.

If you just want the image to use in any way you see fit you can just use:

FontImage img = FontImage.createMaterial(FontImage.MATERIAL_THUMB_UP, style);

Where the style object indicates the size and color of the image. Notice you can also use getMaterialDesignFont to get the actual font object.

Background Music

As part of our continuing effort to improve the portability of background processes within Codename One we recently added support for background music playback. This support isn't totally portable since the Android and iOS approaches for background music playback differ a great deal. To get this to work on Android we added the new API: MediaManager.createBackgroundMedia.
You should use that API when you want to create a media stream that will work even when your app is minimized and this should work for Android.

For iOS you will also need to add a special build hint: ios.background_modes=music. Which should allow background playback of music.

Background Location Updates - Geofencing

This was actually committed last month but documenting this has been a bit more challenging so it got pushed back a bit. Background location is even more complex than background media, polling location is generally expensive and requires a special permission on iOS. Its also implemented rather differently both in iOS and Android.
Because of the nature of background location the API is non-trivial. It starts with the venerable LocationManager but instead of using the standard API you need to use setBackgroundLocationListener, but this is where it flips. Instead of passing a location listener instance you need to pass a Class object instance. This is important because background location might be invoked when the app isn't running and an object would need to be allocated.

Notice that you should NOT perform long operations in the background listener callback. IOS wake-up time is limited to approximately 10 seconds and the app could get killed if it exceeds that time slice.
Notice that the listener can sends events also when the app is in the foreground, therefore it is recommended to check the app state before deciding how to process this event. You can use Display.isMinimized() to determine if the app is currently running or in the background.

When implementing this make sure that the class passed is a public class in the global scope (not inner class or anything like that). Make sure that the class has a public no-argument constructor and make sure you pass it as a class literal e.g. MyClassName.class. Don't use Class.forName("my.package.MyClassName")!
Class names are problematic since device builds are obfuscated, you should only use literals which the obfuscator detects and handles correctly.

Android Native Gradle Support

Google made a lot of changes to their native tooling since we launched Codename One. Up until now we are still using Ant on our servers to build the native Android apps. This has been rather convenient but as Google is migrating away from Ant to Gradle its becoming an issue where some features can't be supported by the build system.

So we introduced the new build hint: android.gradle. This hint can be either true or false and will default to true once 3.3 launches so we suggest kicking the wheels right now by setting it to true and reporting issues!
One of the really great features you get by building with Gradle support is that you will now be able to just open the Android project in Android studio when using include source and just run them as usual!

Share this Post:

Posted by Shai Almog

Shai is the co-founder of Codename One. He's been a professional programmer for over 25 years. During that time he has worked with dozens of companies including Sun Microsystems.
For more follow Shai on Twitter & github.