This repository has been archived by the owner on Dec 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add MAUI specific binding, library, and sample app
- Loading branch information
1 parent
05193ad
commit c65da4e
Showing
51 changed files
with
1,542 additions
and
96 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
Bindings/mParticle.MAUI.AndroidBinding/Additions/AboutAdditions.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
15
Bindings/mParticle.MAUI.AndroidBinding/Transforms/EnumFields.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
14
Bindings/mParticle.MAUI.AndroidBinding/Transforms/EnumMethods.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
115
Bindings/mParticle.MAUI.AndroidBinding/Transforms/Metadata.xml
Large diffs are not rendered by default.
Oops, something went wrong.
20 changes: 20 additions & 0 deletions
20
Bindings/mParticle.MAUI.AndroidBinding/mParticle.MAUI.AndroidBinding.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
Oops, something went wrong.