Giter Site home page Giter Site logo

rontian / frekotlin-android-ane Goto Github PK

View Code? Open in Web Editor NEW

This project forked from tuarua/frekotlin-android-ane

0.0 0.0 0.0 26.57 MB

Example Air Native Extension written in Kotlin 1.1 for Android

License: Apache License 2.0

ActionScript 28.84% Batchfile 4.07% Shell 4.29% Java 9.19% Kotlin 53.35% PowerShell 0.25%

frekotlin-android-ane's Introduction

FreKotlin

Download

Example Android Studio project showing how to create Air Native Extensions for Android using Kotlin.

This project is used as the basis for the following ANEs
Google Maps ANE
AdMob ANE
WebViewANE
FirebaseANE
ZipANE


Getting Started

A basic Hello World starter project is included

How to use

Converting from FREObject args into Kotlin types, returning FREObjects

The following table shows the primitive as3 types which can easily be converted to/from Kotlin types

AS3 type Kotlin type AS3 param->Kotlin return Kotlin->AS3
String String val s = String(argv[0]) return s.toFREObject()
int Int val i = Int(argv[0]) return i.toFREObject()
Boolean Boolean val b = Boolean(argv[0]) return b.toFREObject()
Number Double val d = Double(argv[0]) return d.toFREObject()
Number Float val fl = Float(argv[0]) return fl.toFREObject()
Date Date val dt = Date(argv[0]) return dt.toFREObject()
Rectangle Rect val r = Rect(argv[0]) return r.toFREObject()
Rectangle RectF val r = RectF(argv[0]) return r.toFREObject()
Point Point val pnt = Point(argv[0]) return pnt.toFREObject()
Point PointF val pnt = PointF(argv[0]) return pnt.toFREObject()
Vector Int IntArray val arr = IntArray(argv[0]) return arr.toFREObject()
Vector Boolean BooleanArray val arr = BooleanArray(argv[0]) return arr.toFREObject()
Vector Number DoubleArray val arr = DoubleArray(argv[0]) return arr.toFREObject()
Vector String List val al = List<String>(argv[0]) return al.toFREObject()
Object Map<String, Any>? val dict: Map<String, Any>? = Map(argv[0])
null null return null

Basic Types

val myString: String? = String(argv[0])
val myInt = Int(argv[1])
val myBool = Boolean(argv[2])

val kotlinString = "I am a string from Kotlin"
return kotlinString.toFREObject()

Creating new FREObjects

val newPerson = FREObject("com.tuarua.Person")

// create a FREObject passing args
// 
// The following param types are allowed: 
// String, Int, Double, Float, Long, Short, Boolean, Date, FREObject
val frePerson = FREObject("com.tuarua.Person", "Bob", "Doe", 28, myFREObject)

Calling Methods

// call a FREObject method passing args
// 
// The following param types are allowed: 
// String, Int, Double, Float, Long, Short, Boolean, Date, FREObject
val addition = freCalculator.call("add", 100, 31)

Getting / Setting Properties

val oldAge = Int(person["age"])
val newAge = oldAge + 10

// Set property using braces access
person["age"] = (oldAge + 10).toFREObject()

// Set property using setProp
person.setProp("age", oldAge + 10)

Arrays

val airArray: FREArray? = FREArray(argv[0])
// convert to a Kotlin List<String>
val airStringVector = List<String>(argv[0])

// create a Vector.<com.tuarua.Person> with fixed length of 5
val newFreArray = FREArray("com.tuarua.Person", 5, true)
val len = newFreArray.length

// loop over FREArray
for (fre: FREObject? in airArray) {
    trace(Int(fre))
}

// set element 0 to 123
airArray[0] = 123.toFREObject()

// append element FREArray
airArray.append(456)

// return Kotlin IntArray to AIR
val kotArr: IntArray = intArrayOf(99, 98, 92, 97, 95)
return kotArr.toFREArray()

Sending Events back to AIR

trace("Hi", "There")

// with interpolation
trace("My name is: $name")

dispatchEvent("MY_EVENT", "this is a test")

Bitmapdata

val icon: Bitmap? = Bitmap(argv[0])
return icon.toFREObject()

ByteArrays

val byteArray = ByteArray(argv[0])
if (byteArray != null) {
    val str = String(Base64.encode(byteArray, Base64.NO_WRAP), Charset.forName("utf-8"))
}

Error Handling

FreKotlinLogger.context = this.context
if (inFRE1.type != FreObjectTypeKotlin.INT) {
    return FreException("Oops, we expected the FREObject to be passed as an int but it's not").getError();
}

Advanced Example - Extending. Convert to/from LatLng

package com.tuarua.frekotlin

import com.adobe.fre.FREObject
import com.google.android.gms.maps.model.LatLng

fun LatLng(freObject: FREObject?): LatLng {
    return LatLng(Double(freObject["latitude"]) ?: 0.0,
            Double(freObject["longitude"]) ?: 0.0)
}

fun LatLng.toFREObject(): FREObject? {
    return FREObject("com.tuarua.googlemaps.Coordinate", this.latitude, this.longitude)
}

Prerequisites

You will need

  • Android Studio 3.0
  • IntelliJ IDEA
  • AIR 31+

frekotlin-android-ane's People

Contributors

rontian avatar tuarua avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.