This commit is contained in:
runette committed 2024-04-22 15:52:51 +01:00
commit e0afef912e
1285 files changed
+83219

No files matched your search

+9
View File
@@ -0,0 +1,9 @@
namespace Mapbox.Unity.Telemetry
{
public interface ITelemetryLibrary
{
void Initialize(string accessToken);
void SendTurnstile();
void SetLocationCollectionState(bool enable);
}
}
+12
View File
@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 60254f905810e483288c61f416978e74
timeCreated: 1495739868
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
+126
View File
@@ -0,0 +1,126 @@
using System.Collections;
using Mapbox.Unity.Utilities;
#if UNITY_ANDROID
namespace Mapbox.Unity.Telemetry
{
using UnityEngine;
public static class AndroidJavaObjectExtensions
{
public static AndroidJavaObject ClassForName(string className)
{
using (var clazz = new AndroidJavaClass("java.lang.Class"))
{
return clazz.CallStatic<AndroidJavaObject>("forName", className);
}
}
// Cast extension method
public static AndroidJavaObject Cast(this AndroidJavaObject source, string destClass)
{
using (var destClassAJC = ClassForName(destClass))
{
return destClassAJC.Call<AndroidJavaObject>("cast", source);
}
}
}
public class TelemetryAndroid : ITelemetryLibrary
{
AndroidJavaObject _activityContext = null;
AndroidJavaObject _telemInstance = null;
static ITelemetryLibrary _instance = new TelemetryAndroid();
public static ITelemetryLibrary Instance
{
get
{
return _instance;
}
}
public void Initialize(string accessToken)
{
if (string.IsNullOrEmpty(accessToken))
{
throw new System.ArgumentNullException("accessToken");
}
using (AndroidJavaClass activityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
{
_activityContext = activityClass.GetStatic<AndroidJavaObject>("currentActivity");
}
if (null == _activityContext)
{
Debug.LogError("Could not get current activity");
return;
}
_telemInstance = new AndroidJavaObject("com.mapbox.android.telemetry.MapboxTelemetry",
_activityContext,
accessToken,
"MapboxEventsUnityAndroid/" + Constants.SDK_VERSION);
if (null == _telemInstance)
{
Debug.LogError("Could not get class 'MapboxTelemetry'");
return;
}
else
_telemInstance.Call<bool>("disable");
}
public void SendTurnstile()
{
using (AndroidJavaObject MapboxAndroidTurnstileEvent = new AndroidJavaObject("com.mapbox.android.telemetry.AppUserTurnstile", "MapboxEventsUnityAndroid", Constants.SDK_VERSION))
{
if (null == MapboxAndroidTurnstileEvent)
{
Debug.LogError("Could not get class 'AppUserTurnstile'");
return;
}
MapboxAndroidTurnstileEvent.Call("setSkuId", Constants.SDK_SKU_ID);
_telemInstance.Call<bool>("push", MapboxAndroidTurnstileEvent);
}
}
public void SetLocationCollectionState(bool enable)
{
if (Input.location.status != LocationServiceStatus.Running)
{
Runnable.Run(TelemetryDelay(enable));
}
else
{
SetTelemetry(enable);
}
}
private void SetTelemetry(bool enable)
{
if (enable)
{
_telemInstance.Call<bool>("enable");
}
else
{
_telemInstance.Call<bool>("disable");
}
}
private IEnumerator TelemetryDelay(bool enable)
{
Input.location.Start();
while (Input.location.status != LocationServiceStatus.Running)
{
yield return new WaitForSeconds(1);
}
SetTelemetry(enable);
}
}
}
#endif
+12
View File
@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 482b4af485a1148558ccb578af5ed81f
timeCreated: 1495731343
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
+29
View File
@@ -0,0 +1,29 @@
namespace Mapbox.Unity.Telemetry
{
public class TelemetryDummy : ITelemetryLibrary
{
static ITelemetryLibrary _instance = new TelemetryDummy();
public static ITelemetryLibrary Instance
{
get
{
return _instance;
}
}
public void Initialize(string accessToken)
{
// empty.
}
public void SendTurnstile()
{
// empty.
}
public void SetLocationCollectionState(bool enable)
{
// empty.
}
}
}
+12
View File
@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 5cf9f00b4c350437ea7ef15fa73733de
timeCreated: 1495739868
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
+143
View File
@@ -0,0 +1,143 @@
#if UNITY_EDITOR
namespace Mapbox.Unity.Telemetry
{
using System.Collections.Generic;
using System.Collections;
using Mapbox.Json;
using System;
using Mapbox.Unity.Utilities;
using UnityEngine;
using System.Text;
using UnityEditor;
using UnityEngine.Networking;
public class TelemetryEditor : ITelemetryLibrary
{
string _url;
static ITelemetryLibrary _instance = new TelemetryEditor();
public static ITelemetryLibrary Instance
{
get
{
return _instance;
}
}
public void Initialize(string accessToken)
{
_url = string.Format("{0}events/v2?access_token={1}", Mapbox.Utils.Constants.EventsAPI, accessToken);
}
public void SendTurnstile()
{
var ticks = DateTime.Now.Ticks;
if (ShouldPostTurnstile(ticks))
{
Runnable.Run(PostWWW(_url, GetPostBody()));
}
}
string GetPostBody()
{
List<Dictionary<string, object>> eventList = new List<Dictionary<string, object>>();
Dictionary<string, object> jsonDict = new Dictionary<string, object>();
long unixTimestamp = (long)Mapbox.Utils.UnixTimestampUtils.To(DateTime.UtcNow);
jsonDict.Add("event", "appUserTurnstile");
jsonDict.Add("created", unixTimestamp);
jsonDict.Add("userId", SystemInfo.deviceUniqueIdentifier);
jsonDict.Add("enabled.telemetry", false);
jsonDict.Add("sdkIdentifier", GetSDKIdentifier());
jsonDict.Add("skuId", Constants.SDK_SKU_ID);
jsonDict.Add("sdkVersion", Constants.SDK_VERSION);
eventList.Add(jsonDict);
var jsonString = JsonConvert.SerializeObject(eventList);
return jsonString;
}
bool ShouldPostTurnstile(long ticks)
{
var date = new DateTime(ticks);
var longAgo = DateTime.Now.AddDays(-100).Ticks.ToString();
var lastDateString = PlayerPrefs.GetString(Constants.Path.TELEMETRY_TURNSTILE_LAST_TICKS_EDITOR_KEY, longAgo);
long lastTicks = 0;
long.TryParse(lastDateString, out lastTicks);
var lastDate = new DateTime(lastTicks);
var timeSpan = date - lastDate;
return timeSpan.Days >= 1;
}
IEnumerator PostWWW(string url, string bodyJsonString)
{
byte[] bodyRaw = Encoding.UTF8.GetBytes(bodyJsonString);
#if UNITY_2017_1_OR_NEWER
UnityWebRequest postRequest = new UnityWebRequest(url, "POST");
postRequest.SetRequestHeader("Content-Type", "application/json");
postRequest.downloadHandler = new DownloadHandlerBuffer();
postRequest.uploadHandler = new UploadHandlerRaw(bodyRaw);
yield return postRequest.SendWebRequest();
while (!postRequest.isDone) { yield return null; }
if (!postRequest.isNetworkError)
{
#else
var headers = new Dictionary<string, string>();
headers.Add("Content-Type", "application/json");
headers.Add("user-agent", GetUserAgent());
var www = new WWW(url, bodyRaw, headers);
yield return www;
while (!www.isDone) { yield return null; }
// www doesn't expose HTTP status code, relay on 'error' property
if (!string.IsNullOrEmpty(www.error))
{
#endif
PlayerPrefs.SetString(Constants.Path.TELEMETRY_TURNSTILE_LAST_TICKS_EDITOR_KEY, "0");
}
else
{
PlayerPrefs.SetString(Constants.Path.TELEMETRY_TURNSTILE_LAST_TICKS_EDITOR_KEY, DateTime.Now.Ticks.ToString());
}
}
static string GetUserAgent()
{
var userAgent = string.Format(
"{0}/{1}/{2} MapboxEventsUnityEditor/{3}",
PlayerSettings.applicationIdentifier,
PlayerSettings.bundleVersion,
#if UNITY_IOS
PlayerSettings.iOS.buildNumber,
#elif UNITY_ANDROID
PlayerSettings.Android.bundleVersionCode,
#else
"0",
#endif
Constants.SDK_VERSION
);
return userAgent;
}
private string GetSDKIdentifier()
{
var sdkIdentifier = string.Format("MapboxEventsUnity{0}",
Application.platform
);
return sdkIdentifier;
}
public void SetLocationCollectionState(bool enable)
{
// Empty.
}
}
}
#endif
+12
View File
@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 27d433fd51a8f43dfb24c4797efe3304
timeCreated: 1499712270
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
+20
View File
@@ -0,0 +1,20 @@
namespace Mapbox.Unity.Telemetry
{
public static class TelemetryFactory
{
public static ITelemetryLibrary GetTelemetryInstance()
{
#if UNITY_EDITOR
return TelemetryEditor.Instance;
#elif UNITY_IOS
return TelemetryIos.Instance;
#elif UNITY_ANDROID
return TelemetryAndroid.Instance;
#elif UNITY_WEBGL
return TelemetryWebgl.Instance;
#else
return TelemetryFallback.Instance;
#endif
}
}
}
+12
View File
@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 22b1b1677ad464ff499ec366ca41ad2b
timeCreated: 1511971959
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
+118
View File
@@ -0,0 +1,118 @@
namespace Mapbox.Unity.Telemetry
{
using System.Collections.Generic;
using System.Collections;
using Mapbox.Json;
using System;
using Mapbox.Unity.Utilities;
using UnityEngine;
using System.Text;
using UnityEngine.Networking;
public class TelemetryFallback : ITelemetryLibrary
{
string _url;
static ITelemetryLibrary _instance = new TelemetryFallback();
public static ITelemetryLibrary Instance
{
get
{
return _instance;
}
}
public void Initialize(string accessToken)
{
_url = string.Format("{0}events/v2?access_token={1}", Mapbox.Utils.Constants.EventsAPI, accessToken);
}
public void SendTurnstile()
{
var ticks = DateTime.Now.Ticks;
if (ShouldPostTurnstile(ticks))
{
Runnable.Run(PostWWW(_url, GetPostBody()));
PlayerPrefs.SetString(Constants.Path.TELEMETRY_TURNSTILE_LAST_TICKS_FALLBACK_KEY, ticks.ToString());
}
}
string GetPostBody()
{
List<Dictionary<string, object>> eventList = new List<Dictionary<string, object>>();
Dictionary<string, object> jsonDict = new Dictionary<string, object>();
long unixTimestamp = (long)Mapbox.Utils.UnixTimestampUtils.To(DateTime.UtcNow);
jsonDict.Add("event", "appUserTurnstile");
jsonDict.Add("created", unixTimestamp);
jsonDict.Add("userId", SystemInfo.deviceUniqueIdentifier);
jsonDict.Add("enabled.telemetry", false);
jsonDict.Add("sdkIdentifier", GetSDKIdentifier());
jsonDict.Add("skuId", Constants.SDK_SKU_ID);
jsonDict.Add("sdkVersion", Constants.SDK_VERSION);
eventList.Add(jsonDict);
var jsonString = JsonConvert.SerializeObject(eventList);
return jsonString;
}
bool ShouldPostTurnstile(long ticks)
{
var date = new DateTime(ticks);
var longAgo = DateTime.Now.AddDays(-100).Ticks.ToString();
var lastDateString = PlayerPrefs.GetString(Constants.Path.TELEMETRY_TURNSTILE_LAST_TICKS_FALLBACK_KEY, longAgo);
long lastTicks = 0;
long.TryParse(lastDateString, out lastTicks);
var lastDate = new DateTime(lastTicks);
var timeSpan = date - lastDate;
return timeSpan.Days >= 1;
}
IEnumerator PostWWW(string url, string bodyJsonString)
{
byte[] bodyRaw = Encoding.UTF8.GetBytes(bodyJsonString);
#if UNITY_2017_1_OR_NEWER
UnityWebRequest postRequest = new UnityWebRequest(url, "POST");
postRequest.SetRequestHeader("Content-Type", "application/json");
postRequest.downloadHandler = new DownloadHandlerBuffer();
postRequest.uploadHandler = new UploadHandlerRaw(bodyRaw);
yield return postRequest.SendWebRequest();
#else
var headers = new Dictionary<string, string>();
headers.Add("Content-Type", "application/json");
headers.Add("user-agent", GetUserAgent());
var www = new WWW(url, bodyRaw, headers);
yield return www;
#endif
}
static string GetUserAgent()
{
var userAgent = string.Format("{0}/{1}/{2} MapboxEventsUnity{3}/{4}",
Application.identifier,
Application.version,
"0",
Application.platform,
Constants.SDK_VERSION
);
return userAgent;
}
private string GetSDKIdentifier()
{
var sdkIdentifier = string.Format("MapboxEventsUnity{0}",
Application.platform
);
return sdkIdentifier;
}
public void SetLocationCollectionState(bool enable)
{
// empty.
}
}
}
+12
View File
@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 30faf59456fb74b29a054e8684302e5e
timeCreated: 1499713323
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
+46
View File
@@ -0,0 +1,46 @@
#if UNITY_IOS
namespace Mapbox.Unity.Telemetry
{
using System.Runtime.InteropServices;
public class TelemetryIos : ITelemetryLibrary
{
[DllImport("__Internal")]
private static extern void initialize(string accessToken, string userAgentBase, string hostSDKVersion);
[DllImport("__Internal")]
static extern void sendTurnstileEvent();
[DllImport("__Internal")]
private static extern void setLocationCollectionState(bool enable);
[DllImport("__Internal")]
private static extern void setSkuId(string skuId);
static ITelemetryLibrary _instance = new TelemetryIos();
public static ITelemetryLibrary Instance
{
get
{
return _instance;
}
}
public void Initialize(string accessToken)
{
initialize(accessToken, "MapboxEventsUnityiOS", Constants.SDK_VERSION);
}
public void SendTurnstile()
{
setSkuId(Constants.SDK_SKU_ID);
sendTurnstileEvent();
}
public void SetLocationCollectionState(bool enable)
{
setLocationCollectionState(enable);
}
}
}
#endif
+12
View File
@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 1089e784167a94312a00eacedff66aa7
timeCreated: 1495731331
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
+122
View File
@@ -0,0 +1,122 @@
namespace Mapbox.Unity.Telemetry
{
using System.Collections.Generic;
using System.Collections;
using Mapbox.Json;
using System;
using Mapbox.Unity.Utilities;
using UnityEngine;
using System.Text;
using UnityEngine.Networking;
public class TelemetryWebgl : ITelemetryLibrary
{
string _url;
static ITelemetryLibrary _instance = new TelemetryFallback();
public static ITelemetryLibrary Instance
{
get
{
return _instance;
}
}
public void Initialize(string accessToken)
{
_url = string.Format("{0}events/v2?access_token={1}", Mapbox.Utils.Constants.EventsAPI, accessToken);
}
public void SendTurnstile()
{
var ticks = DateTime.Now.Ticks;
if (ShouldPostTurnstile(ticks))
{
Runnable.Run(PostWWW(_url, GetPostBody()));
PlayerPrefs.SetString(Constants.Path.TELEMETRY_TURNSTILE_LAST_TICKS_FALLBACK_KEY, ticks.ToString());
}
}
string GetPostBody()
{
List<Dictionary<string, object>> eventList = new List<Dictionary<string, object>>();
Dictionary<string, object> jsonDict = new Dictionary<string, object>();
long unixTimestamp = (long)Mapbox.Utils.UnixTimestampUtils.To(DateTime.UtcNow);
jsonDict.Add("event", "appUserTurnstile");
jsonDict.Add("created", unixTimestamp);
jsonDict.Add("userId", SystemInfo.deviceUniqueIdentifier);
jsonDict.Add("enabled.telemetry", false);
jsonDict.Add("sdkIdentifier", GetSDKIdentifier());
jsonDict.Add("skuId", Constants.SDK_SKU_ID);
jsonDict.Add("sdkVersion", Constants.SDK_VERSION);
// user-agent cannot be set from web broswer, so we send in payload, instead!
jsonDict.Add("userAgent", GetUserAgent());
eventList.Add(jsonDict);
var jsonString = JsonConvert.SerializeObject(eventList);
return jsonString;
}
bool ShouldPostTurnstile(long ticks)
{
var date = new DateTime(ticks);
var longAgo = DateTime.Now.AddDays(-100).Ticks.ToString();
var lastDateString = PlayerPrefs.GetString(Constants.Path.TELEMETRY_TURNSTILE_LAST_TICKS_FALLBACK_KEY, longAgo);
long lastTicks = 0;
long.TryParse(lastDateString, out lastTicks);
var lastDate = new DateTime(lastTicks);
var timeSpan = date - lastDate;
return timeSpan.Days >= 1;
}
IEnumerator PostWWW(string url, string bodyJsonString)
{
byte[] bodyRaw = Encoding.UTF8.GetBytes(bodyJsonString);
#if UNITY_2017_1_OR_NEWER
UnityWebRequest postRequest = new UnityWebRequest(url, "POST");
postRequest.SetRequestHeader("Content-Type", "application/json");
postRequest.downloadHandler = new DownloadHandlerBuffer();
postRequest.uploadHandler = new UploadHandlerRaw(bodyRaw);
yield return postRequest.SendWebRequest();
#else
var headers = new Dictionary<string, string>();
headers.Add("Content-Type", "application/json");
headers.Add("user-agent", GetUserAgent());
var www = new WWW(url, bodyRaw, headers);
yield return www;
#endif
}
static string GetUserAgent()
{
var userAgent = string.Format("{0}/{1}/{2} MapboxEventsUnity{3}/{4}",
Application.identifier,
Application.version,
"0",
Application.platform,
Constants.SDK_VERSION
);
return userAgent;
}
private string GetSDKIdentifier()
{
var sdkIdentifier = string.Format("MapboxEventsUnity{0}",
Application.platform
);
return sdkIdentifier;
}
public void SetLocationCollectionState(bool enable)
{
// empty.
}
}
}
+12
View File
@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: cd7351c4625584230b9b6fbe75e801ca
timeCreated: 1511971959
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: