Upgrade to Pro — share decks privately, control downloads, hide ads and more …

AIR Native Extensions

AIR Native Extensions

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

David Ortinau

March 25, 2012
Tweet

More Decks by David Ortinau

Other Decks in Programming

Transcript

  1. 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.”
  2. 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
  3. 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
  4. 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.”
  5. Like What? • Notifications/Toasts • Push Notifications • Gyroscope •

    NetworkInfo • Licensing • Vibration • Speech Recognition • Battery Status • Kinect • Ads • Flurry Analytics • Mute • GameCenter
  6. AIR App AIR Runtime Device OS and Libraries Actionscript Extension

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

    Classes AIR Native Extension (*.ane) Resources (images, etc) Native Implementation
  8. 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
  9. 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?
  10. 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
  11. 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.
  12. 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; }
  13. 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>
  14. 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
  15. Tips • Provide a default implementation for testing • Write

    your Actionscript API first • Validate all input in Actionscript • Don’t include external dependencies (Flex SDK)
  16. 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/