Developing Android Applications with Adobe AIR [23]
Performance
The device’s performance is measured by its speed of execution and how much it can hold in memory. Methods and tools can be used to perform a benchmark. We will cover this topic in Chapter 19. For a quick analysis, try the AIRBench application developed by Christian Cantrell, from Adobe, and read his blog at http://blogs.adobe.com/cantrell/archives/2010/10/using-airbench-to-test-air-across-android-phones.html.
AIRBench tests devices’ capabilities, but also runs performance analyses. I used it on three different devices. Table 5-2 shows the performance results I achieved in milliseconds (lowest numbers show best performance).
Table 5-2. AIRBench performance results for the Samsung Galaxy Tab, Droid 2, and Nexus One devices
Test
Samsung Galaxy Tab
Droid 2
Nexus One
Parse a 145 KB XML file
103
128
142
Allocate 8,192 KB of memory
85
23
57
Write, read, and delete a 1,024 KB file
147
97
3,646
Insert, select, and delete 500 rows of data in an SQLite database
636
751
875
Perform several string operations
336
488
470
Perform an SHA256 hash on a 45 KB image
117
145
151
Calculate sunrise and sunset times for 10 cities 100 times
156
183
128
Calculate frame rate
60
60
60
Capabilities
You can request information on the device at runtime to automate some of the presentation of your application. The Android Market, unlike the Apple Store, does not separate applications between phones and tablets. Be prepared for all devices.
Rather than trying to make your application look identical on all devices, adapt the layout for the resolution used.
The flash.system.Capabilities class provides information on the environment. To determine that your application is running on a mobile device, use cpuArchitecture:
import flash.system.Capabilities;
if (Capabilities.cpuArchitecture == "ARM") {
trace("this is probably a mobile phone");
}
Alternatively, you can compare screenDPI and screenResolutionX. Here is the code to calculate the diagonal dimension of the screen:
var dpi:int = Capabilities.screenDPI;
var screenX:int = Capabilities.screenResolutionX;
var screenY:int = Capabilities.screenResolutionY;
var diagonal:Number = Math.sqrt((screenX*screenX)+(screenY*screenY))/dpi;
if (diagonal < 5) {
trace("this must be a mobile phone");
}
You can also store the screenDPI that will be used to scale and position your assets, as demonstrated further in the section Creating Content for Multiple Screens:
var dpi:int = Capabilities.screenDPI;
Orientation
To control the look of your application, you can define the application’s scaling and alignment. The scaleMode as set in the following code prevents automatic scaling and the align setting always keeps your application at the upper-left corner. When you choose auto-orient, it is added automatically in Flash Builder Mobile:
import flash.display.StageScaleMode;
import flash.display.StageAlign;
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
Android devices equipped with an accelerometer can determine device orientation. As the user moves the hardware, your content rotates. Define a custom background color to prevent distracting white borders from appearing while in transition:
[SWF(backgroundColor="#999999")]
If you always want your application in its original aspect ratio, in Flash Professional select File→AIR Android settings, and under the General tab, deselect “Auto orientation”. In Flash Builder, under Mobile Settings, deselect “Automatically reorient”.
To get the initial orientation, use the following:
var isPortrait:Boolean = getOrientation();
function getOrientation():Boolean {
return stage.stageHeight > stage.stageWidth;
}
To listen for a device’s orientation and set a stage resize on the desktop, set autoOrients to true:
...
and register for the Event.RESIZE event:
import flash.events.Event;