trigger a Function in Android

Hi, I’m trying to trigger a function in JS only if is a Android device. In the UX code I have this:

<Android>
    <Callback Handler="{EsAndroid}" />
</Android>

and in JS:

function EsAndroid(){
    DEBUG.value = "android"; 
    WhatDevice = "android";
}

module.exports = {
    EsAndroid: EsAndroid,
    DEBUG: DEBUG
};

But never trigger the function…

I tested this and it works:

<App Theme="Basic">
    <Panel>
        <JavaScript>
            function callback (argument) {
                debug_log("Hello!");
            }
            module.exports = {
                callback: callback
            };
        </JavaScript>
        <Button Text="Callback">
            <Clicked>
                <Android>
                    <Callback Handler="{callback}" />
                </Android>
            </Clicked>
        </Button>
    </Panel>
</App>

<Callback /> is a TriggerAction so it has to be inside a Trigger block.

I try with:

<Android>
    <WhileTrue Value="true">
        <Callback Handler="{EsAndroid}" />
    </WhileTrue>
</Android>

But don’t work…

Just to resolve this issue: I talked with Cristian Karmy on our slack community channel and we ended up with this as a solution:

public class PlatformModule : NativeModule
{
    public PlatformModule()
    {
        AddMember(new NativeFunction("Android", (NativeCallback)Android));
        AddMember(new NativeFunction("iOS", (NativeCallback)iOS));
        AddMember(new NativeFunction("Desktop", (NativeCallback)Desktop));
    }

    static object Android(Context c, object[] args)
    {
        if defined(Android) return true;
        else return false;
    }

    static object iOS(Context c, object[] args)
    {
        if defined(iOS) return true;
        else return false;
    }

    static object Desktop(Context c, object[] args)
    {
        if defined(!Mobile) return true;
        else return false;
    }

}


<App Theme="Basic" ux:AutoCtor="false">
    <Panel>
        <PlatformModule ux:Global="PlatformModule" />
        <JavaScript>
            var Platform = require("PlatformModule");
            if (Platform.Android()) {
                debug_log("Running on Android");
            }
            else if (Platform.iOS()) {
                debug_log("Running on iOS");
            }
            else if (Platform.Desktop()) {
                debug_log("Running on Desktop");
            }
        </JavaScript>
    </Panel>
</App>

An API for checking what platform the app is running on is in our pipeline, but this can be used for now if anyone needs it :slight_smile:

Please add:

    AddMember(new NativeFunction("platformname", (NativeCallback)Platformname));


object Platformname(Context c, object[] args) {
            string result = "Unknown";
            if defined(iOS) {
                result = "iOS";
            }
            else if defined(CIL) {
                result = "CIL";
            }
            else if defined(Android) {
                result = "Android";
            }
            else if defined(CPlusPlus) {
                result = "C++";
            }
            return result;
}

And rename to FuseJS.PlatformModule

What we are planning for FuseJS will look something like this:

var Environment = require("FuseJS/Enviornment");

if (Environment.iOS) { }
if (Environment.Android) { }
etc..

The reason to have it as a string in addition, it to make it easier to show, and to send it over http etc. Else you will end up doing like this:

var string = 'unknown';
if (Enviroment.iOS) { string = 'iOS'; }
else if (Environment.Android { string = 'Android'; }
...

in the apps that needs this.