MUSIC on Android (phase II)
This article states the design and implementation of the definitive porting of OSGi and MUSIC to Android. This second phase overcomes the lacks and weaknesses detected with the first approach (phase I).
See the article: MUSIC on Android (phase I) to find a brief description about Android, the synergies between OSGi/MUSIC and Android, and the implementation of the first porting to Android.
The source code and Android application can be found at the end of the article.
Requirements for phase II
The initial porting to Android was successful but we identified several weaknesses which prevent us from getting an optimal integration between OSGi/MUSIC and Android:
- A hack was required because Android copied the OSGi bundles into the directory /data/dalvik-cache, but this directory was protected. Runtime deployment of OSGi bundles was not possible due to this problem. Moreover, this hack required root access which was not available in commercial Android phones.
- The GUI of the middleware and the applications was implemented by code. It was not possible to use the Android XML files (e.g. layouts) because the GUI had to be offered as OSGi bundles (and they are not Android applications).
- The MUSIC applications were installed and launched with the MUSIC middleware GUI. It was not possible to work with MUSIC applications as Android applications.
- The communication services did not work due to problems with multicast messaging. Moreover, the IP address returned by Android was the localhost (this problem is extensible to any operating system based on Linux).
- The MUSIC middleware:
- The MUSIC middleware will be an Android application
- The MUSIC middleware will be launched as an Android application
- The MUSIC middleware will be installed as an Android application
- The OSGi bundles of the MUSIC middleware will be zipped inside the Android application.
- The MUSIC middleware GUI will be implemented with the Android IDE (possibility to use the Android tools to implement the GUI)
- The OSGi middleware will be automatically launched as an OSGi service when launching the Android application.
- The MUSIC applications:
- Each MUSIC application will be an Android application
- The MUSIC application can be launched as either an Android application or by the MUSIC middleware GUI.
- The MUSIC application can be installed as either an Android application or by the MUSIC middleware GUI.
- The OSGi bundles for the MUSIC application will be included inside the Android application.
- The MUSIC application GUI will be implemented with the Android IDE (possibility to use the Android tools to implement the GUI)
- When launching the MUSIC application as an Android application, it will start the Android service corresponding to the MUSIC middleware, register the OSGi bundles included in the application, and launch the MUSIC application. This process must be transparent to the user.
- The communication services:
- The Android platform must support multicast messaging (for service discovery)
- It must support network disconnections as well as changes in the IP address (when roaming between different WiFi routers).
Design and architecture
Some important design decisions were considered:
- The multicast support requires Android 1.6 (or later version).
- The Apache's Felix OSGi framework is used because it is the only one (according to our experience) that does not require hacking the /data/dalvik-cache directory. Moreover, its performance is rather good in Android devices.
- A JAR library (android.util.jar) will help to interact between the MUSIC applications and the MUSIC middleware.
- The interaction between the OSGi bundles and the Android activities is realized by BroadcastReceivers and the Android Context.
The interaction between OSGi and Android
We provide two different mechanisms to interact between OSGi and Android:- The Android Context. All the OSGi bundle will be able to interact with the Android OS through the Context instance provided by the Android MUSIC service. Currently, there are some context sensors which make use of the Context (e.g. location sensors, screen orientation sensor, ...). This instance is available to any OSGi bundle as an OSGi service. By using OSGi Declarative Services, it is possible to acquire this service dependency by the following XML snippet:
<reference name="android.content.Context"
interface="android.content.Context"
cardinality="1..1"
bind="setAndroidContext"
target="(platform=android)"
policy="dynamic"
/>
- The Android BroadcastReceivers. Android provides a useful mechanism to send messages (Android Intents) to any Android application listening to those Intents by a BroadcastReceiver. While the Android Context only admits a unidirection interaction (from OSGi to Android), this mechanism is bidirectional. It is possible to have several broadcast receivers for the same messages. It is the unique alternative in which an Android activity can retrieve information or invoke an OSGi service. All the Android activities (which provide the GUI) retrieve information by using BroadcastReceivers. The following figure represents how an Android activity could get information (to be rendered in the screen) from an OSGi bundle.

The deployment
The MUSIC middleware is published as an Android application (.apk file). In the future, it will be possible to install the MUSIC middleware from the Android market (or any other proprietary application market). This better integration of MUSIC and Android will enable a seamless installation and start of the MUSIC middleware.

The previous figure represents the "artifacts" included in the deployment file (.apk):
- Android resources. The most important resource is a zip file which includes all the OSGi bundles related to the MUSIC middleware (as well as the required Felix OSGi bundles). This zip file has a convenient directory structure according to Felix guidelines.
- Java libraries. The felix.jar library (corresponding to the OSGi core framework) is required to launch the OSGi/MUSIC middleware.
- Android MUSIC service. This Android service is in charge of launching the OSGi framework.
- Android activities. These Android activities provide the GUI for the management of the MUSIC middleware. These activities interact with the MUSIC OSGi bundles by broadcast receivers and broadcast intents.
In the same manner, each MUSIC application is distributed as an Android application (.apk file). Its structure can be seen in the following picture. In this case, the OSGi bundles required by the MUSIC application (which implement the plans, components, ... of the MUSIC application) are not compressed in a zip file. These bundles are installed in the MUSIC middleware by means of the android.util.jar library. The Android activities of the MUSIC application provide the GUI of the application.

Of course, reusability is still a key concept of MUSIC. A MUSIC application can reuse the components provided by other applications (or even the MUSIC middleware) because the OSGi framework is shared by all the Android/MUSIC applications.
The MUSIC middleware
There are 3 main "entities" which participate in the launch of the MUSIC middleware:- Android launcher activity. This is a GUI activity which shows a wait screen while the MUSIC middleware is being launched. The activity starts the MUSIC Android service, which is only effective if the service is not already started, and waits until the MUSIC middleware is up. It registers a BroadcastReceiver which will listen to an Intent reporting that the middleware is up. When the middleware is launched, it starts a new Android activity: MenuActivity which lists all the options to manage the MUSIC middleware.
- Android MUSIC service. This is an Android service which is in charge of launching the OSGi framework. By means of the Android service, we assure that there is only one instance of the OSGi framework (the MUSIC middleware) and we reduce the application startup time (in case that this service is already started). The MUSIC service also registers the Android Context instance as an OSGi service so that any OSGi bundle can interact with Android by using this OSGi service.
- The OSGi framework and OSGi bundles. The MUSIC middleware is composed of a set of OSGi bundles. These bundles, together with the OSGi bundles corresponding to the OSGi framework and the configuration files to launch the OSGi framework, are zipped in a file. This file is accessible from the Android application and is unzipped so that the OSGi framework is launched. The MUSIC applications will also provide their custom OSGi bundles but they will be installed by a different mechanism (see later on). The OSGi framework is launched and the OSGi bundles are started according to the OSGi specification.

The previous figure represents the launching of the MUSIC middleware. Although multiple OSGi bundles are started, only the OSGi gui.android bundle is relevant because it participated in this lifecycle. When this bundle is activated (by OSGi declarative services), it will register a IGuiPage service corresponding to the MainMenuPage. Then, the OSGi bundle sends a Broadcast Intent (by using the Android Context service) to notify that the MUSIC middleware is up. This notification triggers the Android Launcher Activity to progress and start the MenuActivity.
The android.util.jar library
This Java library is recommended to be used by any MUSIC application so that the integration with the MUSIC middleware is highly simplified. The role of the library is to provide methods to install/uninstall OSGi bundles and launch/shutdown MUSIC applications. It will launch the MUSIC middleware if it was not launched yet and it assures the correct lifecycle (e.g., an application is not launched before it is registered in the MUSIC middleware).The library offers:
- IApplicationCallback interface. It is used to notify the Android activity (associated to a MUSIC application) about different events related to MUSIC.
/**
* Callback interface to notify about installation/uninstallation of a bundle
* and for the launch/shutdown of an application
*
* @author Telefonica I+D
*/
public interface IApplicationCallback {
/**
* Notifies that the MUSIC middleware is already launched
*/
public void onLaunchedMiddleware();
/**
* Notifies that the application has been registered in the MUSIC platform
*
* @param applicationType
*/
public void onInstalledApplication(String applicationType);
/**
* Notifies that the application has been unregistered in the MUSIC platform
*
* @param applicationType
*/
public void onUninstalledApplication(String applicationType);
/**
* Notifies that the application has been launched
*
* @param applicationType MUSIC application type
*/
public void onLaunchedApplication(String applicationType);
/**
* Notifies that the application has been shutdown
*
* @param applicationType MUSIC application type
*/
public void onShutdownApplication(String applicationType);
/**
* Notifies that the bundle has been installed in the OSGi framework
*
* @param bundleName Bundle-Name attribute of the OSGi bundle
*/
public void onInstalledBundle(String bundleName);
/**
* Notifies that the bundle has been uninstalled in the OSGi framework
*
* @param bundleName Bundle-Name attribute of the OSGi bundle
*/
public void onUninstalledBundle(String bundleName);
}
- ApplicationUtil class. The Android activity (associated to the MUSIC application) will instantiate this class, passing as a parameter an instance of IApplicationCallback so that it is possible to start the MUSIC application conveniently.
public class DemoAndroid extends Activity {
private ApplicationUtil applicationUtil;
private final static String APPLICATION_TYPE = "type/org.istmusic.demoandroid.realizations/HelloWorldApp";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Instantiate the utility class for simplify the integration between MUSIC and Android
applicationUtil = new ApplicationUtil(this, new ApplicationCallback());
}
@Override
public void onDestroy() {
// Clean resources (listeners) in ApplicationUtil instance
applicationUtil.setCallback(null);
super.onDestroy();
}
private class ApplicationCallback implements IApplicationCallback {
@Override
public void onLaunchedMiddleware() {
// When the MUSIC middleware is launched, install the bundles associated to the MUSIC application
// Install the bundle (file referenced by R.raw.bundle in the Android application)
applicationUtil.installBundle(R.raw.bundle);
}
@Override
public void onInstalledApplication(String applicationType) {
// When the application is registered, the application is launched
if (applicationType != null && applicationType.equals(APPLICATION_TYPE))
applicationUtil.launchApplication(APPLICATION_TYPE);
}
@Override
public void onInstalledBundle(String bundleName) {
}
@Override
public void onLaunchedApplication(String applicationType) {
}
@Override
public void onShutdownApplication(String applicationType) {
}
@Override
public void onUninstalledApplication(String applicationType) {
}
@Override
public void onUninstalledBundle(String bundleName) {
}
}
}
References
The source code can be downloaded from BerliOS:
The MUSIC middleware is also available as an Android application in BerliOS: music-middleware-android-0.6.0.apkCheck the file releases in BerliOS for the latest version.