Save 37% off PRO during our Black Friday Sale! »

AIR Native Extensions

AIR Native Extensions

Overview of using and creating Native Extensions for use with Adobe AIR desktop and mobile applications.

340d396deb6c4987147b061705edcb54?s=128

David Ortinau

March 25, 2012
Tweet

Transcript

  1. AIR Native Extensions for Mobile

  2. David Ortinau @davidortinau http://davidortinau.com 16 yrs web, interactive, mobile. Flash,

    iPhone, Android, WP7 BA English, Maryville University
  3. None
  4. AIR Defined

  5. 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.”
  6. 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
  7. 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
  8. AIR Native Extensions

  9. 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.”
  10. Like What? • Notifications/Toasts • Push Notifications • Gyroscope •

    NetworkInfo • Licensing • Vibration • Speech Recognition • Battery Status • Kinect • Ads • Flurry Analytics • Mute • GameCenter
  11. Performance!!! Native code executes OUTSIDE the Flash Runtime

  12. Multi-Threading!!! Native code supports multiple threads

  13. AIR App AIR Runtime Device OS and Libraries Actionscript Extension

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

    Classes AIR Native Extension (*.ane) Resources (images, etc) Native Implementation
  15. ANEs are Platform Specific

  16. 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
  17. How to Use ANEs

  18. 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?
  19. 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/
  20. None
  21. How to Make ANEs

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

  23. 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
  24. 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.
  25. 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; }
  26. Extension Descriptor <extension xmlns="http://ns.adobe.com/air/extension/3.1"> <id>com.simplyprofound.BatteryStatus</id> <versionNumber>1</versionNumber> <platforms> <platform name="iPhone-ARM"> <applicationDeployment>

    <nativeLibrary>libBatteryStatus.a</nativeLibrary> <initializer>ExtInitializer</initializer> <finalizer>ExtFinalizer</finalizer> </applicationDeployment> </platform> </platforms> </extension>
  27. 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
  28. Tips • Provide a default implementation for testing • Write

    your Actionscript API first • Validate all input in Actionscript • Don’t include external dependencies (Flex SDK)
  29. 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/
  30. Thanks! @davidortinau http://davidortinau.com dave@davidortinau.com