Testing for the Android OS version using Actionscript 3.0

In my previous post, I had run into a problem using XSL transforms on XML files stored on the client side of an Android device. The following will illustrate how I handled the problem using Flex and AS3.

The first thing that I did was in my project mxml file, I added a method that will be called when the app dispatches an Initialize event.

<s:TabbedViewNavigatorApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
		xmlns:s="library://ns.adobe.com/flex/spark" 
		xmlns:pn="mobile.pocketnotes.*"
		splashScreenImage="@Embed('NIMSplashIcon.png')"
		splashScreenScaleMode="none"
		initialize="viewnavigatorapplication1_initializeHandler(event)"
		creationComplete="viewnavigatorapplication1_creationCompleteHandler(event)"
		applicationDPI="160">

In the method, viewnavigatorapplication1_initializeHandler(event), I added some code to handle the detection of the Android OS based on the AS3 class from Sidney de Koning. The added code is displayed below:

//actual operating system application is running in 
//(debug will be desktop version)
//will display on About Us page
currentOS =  Capabilities.os;

//flash based air version target 
currentVersion = flash.system.Capabilities.version;
				
//check air runtime version target, 
//in this case check for android
//Note that this info has the version number  
//of the android air runtime not the android os
if (currentVersion.substr(0,3) == "AND"){				
	//need to check the OS number
	//android is linux os
	if (currentOS.substr(0,5) == "Linux"){
	    var deviceInfo:NativeDeviceInfo = new NativeDeviceInfo();
	    //android, leave default local value 
        //until succesful os check
		deviceInfo.addEventListener(NativeDeviceInfoEvent.PROPERTIES_PARSED, handleDevicePropertiesParsed);
		deviceInfo.setDebug(false);
		deviceInfo.parse();
	} else {
		//not sure will get here
		this.pnData.currentOS = "Device OS version: " + currentOS;
	}
} else {
	//not android, leave default local value
	this.pnData.currentOS = "Device OS version: " +currentOS;
}

The initialize method checks to see if the device is an android device. If it is, it creates the NativeDeviceInfo class. An event listener method is added which is called once the build.properties file on the device has been parsed by this class. The key line is this:

deviceInfo.addEventListener(NativeDeviceInfoEvent.PROPERTIES_PARSED, handleDevicePropertiesParsed);

The handleDevicePropertiesParse method checks the devices OS version and then updates a boolean variable, pnData.isLive, to indicate whether the app should load a local version or a live version of an xml file:

private function handleDevicePropertiesParsed(event : NativeDeviceInfoEvent) : void 
{
	NativeDeviceInfo(event.target).removeEventListener(NativeDeviceInfoEvent.PROPERTIES_PARSED, handleDevicePropertiesParsed);
				
	var osVersion:String = NativeDeviceProperties.OS_VERSION.value;
	//split string via periods 
	//into major,minor,buildRevision ie - 4.0.4
	var major:int;
	
	//create version array which contains
	//values for major.minor.buildRevision
	var versionArray:Array = [];
	versionArray = osVersion.split(".");
				
	//set the variable to be displayed
	//on the about us page
	this.pnData.currentOS = "Device OS version: Android " + osVersion;
	
	//make sure that I can detect a version,
	//before trying to process data
	if (versionArray.length >0){
		major = versionArray[0];
		
		//check to see if os is too early to
		//handle client side xsl transforms of xml
		if (major < 4){
			//set the device to ping the website
			//for the info to be displayed
			this.pnData.isLive = true;
		}
	}
}

The default behavior of the app is to use the local version of the xml files to be displayed. So the default value of pnData.isLive = false. For Android OS versions before 4.0, this value is set to true. Based on this boolean, the App will load the local xml files if the value is false and will request the live internet based version if the value is true.

Advertisements

One Response to Testing for the Android OS version using Actionscript 3.0

  1. […] – with or without an internet connection. The implementation details are discussed in Testing for the Android OS version using Actionscript 3.0. Share this:ShareLinkedInFacebookLike this:LikeBe the first to like this […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: