Slide 1

Slide 1 text

AIR Native Extensions for Mobile

Slide 2

Slide 2 text

David Ortinau @davidortinau http://davidortinau.com 16 yrs web, interactive, mobile. Flash, iPhone, Android, WP7 BA English, Maryville University

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

AIR Defined

Slide 5

Slide 5 text

http://onair.adobe.com/air/ “Adobe AIR is a cross-operating system runtime that enables web developers to use their existing web development skills, code and tools to build and deploy rich web applications and content to the desktop. Adobe AIR has a rich set of features, with support for building applications using HTML, JavaScript, Flex and Flash.”

Slide 6

Slide 6 text

AIR Runtime • Adobe Integrated Runtime Runtime = AIR Runtime • Runtime environment contains: • Flash Player: runs Flash, Flex • WebKit: runs HTML, Javascript • Platforms supported • Windows • OS X • Linux* • Android • Blackberry Tablet OS • iOS *Adobe ended support at 2.6

Slide 7

Slide 7 text

AIR SDK • Software Development Kit provides additional APIs • File System • Native Window Chrome • Enhanced Drag and Drop • Stage3D • Captive Runtime • Stage Video Hardware Acceleration • h.264 Video Encoding • Front Camera Support* • Hi-Res Bitmap Support • Multitouch and Gestures • Native Text* • Encrypted local storage* • Native JSON • Accelerometer support • StageWebView • Screen Orientation support* http://www.adobe.com/products/air/features._sl_id-contentfilter_sl_featuredisplaytypes_sl_all.html * mobile only

Slide 8

Slide 8 text

AIR Native Extensions

Slide 9

Slide 9 text

An Adobian “With we released of AIR 3.0 we added this awesome new feature called native extensions. Trust me these are cool. Essentially, a native extension will let you extend the functionality of your AIR app so it can access the native capabilities of a device.”

Slide 10

Slide 10 text

Like What? • Notifications/Toasts • Push Notifications • Gyroscope • NetworkInfo • Licensing • Vibration • Speech Recognition • Battery Status • Kinect • Ads • Flurry Analytics • Mute • GameCenter

Slide 11

Slide 11 text

Performance!!! Native code executes OUTSIDE the Flash Runtime

Slide 12

Slide 12 text

Multi-Threading!!! Native code supports multiple threads

Slide 13

Slide 13 text

AIR App AIR Runtime Device OS and Libraries Actionscript Extension Classes AIR Native Extension (*.ane) Resources (images, etc) Native Implementation

Slide 14

Slide 14 text

AIR App AIR Runtime Device OS and Libraries Actionscript Extension Classes AIR Native Extension (*.ane) Resources (images, etc) Native Implementation

Slide 15

Slide 15 text

ANEs are Platform Specific

Slide 16

Slide 16 text

References AIR Native Extensions from Adobe and Community http://www.adobe.com/devnet/air/native-extensions-for- air.html What the Heck are AIR Native Extensions? http://e-musings.tumblr.com/post/11994829993/what-the- heck-are-air-native-extensions

Slide 17

Slide 17 text

How to Use ANEs

Slide 18

Slide 18 text

Where to find ANEs • Adobe http://www.adobe.com/devnet/air/native-extensions-for- air.html • Community Sites http://extensionsforair.com/ • GitHub https://github.com/search? q=ANE&repo=&langOverride=&start_value=1&type=Everyt hing&language=ActionScript • ANE Explorer - Android App https://play.google.com/store/apps/details?

Slide 19

Slide 19 text

Using an ANE • AIRKinect.ane example http://as3nui.github.com/airkinect-2-core • Space Invaders http://quetwo.com/2012/02/01/microsoft-kinect-and-adobe- air/

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

How to Make ANEs

Slide 22

Slide 22 text

Battery Status adapted from Todd Anderson’s post http://custardbelly.com/blog/2011/09/21/air-native-extension-example-ibattery-for-ios/

Slide 23

Slide 23 text

package { import flash.events.EventDispatcher; import flash.external.ExtensionContext; public class BatteryStatus extends EventDispatcher { protected var _extensionContext:ExtensionContext; private static const COMMAND_LIFE:String = "GetBatteryLife"; private static const COMMAND_INFO:String = "GetBatteryInfo"; public function BatteryStatus(){ super(); init(); } private function init():void { _extensionContext = ExtensionContext.createExtensionContext( "com.simplyprofound.BatteryStatus", "main" ); } /** * Returns the battery life in percent (0-1). * @return Number The percentage of battery life left on the iOS. Returns a value from 0 to 1. */ public function getBatteryLife():Number { return _extensionContext.call( COMMAND_LIFE ) as Number; } /** * Returns the current state of the battery on the iOS device. * @return int Value corresponding to the state of the battery. UNKNOWN, UNPLUGGED, CHARGING, FULL */ public function getBatteryState():int { return _extensionContext.call( COMMAND_INFO ) as int; } } } Actionscript Code

Slide 24

Slide 24 text

ExtensionContext The ExtensionContext class provides an interface for calling functions in the native implementation of an ActionScript extension. You can use this class only in ActionScript classes that are part of the extension.

Slide 25

Slide 25 text

Native Code #import "FlashRuntimeExtensions.h" // Access battery life. FREObject GetBatteryLife(FREContext ctx, void* funcData, uint32_t argc, FREObject argv[]) { UIDevice *device = [UIDevice currentDevice]; [device setBatteryMonitoringEnabled:YES]; float life = [device batteryLevel]; FREObject retVal; FRENewObjectFromDouble( life, &retVal ); return retVal; } // Access info about battery FREObject GetBatteryInfo(FREContext ctx, void* funcData, uint32_t argc, FREObject argv[]) { UIDevice *device = [UIDevice currentDevice]; [device setBatteryMonitoringEnabled:YES]; int info = [device batteryState]; FREObject retVal; FRENewObjectFromInt32( info, &retVal ); return retVal; } // A native context instance is created void ContextInitializer(void* extData, const uint8_t* ctxType, FREContext ctx, uint32_t* numFunctionsToTest, const FRENamedFunction** functionsToSet) { ! *numFunctionsToTest = 2; ! FRENamedFunction* func = (FRENamedFunction*)malloc(sizeof(FRENamedFunction)*2); ! func[0].name = (const uint8_t*)"GetBatteryLife"; ! func[0].functionData = NULL; ! func[0].function = &GetBatteryLife; ! ! func[1].name = (const uint8_t*)"GetBatteryInfo"; ! func[1].functionData = NULL; ! func[1].function = &GetBatteryInfo; ! ! *functionsToSet = func; }

Slide 26

Slide 26 text

Extension Descriptor com.simplyprofound.BatteryStatus 1 libBatteryStatus.a ExtInitializer <finalizer>ExtFinalizerfinalizer>

Slide 27

Slide 27 text

Package the ANE "/Applications/Adobe Flash Builder 4.6/sdks/4.6.0/bin/adt" - package -target ane ../release/BatteryStatus.ane extension.xml -swc ../bin/BatteryStatusANE.swc -platform iPhone-ARM library.swf libBatteryStatus.a

Slide 28

Slide 28 text

Tips • Provide a default implementation for testing • Write your Actionscript API first • Validate all input in Actionscript • Don’t include external dependencies (Flex SDK)

Slide 29

Slide 29 text

References 20 tips for creating Air Native Extensions for iOS http://www.richardlord.net/blog/20-tips-for-creating-air- native-extensions-for-ios AIR Native Extension Example: iBattery for iOS http://custardbelly.com/blog/2011/09/21/air-native- extension-example-ibattery-for-ios/

Slide 30

Slide 30 text

Thanks! @davidortinau http://davidortinau.com [email protected]