Published on

Published in: Technology, Education
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide


  1. 1. Flash Runtime Native ExtensionsBuilding AIR apps with native capabilitiesRajorshi Ghosh Choudhury Syed Mohd MehadiComputer Scientist Lead Software Engineer Your logo (optional)
  2. 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. 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. 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. 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. 6. Accessing the native codeCreating 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 =“myNativeFunction") as String; ... ctx.dispose(); 6
  7. 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. 8. Writing the native codeInitializing 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. 9. Writing the native codeInitializing 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
  10. 10. Initialization Sequence 10
  11. 11. Extension APIsAccessing 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. 12. Multi-threading supportThreading• 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. 13. Describing the ExtensionThe extension descriptor<extension xmlns=""> <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. 14. Consuming an Extension extension.xml Applicati output.ane on.xml native library + library.swf + assets extensionlibrary (.swc) Application.s wf output.ane AIR application 14
  15. 15. C API ListFREResult 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. 16. Java API Listinterface 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, intpublic 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. 17. Demo 17
  18. 18. DemoAIR 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;“VibrateMethod”); } } … … … } … 18