Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Use AOP to Help Get Mobile Content to Market Faster  : Page 2

Developers fight an uphill battle to make their applications work efficiently on a litany of mobile devices. But those who employ the power of aspects will have a powerful instrument for solving porting problems without repetitious code.

Using AOP to Help Mobile Development
Typically, when porting to multiple devices due to differences in performance, available heap memory, and screen sizes, you're forced to modify the original source code. For example, to port a game application to a small screen device, one might need to use a smaller font in the main menu to avoid scrolling. One might also need to remove certain screen elements if the screen is too narrow. Depending on the screen size, you might have to do both. Similar requirements apply to changes of performance and heap memory optimization. The end result is a set of similar but unique source code for each device.

Using AOP, these changes and the conditions for these changes to be applied are stored in the specific aspects instead of being scattered randomly throughout the source code (see Figure 2).

Figure 2. Aspects: The diagram shows the abstraction that aspects are capable of. Here, three aspects are wrapped in a weaver and each device takes from it what it needs to run the application correctly.
AOP provides many possibilities when it comes to application-specific optimization or meeting channel packaging needs. For example, channel-packaging APIs such as Sprint's Game Lobby add another dimension to the complexity of mobile content deployment. Using AOP, a developer can create a generic, application-agnostic aspect that does all of the above without modifying the original code.

In the case of application-specific optimization, when porting to a larger screen device it is common that font size and spacing between strings must be changed to improve legibility. AOP enables developers to create a single aspect similar to the those in Figure 2 that encapsulates all the changes related to large-screen optimization—without touching the original source code.

Device Bugs
The Java ME Mobile Media API (MMAPI) is one of the most common developer kits for building easily portable mobile applications. But there are a few issues which MMAPI is simply ill-equipped to handle. For example, some devices can only play one sound at a time. To work around this issue in the MMAPI, one needs to call Player.stop() for the player in use before playing a new sound. In other words, the following changes are required:

  1. Add static javax.microedition.media.Player currentPlayer = null; to the MIDlet class
  2. In all occurrences of player.start(), change the code to the following:

    if(MyMIDlet.currentPlayer != null) { currentPlayer.stop(); } player.start(); MyMIDlet.currentPlayer = player;

  3. In all occurrences of player.deallocate() and player.close(), insert the following code before the above:

    if(MyMIDlet.currentPlayer == player) { currentPlayer = null; }

There are plenty of traditional techniques to resolve the problem above. Unfortunately, these are not without complications. For one, the code to fix this sound issue is scattered in at least three places. Worse, if done improperly, you'll quickly find yourself in "pre-processor hell" where, before every occurrence of player.start(), you must insert:

//#if (manufacturer=="NOKIA" AND model=="4321") //OR (manufacturer=="NOKIA" AND model=="5432") //OR (manufacturer=="MOTOROLA" AND model=="V432") //OR (manufacturer=="MOTOROLA" AND model=="V321") //OR (manufacturer=="SONYERICSSON" AND model=="W100") //OR (manufacturer=="SONYERICSSON" AND model=="W200") //OR (manufacturer=="SAMSUNG" AND model=="X123") //OR (manufacturer=="SAMSUNG" AND model=="X456") if(MyMIDlet.currentPlayer != null) { MyMIDlet.currentPlayer.stop(); } MyMIDlet.currentPlayer = player; //#endif

every occurrence of player.close() and player.deallocate() with:

//#if (manufacturer=="NOKIA" AND model=="4321") //OR (manufacturer=="NOKIA" AND model=="5432") //OR (manufacturer=="MOTOROLA" AND model=="V432") //OR (manufacturer=="MOTOROLA" AND model=="V321") //OR (manufacturer=="SONYERICSSON" AND model=="W100") //OR (manufacturer=="SONYERICSSON" AND model=="W200") //OR (manufacturer=="SAMSUNG" AND model=="X123") //OR (manufacturer=="SAMSUNG" AND model=="X456") if(MyMIDlet.currentPlayer == player) { MyMIDlet.currentPlayer = null; } MyMIDlet.currentPlayer = player; //#endif

Plus, you must add the following to the MyMIDlet class:

static javax.microedition.media.Player currentPlayer = null;

Needless to say, the changes are very scattered and unreadable.

By using AOP you can consolidate all the changes into a single aspect. The following pseudo-code best illustrates how it works:

ensureSingleActivePlayerAspect { pointcut1 = class: MyMIDlet; addField : pointcut1 { static javax.microedition.media.Player currentPlayer = null; } pointcut2 = methodCall: Player.start(); insertBefore : pointcut2 { if(MyMIDlet.currentPlayer != null) { MyMIDlet.currentPlayer.stop(); } MyMIDlet.currentPlayer = player; } pointcut3 = methodCall: Player.close(), Player.deallocate(); insertBefore : pointcut3 { if(MyMIDlet.currentPlayer == player) { MyMIDlet.currentPlayer = null; } } }

The beauty of this approach is that all the code related to this particular sound issue is now nicely contained in one module without contaminating the original source code. And the pointcuts clearly specify where this code should apply to the original source code. The maintainability and readability are greatly improved.

It should be noted that it is possible to implement AOP-like behavior with pre-processors by using #define statements to replace Player.start(), Player.close(), and Player.deallocate(). That said, it is more desirable to use a language or a system that has native support for AOP. It is analogous to the use of procedural language to implement OOP. When C++ became a mainstream programming language, there were certain implementations of OOP that favor C instead. Over time, these C based implementations disappeared as more and more developers adopted languages with native OOP support.

Once mobile content is developed, AOP can provide developers or publishers with a powerful tool to eliminate complexity and successfully deploy content. Using AOP can accelerate time to market by addressing cost-cutting concerns, modularizing concerns into aspects, and reusing aspects for more than one device. The result is a more efficient mobile deployment process where it is easier to ensure projects stay on time and within budget.

Allen Lau is the CTO and Co-Founder of Tira Wireless, the premier provider of advanced technology for mobile content deployment.
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date