// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. #if UNITY_ANDROID using Microsoft.AppCenter.Unity; using Microsoft.AppCenter.Unity.Crashes; using Microsoft.AppCenter.Unity.Crashes.Models; using Microsoft.AppCenter.Unity.Internal.Utility; using UnityEngine; public class JavaObjectsConverter { private static readonly AndroidJavaClass _errorAttachmentLogClass = new AndroidJavaClass("com.microsoft.appcenter.crashes.ingestion.models.ErrorAttachmentLog"); public static ErrorReport ConvertErrorReport(AndroidJavaObject errorReport) { if (errorReport == null) { return null; } try { var id = errorReport.Call("getId"); var threadName = errorReport.Call("getThreadName"); var startTime = JavaDateHelper.DateTimeConvert(errorReport.Call("getAppStartTime")); var errorTime = JavaDateHelper.DateTimeConvert(errorReport.Call("getAppErrorTime")); var exception = ConvertException(errorReport.Call("getStackTrace")); var device = ConvertDevice(errorReport.Call("getDevice")); return new ErrorReport(id, startTime, errorTime, exception, device, threadName); } catch (System.Exception e) { Debug.LogErrorFormat("Failed to convert error report Java object to .Net object: {0}", e.ToString()); return null; } } public static Exception ConvertException(string stackTrace) { return new Exception(null, stackTrace); } private static Device ConvertDevice(AndroidJavaObject device) { return new Device( device.Call("getSdkName"), device.Call("getSdkVersion"), device.Call("getModel"), device.Call("getOemName"), device.Call("getOsName"), device.Call("getOsVersion"), device.Call("getOsBuild"), GetIntValue(device, "getOsApiLevel"), device.Call("getLocale"), GetIntValue(device, "getTimeZoneOffset"), device.Call("getScreenSize"), device.Call("getAppVersion"), device.Call("getCarrierName"), device.Call("getCarrierCountry"), device.Call("getAppBuild"), device.Call("getAppNamespace")); } private static int GetIntValue(AndroidJavaObject javaObject, string getterName) { var integer = javaObject.Call(getterName); return integer.Call("intValue"); } internal static AndroidJavaObject ToJavaAttachments(ErrorAttachmentLog[] attachments) { if (attachments == null) { return null; } var javaList = new AndroidJavaObject("java.util.ArrayList", attachments.Length); foreach (var errorAttachmentLog in attachments) { if (errorAttachmentLog != null) { AndroidJavaObject nativeLog = null; if (errorAttachmentLog.Type == ErrorAttachmentLog.AttachmentType.Text) { nativeLog = _errorAttachmentLogClass.CallStatic("attachmentWithText", errorAttachmentLog.Text, errorAttachmentLog.FileName); } else { nativeLog = _errorAttachmentLogClass.CallStatic("attachmentWithBinary", errorAttachmentLog.Data, errorAttachmentLog.FileName, errorAttachmentLog.ContentType); } javaList.Call("add", nativeLog); } } return javaList; } } #endif