# how to use foreign code

**URL:** <https://forums.fusetools.com/t/how-to-use-foreign-code/6625>\
**Category:** How-to Discussions\
**Created:** [September 14, 2017, 5:53am UTC](https://forums.fusetools.com/t/how-to-use-foreign-code/6625 "2017-09-14T05:53:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![inod](https://yyz1.discourse-cdn.com/flex035/user_avatar/forums.fusetools.com/inod/32/524_2.png) [@inod](https://forums.fusetools.com/u/inod)\
**Post date:** [September 14, 2017, 5:53am UTC](https://forums.fusetools.com/t/how-to-use-foreign-code/6625/1 "2017-09-14T05:53:34Z")

</div>

Hi fuse. I am trying to figure out how to use foreign code. this is the code i wrote to get print out some text using Log.d .  
can anyone please help me to figure this out. i really want to learn this and i will write some community libraries after that.  
Thank you…

my uno file - (CallLog.uno)

```auto
using Uno.Threading;
using Uno;
using Uno.UX;
using Uno.Compiler.ExportTargetInterop;
using Android;
using Uno.Permissions;
using Fuse;
using Fuse.Scripting;

[extern(Android) ForeignInclude(Language.Java, "android.content.Context")]

[UXGlobalModule]
public class CallLog : NativeModule{
    public CallLog(){
        AddMember(new NativeFunction("callHistory",(NativeCallback)callHistory));
    }

    object callHistory(Context c, object[] args){
        callHistory();
        return null;
    }

    [Foreign(Language.Java)]
    public static extern(Android) void callHistory()
    @{  

        android.util.Log.d("ForeignCodeExample","hi");

    @}      

    static extern(!Android) void callHistory(){
        debug_log("Notifications not supported on this platform.");
    }

}

```

my ux file

```auto
<App>
    <DockPanel>
        <StatusBarBackground Dock="Top" />
        <BottomBarBackground Dock="Bottom" />

        <CallLog ux:Global="CallLog" />

        <JavaScript>
            var Observable = require("FuseJS/Observable");
            var Log = require("CallLog");
            var val = Observable("");

            function call() {
                Log.callHistory();
            }
            module.exports = {call};

        </JavaScript>
        <Panel Height="60" Width="200">
        <Rectangle Color="Blue">
            <Clicked Handler="{call}" />
        </Rectangle>
        </Panel>
    </DockPanel>
</App>

```

my unoproj file -

```auto
{
  "RootNamespace":"",
  "Packages": [
    "Fuse",
    "FuseJS",
    "Uno.Permissions"
  ],
  "Includes": [
    "*",
    "*.uno"
  ]
}

```

---

<div class="post-metadata">

**Author:** ![Freak](https://yyz1.discourse-cdn.com/flex035/user_avatar/forums.fusetools.com/freak/32/541_2.png) [@Freak](https://forums.fusetools.com/u/Freak)\
**Post date:** [September 14, 2017, 5:59am UTC](https://forums.fusetools.com/t/how-to-use-foreign-code/6625/2 "2017-09-14T05:59:16Z")

</div>

I am also having a problem with the foreign code. Hope this answer will help me. I really appreciate if you can improve the foreign code documentation with good examples.

---

<div class="post-metadata">

**Author:** ![Uldis](https://yyz1.discourse-cdn.com/flex035/user_avatar/forums.fusetools.com/uldis/32/657_2.png) [@Uldis](https://forums.fusetools.com/u/Uldis)\
**Post date:** [September 14, 2017, 9:22am UTC](https://forums.fusetools.com/t/how-to-use-foreign-code/6625/3 "2017-09-14T09:22:44Z")

</div>

Pretty decent attempt right there, and I’m wondering if there was any particular issues you wanted to ask about?

There’s a couple suggestions I have:

1. You don’t need the `"*.uno"` line in `.unoproj`, because `"*"` already includes all `.uno` files.
2. Global modules need to register an instance (showing in code below).
3. You should use a bit better naming for the methods, and avoid using the same names for different things (showing in code below).

Other than that, yeah man! Keep going!

```auto
using Uno.Threading;
using Uno;
using Uno.UX;
using Uno.Compiler.ExportTargetInterop;
using Android;
using Uno.Permissions;
using Fuse;
using Fuse.Scripting;

[extern(Android) ForeignInclude(Language.Java, "android.content.Context")]

[UXGlobalModule]
public class CallLog : NativeModule{
	static readonly CallLog _instance;
    public CallLog(){

    	if (_instance != null) return;
    	_instance = this;
    	Resource.SetGlobalKey(_instance, "CallLog");

        AddMember(new NativeFunction("callHistory",(NativeCallback)CallHistory));
    }

    object CallHistory(Context c, object[] args){
        CallHistoryImpl();
        return null;
    }

    [Foreign(Language.Java)]
    public static extern(Android) void CallHistoryImpl()
    @{  

        android.util.Log.d("ForeignCodeExample","hi");

    @}      

    static extern(!Android) void CallHistoryImpl(){
        debug_log("Notifications not supported on this platform.");
    }

}

```
