Skip to content

Commit

Permalink
Add MAUI specific binding, library, and sample app
Browse files Browse the repository at this point in the history
  • Loading branch information
einsteinx2 committed Aug 13, 2024
1 parent 05193ad commit c65da4e
Show file tree
Hide file tree
Showing 51 changed files with 1,542 additions and 96 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
Additions allow you to add arbitrary C# to the generated classes
before they are compiled. This can be helpful for providing convenience
methods or adding pure C# classes.

== Adding Methods to Generated Classes ==

Let's say the library being bound has a Rectangle class with a constructor
that takes an x and y position, and a width and length size. It will look like
this:

public partial class Rectangle
{
public Rectangle (int x, int y, int width, int height)
{
// JNI bindings
}
}

Imagine we want to add a constructor to this class that takes a Point and
Size structure instead of 4 ints. We can add a new file called Rectangle.cs
with a partial class containing our new method:

public partial class Rectangle
{
public Rectangle (Point location, Size size) :
this (location.X, location.Y, size.Width, size.Height)
{
}
}

At compile time, the additions class will be added to the generated class
and the final assembly will a Rectangle class with both constructors.


== Adding C# Classes ==

Another thing that can be done is adding fully C# managed classes to the
generated library. In the above example, let's assume that there isn't a
Point class available in Java or our library. The one we create doesn't need
to interact with Java, so we'll create it like a normal class in C#.

By adding a Point.cs file with this class, it will end up in the binding library:

public class Point
{
public int X { get; set; }
public int Y { get; set; }
}
Binary file not shown.
15 changes: 15 additions & 0 deletions Bindings/mParticle.MAUI.AndroidBinding/Transforms/EnumFields.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<enum-field-mappings>
<!--
This example converts the constants Fragment_id, Fragment_name,
and Fragment_tag from android.support.v4.app.FragmentActivity.FragmentTag
to an enum called Android.Support.V4.App.FragmentTagType with values
Id, Name, and Tag.
<mapping jni-class="android/support/v4/app/FragmentActivity$FragmentTag" clr-enum-type="Android.Support.V4.App.FragmentTagType">
<field jni-name="Fragment_name" clr-name="Name" value="0" />
<field jni-name="Fragment_id" clr-name="Id" value="1" />
<field jni-name="Fragment_tag" clr-name="Tag" value="2" />
</mapping>
-->
</enum-field-mappings>

14 changes: 14 additions & 0 deletions Bindings/mParticle.MAUI.AndroidBinding/Transforms/EnumMethods.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<enum-method-mappings>
<!--
This example changes the Java method:
android.support.v4.app.Fragment.SavedState.writeToParcel (int flags)
to be:
android.support.v4.app.Fragment.SavedState.writeToParcel (Android.OS.ParcelableWriteFlags flags)
when bound in C#.
<mapping jni-class="android/support/v4/app/Fragment.SavedState">
<method jni-name="writeToParcel" parameter="flags" clr-enum-type="Android.OS.ParcelableWriteFlags" />
</mapping>
-->
</enum-method-mappings>

115 changes: 115 additions & 0 deletions Bindings/mParticle.MAUI.AndroidBinding/Transforms/Metadata.xml

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0-android</TargetFramework>
<SupportedOSPlatformVersion>21</SupportedOSPlatformVersion>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<None Remove="Jars\" />
<None Remove="Jars\android-core-5.56.5.aar" />
</ItemGroup>
<ItemGroup>
<Folder Include="Jars\" />
</ItemGroup>
<ItemGroup>
<LibraryProjectZip Include="Jars\android-core-5.56.5.aar">
<Pack></Pack>
</LibraryProjectZip>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,12 @@ public static MParticleSDK Instance

static MParticleSDK CreateInstance()
{
#if PORTABLE
return null;
#else
return new MParticleSDKImpl();
#endif
return new MParticleSDKImpl();
}

internal static Exception NotImplementedInReferenceAssembly()
{
return new NotImplementedException("This functionality is not implemented in the portable version of this assembly. You should reference the mParticle.Xamarin NuGet package from your main application project in order to reference the platform-specific implementation.");
}
}

160 changes: 160 additions & 0 deletions Library/mParticle.MAUI.Android/MParticleSDKImpl.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
using mParticle.MAUI.Android.Wrappers;
using mParticle.MAUI.Android;

namespace mParticle.MAUI;

public class MParticleSDKImpl : MParticleSDK
{
internal MParticleSDKImpl()
{
}

public override Environment Environment
{
get
{
var environment = AndroidBinding.MParticle.Instance.GetEnvironment();
if (environment == AndroidBinding.MParticle.Environment.AutoDetect)
return MAUI.Environment.AutoDetect;
else if (environment == AndroidBinding.MParticle.Environment.Production)
return MAUI.Environment.Production;
else
return MAUI.Environment.Development;
}
}


public override void LeaveBreadcrumb(string breadcrumbName)
{
AndroidBinding.MParticle.Instance.LeaveBreadcrumb(breadcrumbName);
}

public override void LogCommerceEvent(CommerceEvent commerceEvent, bool shouldUploadEvent = true)
{
Android.CommerceBinding.CommerceEvent.Builder bindingCommerceEventBuilder = null;

if (commerceEvent.ProductAction > 0 && commerceEvent.Products != null && commerceEvent.Products.Length > 0)
{
bindingCommerceEventBuilder = new Android.CommerceBinding.CommerceEvent.Builder(Utils.ConvertToMpProductAction(commerceEvent.ProductAction), Utils.ConvertToMpProduct(commerceEvent.Products[0]));
var temp = new List<Product>(commerceEvent.Products);
temp.RemoveAt(0);
commerceEvent.Products = temp.ToArray();
}
else if (commerceEvent.Promotions != null && commerceEvent.Promotions.Length > 0)
{
bindingCommerceEventBuilder = new Android.CommerceBinding.CommerceEvent.Builder(Utils.ConvertToMpPromotionAction(commerceEvent.PromotionAction), Utils.ConvertToMpPromotion(commerceEvent.Promotions[0]));
var temp = new List<Promotion>(commerceEvent.Promotions);
temp.RemoveAt(0);
commerceEvent.Promotions = temp.ToArray();
}
else
{
bindingCommerceEventBuilder = new Android.CommerceBinding.CommerceEvent.Builder(Utils.ConvertToMpImpression(commerceEvent.Impressions[0]));
var temp = new List<Impression>(commerceEvent.Impressions);
temp.RemoveAt(0);
commerceEvent.Impressions = temp.ToArray();
}

if (bindingCommerceEventBuilder == null)
return;

if (commerceEvent.TransactionAttributes != null)
bindingCommerceEventBuilder.TransactionAttributes(Utils.ConvertToMpTransactionAttributes(commerceEvent.TransactionAttributes));

bindingCommerceEventBuilder.Screen(commerceEvent.ScreenName);
bindingCommerceEventBuilder.Currency(commerceEvent.Currency);
bindingCommerceEventBuilder.CustomAttributes(commerceEvent.CustomAttributes);
bindingCommerceEventBuilder.CheckoutOptions(commerceEvent.CheckoutOptions);

if (commerceEvent.Products != null)
{
foreach (var product in commerceEvent.Products)
{
bindingCommerceEventBuilder.AddProduct(Utils.ConvertToMpProduct(product));
}
}

if (commerceEvent.CheckoutStep != null)
bindingCommerceEventBuilder.CheckoutStep(new Java.Lang.Integer(commerceEvent.CheckoutStep.Value));

if (commerceEvent.NonInteractive.HasValue)
bindingCommerceEventBuilder.NonInteraction(commerceEvent.NonInteractive.Value);

if (commerceEvent.Promotions != null)
{
foreach (var promotion in commerceEvent.Promotions)
{
bindingCommerceEventBuilder.AddPromotion(Utils.ConvertToMpPromotion(promotion));
}
}

if (commerceEvent.Impressions != null)
{
foreach (var impression in commerceEvent.Impressions)
{
bindingCommerceEventBuilder.AddImpression(Utils.ConvertToMpImpression(impression));
}
}

bindingCommerceEventBuilder.ShouldUploadEvent(shouldUploadEvent);

AndroidBinding.MParticle.Instance.LogEvent(bindingCommerceEventBuilder.Build());
}

public override void LogEvent(string eventName, EventType eventType, Dictionary<string, string> eventInfo, bool shouldUploadEvent = true)
{
var mpEventType = AndroidBinding.MParticle.EventType.ValueOf(eventType.ToString());
IDictionary<string, object> castedEventInfo = eventInfo.ToDictionary(kvp => kvp.Key, kvp => (object)kvp.Value);
var mpEvent = new AndroidBinding.MPEvent.Builder(eventName, mpEventType).CustomAttributes(castedEventInfo).ShouldUploadEvent(shouldUploadEvent).Build();
AndroidBinding.MParticle.Instance.LogEvent(mpEvent);
}

public override void LogScreen(string screenName, Dictionary<string, string> eventInfo)
{
AndroidBinding.MParticle.Instance.LogScreen(screenName, eventInfo);
}

public override void SetATTStatus(MPATTAuthorizationStatus status, long? attStatusTimestampMillis)
{
}

public override void SetOptOut(bool optOut)
{
AndroidBinding.MParticle.Instance.OptOut = new Java.Lang.Boolean(optOut);
}

public override object GetBindingInstance()
{
return AndroidBinding.MParticle.Instance;
}

public override MParticleSDK Initialize(MParticleOptions options)
{
AndroidBinding.MParticleOptions boundOptions = Utils.ConvertToMpOptions(options);
AndroidBinding.MParticle.Start(boundOptions);
var instance = new MParticleSDKImpl();;
if (options.IdentityStateListener != null)
{
instance.Identity.AddIdentityStateListener(options.IdentityStateListener);
}
return instance;
}

public override IdentityApi Identity
{
get
{
return IdentityApiWrapper.GetInstance(AndroidBinding.MParticle.Instance.Identity());
}
}

public override void Destroy()
{
AndroidBinding.MParticle.Instance = null;
}

public override void Upload()
{
AndroidBinding.MParticle.Instance.Upload();
}
}
99 changes: 99 additions & 0 deletions Library/mParticle.MAUI.Android/Utils/BaseTaskWrapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
namespace mParticle.MAUI.Android.Wrappers;

public class MParticleTaskWrapper : IMParticleTask<IdentityApiResult>
{
Android.IdentityBinding.BaseIdentityTask _task;

internal MParticleTaskWrapper(Android.IdentityBinding.BaseIdentityTask task)
{
_task = task;
}

public IMParticleTask<IdentityApiResult> AddFailureListener(OnFailure listener)
{
_task.AddFailureListener(new TaskFailureWrapper(listener));
return this;
}

public IMParticleTask<IdentityApiResult> AddSuccessListener(OnSuccess listener)
{
_task.AddSuccessListener(new TaskSuccessWrapper(listener));
return this;
}

public IdentityApiResult GetResult()
{
if (_task.Result.User == null)
{
return null;
}
return new IdentityApiResult()
{
User = new MParticleUserWrapper(_task.Result.User)
};
}

public bool IsComplete()
{
return _task.IsComplete;
}

public bool IsSuccessful()
{
return _task.IsSuccessful;
}
}

internal class TaskFailureWrapper : Java.Lang.Object, Android.IdentityBinding.ITaskFailureListener
{
private OnFailure _listener;

internal TaskFailureWrapper(OnFailure listener)
{
_listener = listener;
}

public void OnFailure(IdentityBinding.IdentityHttpResponse result)
{
if (_listener != null)
{
_listener.Invoke(new IdentityHttpResponse()
{
Errors = result.Errors.Select(arg => new Error()
{
Message = arg.Message,
Code = arg.Code
}).ToList(),
IsSuccessful = result.IsSuccessful,
HttpCode = result.HttpCode,
});
}
}
}

class TaskSuccessWrapper : Java.Lang.Object, Android.IdentityBinding.ITaskSuccessListener
{
private OnSuccess _listener;

public TaskSuccessWrapper(OnSuccess listener)
{
_listener = listener;
}

public void OnSuccess(IdentityBinding.IdentityApiResult result)
{
if (_listener != null)
{
var boundResult = new IdentityApiResult();
if (result != null && result.User != null)
{
boundResult.User = new MParticleUserWrapper(result.User);
}
else
{
boundResult.User = null;
}
_listener.Invoke(boundResult);
}
}
}
Loading

0 comments on commit c65da4e

Please sign in to comment.