SlideShare a Scribd company logo
1 of 18
Flash Runtime Native Extensions
Building AIR apps with native capabilities
Rajorshi Ghosh Choudhury   Syed Mohd Mehadi
Computer Scientist         Lead Software Engineer


 Your logo
 (optional)
What are native extensions?

Third-party, native-code backed ActionScript API
          additions to AIR applications

      Flash Builder

     Android SDK & NDK
                           AIR SDK
      XCode




                                                   2
Who needs them anyway…
• Adobe AIR is awesome
  –   Allows you to create cross platform applications
  –   Rapid multiscreen development
  –   A large feature set (Accelerometer, Geolocation, Gestures and more)



• But…
  –   No access to some device specific capabilities (Contacts, Bluetooth, etc)
  –   Waits for a native feature to mature before providing access to it
  –   Porting existing native applications to AIR might be a pain




                                                                                  3
Bridging the gap
•   Provides access to device specific features
•   Native developers can reuse existing code
•   AS API set can be extended
•   Same interface can be used on different platforms




                                                        4
Anatomy of an extension
• A set of ActionScript classes
• Associated native code for one or more target
  devices
• A descriptor that contains deployment information
• A signature to ensure secure delivery (optional)




                                                      5
Accessing the native code
Creating the Extension Context


• A context object binds the ActionScript and native halves of
  an extension.
• Calls to native functions happen within a context.
• There may be multiple extension context objects.
   import flash.external.ExtensionContext;

   ...
   var ctx : ExtensionContext =
                      ExtensionContext.createExtensionContext(“com.sample.myExtension", “basicType");
   var str : String = ctx.call(“myNativeFunction") as String;

   ...
   ctx.dispose();


                                                                                                        6
Writing the native code

•   Use either C or Java APIs
•   APIs are mostly cross-platform
•   Can be multi-threaded
•   Compiled into static or dynamic libraries depending on
    target platform




                                                             7
Writing the native code
Initializing and finalizing the extension

• Extension Initializer
    – Called when the first extension context is created.
       typedef void (*FREInitializer)(
                   void** extDataToSet ,
                   FREContextInitializer* ctxInitializerToSet,
                   FREContextFinalizer* ctxFinalizerToSet );



• Extension Finalizer
    – Called (if and) when extension is unloaded.

       typedef void (* FREFinalizer)(
                   void** extDataToSet);




                                                                 8
Writing the native code
Initializing and finalizing the context

• Context Initializer
    – Called every time a new extension context is created.
    – Registers native functions to be called in this context.
       typedef void (*FREContextInitializer)(
                   void** extDataToSet ,
                   const uint8_t* ctxType,
                   FREContext     ctx,
                   uint32_t* numFunctionsToSet,
                   const FRENamedFunction** functionsToSet);




• Context Finalizer
    – Called when an extension context is disposed.
      typedef void (* FREContextFinalizer)(
                   FREContext     ctx);
                                                                 9
Initialization Sequence




                          10
Extension APIs
Accessing ActionScript Objects from native code

• Manipulate ActionScript types in native code
    –   FREResult   FRENewObject(const uint8_t* className,
                                   uint32_t argc,
                                   FREObject[] argv,
                                   FREObject* object,
                                   FREObject* thrownException );


• Get/Set properties of ActionScript objects
    –   FREResult FREGetObjectProperty( FREObject object,
                                                          const uint8_t* propertyName,
                                                          FREObject* propertyValue,
                                                          FREObject* thrownException );
    –   FREResult FRESetObjectProperty( FREObject object,
                                                          const uint8_t* propertyName,
                                                          FREObject propertyValue,
                                                          FREObject* thrownException );


• Call methods of ActionScript objects
    –   FREResult FRECallObjectMethod(FREObject object,
                                              const uint8_t* methodName,
                                              uint32_t argc,
                                              FREObject[] argv,
                                              FREObject* result,
                                              FREObject* thrownException);
                                                                                          11
Multi-threading support
Threading


• Native libraries may be multi-threaded
• Native calls are serialized
• All functions in the API except one can be called only from a
  thread on which a registered function is in progress.
• Only function that can be called from any thread is
  FREDispatchStatusEventAsync()/FREContext.DispatchStatusEventAsy
  nc()




                                                               12
Describing the Extension
The extension descriptor
<extension xmlns="http://ns.adobe.com/air/extension/2.5">
    <id>com.sample.myExtension</id>
    <version>1</version>
    <platforms>
      <platform name="Android-ARM">
        <applicationDeployment>
          <nativeLibrary>MyExtension.jar</nativeLibrary>
          <initializer>com.example.MyExtension</initializer>
        </applicationDeployment>
      </platform>
      <platform name="MacOS-x86">
        <applicationDeployment>
          <nativeLibrary>MyExtension.framework</nativeLibrary>
          <initializer>InitMyExtension</initializer>
          <finalizer>FiniMyExtension</finalizer>
        </applicationDeployment>
      </platform>
      <!-- Hypothetical example of a possible Digital Home platform -->
      <platform name="Samsung-MIPS">
        <deviceDeployment/>
      </platform>
      <!-- Optional default impl -->
      <platform name="default">
        <applicationDeployment/>
      </platform>
    </platforms>
</extension>

                                                                          13
Consuming an Extension
            extension.xml                                      Applicati
                                               output.ane       on.xml
                            native library +
                             library.swf +
                                 assets
  extension
library (.swc)                                        Application.s
                                                           wf




                  output.ane
                                                  AIR application




                                                                           14
C API List
FREResult FREGetContextNativeData( FREContext ctx, void** nativeData );
FREResult FRESetContextNativeData( FREContext ctx, void* nativeData );
FREResult FREGetContextActionScriptData( FREContext ctx, FREObject *actionScriptData );
FREResult FREGetObjectType( FREObject object, FREObjectType *objectType );
FREResult FREGetObjectAsInt32 ( FREObject object, int32_t *value );
FREResult FREGetObjectAsUint32( FREObject object, uint32_t *value ); FREResult FREGetObjectAsDouble( FREObject object,
     double *value ); FREResult FREGetObjectAsBool ( FREObject object, bool *value );
FREResult FRENewObjectFromInt32 ( int32_t value, FREObject *object );
FREResult FRENewObjectFromUint32( uint32_t value, FREObject *object );
FREResult FRENewObjectFromDouble( double value, FREObject *object );
FREResult FRENewObjectFromBool ( FREBool value, FREObject *object );
FREResult FREGetObjectAsUTF8( FREObject object, uint32_t* length, const uint8_t** value );
FREResult FRENewObjectFromUTF8( uint32_t length, const uint8_t* value , FREObject* object );
FREResult FRENewObject( const uint8_t* className , uint32_t argc , FREObject[] argv , FREObject* object , FREObject*
     thrownException );
FREResult FREGetObjectProperty( FREObject object , const uint8_t* propertyName , FREObject* propertyValue , FREObject*
     thrownException );
FREResult FRESetObjectProperty( FREObject object , const uint8_t* propertyName , FREObject propertyValue , FREObject*
     thrownException );
FREResult FRECallObjectMethod ( FREObject object , const uint8_t* methodName , uint32_t argc , FREObject[] argv ,
     FREObject* result , FREObject* thrownException );
FREResult FREAcquireBitmapData( FREObject object , FREBitmapData* descriptor );
FREResult FREInvalidateBitmapDataRect( FREObject object, uint32_t x , uint32_t y , uint32_t width , uint32_t height );
FREResult FREReleaseBitmapData( FREObject object );
FREResult FREAcquireByteArray( FREObject object , FREByteArray* byteArray );
FREResult FREReleaseByteArray( FREObject object );
FREResult FRESetArrayLength( FREObject arrayOrVector, uint32_t length );
FREResult FREGetArrayLength( FREObject arrayOrVector, uint32_t* length );
FREResult FRESetArrayElementAt( FREObject arrayOrVector, uint32_t index , FREObject value );
FREResult FREGetArrayElementAt( FREObject arrayOrVector, uint32_t index , FREObject* value );
FREResult FREDispatchStatusEventAsync( FREContext ctx , const uint8_t* code , const uint8_t* level );               15
Java API List
interface FREExtension {
                                                                            public static native FREObject newObject ( String
     void initialize ();
                                                                                      className, FREObject constructorArgs[]);
     FREContext createContext( String contextType);            }
     void dispose ();
}
                                                               public class FREBitmapData extends FREObject {
                                                                    public static FREBitmapData newBitmapData (int width, int
public abstract class FREContext {
                                                                    height, boolean transparent, Byte[] fillColor);
     public abstract Map<String,FREFunction> getFunctions();
                                                                    public int getWidth();
     public FREObject getActionScriptData();
                                                                    public int getHeight() ;
     public void setActionScriptData( FREObject object );
                                                                    public java.nio.ByteBuffer getBits();
     public abstract void dispose();
                                                                    public void acquire();
     public void dispatchStatusEventAsync( String code,
                                          String level );
                                                                    void invalidateRect( int x , int y , int width , int height );
}                                                              }


interface FREFunction {                                        public class FREByteArray extends FREObject {
     FREObject call( FREContext ctx, FREObject[] args );            public static FREByteArray newByteArray ();
 }                                                                  public long getLength();
                                                                    public java.nio.ByteBuffer getBytes();
public class FREObject {                                            public void acquire();
     public static FREObject newObject ( int value );               public void release();
     public static FREObject newObject ( double value );       }
     public static FREObject newObject ( boolean value );
     public static FREObject newObject ( String value );        public class FREArray extends FREObject {
     public int getAsInt (); public double getAsDouble();            public static FREArray newArray (String classname, int
     public bool void setProperty( String propertyName, FREObject    numElements, boolean fixed);
                                          propertyValue );
                                                                     public static FREArray newArray (int numElements);
     FREObject callMethod( String methodName,
                                                                     public long getLength();
                            FREObject methodArgs[] );
                                                                     public void setLength( long length );
     getAsBool ();
                                                                     public FREObject getObjectAt( long index );
     public String getAsString();
                                                                    public void setObjectAt( long index, FREObject value );
     FREObject getProperty( String propertyName );
                                                               }
                                                                                                                          16
Demo




       17
Demo
AIR Part (.swf)                          AS library (.swc)                                Native code (.a)                 Extension


…                                        …                                                …

…                                        Class ASLayer {                                  …

var asLayer : ASLayer = new ASLayer();   …                                                FREObject VibrateMethod(arguments) {

asLayer.VibrateDevice();                 private var context : ExtensionContext =             NSLog(@”*********In NativeVibrateMethod”);
                                         ExtensionContext.createExtensionContext(extID,
…                                        type);                                            AudioSeervicesPlaySystemSound(
                                                                                          kSystemSoundID_Vibrate );
…                                        public function VibrateDevice() : void {
                                                                                              return NULL;
                                             Context.call(“VibrateMethod”);
                                                                                          }
                                         }
                                                                                          …
                                         …
                                                                                          …
                                         }

                                         …




                                                                                                                                 18

More Related Content

What's hot

Devirtualizing FinSpy
Devirtualizing FinSpyDevirtualizing FinSpy
Devirtualizing FinSpy
jduart
 
これからのPerlプロダクトのかたち(YAPC::Asia 2013)
これからのPerlプロダクトのかたち(YAPC::Asia 2013)これからのPerlプロダクトのかたち(YAPC::Asia 2013)
これからのPerlプロダクトのかたち(YAPC::Asia 2013)
goccy
 
Reconstructing Gapz: Position-Independent Code Analysis Problem
Reconstructing Gapz: Position-Independent Code Analysis ProblemReconstructing Gapz: Position-Independent Code Analysis Problem
Reconstructing Gapz: Position-Independent Code Analysis Problem
Alex Matrosov
 

What's hot (20)

PHP7 is coming
PHP7 is comingPHP7 is coming
PHP7 is coming
 
TensorFlow Lite (r1.5) & Android 8.1 Neural Network API
TensorFlow Lite (r1.5) & Android 8.1 Neural Network APITensorFlow Lite (r1.5) & Android 8.1 Neural Network API
TensorFlow Lite (r1.5) & Android 8.1 Neural Network API
 
Static Analysis in Go
Static Analysis in GoStatic Analysis in Go
Static Analysis in Go
 
A Brief History of UniRx/UniTask, IUniTaskSource in Depth
A Brief History of UniRx/UniTask, IUniTaskSource in DepthA Brief History of UniRx/UniTask, IUniTaskSource in Depth
A Brief History of UniRx/UniTask, IUniTaskSource in Depth
 
Using SWIG to Control, Prototype, and Debug C Programs with Python
Using SWIG to Control, Prototype, and Debug C Programs with PythonUsing SWIG to Control, Prototype, and Debug C Programs with Python
Using SWIG to Control, Prototype, and Debug C Programs with Python
 
AntiRE en Masse
AntiRE en MasseAntiRE en Masse
AntiRE en Masse
 
Posix Threads
Posix ThreadsPosix Threads
Posix Threads
 
Building Custom PHP Extensions
Building Custom PHP ExtensionsBuilding Custom PHP Extensions
Building Custom PHP Extensions
 
Devirtualizing FinSpy
Devirtualizing FinSpyDevirtualizing FinSpy
Devirtualizing FinSpy
 
IPC2010SE Doctrine2 Enterprise Persistence Layer for PHP
IPC2010SE Doctrine2 Enterprise Persistence Layer for PHPIPC2010SE Doctrine2 Enterprise Persistence Layer for PHP
IPC2010SE Doctrine2 Enterprise Persistence Layer for PHP
 
Interfacing C/C++ and Python with SWIG
Interfacing C/C++ and Python with SWIGInterfacing C/C++ and Python with SWIG
Interfacing C/C++ and Python with SWIG
 
Quick tour of PHP from inside
Quick tour of PHP from insideQuick tour of PHP from inside
Quick tour of PHP from inside
 
これからのPerlプロダクトのかたち(YAPC::Asia 2013)
これからのPerlプロダクトのかたち(YAPC::Asia 2013)これからのPerlプロダクトのかたち(YAPC::Asia 2013)
これからのPerlプロダクトのかたち(YAPC::Asia 2013)
 
Sphinx autodoc - automated api documentation - PyCon.MY 2015
Sphinx autodoc - automated api documentation - PyCon.MY 2015Sphinx autodoc - automated api documentation - PyCon.MY 2015
Sphinx autodoc - automated api documentation - PyCon.MY 2015
 
Memory Management In Python The Basics
Memory Management In Python The BasicsMemory Management In Python The Basics
Memory Management In Python The Basics
 
An Embedded Error Recovery and Debugging Mechanism for Scripting Language Ext...
An Embedded Error Recovery and Debugging Mechanism for Scripting Language Ext...An Embedded Error Recovery and Debugging Mechanism for Scripting Language Ext...
An Embedded Error Recovery and Debugging Mechanism for Scripting Language Ext...
 
WAD : A Module for Converting Fatal Extension Errors into Python Exceptions
WAD : A Module for Converting Fatal Extension Errors into Python ExceptionsWAD : A Module for Converting Fatal Extension Errors into Python Exceptions
WAD : A Module for Converting Fatal Extension Errors into Python Exceptions
 
SWIG : An Easy to Use Tool for Integrating Scripting Languages with C and C++
SWIG : An Easy to Use Tool for Integrating Scripting Languages with C and C++SWIG : An Easy to Use Tool for Integrating Scripting Languages with C and C++
SWIG : An Easy to Use Tool for Integrating Scripting Languages with C and C++
 
0d1n
0d1n0d1n
0d1n
 
Reconstructing Gapz: Position-Independent Code Analysis Problem
Reconstructing Gapz: Position-Independent Code Analysis ProblemReconstructing Gapz: Position-Independent Code Analysis Problem
Reconstructing Gapz: Position-Independent Code Analysis Problem
 

Similar to AFPS_2011

CSW2017 Henry li how to find the vulnerability to bypass the control flow gua...
CSW2017 Henry li how to find the vulnerability to bypass the control flow gua...CSW2017 Henry li how to find the vulnerability to bypass the control flow gua...
CSW2017 Henry li how to find the vulnerability to bypass the control flow gua...
CanSecWest
 
GateIn Frameworks
GateIn FrameworksGateIn Frameworks
GateIn Frameworks
jviet
 

Similar to AFPS_2011 (20)

Android RenderScript
Android RenderScriptAndroid RenderScript
Android RenderScript
 
Annotation Processing
Annotation ProcessingAnnotation Processing
Annotation Processing
 
Cross-Platform Native Mobile Development with Eclipse
Cross-Platform Native Mobile Development with EclipseCross-Platform Native Mobile Development with Eclipse
Cross-Platform Native Mobile Development with Eclipse
 
Android - Anatomy of android elements & layouts
Android - Anatomy of android elements & layoutsAndroid - Anatomy of android elements & layouts
Android - Anatomy of android elements & layouts
 
SenchaCon 2016: Modernizing the Ext JS Class System - Don Griffin
SenchaCon 2016: Modernizing the Ext JS Class System - Don GriffinSenchaCon 2016: Modernizing the Ext JS Class System - Don Griffin
SenchaCon 2016: Modernizing the Ext JS Class System - Don Griffin
 
Next .NET and C#
Next .NET and C#Next .NET and C#
Next .NET and C#
 
JavaScript Basics
JavaScript BasicsJavaScript Basics
JavaScript Basics
 
srgoc
srgocsrgoc
srgoc
 
Node intro
Node introNode intro
Node intro
 
CSW2017 Henry li how to find the vulnerability to bypass the control flow gua...
CSW2017 Henry li how to find the vulnerability to bypass the control flow gua...CSW2017 Henry li how to find the vulnerability to bypass the control flow gua...
CSW2017 Henry li how to find the vulnerability to bypass the control flow gua...
 
Tamir Dresher - Demystifying the Core of .NET Core
Tamir Dresher  - Demystifying the Core of .NET CoreTamir Dresher  - Demystifying the Core of .NET Core
Tamir Dresher - Demystifying the Core of .NET Core
 
Android training in mumbai
Android training in mumbaiAndroid training in mumbai
Android training in mumbai
 
React native
React nativeReact native
React native
 
Formatting ForThe Masses
Formatting ForThe MassesFormatting ForThe Masses
Formatting ForThe Masses
 
Titanium appcelerator my first app
Titanium appcelerator my first appTitanium appcelerator my first app
Titanium appcelerator my first app
 
devLink - What's New in C# 4?
devLink - What's New in C# 4?devLink - What's New in C# 4?
devLink - What's New in C# 4?
 
[Srijan Wednesday Webinars] Ruling Drupal 8 with #d8rules
[Srijan Wednesday Webinars] Ruling Drupal 8 with #d8rules[Srijan Wednesday Webinars] Ruling Drupal 8 with #d8rules
[Srijan Wednesday Webinars] Ruling Drupal 8 with #d8rules
 
GateIn Frameworks
GateIn FrameworksGateIn Frameworks
GateIn Frameworks
 
WinRT Holy COw
WinRT Holy COwWinRT Holy COw
WinRT Holy COw
 
Android ndk
Android ndkAndroid ndk
Android ndk
 

Recently uploaded

Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 

Recently uploaded (20)

Decarbonising Commercial Real Estate: The Role of Operational Performance
Decarbonising Commercial Real Estate: The Role of Operational PerformanceDecarbonising Commercial Real Estate: The Role of Operational Performance
Decarbonising Commercial Real Estate: The Role of Operational Performance
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
API Governance and Monetization - The evolution of API governance
API Governance and Monetization -  The evolution of API governanceAPI Governance and Monetization -  The evolution of API governance
API Governance and Monetization - The evolution of API governance
 
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Design and Development of a Provenance Capture Platform for Data Science
Design and Development of a Provenance Capture Platform for Data ScienceDesign and Development of a Provenance Capture Platform for Data Science
Design and Development of a Provenance Capture Platform for Data Science
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Quantum Leap in Next-Generation Computing
Quantum Leap in Next-Generation ComputingQuantum Leap in Next-Generation Computing
Quantum Leap in Next-Generation Computing
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Less Is More: Utilizing Ballerina to Architect a Cloud Data Platform
Less Is More: Utilizing Ballerina to Architect a Cloud Data PlatformLess Is More: Utilizing Ballerina to Architect a Cloud Data Platform
Less Is More: Utilizing Ballerina to Architect a Cloud Data Platform
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Stronger Together: Developing an Organizational Strategy for Accessible Desig...
Stronger Together: Developing an Organizational Strategy for Accessible Desig...Stronger Together: Developing an Organizational Strategy for Accessible Desig...
Stronger Together: Developing an Organizational Strategy for Accessible Desig...
 

AFPS_2011

  • 1. Flash Runtime Native Extensions Building AIR apps with native capabilities Rajorshi Ghosh Choudhury Syed Mohd Mehadi Computer Scientist Lead Software Engineer Your logo (optional)
  • 2. What are native extensions? Third-party, native-code backed ActionScript API additions to AIR applications Flash Builder Android SDK & NDK AIR SDK XCode 2
  • 3. Who needs them anyway… • Adobe AIR is awesome – Allows you to create cross platform applications – Rapid multiscreen development – A large feature set (Accelerometer, Geolocation, Gestures and more) • But… – No access to some device specific capabilities (Contacts, Bluetooth, etc) – Waits for a native feature to mature before providing access to it – Porting existing native applications to AIR might be a pain 3
  • 4. Bridging the gap • Provides access to device specific features • Native developers can reuse existing code • AS API set can be extended • Same interface can be used on different platforms 4
  • 5. Anatomy of an extension • A set of ActionScript classes • Associated native code for one or more target devices • A descriptor that contains deployment information • A signature to ensure secure delivery (optional) 5
  • 6. Accessing the native code Creating the Extension Context • A context object binds the ActionScript and native halves of an extension. • Calls to native functions happen within a context. • There may be multiple extension context objects. import flash.external.ExtensionContext; ... var ctx : ExtensionContext = ExtensionContext.createExtensionContext(“com.sample.myExtension", “basicType"); var str : String = ctx.call(“myNativeFunction") as String; ... ctx.dispose(); 6
  • 7. Writing the native code • Use either C or Java APIs • APIs are mostly cross-platform • Can be multi-threaded • Compiled into static or dynamic libraries depending on target platform 7
  • 8. Writing the native code Initializing and finalizing the extension • Extension Initializer – Called when the first extension context is created. typedef void (*FREInitializer)( void** extDataToSet , FREContextInitializer* ctxInitializerToSet, FREContextFinalizer* ctxFinalizerToSet ); • Extension Finalizer – Called (if and) when extension is unloaded. typedef void (* FREFinalizer)( void** extDataToSet); 8
  • 9. Writing the native code Initializing and finalizing the context • Context Initializer – Called every time a new extension context is created. – Registers native functions to be called in this context. typedef void (*FREContextInitializer)( void** extDataToSet , const uint8_t* ctxType, FREContext ctx, uint32_t* numFunctionsToSet, const FRENamedFunction** functionsToSet); • Context Finalizer – Called when an extension context is disposed. typedef void (* FREContextFinalizer)( FREContext ctx); 9
  • 11. Extension APIs Accessing ActionScript Objects from native code • Manipulate ActionScript types in native code – FREResult FRENewObject(const uint8_t* className, uint32_t argc, FREObject[] argv, FREObject* object, FREObject* thrownException ); • Get/Set properties of ActionScript objects – FREResult FREGetObjectProperty( FREObject object, const uint8_t* propertyName, FREObject* propertyValue, FREObject* thrownException ); – FREResult FRESetObjectProperty( FREObject object, const uint8_t* propertyName, FREObject propertyValue, FREObject* thrownException ); • Call methods of ActionScript objects – FREResult FRECallObjectMethod(FREObject object, const uint8_t* methodName, uint32_t argc, FREObject[] argv, FREObject* result, FREObject* thrownException); 11
  • 12. Multi-threading support Threading • Native libraries may be multi-threaded • Native calls are serialized • All functions in the API except one can be called only from a thread on which a registered function is in progress. • Only function that can be called from any thread is FREDispatchStatusEventAsync()/FREContext.DispatchStatusEventAsy nc() 12
  • 13. Describing the Extension The extension descriptor <extension xmlns="http://ns.adobe.com/air/extension/2.5"> <id>com.sample.myExtension</id> <version>1</version> <platforms> <platform name="Android-ARM"> <applicationDeployment> <nativeLibrary>MyExtension.jar</nativeLibrary> <initializer>com.example.MyExtension</initializer> </applicationDeployment> </platform> <platform name="MacOS-x86"> <applicationDeployment> <nativeLibrary>MyExtension.framework</nativeLibrary> <initializer>InitMyExtension</initializer> <finalizer>FiniMyExtension</finalizer> </applicationDeployment> </platform> <!-- Hypothetical example of a possible Digital Home platform --> <platform name="Samsung-MIPS"> <deviceDeployment/> </platform> <!-- Optional default impl --> <platform name="default"> <applicationDeployment/> </platform> </platforms> </extension> 13
  • 14. Consuming an Extension extension.xml Applicati output.ane on.xml native library + library.swf + assets extension library (.swc) Application.s wf output.ane AIR application 14
  • 15. C API List FREResult FREGetContextNativeData( FREContext ctx, void** nativeData ); FREResult FRESetContextNativeData( FREContext ctx, void* nativeData ); FREResult FREGetContextActionScriptData( FREContext ctx, FREObject *actionScriptData ); FREResult FREGetObjectType( FREObject object, FREObjectType *objectType ); FREResult FREGetObjectAsInt32 ( FREObject object, int32_t *value ); FREResult FREGetObjectAsUint32( FREObject object, uint32_t *value ); FREResult FREGetObjectAsDouble( FREObject object, double *value ); FREResult FREGetObjectAsBool ( FREObject object, bool *value ); FREResult FRENewObjectFromInt32 ( int32_t value, FREObject *object ); FREResult FRENewObjectFromUint32( uint32_t value, FREObject *object ); FREResult FRENewObjectFromDouble( double value, FREObject *object ); FREResult FRENewObjectFromBool ( FREBool value, FREObject *object ); FREResult FREGetObjectAsUTF8( FREObject object, uint32_t* length, const uint8_t** value ); FREResult FRENewObjectFromUTF8( uint32_t length, const uint8_t* value , FREObject* object ); FREResult FRENewObject( const uint8_t* className , uint32_t argc , FREObject[] argv , FREObject* object , FREObject* thrownException ); FREResult FREGetObjectProperty( FREObject object , const uint8_t* propertyName , FREObject* propertyValue , FREObject* thrownException ); FREResult FRESetObjectProperty( FREObject object , const uint8_t* propertyName , FREObject propertyValue , FREObject* thrownException ); FREResult FRECallObjectMethod ( FREObject object , const uint8_t* methodName , uint32_t argc , FREObject[] argv , FREObject* result , FREObject* thrownException ); FREResult FREAcquireBitmapData( FREObject object , FREBitmapData* descriptor ); FREResult FREInvalidateBitmapDataRect( FREObject object, uint32_t x , uint32_t y , uint32_t width , uint32_t height ); FREResult FREReleaseBitmapData( FREObject object ); FREResult FREAcquireByteArray( FREObject object , FREByteArray* byteArray ); FREResult FREReleaseByteArray( FREObject object ); FREResult FRESetArrayLength( FREObject arrayOrVector, uint32_t length ); FREResult FREGetArrayLength( FREObject arrayOrVector, uint32_t* length ); FREResult FRESetArrayElementAt( FREObject arrayOrVector, uint32_t index , FREObject value ); FREResult FREGetArrayElementAt( FREObject arrayOrVector, uint32_t index , FREObject* value ); FREResult FREDispatchStatusEventAsync( FREContext ctx , const uint8_t* code , const uint8_t* level ); 15
  • 16. Java API List interface FREExtension { public static native FREObject newObject ( String void initialize (); className, FREObject constructorArgs[]); FREContext createContext( String contextType); } void dispose (); } public class FREBitmapData extends FREObject { public static FREBitmapData newBitmapData (int width, int public abstract class FREContext { height, boolean transparent, Byte[] fillColor); public abstract Map<String,FREFunction> getFunctions(); public int getWidth(); public FREObject getActionScriptData(); public int getHeight() ; public void setActionScriptData( FREObject object ); public java.nio.ByteBuffer getBits(); public abstract void dispose(); public void acquire(); public void dispatchStatusEventAsync( String code, String level ); void invalidateRect( int x , int y , int width , int height ); } } interface FREFunction { public class FREByteArray extends FREObject { FREObject call( FREContext ctx, FREObject[] args ); public static FREByteArray newByteArray (); } public long getLength(); public java.nio.ByteBuffer getBytes(); public class FREObject { public void acquire(); public static FREObject newObject ( int value ); public void release(); public static FREObject newObject ( double value ); } public static FREObject newObject ( boolean value ); public static FREObject newObject ( String value ); public class FREArray extends FREObject { public int getAsInt (); public double getAsDouble(); public static FREArray newArray (String classname, int public bool void setProperty( String propertyName, FREObject numElements, boolean fixed); propertyValue ); public static FREArray newArray (int numElements); FREObject callMethod( String methodName, public long getLength(); FREObject methodArgs[] ); public void setLength( long length ); getAsBool (); public FREObject getObjectAt( long index ); public String getAsString(); public void setObjectAt( long index, FREObject value ); FREObject getProperty( String propertyName ); } 16
  • 17. Demo 17
  • 18. Demo AIR Part (.swf) AS library (.swc) Native code (.a) Extension … … … … Class ASLayer { … var asLayer : ASLayer = new ASLayer(); … FREObject VibrateMethod(arguments) { asLayer.VibrateDevice(); private var context : ExtensionContext = NSLog(@”*********In NativeVibrateMethod”); ExtensionContext.createExtensionContext(extID, … type); AudioSeervicesPlaySystemSound( kSystemSoundID_Vibrate ); … public function VibrateDevice() : void { return NULL; Context.call(“VibrateMethod”); } } … … … } … 18